2016-03-05 48 views
0

我google了並嘗試,但我不能讓供應商在我的項目中工作。無法讓供應商工作澤西島

的pom.xml:

<dependencies> 

     <dependency> 
      <groupId>org.glassfish.jersey.containers</groupId> 
      <artifactId>jersey-container-servlet</artifactId> 
      <version>2.22.1</version> 
     </dependency> 

     <dependency> 
      <groupId>org.glassfish.jersey.core</groupId> 
      <artifactId>jersey-client</artifactId> 
      <version>2.22.1</version> 
     </dependency> 

     <dependency> 
      <groupId>org.apache.tomcat</groupId> 
      <artifactId>tomcat-servlet-api</artifactId> 
      <version>9.0.0.M1</version> 
     </dependency> 

     <dependency> 
      <groupId>org.glassfish.jersey.media</groupId> 
      <artifactId>jersey-media-json-jackson</artifactId> 
      <version>2.22.1</version> 
     </dependency> 

     <dependency> 
      <groupId>com.sun.jersey</groupId> 
      <artifactId>jersey-servlet</artifactId> 
      <version>1.19</version> 
     </dependency> 



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

我的篩選:

package xxxxxxx 
import java.io.IOException; 
import java.io.UnsupportedEncodingException; 
import javax.ws.rs.container.ContainerRequestContext; 
import javax.ws.rs.container.ContainerRequestFilter; 
import javax.ws.rs.core.Response; 
import javax.ws.rs.ext.Provider; 
import javax.xml.bind.DatatypeConverter; 

@Provider 
public class BasicAuthFilter implements ContainerRequestFilter { 

    @Override 
    public void filter(ContainerRequestContext requestContext) 
        throws IOException { 

     String authorization = requestContext.getHeaderString("Authorization"); 
     if (!auth(authorization)) { 

       requestContext.abortWith(Response 
        .status(Response.Status.UNAUTHORIZED) 
        .entity("User cannot access the resource.") 
        .build()); 
     } 
    } 


    private Boolean auth(String authorization) 
      throws UnsupportedEncodingException { 
     if (authorization != null && authorization.startsWith("Basic")) { 
      String base64Credentials = authorization 
        .substring("Basic".length()).trim(); 

      byte[] decoded = DatatypeConverter 
        .parseBase64Binary(base64Credentials); 

      final String[] values = new String(decoded, "UTF-8").split(":", 2); 
      if (values.length == 2 && values[0].equals("usuario") 
        && values[1].equals("senha")) { 
       return true; 
      } 
     } 
     return false; 
    } 

} 

我ResourceConfig類:

import javax.ws.rs.ApplicationPath; 

import org.glassfish.jersey.server.ResourceConfig; 

import xxx.providers.BasicAuthFilter; 
import xxx.resource.LogParserResource; 

@ApplicationPath("rest") 
public class ApplicationConfig extends ResourceConfig { 
    public ApplicationConfig() { 
     register(BasicAuthFilter.class); 
     register(MyResource.class); 
    } 
} 

過濾器不會被觸發。在我的配置中有任何錯誤? WEB.XML爲空,資源工作正常。有什麼建議麼?? 謝謝。

回答

1

我注意到,這依賴不是其他的版本:

<dependency> 
     <groupId>com.sun.jersey</groupId> 
     <artifactId>jersey-servlet</artifactId> 
     <version>1.19</version> 
    </dependency> 

,你可以嘗試使用:

<dependency> 
    <groupId>org.glassfish.jersey.containers</groupId> 
    <artifactId>jersey-container-servlet</artifactId> 
    <version>2.22.1</version> 
</dependency> 

然後注意文件:

重要

Servlet 2.x API do不提供如何以編程方式讀取過濾器映射的方法。要正確使用篩選器工作 部署應用程序,必須使用Servlet 3.x容器 (jersey-container-servlet而不是jersey-container-servlet-core), 或者應用程序的上下文路徑需要使用init 參數jersey.config.servlet.filter.contextPath。

見:https://jersey.java.net/documentation/latest/deployment.html#deployment.servlet

+0

我想我是用的servlet的球衣1.xx版本非常感謝你。有效 – fredcrs