【发布时间】: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