我的网络安全配置中有以下代码:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/**")
.hasRole("ADMIN")
.and()
.httpBasic().and().csrf().disable();
}
因此,我在数据库中添加了一个具有“ADMIN”角色的用户,当我尝试使用该用户登录时,我总是收到 403 错误,然后我启用了 spring 的日志,并发现了这一行:
2015-10-18 23:13:24.112 DEBUG 4899 --- [nio-8080-exec-1] o.s.s.w.a.i.FilterSecurityInterceptor : Secure object: FilterInvocation: URL: /api/user/login; Attributes: [hasRole('ROLE_ADMIN')]
为什么 Spring Security 正在寻找“ROLE_ADMIN”而不是“ADMIN”?
Spring security 添加前缀“ROLE_“ 默认情况下。
如果您想删除或更改此内容,请查看
如何从拦截器 URL 更改角色? https://web.archive.org/web/20180201001929/http://forum.spring.io/forum/spring-projects/security/51066-how-to-change-role-from-interceptor-url
编辑:也发现了这个:Spring Security 删除 RoleVoter 前缀 https://stackoverflow.com/questions/21620076/spring-security-remove-rolevoter-prefix
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)