2016-03-02 70 views
0

我試圖在oauth2中開發一個客戶端服務器。我的資源服務器和auth服務器已經就位。爲了使客戶端服務器作爲SSO我的安全配置,那麼以下Spring - Oauth2客戶端服務器。 NoSuchMethodError:javax.servlet.ServletContext.getVirtualServerName()在tomcat中運行時

@Configuration 
public class SecurityConfig extends OAuth2SsoConfigurerAdapter { 

    //@Override 
    public void configure(HttpSecurity http) throws Exception { 
     // @formatter:off 
     http.logout() 
       .and() 
      .antMatcher("/**").authorizeRequests() 
       .antMatchers("/index.html", "/home.html", "/", "/login").permitAll().anyRequest() 
       .authenticated() 
       .and() 
      .csrf() 
       .csrfTokenRepository(csrfTokenRepository()) 
       .and() 
      .addFilterAfter(csrfHeaderFilter(), CsrfFilter.class); 
     // @formatter:on 
    } 

    private Filter csrfHeaderFilter() { 
     return new OncePerRequestFilter() { 
      @Override 
      protected void doFilterInternal(HttpServletRequest request, 
        HttpServletResponse response, FilterChain filterChain) 
        throws ServletException, IOException { 
       CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class 
         .getName()); 
       if (csrf != null) { 
        Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN"); 
        String token = csrf.getToken(); 
        if (cookie == null || token != null 
          && !token.equals(cookie.getValue())) { 
         cookie = new Cookie("XSRF-TOKEN", token); 
         cookie.setPath("/"); 
         response.addCookie(cookie); 
        } 
       } 
       filterChain.doFilter(request, response); 
      } 
     }; 
    } 

    private CsrfTokenRepository csrfTokenRepository() { 
     HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository(); 
     repository.setHeaderName("X-XSRF-TOKEN"); 
     return repository; 
    } 
} 

對於OAuth2SsoConfigurerAdapter當我包括我得到的錯誤,當我嘗試它在Tomcat部署POM以下。

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-tx</artifactId> 
    <version>1.0.3.RELEASE</version> 
</dependency> 

例外:

org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/rivigo-oauth-client]] 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154) 
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) 
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) 
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652) 
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1095) 
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1930) 
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:262) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String; 
    at org.apache.tomcat.websocket.server.WsServerContainer.<init>(WsServerContainer.java:149) 
    at org.apache.tomcat.websocket.server.WsSci.init(WsSci.java:131) 
    at org.apache.tomcat.websocket.server.WsSci.onStartup(WsSci.java:47) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5506) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    ... 10 more 

Mar 01, 2016 8:42:22 PM org.apache.catalina.startup.HostConfig deployWAR 
SEVERE: Error deploying web application archive /home/rath/work/installations/apache-tomcat-7.0.64/webapps/rivigo-oauth-client.war 
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/rivigo-oauth-client]] 
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:905) 
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) 
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652) 
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1095) 
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1930) 
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:262) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:745) 

回答

0

明白了。

在pom中包含以下依賴項解決了該問題。

<dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-tomcat</artifactId> 
     <version>1.3.3.RELEASE</version> 
     <scope>provided</scope> 
    </dependency> 
相關問題