Spring boot @EnableWebSecurity和#oauth2.clientHasRole-为什么';这不管用吗?
我绞尽脑汁反对这件事已经有一段时间了。我有一个来自Azure的Spring boot @EnableWebSecurity和#oauth2.clientHasRole-为什么';这不管用吗?,spring-boot,azure-active-directory,spring-security-oauth2,Spring Boot,Azure Active Directory,Spring Security Oauth2,我绞尽脑汁反对这件事已经有一段时间了。我有一个来自Azure的client\u凭据JWT令牌,有效负载中包含以下内容: "roles": [ "read", "write" ] 我的安全配置与此类似: @Configuration @EnableWebSecurity public SecurityConfig extends WebSecurityConfigurerAdapter { @Override public void configure(HttpSecurity
client\u凭据JWT令牌,有效负载中包含以下内容:
"roles": [ "read", "write" ]
我的安全配置与此类似:
@Configuration
@EnableWebSecurity
public SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.antMatcher("/**").authorizeRequests()
.antMatchers(HttpMethod.GET, "/api", "/api/**").access("#oauth2.clientHasRole(\"read\")")
.antMatchers(HttpMethod.POST, "/api", "/api/**").access("#oauth2.clientHasRole(\"write\")")
.anyRequest().access("#oauth2.isClient()");
}
}
我正在使用:
org.springframework.boot:spring-boot-starter-oauth2-resource-server:2.1.0.RELEASE
我们使用的是一个私有库,它使用了@EnableResourceServer
,但后来停止使用它,因为它需要通过创建自定义代码来连接Azure的内部支持,而不是使用@EnableWebSecurity
,除了这些特殊权限,它似乎是开箱即用的
那么为什么#oauth2
安全表达式方法不能工作呢?我得到以下例外情况:
java.lang.IllegalArgumentException: Failed to evaluate expression '#oauth2.clientHasRole("read")'
我尝试了@EnableGlobalMethodSecurity(prespenabled=true)
并通过重写GlobalMethodSecurity配置.createExpressionHandler()
手动返回了一个OAuth2MethodSecurity ExpressionHandler()
,但没有成功。请将此作为参考
作为另一种方法,我还尝试添加一些新的GrantedAuthority
s,但在不亲自手动验证令牌的情况下,我没有找到解决方法。我看到使用了SecurityExpressionRoot
,但在检查hasRole()
时,它依赖于要设置的权限,而权限是空的
因此,我希望#oauth.clientHasRole()
功能能够正常工作。任何帮助都将不胜感激 我最终遵循了这一点,并通过生成一个bean并引用它来创建自己的安全表达式。我不喜欢将此作为答案,希望#oauth2
表达式能够开箱即用。如果有人有首选答案,请发布。我最终遵循了这一点,并通过生成一个bean并引用它来创建自己的安全表达式。我不喜欢将此作为答案,希望#oauth2
表达式能够开箱即用。如果有人有更喜欢的答案,请张贴出来