【问题标题】:SUPER_ADMIN_ROLE and @PreAuthorize("hasRole('SUPER_ADMIN')") return 403 errorSUPER_ADMIN_ROLE 和 @PreAuthorize("hasRole('SUPER_ADMIN')") 返回 403 错误
【发布时间】:2021-01-08 18:40:34
【问题描述】:

我有一个需要 JWT 令牌的 SPring 入口点。 我解码了令牌并获得了一个对象:

{
  "aud": "mySite",
  "sub": "admin@mysite.fr",
  "iss": "My Company",
  "Authorities": [
    "WRITE_AUTHORITY",
    "READ_AUTHORITY",
    "DELETE_AUTHORITY",
    "ROLE_ADMIN",
    "ROLE_SUPER_ADMIN"
  ],
  "exp": 1610910605,
  "iat": 1610046605
}

我们可以注意到我有一系列权限。

@PreAuthorize("hasRole('SUPER_ADMIN')")
@PostMapping(
        path = "/langs",
        produces = { MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE }
)
public LangResponseModel addSiteLang(@RequestBody LangRequestModel langRequestModel) {
    log.info("addLang() called");
    LangResponseModel returnValue;

    ModelMapper modelMapper = new CustomMapper();
    modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
    LangDto lang = modelMapper.map(langRequestModel, LangDto.class);
    LangDto createdLang = siteService.addGlobalLang(lang);

    returnValue = modelMapper.map(createdLang, LangResponseModel.class);

    return returnValue;
}

我的 WebSecurity 类似乎使用我的公共 URL 和需要身份验证的 URL 正确实现。

@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
public class WebSecurity extends WebSecurityConfigurerAdapter {

    ...
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // define public & private entry points
        http
                .cors().and()
                .csrf().disable()
                .authorizeRequests()
                   .antMatchers(HttpMethod.GET, "/").permitAll()
                   // my public entry points here
                .anyRequest()
                    .authenticated()
                    .and()
                    // .addFilter(getUserAuthenticationFilter())
                    // .addFilter(getAdminAuthenticationFilter())
                        .addFilter(new AuthorizationFilter(authenticationManager(), jwtTokenProvider))
                        .addFilter(new AdminAuthorizationFilter(authenticationManager(), jwtTokenProvider))
                    .sessionManagement()
                    .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }

}

我确实有正确的权限...

但我收到 403 错误消息:

{
    "timestamp": "2021-01-07T19:33:23.963+00:00",
    "status": 403,
    "error": "Forbidden",
    "message": "Access Denied",
    "path": "/app-ws/sites/langs"
}

【问题讨论】:

  • 我用的和你不同的大小写,比如Role_USER
  • 如果你检查你的调试日志,它会告诉你为什么你得到一个 403
  • 我有这个错误:在 SecurityContext 中找不到身份验证对象。诡异的。它只是在 mvn clean 之后显示..

标签: spring-boot spring-security


【解决方案1】:

终于找到问题所在了。我想实现 2 个独特的 AuthorizationFilter,因为我分别处理用户和管理员。

.addFilter(new AuthorizationFilter(authenticationManager(), jwtTokenProvider))
.addFilter(new AdminAuthorizationFilter(authenticationManager(), jwtTokenProvider))

我只是添加删除一个授权过滤器...好

【讨论】:

    猜你喜欢
    • 2020-10-14
    • 2017-09-28
    • 2020-06-24
    • 2020-07-25
    • 1970-01-01
    • 2013-08-14
    • 2020-06-15
    • 2019-09-18
    • 2015-12-09
    相关资源
    最近更新 更多