为什么我的 OAuth2 不能与 Spring 启动一起使用?

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

我正在尝试使用 OAuth2 为 Spring 引导设置 Facebook 登录。

首先我有我的 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 安全和 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 我的文本显示 "You have no rights here! Please login"。

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

为什么这没有发生?

现在找到解决办法了。需要完成此操作才能使其与 Facebook 一起使用。

安全:

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

和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

和pom.xml文件:

 <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>