4

How To Implement OAuth2 Social Login(Single Sign-On)Using Facebook & Spring...

 2 years ago
source link: https://dev.to/rohan2596/how-to-implement-oauth2-social-loginsingle-sign-onusing-facebook-spring-boot-part-2-33mf
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

OAuth2 Facebook

Hello, Its Rohan Kadam

Hello All, Hope you are doing well. Today we are going to understand how we can implement OAuth2 Social Login Using Facebook and Spring Boot. Let us start coding.

Before going forward implementing Part 2. I request to go into taking a look in Part 1 — How To Implement OAuth2 Social Login Using Facebook
How To Implement OAuth2 Social Login Using Facebook — Part 1

**Step 1: **Create a Spring Boot Project using Spring initializer.

Spring Initializr

**Step 2: **Add the following dependencies in your project pom.xml

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-oauth2-client</artifactId>
</dependency>
Enter fullscreen modeExit fullscreen mode

**Step 3: **Create a Configuration File for implementing OAuth2.

@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.cors() .and() .csrf() .disable() .formLogin() .disable() .and() .authorizeRequests() .antMatchers("/", "/error", "/favicon.ico", "/**/*.png", "/**/*.gif", "/**/*.svg", "/**/*.jpg", "/**/*.html", "/**/*.css", "/**/*.js") .permitAll() .antMatchers("/auth/**", "/oauth2/**") .permitAll() .anyRequest() .authenticated() .and() .oauth2Login();

} }

Step 4:- Create Application Yml which will consist of Facebook Configuration.

spring: security: oauth2: client: registration: facebook: clientId: <client_id> clientSecret: <client_secret_key> redirectUri: "http://localhost:8080/oauth2/callback/facebook" authorization-grant-type: authorization_code scope: - email - profile provider: facebook: authorizationUri: https://www.facebook.com/v3.0/dialog/oauth tokenUri: https://graph.facebook.com/v3.0/oauth/access_token userInfoUri: https://graph.facebook.com/v3.0/me?fields=id,first_name,middle_name,last_name,name,email,verified,is_verified,picture.width(250).height(250)

Step 5:- Create a Rest Controller which consists of two endpoints.

@RestController public class FacebookController {

@GetMapping("/auth/facebook") public void startFacebookAuth(HttpServletRequest request, HttpServletResponse response) { System.out.println("Facebook Oauth Login Initiated); String uri = request.getRequestURI(); System.out.println(uri); }

// On Successfull OAuth Facebook will return principal object // Principal Object Consist of username ,name ,email depending on scope mention in yml. @GetMapping("/oauth2/callback/facebook") public Principal callbackFacebook(Principal principal) { System.out.println(principal.toString()); return principal;

} }

Note:-

  1. Principal Object contains username, email, and profile image depending on scope.

  2. If an error occurs regarding an incorrect redirect Url add the following URL **https://localhost:8080/oauth2/callback/facebook**

Test Endpoints for localhost:-

**http://localhost:8080/oauth2/authorize/facebook?redirect_uri=http://localhost:8080/oauth2/callback/facebook**


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK