【发布时间】:2020-07-20 21:38:45
【问题描述】:
我正在使用 Spring Security(@EnableWebSecurity)。当我尝试在 "/invoice" 或 "/notification" 端点上使用此令牌进行授权时,我总是收到禁止响应,但如果我不使用 hasRole 属性或仅使用一个角色的令牌,一切都会正常工作。
代币:
{
....
"exp": 1586366900,
"iat": 1586348900,
"authorities": "ROLE_INVOICE,ROLE_NOTIFICATION",
....
}
WebSecurityConfigurerAdapter 类:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable() //Disabling CSRF
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.exceptionHandling().authenticationEntryPoint((req, rsp, e) -> rsp.sendError(HttpServletResponse.SC_UNAUTHORIZED))
.and()
.addFilterAfter(new JwtTokenAuthenticationFilter(jwtConfig), UsernamePasswordAuthenticationFilter.class)
.authorizeRequests()
.antMatchers(HttpMethod.POST, jwtConfig.getUri()).permitAll()
.antMatchers("/invoice/**").hasRole("INVOICE")
.antMatchers("/notification/**").hasRole("NOTIFICATION")
.antMatchers("/test/**")
// Any other request must be authenticated
.anyRequest().authenticated();
}
就我而言,我想在不同服务上使用一个令牌进行身份验证。有什么建议吗?
【问题讨论】:
标签: spring spring-security jwt express-jwt