【发布时间】:2017-01-06 23:15:58
【问题描述】:
我有一个简单的 Spring Boot 应用程序,它公开了一个 REST API。 我已经使用 @PreAuthorize("hasRole('ROLE_4')") 注释成功地配置了 Spring Security,以根据其 ROLE 保护其余 API 中的每个方法。
我注意到,如果我根本不放置 @PreAuthorize 注释,则框架允许向任何经过身份验证的用户发出此请求。我想扭转这种行为。因此,如果其中一位程序员忘记添加 @PreAuthorize 注释,任何对该方法的请求都将被自动拒绝。
这是我的配置:
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//Disable HTTP Basic authentication
http.httpBasic().disable();
//Add the filters
http.addFilterBefore(new AuthenticationFilter(authenticationManager()), BasicAuthenticationFilter.class);
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(securityServiceAuthenticationProvider());
}
@Bean
public AuthenticationProvider securityServiceAuthenticationProvider() {
return new SecurityServiceAuthenticationProvider();
}
}
谢谢 盖伊·胡达拉
【问题讨论】:
标签: java spring spring-security