2016-09-16 55 views
0

我想我的REST API中實現Spring Security的實例。在我開始實施Spring之前,我的項目工作得很好。我相信這個問題有東西在一些這樣或那樣做春天開機。異常發送上下文初始化事件監聽器類org.springframework.web.context.ContextLoaderListener錯誤

堆棧跟蹤:

Sep 16, 2016 12:52:27 PM org.apache.catalina.core.StandardContext listenerStart 
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener 
java.lang.IllegalAccessError: tried to access method org.springframework.core.convert.support.DefaultConversionService.addCollectionConverters(Lorg/springframework/core/convert/converter/ConverterRegistry;)V from class org.springframework.boot.bind.RelaxedConversionService 
    at org.springframework.boot.bind.RelaxedConversionService.<init>(RelaxedConversionService.java:52) 
    at org.springframework.boot.bind.RelaxedDataBinder.modifyProperties(RelaxedDataBinder.java:148) 
    at org.springframework.boot.bind.RelaxedDataBinder.doBind(RelaxedDataBinder.java:128) 
    at org.springframework.validation.DataBinder.bind(DataBinder.java:630) 
    at org.springframework.boot.autoconfigure.EnableAutoConfigurationImportSelector.getExcludeAutoConfigurationsProperty(EnableAutoConfigurationImportSelector.java:161) 
    at org.springframework.boot.autoconfigure.EnableAutoConfigurationImportSelector.getExclusions(EnableAutoConfigurationImportSelector.java:152) 
    at org.springframework.boot.autoconfigure.EnableAutoConfigurationImportSelector.selectImports(EnableAutoConfigurationImportSelector.java:80) 
    at org.springframework.context.annotation.ConfigurationClassParser.processDeferredImportSelectors(ConfigurationClassParser.java:429) 
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:183) 
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:306) 
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:239) 
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:254) 
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:94) 
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:606) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462) 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4729) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5167) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at java.lang.Thread.run(Thread.java:745) 

的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/maven-v4_0_0.xsd"> 

    <modelVersion>4.0.0</modelVersion> 

    <groupId>org.dbran.api</groupId> 
    <artifactId>blog</artifactId> 
    <packaging>war</packaging> 
    <version>0.0.1-SNAPSHOT</version> 
    <name>blog</name> 

    <build> 
     <finalName>blog</finalName> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.5.1</version> 
       <inherited>true</inherited> 
       <configuration> 
        <source>1.7</source> 
        <target>1.7</target> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 

    <dependencyManagement> 
     <dependencies> 
      <dependency> 
       <groupId>org.glassfish.jersey</groupId> 
       <artifactId>jersey-bom</artifactId> 
       <version>${jersey.version}</version> 
       <type>pom</type> 
       <scope>import</scope> 
      </dependency> 
     </dependencies> 
    </dependencyManagement> 

    <dependencies> 
     <dependency> 
      <groupId>org.glassfish.jersey.containers</groupId> 
      <artifactId>jersey-container-servlet-core</artifactId> 
      <!-- use the following artifactId if you don't need servlet 2.x compatibility --> 
      <!-- artifactId>jersey-container-servlet</artifactId --> 
     </dependency> 

     <dependency> 
      <groupId>org.glassfish.jersey.media</groupId> 
      <artifactId>jersey-media-moxy</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>3.8.1</version> 
      <scope>test</scope> 
     </dependency> 

     <!-- spring-context which provides core functionality --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
      <version>4.1.6.RELEASE</version> 
     </dependency> 

     <!-- The spring-aop module provides an AOP Alliance-compliant aspect-oriented 
      programming implementation allowing you to define --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-aop</artifactId> 
      <version>4.2.5.RELEASE</version> 
     </dependency> 

     <!-- The spring-webmvc module (also known as the Web-Servlet module) contains 
      Spring’s model-view-controller (MVC) and REST Web Services implementation 
      for web applications --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>4.2.5.RELEASE</version> 
     </dependency> 

     <!-- The spring-web module provides basic web-oriented integration features 
      such as multipart file upload functionality and the initialization of the 
      IoC container using Servlet listeners and a web-oriented application context --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
      <version>4.2.5.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-core</artifactId> 
      <version>4.1.3.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-web</artifactId> 
      <version>4.1.3.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-config</artifactId> 
      <version>4.1.3.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-taglibs</artifactId> 
      <version>4.1.3.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-acl</artifactId> 
      <version>4.1.3.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.codehaus.jackson</groupId> 
      <artifactId>jackson-jaxrs</artifactId> 
      <version>1.9.13</version> 
     </dependency> 
     <dependency> 
      <groupId>io.jsonwebtoken</groupId> 
      <artifactId>jjwt</artifactId> 
      <version>0.6.0</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>javax.servlet-api</artifactId> 
      <version>3.1.0</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-actuator</artifactId> 
      <version>1.3.3.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
      <version>1.3.3.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-security</artifactId> 
      <version>1.3.3.RELEASE</version> 
     </dependency> 
    </dependencies> 
    <properties> 
     <jersey.version>2.16</jersey.version> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 

</project> 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE xml> 
<!-- This web.xml file is not required when using Servlet 3.0 container, 
    see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html --> 
<web-app id="WebApp_ID" version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 

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

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/applicationContext.xml</param-value> 
    </context-param> 


    <servlet> 
     <servlet-name>Jersey Web Application</servlet-name> 
     <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
     <init-param> 
      <param-name>jersey.config.server.provider.packages</param-name> 
      <param-value>org.dbran.api.blog</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Jersey Web Application</servlet-name> 
     <url-pattern>/webapi/*</url-pattern> 
    </servlet-mapping> 
    <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>/webapi/*</url-pattern> 
    </filter-mapping> 
</web-app> 

的applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:security="http://www.springframework.org/schema/security" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.1.xsd 
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-4.1.xsd"> 

    <context:component-scan base-package="org.dbran.api.blog" /> 

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

    <security:http pattern="/webapi/auth" security="none"/> 

    <security:http pattern="/webapi/**" entry-point-ref="jwtAuthenticationEntryPoint" create-session="stateless"> 
     <security:csrf disabled="true"/> 
     <security:custom-filter before="FORM_LOGIN_FILTER" ref="jwtAuthenticationFilter"/> 
    </security:http> 

    <bean id="jwtAuthenticationFilter" class="org.dbran.api.blog.security.JwtAuthenticationTokenFilter"> 
     <property name="authenticationManager" ref="authenticationManager" /> 
     <property name="authenticationSuccessHandler" ref="jwtAuthenticationSuccessHandler" /> 
    </bean> 
    <security:authentication-manager alias="authenticationManager"> 
     <security:authentication-provider ref="jwtAuthenticationProvider" /> 
    </security:authentication-manager> 
</beans> 

回答

0

此錯誤是在你的類路徑的Spring庫的一些非兼容版本的結果。彈簧引導依賴通常應注意在其所有的需要的依賴拉動的,但如果因爲任何原因,你必須手動聲明所有的相關性,則始終確保您使用相同的春季版本。在你的pom目前你有多個4.1.3和4.2.5的依賴關係。

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-web</artifactId> 
    <version>4.2.5.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework.security</groupId> 
    <artifactId>spring-security-core</artifactId> 
    <version>4.1.3.RELEASE</version> 
</dependency> 
相關問題