我正在使用Jersey客戶端API將SOAP請求提交給JAX-WS webservice。默認情況下,澤西島以某種方式使用我的Windows Nt憑據進行身份驗證時遇到問題。任何人都可以解釋澤西在代碼中做了什麼嗎?它可以被覆蓋嗎?Jersey Client API - 身份驗證
我試過使用HTTPBasicAuthFilter並添加爲客戶端上的過濾器。我也嘗試將我的憑據添加到WebResoruce queryParams字段,但是都沒有被拾起。
我正在使用Jersey客戶端API將SOAP請求提交給JAX-WS webservice。默認情況下,澤西島以某種方式使用我的Windows Nt憑據進行身份驗證時遇到問題。任何人都可以解釋澤西在代碼中做了什麼嗎?它可以被覆蓋嗎?Jersey Client API - 身份驗證
我試過使用HTTPBasicAuthFilter並添加爲客戶端上的過濾器。我也嘗試將我的憑據添加到WebResoruce queryParams字段,但是都沒有被拾起。
起初,我得到這個工作作爲澤西用戶指南
Authenticator.setDefault (authinstance);
但是我不喜歡這個,因爲它依賴於建立全球化的認證中記錄。經過一些研究後,我發現澤西島有一個HTTPBasicAuthFilter
,它更容易使用。
Client c = Client.create();
c.addFilter(new HTTPBasicAuthFilter(user, password));
參見: https://jersey.github.io/nonav/apidocs/1.10/jersey/com/sun/jersey/api/client/filter/HTTPBasicAuthFilter.html https://jersey.github.io/nonav/apidocs/1.10/jersey/com/sun/jersey/api/client/filter/Filterable.html#addFilter(com.sun.jersey.api.client.filter.ClientFilter)
澤西島用戶指南中有一小部分關於Client authentication。我建議你遵循它的建議,並嘗試使用Apache HTTP Client而不是HttpURLConnection,因爲它對任何你想做的事情有更好的支持。
新澤西2.X:
HttpAuthenticationFeature feature = HttpAuthenticationFeature.basicBuilder()
.nonPreemptive()
.credentials("user", "password")
.build();
ClientConfig clientConfig = new ClientConfig();
clientConfig.register(feature) ;
Client client = ClientBuilder.newClient(clientConfig);
或:response response = client.target(「http:// localhost:8080/rest/homer/contact」).request() .property(HTTP_AUTHENTICATION_BASIC_USERNAME,「homer」) .property(HTTP_AUTHENTICATION_BASIC_PASSWORD,「p1swd745」) 。得到(); – Dejell
如果您正在測試Dropwizard應用程序(也許它適合任何REST服務),你可以用這個作爲例子: https://github.com/dropwizard/dropwizard/blob/v0.8.1/dropwizard-auth/src/test/java/io/dropwizard/auth/basic/BasicAuthProviderTest.java
請找出以下不使用SSL
我使用PUT請求工作的代碼,如果需要郵寄/得到的只是改變它。
的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>
的Java類
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 = "[email protected]";
//{"userId":"12345","name ":"Viquar","surname":"Khan","email":"[email protected]"}
Employee employee = new Employee("Viquar", "Khan", "[email protected]");
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();
}
}
}
POJO
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;
}
}
添加這個答案,因爲我一直在尋找答案了舊版本的球衣是沒有較長有關2.x版本
澤西島2有幾種方法。 看看:
JavaDoc for org.glassfish.jersey.client.authentication.HttpAuthenticationFeature
這裏是爲我工作的一個(最簡單的基本身份驗證恕我直言)
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));
是的球衣2。X,你可以做到這一點來驗證使用基本身份驗證(搶佔模式)每個請求:
client.register(HttpAuthenticationFeature.basic(userName, password));
// rest invocation code ..
這正是我一直在尋找。我將其添加到我們的客戶端,並在服務器部分使用了Spring Security。非常優雅地爲應用程序添加安全性。 – bh5k
查看下面的答案Jersey 2.x – Dejell
我這樣做:'HTTPBasicAuthFilter功能=新的HTTPBasicAuthFilter(restUsername,restPassword); client.addFilter(feature);'但由於某些未知的原因,我一直以'null'爲特徵。爲什麼會發生,有什麼想法? – HitchHiker