2014-10-25 10 views
17

Ho un'applicazione Spring WebMVC utilizzando le annotazioni @PreAuthorize e @PostAuthorice sui metodi del controller. Ma queste annotazioni sono ignorate poiché non l'ho abilitato nella mia sicurezza di primavera.Abilita preventivamente le annotazioni pre-post di Spring Security

Se avrei un spring-security.xml ho potuto abilitarlo con la seguente riga:

<global-method-security pre-post-annotations="enabled" /> 

Purtroppo ho una configurazione completa Annotazione-based. Spring-Security in linea di principio funziona nella mia applicazione.

La mia domanda: Come abilitare l'annotazione pre-post con una configrazione MVC basata su annotazione?

  • Primavera-Version: 4.0.5.RELEASE
  • Primavera-Security-Version: 3.2.4.RELEASE

Questo è il mio WebSecurityConfigurerAdapter implementazione:

@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(); 
    } 
} 

mio MessageSecurityWebApplicationInitializer è vuoto:

public class MessageSecurityWebApplicationInitializer extends 
     AbstractSecurityWebApplicationInitializer { 

} 

risposta

35

Ho dovuto aggiungere la seguente annotazione alla classe di configurazione: @EnableGlobalMethodSecurity(prePostEnabled=true)