【问题标题】:Enable Spring Security pre-post-annotations programmatically以编程方式启用 Spring Security pre-post-annotations
【发布时间】: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


    【解决方案1】:

    我必须在配置类中添加以下注释:@EnableGlobalMethodSecurity(prePostEnabled=true)

    【讨论】:

      猜你喜欢
      • 2011-03-31
      • 1970-01-01
      • 1970-01-01
      • 2012-06-04
      • 2013-11-17
      • 2011-04-26
      • 2015-02-04
      • 2011-12-15
      • 1970-01-01
      相关资源
      最近更新 更多