【发布时间】:2021-08-14 10:36:52
【问题描述】:
我正在尝试使用 Spring Security 集成 oauth2,并添加了一些用于保护路径的安全规则。当我尝试访问安全 URL 时,它会重定向到默认身份验证 URL,而不是从 application.yml 文件中获取一个。有人可以帮助我了解我缺少什么吗?
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 文件中配置的授权 URL https://x.y.com/oauth2.0/authorize
代码上传到github:https://github.com/rajeevprasanna/webflux-oauth-test
【问题讨论】:
-
当您添加
EnableWebfluxSecurity时,您将覆盖默认安全性,您必须手动配置所有内容。这可能就是它不读取 yml 的原因。该文档显示了如何手动配置 oauth 客户端。 -
当我删除 securityMatcher 时,规则无法正常工作。它将进入适当的授权流程。使用 securityMatcher,授权流程失败。不知道为什么行为是冲突的
-
这似乎是 securityMatcher 的问题。如果我删除 securityMatcher,路由特定规则不起作用
标签: spring-boot spring-security spring-webflux spring-security-oauth2