【发布时间】:2014-12-20 21:53:06
【问题描述】:
我有 Spring WebMVC 应用程序,在控制器方法上使用 @PreAuthorize 和 @PostAuthorice 注释。但是这些注释被忽略了,因为我没有在我的 Spring Security 中启用它。
如果我有spring-security.xml,我可以使用以下行启用它:
<global-method-security pre-post-annotations="enabled" />
不幸的是,我有一个完整的基于注释的配置。 Spring-Security 原则上适用于我的应用程序。
我的问题:如何使用基于注释的 MVC 配置启用 pre-post-annotation?
- 春季版本:4.0.5.RELEASE
- Spring-Security-版本:3.2.4.RELEASE
这是我的WebSecurityConfigurerAdapter 实现:
@Configuration
@EnableWebMvcSecurity()
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired DataSource dataSource;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication()
.dataSource(dataSource)
.passwordEncoder(new ShaPasswordEncoder(256))
.usersByUsernameQuery("select username,password, enabled from user where USERNAME=?")
.authoritiesByUsernameQuery("select u.username, r.name from user u, role r, user_has_role uhr where u.id = uhr.user_id and r.id = uhr.role_id and u.username = ? ");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/resources/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.defaultSuccessUrl("/", true)
.and()
.logout()
//.logoutUrl("/logout") //this is the default
// Call the URL invalidate_session after logout...
.logoutSuccessUrl("/invalidate_session")
.permitAll()
.and()
// @see http://docs.spring.io/spring-security/site/docs/3.2.x/reference/htmlsingle/#csrf-configure
.csrf().disable();
}
}
我的MessageSecurityWebApplicationInitializer 是空的:
public class MessageSecurityWebApplicationInitializer extends
AbstractSecurityWebApplicationInitializer {
}
【问题讨论】:
标签: java spring spring-mvc spring-security annotations