Spring Security 未重定向到给定的oauth身份验证url

k5ifujac  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(324)

我试图使用Spring Security 集成oauth2,并添加了一些保护路径的安全规则。当我尝试访问安全url时,它将重定向到默认的身份验证url,而不是从application.yml文件中获取该url。有人能帮我理解我错过了什么吗?
yml文件中的oauth配置

spring:
  security:
    oauth2:
      client:
        registration:
          testProvider:
            client-id: test
            client-secret: xxx
            clientName: cas
            authorization-grant-type: authorization_code
            redirect-uri: https://x.y.com/login/oauth2/code/idc
        provider:
          testProvider:
            authorization-uri: https://x.y.com/oauth2.0/authorize
            user-info-uri: https://x.y.com/oauth2.0/profile
            token-uri: https://x.y.com/oauth2.0/accessToken
            user-name-attribute: id

安全依赖项:

implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'

在安全筛选器中配置的规则:

@EnableWebFluxSecurity
@EnableReactiveMethodSecurity
public class SpringSecurityConfig {
    @Bean
    @Order(1)
    public SecurityWebFilterChain openAccess(ServerHttpSecurity http) {
        http.securityMatcher(new PathPatternParserServerWebExchangeMatcher("/open"))
                .authorizeExchange(exchanges -> exchanges.anyExchange().permitAll())
                .httpBasic()
                .disable()
                .formLogin()
                .disable();
        return http.build();
    }

    @Bean
    @Order(3)
    public SecurityWebFilterChain oauthAccess(ServerHttpSecurity http) {
        http
                .securityMatcher(new PathPatternParserServerWebExchangeMatcher("/secure"))
                .authorizeExchange(exchanges -> exchanges.anyExchange().authenticated())
                .oauth2Login();
        return http.build();
    }
}

当我输入url时http://localhost:8080/secure 在浏览器中,它正在重定向到http://localhost:8080/oauth2/authorization/testprovider . 它没有在yml文件中使用配置的授权urlhttps://x.y.com/oauth2.0/authorize
代码已上载到github:https://github.com/rajeevprasanna/webflux-oauth-test

vngu2lb8

vngu2lb81#

spring security启动授权代码流的方式是首先重定向到自己的授权代码流 OAuth2AuthorizationRequestRedirectFilter 终点。
然后,该端点将在 ClientRegistration 并重定向到配置的值。
然而,我相信你的想法有问题 securityMatcher 因此,它说Spring Security 应该只参与以 /secure . 一旦spring安全重定向到 /oauth2/authorization/testProvider ,筛选链不用于该请求(因为它不是以 /secure ).
为了解决这个问题,我认为你应该改变这一点:

.securityMatcher(new PathPatternParserServerWebExchangeMatcher("/secure"))

为此:

.securityMatcher(new OrServerWebExchangeMatcher(
    new PathPatternParserServerWebExchangeMatcher("/secure"),
    new PathPatternParserServerWebExchangeMatcher("/oauth2")
))

或者,spring security设计用于在一个dsl配置中为您协商授权规则。虽然有两个不同的过滤链的合理使用案例,但你可以考虑只使用一个:

@Bean
public SecurityWebFilterChain web(ServerHttpSecurity http) {
    http
        .authorizeExchange(exchanges -> exchanges
            .pathMatchers("/open").permitAll()
            .pathMatchers("/secure").authenticated()
        )
        .oauth2Login(Customizer.withDefaults());

    return http.build();
}

然后,由于此dsl配置为过滤所有端点,因此不需要额外的安全匹配器。
另外,, httpBasicformLogin 默认情况下未初始化,因此无需主动禁用它们。

相关问题