为什么我的OAuth2无法与Spring Boot一起使用?

时间:2020-03-17 12:42:30

标签: java spring spring-boot oauth-2.0 facebook-oauth

我正在尝试使用OAuth2设置Facebook登录以用于Spring Boot。

首先,我有我的spring安全配置。我希望www.localhost:8080 / Intranet / **中的每个页面都被那些未经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();
    }

}

我在这里创建application.yml

  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

然后我对Spring Security和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>

正在发生的事情:

  1. 当我访问www.localhost:8080 / Intranet时<-可以正常工作!
  2. 当我访问www.localhost:8080 / Intranet / Bokning <-时,我将导航至/ error,其中我的文本显示为“您在这里没有权限!请登录”。

但是我希望用户在进入/ Intranet / **时能够自动导航到Facebook的登录页面

为什么这种情况不会发生?

1 个答案:

答案 0 :(得分:0)

现在找到了解决方案。需要这样做才能使其与Facebook一起使用。

安全性:

FindAll()

和appllication.yml

@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();
    }
}

和pom.xml文件:

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