6

Spring-Security 4 を Spring MVC Web および REST アプリに実装しようとしています。Spring Security を Java 設定方法で有効にするために 2 つのクラスを追加しました。私はまだ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)

ご覧のとおり、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
}

奇妙なことに、私の web.xml に springSecurityFilterChain を追加すると、実行時に不平を言い、重複した springSecurityFilterChain があると言います。最終的にJava構成で、彼らはこれを行うことに気付きました:

public class MvcWebApplicationInitializer extends
    AbstractAnnotationConfigDispatcherServletInitializer {

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

SecurityConfig を MvcWebApp の Java 構成に登録する場所。これは基本的に、私が信じているサーブレットコンテキストの一部になります。

全体として、springSecurityFilterChain を認識させるにはどうすればよいですか? SecurityConfig クラスを web.xml または既存のアプリケーション コンテキストに登録する必要がありますか?

これが最初の問題領域である可能性があると思います。

public class SecurityWebApplicationInitializer
  extends AbstractSecurityWebApplicationInitializer {

}

春のドキュメントは言う:

代わりに、Spring Security を既存の ApplicationContext に登録する必要があります。たとえば、Spring MVC を使用していた場合、SecurityWebApplicationInitializer.

これは私の場合、SecurityConfig を取り、それを Bean にする必要があることを意味しますか? それとも、@Configuration を介して検出できるため、まだ Bean ではないのでしょうか?

新しいJava構成も使用しながら、XML経由でSecurityConfigを認識させるにはどうすればよいですか?

4

3 に答える 3

2

必要なのは、Spring Security Java Config を 2 つのクラスSecurityConfigとのみで動作させることであると仮定するとSecurityWebApplicationInitializer、動作させるには次の手順を実行する必要があります。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>
于 2015-06-01T11:45:10.980 に答える