2016-10-06 1 views
0

Spring MVC에서 Oauth 보안을 구현하려고합니다. java.lang.NoClassDefFoundError를 해결하는 방법 : org.springframework.web.filter.CorsFilter

내 자바 기반의 구성입니다 :

<properties> 
    <spring.version>4.1.2.RELEASE</spring.version> 
    <jackson.version>1.9.13</jackson.version> 
    <javax.validation.version>1.1.0.Final</javax.validation.version> 
    <springsecurity.version>4.1.1.RELEASE</springsecurity.version> 
    <springsecurityoauth2.version>2.0.10.RELEASE</springsecurityoauth2.version> 
    <jackson.library>2.7.5</jackson.library> 
</properties> 

<dependencies> 
    <dependency> 
     <groupId>javax.validation</groupId> 
     <artifactId>validation-api</artifactId> 
     <version>${javax.validation.version}</version> 
    </dependency> 
    <!-- <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>3.8.1</version> 
     <scope>test</scope> 
    </dependency> --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-core</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-web</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-jdbc</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
    <!-- <dependency> 
     <groupId>com.fasterxml.jackson.core</groupId> 
     <artifactId>jackson-core</artifactId> 
     <version>2.4.1</version> 
    </dependency> 
    <dependency> 
     <groupId>com.fasterxml.jackson.core</groupId> 
     <artifactId>jackson-databind</artifactId> 
     <version>2.4.1.1</version> 
    </dependency> --> 

     <dependency> 
     <groupId>com.fasterxml.jackson.core</groupId> 
     <artifactId>jackson-databind</artifactId> 
     <version>${jackson.library}</version> 
    </dependency> 
    <dependency> 
     <groupId>com.fasterxml.jackson.dataformat</groupId> 
     <artifactId>jackson-dataformat-xml</artifactId> 
     <version>${jackson.library}</version> 
    </dependency> 


    <!-- Spring Security --> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-web</artifactId> 
     <version>${springsecurity.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-config</artifactId> 
     <version>${springsecurity.version}</version> 
    </dependency> 

    <!-- Spring Security OAuth2--> 
    <dependency> 
     <groupId>org.springframework.security.oauth</groupId> 
     <artifactId>spring-security-oauth2</artifactId> 
     <version>${springsecurityoauth2.version}</version> 
    </dependency> 



    <dependency> 
     <groupId>log4j</groupId> 
     <artifactId>log4j</artifactId> 
     <version>1.2.17</version> 
    </dependency> 


    <dependency> 
     <groupId>org.slf4j</groupId> 
     <artifactId>slf4j-log4j12</artifactId> 
     <version>1.6.1</version> 
    </dependency> 

    <dependency> 
     <groupId>javax.mail</groupId> 
     <artifactId>mail</artifactId> 
     <version>1.5.0-b01</version> 
    </dependency> 

<dependency> 
    <groupId>org.apache.commons</groupId> 
    <artifactId>commons-lang3</artifactId> 
    <version>3.0</version> 
</dependency> 
    <!-- test --> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.7</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.mockito</groupId> 
     <artifactId>mockito-all</artifactId> 
     <version>1.9.5</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.powermock</groupId> 
     <artifactId>powermock-api-mockito</artifactId> 
     <version>1.5.1</version> 
    </dependency> 
    <dependency> 
     <groupId>org.powermock</groupId> 
     <artifactId>powermock-module-junit4</artifactId> 
     <version>1.5.1</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-test</artifactId> 
     <version>${spring.version}</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>javax.servlet-api</artifactId> 
     <version>3.0.1</version> 
     <scope>test</scope> 
    </dependency> 

내가 웹 스피어 애플리케이션을 사용하고 있습니다 :

공용 클래스 HelloWorldConfiguration이 WebMvcConfigurerAdapter를 확장 {

public void addResourceHandlers(final ResourceHandlerRegistry registry) { 
     registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); 
    } 

@Bean 
    public DataSource dataSource() { 
     final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup(); 
     dsLookup.setResourceRef(true); 
     DataSource dataSource = dsLookup.getDataSource("jdbc/peoplecheck"); 
     return dataSource; 
    } 
@Bean 
    public InternalResourceViewResolver viewResolver() { 
     InternalResourceViewResolver viewResolver = 
         new InternalResourceViewResolver(); 
     viewResolver.setViewClass(JstlView.class); 
     viewResolver.setPrefix("/WEB-INF/"); 
     viewResolver.setSuffix(".jsp"); 
     return viewResolver; 
    } 

은}

이 내 치어입니다 섬기는 사람. 때마다 나는이 예외를 얻을 응용 프로그램을 실행하려고하면 매우 혼란스러워 무엇입니까. 만약 내가 단지 XML 기반으로 구현한다면, XML 기반 또는 둘 다 구현할 필요가 있지만, 전체 프로젝트를 변경할 수 없다.

00000056 DispatcherSer E org.springframework.web.servlet.FrameworkServlet initServletBean Context initialization failed 
           Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.NoClassDefFoundError: org.springframework.web.filter.CorsFilter 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) 
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:591) 
    ... 109 more 
Caused by: java.lang.NoClassDefFoundError: org.springframework.web.filter.CorsFilter 
    at org.springframework.security.config.annotation.web.builders.FilterComparator.<init>(FilterComparator.java:74) 
    at org.springframework.security.config.annotation.web.builders.HttpSecurity.<init>(HttpSecurity.java:121) 
    at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.getHttp(WebSecurityConfigurerAdapter.java:178) 
    at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.init(WebSecurityConfigurerAdapter.java:290) 
    at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.init(WebSecurityConfigurerAdapter.java:67) 
    at org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerSecurityConfiguration$$EnhancerBySpringCGLIB$$91437efb.init(<generated>) 
+0

우수 솔루션을 찾을 수 있습니다. http://stackoverflow.com/questions/34413/why-am-i-getting-a-noclassdeffounderror-in-java –

+0

@SundararajGovindasamy XML 기반 구성없이 동일한 작업이 실행됩니다 ... 나는 이해할 수 없다 "factory 메소드 'springSecurityFilterChain'은 예외를 던졌고 중첩 예외는 java.lang.NoClassDefFoundError : org.springframework.web.filter.CorsFilter" – user3341233

+0

내가 공유 한 링크를 읽었습니까? –

답변

0

나는

+0

하지만 웹 스피어 애플리케이션 서버를 사용하고 있다고하셨습니까? – destan

+0

내 실제 프로젝트 websphere에 거짓말 ... 그리고 바람둥이에 로컬 인스턴스에 노력하고있어 .. 비록 모두 동일한 오류를 보여 ... – user3341233

+0

난 알 겠어. 그럼, 내 새로운 대답을 보았 니? 스프링 프레임 워크 버전을 변경하려고 시도 했습니까? – destan

0

spring-security 4.1.1.RELEASEspring-framework version 4.3.1.RELEASE을 사용}

public class CorsFilter implements Filter { 
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { 
    System.out.println("Filtering on..........................................................."); 
    HttpServletResponse response = (HttpServletResponse) res; 
    response.setHeader("Access-Control-Allow-Origin", "*"); 
    response.setHeader("Access-Control-Allow-Credentials", "true"); 
    response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE"); 
    response.setHeader("Access-Control-Max-Age", "3600"); 
    response.setHeader("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Authorization, Origin, Accept, Access-Control-Request-Method, Access-Control-Request-Headers"); 

    chain.doFilter(req, res); 
} 

...이 내 CorsFilter입니다 ... server..the 스프링 웹 라이브러리가 이미 존재 바람둥이를 통해 배포하고있다. 봄 버전을 업데이트 해 주시겠습니까?

http://docs.spring.io/spring-security/site/docs/4.1.1.RELEASE/reference/htmlsingle/#maven-bom

0

당신의 CorsFilter가 제대로 구현되지 않은 것 같다. 이 필터

public class CORSFilter extends OncePerRequestFilter { 

    private final Logger LOG = LoggerFactory.getLogger(CORSFilter.class); 

    @Override 
    protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws ServletException, IOException { 
     LOG.info("Adding CORS Headers ........................");   
     res.setHeader("Access-Control-Allow-Origin", "*"); 
     res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); 
     res.setHeader("Access-Control-Max-Age", "3600"); 
     res.setHeader("Access-Control-Allow-Headers", "X-PINGOTHER,Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization"); 
     res.addHeader("Access-Control-Expose-Headers", "xsrf-token"); 
     if ("OPTIONS".equals(req.getMethod())) { 
     res.setStatus(HttpServletResponse.SC_OK); 
     } else { 
     chain.doFilter(req, res); 
     }   
    } 
} 

당신은 작업 예를 Cross Origin Request Blocked 희망이 도움말을 참조 할 수 있습니다를 시도해야합니다.