2016-08-08 89 views
0

這裏是我的根應用上下文類名爲 'springSecurityFilterChain' 無豆在web.xml和Java的配置的組合定義

@Configuration 
@Import({WebSecurityConfig.class}) 
public class ContextConfig { 
} 

我WebSecurityConfig類

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{ 
     @Override 
     public void configure(WebSecurity web) throws Exception { 
       web.ignoring().antMatchers("/resources/**"); 
     } 

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
       http.authorizeRequests().antMatchers("/Hello/**").permitAll().anyRequest() 
           .hasRole("USER") 
           .and() 
           .formLogin() 
           .permitAll(); 
     } 

     @Override 
     protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
       auth.inMemoryAuthentication().withUser("user").password("password").roles("USER") 
           .and().withUser("admin").password("password").roles("USER", "ADMIN"); 
     } 

     @Autowired 
     public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
      auth.inMemoryAuthentication().withUser("user").password("password").roles("USER"); 
     } 
} 

我的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> 
    <display-name>webtest</display-name> 
    <welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
    <welcome-file>index.htm</welcome-file> 
    <welcome-file>index.jsp</welcome-file> 
    <welcome-file>default.html</welcome-file> 
    <welcome-file>default.htm</welcome-file> 
    <welcome-file>default.jsp</welcome-file> 
    </welcome-file-list> 
    <servlet> 
    <servlet-name>dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>/</url-pattern> 
    </servlet-mapping> 

    <listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <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>classpath:com/iis/webtest/config/ContextConfig</param-value> 
    </context-param> 
    <filter> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    </filter> 
    <filter-mapping> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> 
    </filter-mapping> 
</web-app> 

我的調度員servlet xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <context:component-scan base-package="com.iis.*" annotation-config="true" ></context:component-scan> 
    <mvc:resources location="/test/" mapping="/**"/>  
    <mvc:default-servlet-handler/> 
    <mvc:annotation-driven/> 

</beans> 

我的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.iis</groupId> 
    <artifactId>webtest</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 

    <name>webtest</name> 
    <url>http://maven.apache.org</url> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>3.8.1</version> 
      <scope>test</scope> 
     </dependency> 
     <!-- http://mvnrepository.com/artifact/javax/javaee-api --> 
     <dependency> 
      <groupId>javax</groupId> 
      <artifactId>javaee-api</artifactId> 
      <version>7.0</version> 
      <scope>provided</scope> 
     </dependency> 
     <!-- http://mvnrepository.com/artifact/org.springframework/spring-core --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-core</artifactId> 
      <version>4.3.0.RELEASE</version> 
     </dependency> 
     <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>4.3.0.RELEASE</version> 
     </dependency> 
     <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-core --> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-core</artifactId> 
      <version>4.1.1.RELEASE</version> 
     </dependency> 
     <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-web --> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-web</artifactId> 
      <version>4.1.1.RELEASE</version> 
     </dependency> 
     <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-config --> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-config</artifactId> 
      <version>4.1.1.RELEASE</version> 
     </dependency> 
     <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> 
     <dependency> 
      <groupId>com.fasterxml.jackson.core</groupId> 
      <artifactId>jackson-core</artifactId> 
      <version>2.8.1</version> 
     </dependency> 
     <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> 
     <dependency> 
      <groupId>com.fasterxml.jackson.core</groupId> 
      <artifactId>jackson-databind</artifactId> 
      <version>2.8.1</version> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.5.1</version> 
       <configuration> 
        <source>1.8</source> 
        <target>1.8</target> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

我試過xml配置以確保安全。
我總是得到相同的錯誤。
我已經在尋找解決方案,但我仍然無法找到我要去哪裏的錯誤。

Stacktrace 
Aug 08, 2016 5:44:39 PM org.springframework.web.context.ContextLoader initWebApplicationContext 
INFO: Root WebApplicationContext: initialization completed in 212 ms 
Aug 08, 2016 5:44:39 PM org.apache.catalina.core.StandardContext filterStart 
SEVERE: 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:701) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1180) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) 
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1082) 
    at org.springframework.web.filter.DelegatingFilterProxy.initDelegate(DelegatingFilterProxy.java:326) 
    at org.springframework.web.filter.DelegatingFilterProxy.initFilterBean(DelegatingFilterProxy.java:235) 
    at org.springframework.web.filter.GenericFilterBean.init(GenericFilterBean.java:199) 
    at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279) 
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260) 
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105) 
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4830) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5510) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565) 
    at java.util.concurrent.FutureTask.run(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 

Aug 08, 2016 5:44:39 PM org.apache.catalina.core.StandardContext startInternal 
+0

我可以看到你的堆棧跟蹤...並通過減少春季嘗試 - 安全jar版本到3.2.5(我有相同的prblm,其固定的) –

+0

@PrasannaKumar請參閱stacktrace。 – Piyush

+0

是的...正在減少彈簧安全瓶版本 –

回答

0

不知道的原因,但只是從

<param-value>classpath:com/iis/webtest/config/ContextConfig</param-value> 

不斷變化的環境的配置位置

<param-value>com.iis.webtest.config.ContextConfig</param-value> 

工作

+0

你以前說過你嘗試過用於xml ..在這種情況下,它也不工作(因爲classpath不會進入圖片)? –

+0

是的,我嘗試過,但是我只在類路徑中保存了XML,所以classpath在那裏。 我把xml保存在配置包中。 如果你現在知道異常的原因,那麼請分享。 – Piyush

+0

原因是沒有太多..它試圖找到配置文件/類...因爲給定的路徑是不正確的,它無法找到..因此,它沒有給bean定義... –

0
<context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>classpath:com/iis/webtest/config/ContextConfig, 
classpath:com/iis/webtest/config/WebSecurityConfig </param-value> 
     </context-param> 

使用上面的代碼在web.xml(因爲你需要提及在web.xml春季安全**WebSecurityConfig**)。

+0

XML驗證器在給出context-param內的2個參數值元素時發生錯誤。還刪除xsi和xmlns(忽略驗證)後,我得到相同的錯誤。 – Piyush

+0

此外,我已經使用導入註釋包括WebSecurityConfig – Piyush

+0

刪除導入WebSecurityConfig..keep只有在web.xml ..之後,如果你得到同樣的錯誤讓我知道 –

相關問題