如何使用XML和Java配置实现Spring Security 4

log*_*yer 5 spring-mvc spring-security spring-restcontroller

我正在尝试将Spring-Security 4实现到我的Spring MVC web和rest app中.我添加了2个类来启用Spring安全性的java配置方式.我仍然希望保留我的web.xml而不是将整个项目更改为使用java配置.一旦我这样做,我收到以下错误:

29-May-2015 08:47:12.826 SEVERE [localhost-startStop-1]  
org.apache.catalina.core.StandardContext.filterStart Exception starting   
filter springSecurityFilterChain
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean   
named 'springSecurityFilterChain' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.
getBeanDefinition(DefaultListableBeanFactory.java:687)
Run Code Online (Sandbox Code Playgroud)

如您所见,它表示无法识别springSecurityFilterChain.这当然应该由@EnableWebSecurity启用,如下所示:

用于Spring Security的类:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
  auth.inMemoryAuthentication().withUser("abc").password("123456").roles("USER");
  auth.inMemoryAuthentication().withUser("admin").password("123456").roles("ADMIN");
  auth.inMemoryAuthentication().withUser("dba").password("123456").roles("DBA");
}

@Override
protected void configure(HttpSecurity http) throws Exception {

  http.authorizeRequests()
    .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
    .antMatchers("/dba/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_DBA')")
    .and().formLogin();

    }
}

public class SpringSecurityInitializer extends AbstractSecurityWebApplicationInitializer {
   //do nothing
}
Run Code Online (Sandbox Code Playgroud)

奇怪的是,如果我将springSecurityFilterChain添加到我的web.xml,在运行时它会抱怨并说有一个重复的springSecurityFilterChain.我注意到最终在Java配置中,他们这样做:

public class MvcWebApplicationInitializer extends
    AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class[] { SecurityConfig.class };
}
Run Code Online (Sandbox Code Playgroud)

他们使用MvcWebApp的Java配置注册SecurityConfig.这基本上使它成为我相信的servlet上下文的一部分.

总而言之,我如何识别springSecurityFilterChain?我是否需要在web.xml或现有应用程序上下文中注册我的SecurityConfig类?

我想这可能是第一个问题领域:

public class SecurityWebApplicationInitializer
  extends AbstractSecurityWebApplicationInitializer {

}
Run Code Online (Sandbox Code Playgroud)

春天的文档说:

相反,我们应该使用现有的ApplicationContext注册Spring Security.例如,如果我们使用Spring MVC我们的SecurityWebApplicationInitializer.

这可能意味着在我的情况下,我可以采取SecurityConfig并应该使它成为一个bean?或者它不是一个bean,因为它可以通过@Configuration来解决?

如何在使用新的Java配置时通过XML识别SecurityConfig?

Aru*_*unM 2

假设您想要让 Spring Security Java Config 仅使用 2 个类SecurityConfigSecurityWebApplicationInitializer,您需要执行以下操作才能使其工作。请注意,我假设您的 spring mvc 配置仍然是 XML。请注意,com.mkyong.web.config包中将包含该类SecurityConfig。这将确保 Web 上下文具有可用的安全配置。

web.xml如下

<context-param>
          <param-name>contextClass</param-name>
          <param-value>
              org.springframework.web.context.support.AnnotationConfigWebApplicationContext
          </param-value>
      </context-param>
      <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>com.mkyong.web.config</param-value>
      </context-param>

   <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
Run Code Online (Sandbox Code Playgroud)