Authentication Jersey客户端API-身份验证

Authentication Jersey客户端API-身份验证,authentication,client,jersey,Authentication,Client,Jersey,我正在使用Jersey客户端API向JAX-WS Web服务提交SOAP请求。默认情况下,Jersey在被质询时以某种方式使用我的Windows Nt凭据进行身份验证。有人能解释Jersey在代码中的位置吗?它能被推翻吗 我已经尝试使用HTTPBasicAuthFilter并将其添加为客户端上的筛选器。我还尝试将我的凭据添加到WebResource queryParams字段,但两个字段都没有被选中。在《泽西岛用户指南》中有一小部分介绍了这一点。我建议您按照它的建议,尝试使用HttpURLCon

我正在使用Jersey客户端API向JAX-WS Web服务提交SOAP请求。默认情况下,Jersey在被质询时以某种方式使用我的Windows Nt凭据进行身份验证。有人能解释Jersey在代码中的位置吗?它能被推翻吗


我已经尝试使用HTTPBasicAuthFilter并将其添加为客户端上的筛选器。我还尝试将我的凭据添加到WebResource queryParams字段,但两个字段都没有被选中。

在《泽西岛用户指南》中有一小部分介绍了这一点。我建议您按照它的建议,尝试使用HttpURLConnection而不是HttpURLConnection,因为它对您想做的任何事情都有更好的支持。

起初,我在Jersey用户指南中记录了这一点

Authenticator.setDefault (authinstance);
但是我不喜欢这样,因为它依赖于设置全局身份验证器。经过一些研究,我发现泽西岛上有一个更容易使用的
HTTPBasicAuthFilter

Client c = Client.create();
c.addFilter(new HTTPBasicAuthFilter(user, password));
见: 球衣2.x版:

HttpAuthenticationFeature=HttpAuthenticationFeature.basicBuilder()
.非强制性的
.凭证(“用户”、“密码”)
.build();
ClientConfig ClientConfig=new ClientConfig();
clientConfig.register(功能);
Client Client=ClientBuilder.newClient(clientConfig);

参考资料:

如果您正在测试Dropwizard应用程序(可能适合任何REST服务),您可以使用以下示例:

请查找以下不带SSL的工作代码

我正在使用put请求,如果需要post/get,请更改它

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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.javacodegeeks.enterprise.rest.jersey</groupId>
    <artifactId>JerseyJSONExample</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <repositories>
        <repository>
            <id>maven2-repository.java.net</id>
            <name>Java.net Repository for Maven</name>
            <url>http://download.java.net/maven/2/</url>
            <layout>default</layout>
        </repository>
    </repositories>

    <dependencies>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.9</version>
        </dependency>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</artifactId>
            <version>1.9</version>
        </dependency>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-json</artifactId>
            <version>1.9</version>
        </dependency>

    </dependencies>

</project>
波乔


添加此答案,因为我一直在为旧版本的Jersey寻找答案,这些答案在2.x中不再相关

对于Jersey 2,有几种方法。 看看:

这是一个为我工作的(最简单的基本授权)


是的,对于jersey 2.x,您可以使用基本身份验证(抢占模式)对每个请求进行身份验证:


这正是我要找的。我将其添加到我们的客户端,并在服务器上使用Spring安全性。非常优雅地工作,为应用程序增加了安全性。参见下面的Jase2.XI的答案:<代码> HtpBasic AutoStudio功能=新的HtpBasic AuthFutter(RestuelError,RestPosit);client.addFilter(特性)但是由于一些未知的原因,我一直以
null
的形式获取特性。为什么会发生这种情况,你知道吗?或者:Response Response=client.target(“.property(HTTP\u AUTHENTICATION\u BASIC\u USERNAME,“homer”).property(HTTP\u AUTHENTICATION\u BASIC\u PASSWORD,“p1swd745”).get();我也这么做了!!但仍然没有验证工作!。
package com.rest.jersey.jerseyclient;

import com.rest.jersey.dto.Employee;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import com.sun.jersey.api.client.filter.LoggingFilter;
import com.sun.jersey.api.json.JSONConfiguration;

public class JerseyClient {

    public static void main(String[] args) {
        try {

            String username = "username";
            String password = "p@ssword";


            //{"userId":"12345","name ":"Viquar","surname":"Khan","email":"Vaquar.khan@gmail.com"}





            Employee employee = new Employee("Viquar", "Khan", "Vaquar.khan@gmail.com");


            ClientConfig clientConfig = new DefaultClientConfig();

            clientConfig.getFeatures().put( JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);

            Client client = Client.create(clientConfig);
            //


                final HTTPBasicAuthFilter authFilter = new HTTPBasicAuthFilter(username, password);
                client.addFilter(authFilter);
                client.addFilter(new LoggingFilter());

            //
            WebResource webResource = client
                    .resource("http://localhost:7001/VaquarKhanWeb/employee/api/v1/informations");

              ClientResponse response = webResource.accept("application/json")
                .type("application/json").put(ClientResponse.class, employee);


            if (response.getStatus() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + response.getStatus());
            }

            String output = response.getEntity(String.class);

            System.out.println("Server response .... \n");
            System.out.println(output);

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}
package com.rest.jersey.dto;

public class Employee {

    private String name;
    private String surname;
    private String email;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSurname() {
        return surname;
    }
    public void setSurname(String surname) {
        this.surname = surname;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    @Override
    public String toString() {
        return "Employee [name=" + name + ", surname=" + surname + ", email=" + email + "]";
    }
    public Employee(String name, String surname, String email) {
        super();
        this.name = name;
        this.surname = surname;
        this.email = email;
    }

}
    ClientConfig config = new ClientConfig();

    HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("username", "password");

    Client client = ClientBuilder.newClient(config);
    client.register(feature);

    WebTarget webTarget = client.target("http://api.asite.com/api").path("v1/reports/list");
    Invocation.Builder invocationBuilder =  webTarget.request(MediaType.TEXT_PLAIN_TYPE);

    Response response = invocationBuilder.get();

    System.out.println( response.getStatus() );
    System.out.println( response.readEntity(String.class) );
 client.register(HttpAuthenticationFeature.basic(userName, password));
 // rest invocation code ..