Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot @EnableWebSecurity和#oauth2.clientHasRole-为什么';这不管用吗?_Spring Boot_Azure Active Directory_Spring Security Oauth2 - Fatal编程技术网

Spring boot @EnableWebSecurity和#oauth2.clientHasRole-为什么';这不管用吗?

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

我绞尽脑汁反对这件事已经有一段时间了。我有一个来自Azure的
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
表达式能够开箱即用。如果有人有更喜欢的答案,请张贴出来