无法让提供商工作泽西岛

时间:2016-03-05 21:13:43

标签: java maven jersey jax-rs

我已经用谷歌搜索过但我无法让提供商在我的项目中工作。

的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 个答案:

答案 0 :(得分: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没有提供如何以编程方式阅读的方法   过滤器映射。使用过滤器工作部署应用程序   没错,必须使用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