PrePost注释如何与HttpSecurity配置进行堆栈



我对Spring Security很陌生,我正在努力理解@PreAuthorize@PostAuthorize的实用性。这些注释采用内置表达式,在配置WebSecurityConfigurerAdapter时,这些表达式都可以放在HttpSecurity中。

使用以下SpringJava(Groovy)配置进行尝试:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().anyRequest().authenticated()
}

一种控制器方法定义为:

@RequestMapping('/data/{id}')
@PreAuthorize('permitAll')
Data getData(@PathParam('id') String id) {
    //return some data
}

控制器方法似乎没有任何影响。我一直在阅读Spring的官方文档,但似乎没有更详细的讨论。

这些注释由EnableGlobalMethodSecurity配置对script-security.xml的更改注释启用(您正试图避免)

@EnableGlobalMethodSecurity(prePostEnabled=true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
 ...
}

最新更新