Search code examples
javaspringspring-bootoauth-2.0facebook-oauth

Why does my OAuth2 won't working with Spring Boot?


I'm trying to setup Facebook login with OAuth2 for Spring Boot.

First I have my spring security configuration. I want every page from www.localhost:8080/Intranet/** to become blocked for people that haven't been authorized by Facebook.

@Configuration
@EnableOAuth2Client
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .csrf().disable()
                .antMatcher("/Intranet/**")
                .authorizeRequests()
                .antMatchers("/", "/Intranet")
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .logout().logoutSuccessUrl("/").permitAll();
    }

}

I create my application.yml here:

  spring:
   application:
    name: spektrakonhemsida
  security:
    oauth2:
      client:
        registration:
          facebook:
            clientId: myID
            clientSecret: mySecret
            accessTokenUri: https://graph.facebook.com/oauth/access_token
            userAuthorizationUri: https://www.facebook.com/dialog/oauth
            tokenName: oauth_token
            authenticationScheme: query
            clientAuthenticationScheme: form
            resource:
              userInfoUri: https://graph.facebook.com/me
# Server configuration
server:
  port: 8080
  error:
    whitelabel:
       enabled: false

Then I have my dependencies for Spring Security and OAuth2:

        <dependency>


<groupId>org.springframework.security.oauth.boot</groupId>
    <artifactId>spring-security-oauth2-autoconfigure</artifactId>
    <version>2.2.5.RELEASE</version>
</dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-oauth2-client</artifactId>
    </dependency>

    <!-- Prevent /error to crash -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

This is what's happening:

  1. When I access www.localhost:8080/Intranet <- Works perfekt!
  2. When I access www.localhost:8080/Intranet/Bokning <- I will be navigated to /error where my text shows up "You have no rights here! Please login".

But I want users to become automatically navigated to Facebook's login page when they enters /Intranet/**

Why does this not happening?


Solution

  • Found a solution now. This need to be done to make it work with Facebook.

    Security:

    @Configuration
    public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
        @Override
        public void configure(HttpSecurity http) throws Exception {
    
            http
            .csrf().disable()
            .authorizeRequests()
            .antMatchers("/Intranet/Bokning").authenticated() // Block this 
            .antMatchers("/**", "/Intranet**").permitAll() // Allow this for all
            .anyRequest().authenticated()
            .and().logout().logoutSuccessUrl("/").permitAll()
            .and()
            .oauth2Login();
        }
    }
    

    And appllication.yml

    spring:
      security:
        oauth2:
          client:
            registration:
               facebook:
                  clientId: myID
                  clientSecret: mySecret
                  accessTokenUri: https://graph.facebook.com/oauth/access_token
                  userAuthorizationUri: https://www.facebook.com/dialog/oauth
                  tokenName: oauth_token
                  authenticationScheme: query
                  clientAuthenticationScheme: form
                  resource:
                     userInfoUri: https://graph.facebook.com/me
    
    server:
      port: 8080
    

    And pom.xml file:

     <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    
        <dependency>
          <groupId>org.springframework.security</groupId>
          <artifactId>spring-security-oauth2-client</artifactId>
        </dependency>