Java 403使用@Secured Spring Security时禁止
我正在尝试使用SpringSecurity,特别是@Secured注释,来保护在SpringBoot中创建的RESTAPI。我正在尝试阅读这篇文章:(SpringSecurity2.1.6.RELEASE) 我在我的Java 403使用@Secured Spring Security时禁止,java,spring-boot,spring-security,Java,Spring Boot,Spring Security,我正在尝试使用SpringSecurity,特别是@Secured注释,来保护在SpringBoot中创建的RESTAPI。我正在尝试阅读这篇文章:(SpringSecurity2.1.6.RELEASE) 我在我的@RestController类中有一个方法,名为getUsers(),如下所示: @Secured("USER_READ") @RequestMapping(value = USER_STRING, method = RequestMethod.POST)
@RestController
类中有一个方法,名为getUsers()
,如下所示:
@Secured("USER_READ")
@RequestMapping(value = USER_STRING, method = RequestMethod.POST)
@JsonView(UserViews.ExternalLight.class)
public Iterable<User> getUsers(@RequestBody(required = false) SearchContext context) {
return getUsersHelper(context);
}
当我尝试使用良好的凭据访问API并调试这一行时,我看到getAuthorities()
返回一个SimpleGrandAuthority
对象列表,其中包含“USER\u READ”以及“ROLE\u ADMIN”和其他一些对象。然而,正如标题所示,尽管在GrantedAuthority
对象列表中仍然存在“USER_READ”,但我仍然得到了403
我看过一些文章说我需要在角色前面加上“ROLE_u”,但我已经为我的角色加了前缀,如果我将@Secured(“USER_READ”)
更改为@Secured(“ROLE_ADMIN”)
,我仍然会收到相同的错误
我还使用@EnableGlobalMethodSecurity(securedEnabled=true)
启用了方法安全性
我是Spring的新手,如果您有任何建议,我们将不胜感激。请提供您的安全配置代码
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return getAuthorities(user.getRoles());
}