Table of Contents
Introduction
Thanks for the PR. We’ve already considered this in #2548 where we decided that it isn’t something that we want to include in Spring Boot. That’s still the case so I’m going to close this. Thanks anyway.
So it doesn’t look like sooner or later they are going to add autoconfigure functionality to Spring-Social-Google
- To enable autoconfigure of spring-social-google we need to
- Add Maven dependency
- Add a GoogleAutoConfiguration
- Add GoogleProperties
- Modify HTML pages
Getting started
I would suggest you to first clone the as it does have required page structure and dependencies
Add Maven Dependencies
Add the spring-social-google dependency
1 2 3 4 5 6 7 | <dependency> <groupId>org.springframework.social</groupId> <artifactId>spring-social-google</artifactId> <version>1.0.0.RELEASE</version> </dependency> |
Add a GoogleAutoConfiguration Class
Do Ctrl+Shift+T in your IDE(eclipse) and look for FacebookAutoConfiguration class you should be able to find it either in org.springframework.boot.autoconfigure.social package in spring-autoconfigure.jar
Copy this File and replace Facebook with Google. Alternatively, copy the below class and put in some package, make sure it is available in classpath(src/main/java or inside another source folder)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | @Configuration @ConditionalOnClass({ SocialConfigurerAdapter.class, GoogleConnectionFactory.class }) @ConditionalOnProperty(prefix = "spring.social.google", name = "app-id") @AutoConfigureBefore(SocialWebAutoConfiguration.class) @AutoConfigureAfter(WebMvcAutoConfiguration.class) public class GoogleAutoConfiguration { @Configuration @EnableSocial @EnableConfigurationProperties(GoogleProperties.class) @ConditionalOnWebApplication protected static class GoogleConfigurerAdapter extends SocialAutoConfigurerAdapter { private final GoogleProperties properties; protected GoogleConfigurerAdapter(GoogleProperties properties) { this.properties = properties; } @Bean @ConditionalOnMissingBean(Google.class) @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES) public Google google(ConnectionRepository repository) { Connection connection = repository.findPrimaryConnection(Google.class); return connection != null ? connection.getApi() : null; } @Bean(name = { "connect/googleConnect", "connect/googleConnected" }) @ConditionalOnProperty(prefix = "spring.social", name = "auto-connection-views") public GenericConnectionStatusView googleConnectView() { return new GenericConnectionStatusView("google", "Google"); } @Override protected ConnectionFactory<?> createConnectionFactory() { return new GoogleConnectionFactory(this.properties.getAppId(), this.properties.getAppSecret()); } } } |
Add GoogleProperties
In the same package add the below class
1 2 3 4 5 6 7 8 9 10 11 | import org.springframework.boot.autoconfigure.social.SocialProperties; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "spring.social.google") public class GoogleProperties extends SocialProperties{ } |
Update the application.properties
Now update the application.properties with your secret and client key
1 2 3 4 | spring.social.google.appId=12894100090-tqso3lih5o42isneort886la2pesafmp.apps.googleusercontent.com spring.social.google.appSecret=9xfU16efvxQ-BTMsXT9wO |
Add the HTML for Google
Under src/main/resources/templates/connect you should have facebookConnect.html and facebookConnected.html, if you have cloned the project properly.Again replace facebook with google in HTML and save them as googleConnected.html and googleConnect.html or copy them as shown below.
googleConnect.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <html> <head> <title>Hello Google</title> </head> <body> <h3>Connect to Google</h3> <form action="/connect/google" method="POST"> <input type="hidden" name="scope" value="profile" /> <div class="formInfo"> <p>You aren't connected to Google yet. Click the button to connect this application with your account.</p> </div> <p><button type="submit">Connect to Google</button></p> </form> </body> </html> |
googleConnected.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <html> <head> <title>Hello Google</title> </head> <body> <h3>Connected to Google</h3> <p> You are now connected to your Google account. Click <a href="/google">here</a> to see some entries. </p> </body> </html> |
Add a @Controller for Google
We need to add a rest controller so it can handle google authorization requests
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | @Controller @RequestMapping("/google") public class GoogleController { private Google google; private ConnectionRepository connectionRepository; public GoogleController(Google google, ConnectionRepository connectionRepository) { this.google = google; this.connectionRepository = connectionRepository; } @GetMapping public String helloFacebook(Model model) { if (connectionRepository.findPrimaryConnection(Google.class) == null) { return "redirect:/connect/google"; } Person user = google.plusOperations().getGoogleProfile(); System.out.println(google.isAuthorized()); System.out.println(": "+user.getGivenName() +" : "+ user.getEmailAddresses() +" : "+ user.getFamilyName()); model.addAttribute("googleProfile", user); return "google"; } } |
Add HTML to show user details
In src/main/resources add a file called “google.html” at same level as “hello.html”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <html> <head> <title>Hello Google</title> </head> <body> <h3>Hello, <span th:text="${googleProfile.givenName}"></span><span th:text="${googleProfile.familyName}"></span></h3> <h4><span th:text="${googleProfile.id}"></span></h4> <div th:each="post:${googleProfile.emailAddresses}"> Email : <b th:text="${post}"></b> <hr/> </div> </body> </html> |
That’s all
Run the application on localhost: port/google and you should be authenticated. Drop comments if you face any problem