使用示例
@Secured({"ROLE_user"})
@GetMapping(value = "get")
public String get() {
return "hello get";
}
踩坑記錄:
1潭流、前置條件:必須在@Configuration類中加入如下注解
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
// ...
}
2芝发、注解內(nèi)容:大括號(hào)中的字符串內(nèi)容須加上“ROLE_”前綴
試過寫成@Secured({"user"}),此時(shí)不論用戶含有"user"或"ROLE_user"都校驗(yàn)不通過
查org.springframework.security.access.vote.RoleVoter源碼發(fā)現(xiàn)@Secured僅支持帶前綴的字符串
public boolean supports(ConfigAttribute attribute) {
if ((attribute.getAttribute() != null)
&& attribute.getAttribute().startsWith(getRolePrefix())) {
return true;
}
else {
return false;
}
}