我正在尝试使用@Secured("ADMIN")设置方法安全注释(没有任何XML,只有java配置,Spring Boot).但是通过角色访问不起作用.
安全配置:
@Configuration
@EnableWebSecurity
public class AppSecurityConfiguration extends WebSecurityConfigurerAdapter{
.....
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/**").fullyAuthenticated().and()
.addFilterBefore(tokenAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
}
.....
}
Run Code Online (Sandbox Code Playgroud)
我想限制访问控制器的方法:
@RestController
@RequestMapping("/api/groups")
public class GroupController {
@Autowired
private GroupService groupService;
@Secured("ADMIN")
@RequestMapping
public List<Group> list() {
return groupService.findAll();
}
}
Run Code Online (Sandbox Code Playgroud)
通过网址限制访问是有效的,具有:
.antMatchers("/api/**").hasAuthority("ADMIN")
Run Code Online (Sandbox Code Playgroud)
也许我忘了指定我想要按角色限制?
UPD:
根据规则,在哪一层必须@PreAuthorize("hasRole('ADMIN')")在Controller层或Service层中?