我在控制器方法上使用@PreAuthorize
和@PostAuthorice
注释使用Spring WebMVC应用程序。但是这些注释被忽略了,因为我没有在我的春季安全中启用它。
如果我有spring-security.xml
,我可以使用以下行启用它:
<global-method-security pre-post-annotations="enabled" />
不幸的是,我有一个完整的基于注释的配置。 Spring-Security原则上适用于我的应用程序。
我的问题:如何使用基于注释的MVC配置启用前后注释?
这是我的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 {
}
答案 0 :(得分:49)
我必须将以下注释添加到Configuration-class:@EnableGlobalMethodSecurity(prePostEnabled=true)