Ответ 1
Мне пришлось добавить в класс конфигурации следующую аннотацию: @EnableGlobalMethodSecurity(prePostEnabled=true)
У меня есть приложение Spring WebMVC с использованием аннотаций @PreAuthorize
и @PostAuthorice
для методов контроллера. Но эти аннотации игнорируются, так как я не включил его в свою защиту Spring.
Если бы у меня был spring-security.xml
, я мог бы включить его со следующей строкой:
<global-method-security pre-post-annotations="enabled" />
К сожалению, у меня есть полная настройка на основе аннотаций. Spring -Обеспечение в принципе работает в моем приложении.
Мой вопрос: Как включить предварительную аннотацию с помощью 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();
}
}
My MessageSecurityWebApplicationInitializer
пуст:
public class MessageSecurityWebApplicationInitializer extends
AbstractSecurityWebApplicationInitializer {
}
Мне пришлось добавить в класс конфигурации следующую аннотацию: @EnableGlobalMethodSecurity(prePostEnabled=true)