以编程方式启用 Spring Security 前后注释
2022-09-01 15:17:31
我有Spring WebMVC应用程序使用控制器方法和注释。但是这些注释被忽略了,因为我没有在我的春季安全性中启用它。@PreAuthorize
@PostAuthorice
如果我有一个,我可以用下面的行启用它:spring-security.xml
<global-method-security pre-post-annotations="enabled" />
不幸的是,我有一个完整的基于注释的配置。Spring-Security原则上在我的应用程序中起作用。
我的问题:如何使用基于注释的 MVC 配置启用前后注释?
- 春季版: 4.0.5.发布
- Spring-Security-Version: 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 {
}