Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.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 @RoleAllowed注释始终使用KeyClope抛出403错误_Spring_Spring Boot_Spring Security_Keycloak - Fatal编程技术网

Spring @RoleAllowed注释始终使用KeyClope抛出403错误

Spring @RoleAllowed注释始终使用KeyClope抛出403错误,spring,spring-boot,spring-security,keycloak,Spring,Spring Boot,Spring Security,Keycloak,我是一个新来的春天靴,可能有一个愚蠢的问题。 我有一个简单的SpringBootRESTAPI应用程序,带有SpringSecurity和oauth2。Outh2代理是密钥斗篷 所以我的安全过滤器看起来像这样 @Configuration @EnableWebSecurity public class WebSecurityAdditionalConfig extends WebSecurityConfigurerAdapter { @Override protected vo

我是一个新来的春天靴,可能有一个愚蠢的问题。 我有一个简单的SpringBootRESTAPI应用程序,带有SpringSecurity和oauth2。Outh2代理是密钥斗篷 所以我的安全过滤器看起来像这样

@Configuration
@EnableWebSecurity
public class WebSecurityAdditionalConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors()
                .and()
                .authorizeRequests()
                .antMatchers("/api/**")
                .authenticated()
                .and()
                .oauth2ResourceServer().jwt();
        http.csrf().disable().sessionManagement().sessionCreationPolicy(
                SessionCreationPolicy.STATELESS);
    }

}
我还启用了全局方法安全性


@Configuration
@EnableGlobalMethodSecurity(jsr250Enabled = true, securedEnabled = true, prePostEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
}
但当我尝试将@RolesAllowed('admin')添加到我的控制器方法时,我总是得到403禁止的错误 没有注释,一切都正常,没有令牌,我得到401,如果令牌过期403。 这是我的jwt的例子

{
  
  "realm_access": {
    "roles": [
      "admin"
    ]
  },
  "resource_access": {
    "edge_client": {
      "roles": [
        "cli_admin"
      ]
    }
  },
  "scope": "profile web-origins email",
  "email_verified": false,
  "name": "John Spartan",
  "groups": [
    "admin"
  ],
  "preferred_username": "test_admin",
  "given_name": "John",
  "family_name": "Spartan"
}
我认为这是获得授权的原因。默认情况下,它在
jwt
中查找
scope
scp
索赔。在您的情况下,您有
“范围”:“配置web源代码电子邮件”
。之后,它在每个权限前面加上
DEFAULT\u authority\u PREFIX
等于
SCOPE
。我认为当您从
SecurityContextHolder.getContext().getAuthentication()调试
Authentication
对象时,它的
getAuthories()
返回的权限将等于
SCOPE\u profile
SCOPE\u web-origines
SCOPE\u email
。您应该将代码更改为:

.oauth2ResourceServer()
.jwt(定制者->{
JwtAuthenticationConverter JwtAuthenticationConverter=新的JwtAuthenticationConverter();
//编写自己的转换器jwtGrantedAuthoritiesConverter
//并重写集合转换(Jwt Jwt)以从中获取角色
//realm\u access.roles
setJwtGrantedAuthoritiesConverter(jwtGrantedAuthoritiesConverter);
customizer.jwtAuthenticationConverter(jwtAuthenticationConverter)
})

或者使用而不是
oauth2ResourceServer()

可能是您用于使您的测试没有
admin
角色的用户。