Java 使用JAX-RS进行抢占式身份验证?
我是一个长期的读者和第一次使用,所以请放心我 我正在尝试对javax.ws.rs.client.client使用抢占式身份验证。我已经用HTTPClient实现了这一点,但我不知道如何用JAX验证器实现这一点Java 使用JAX-RS进行抢占式身份验证?,java,authentication,client,jax-rs,preemptive,Java,Authentication,Client,Jax Rs,Preemptive,我是一个长期的读者和第一次使用,所以请放心我 我正在尝试对javax.ws.rs.client.client使用抢占式身份验证。我已经用HTTPClient实现了这一点,但我不知道如何用JAX验证器实现这一点 HttpClient client = new HttpClient(); client.getParams().setAuthenticationPreemptive(true); Credentials creds = new UsernamePasswordC
HttpClient client = new HttpClient();
client.getParams().setAuthenticationPreemptive(true);
Credentials creds = new UsernamePasswordCredentials("user", "pass");
client.getState().setCredentials(AuthScope.ANY, creds);
我确实有使用JAX的基本auth。这是我的客户:
public HttpsConnection() {
// configure ssl
final SslConfigurator sslConfig = SslConfigurator.newInstance().keyStoreFile(keyStore).keyPassword("pass");
final HostnameVerifier hostnameVerifier = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
final SSLContext sslContext = sslConfig.createSSLContext();
// configure client
final Client client = ClientBuilder.newBuilder().sslContext(sslContext).hostnameVerifier(hostnameVerifier)
.register(new Authenticator(username, password)).register(JacksonFeature.class)
.register(new JsonObjectMapper()).build();
WebTarget target = client.target(base_url);
}
这是我的身份验证过滤器:
public class Authenticator implements ClientRequestFilter {
private final String user;
private final String password;
public Authenticator(String user, String password) {
this.user = user;
this.password = password;
}
public void filter(ClientRequestContext requestContext) throws IOException {
MultivaluedMap<String, Object> headers = requestContext.getHeaders();
final String basicAuthentication = getBasicAuthentication();
headers.add("Authorization", basicAuthentication);
}
private String getBasicAuthentication() {
String token = this.user + ":" + this.password;
try {
return "Basic " + DatatypeConverter.printBase64Binary(token.getBytes("UTF-8"));
} catch (UnsupportedEncodingException ex) {
throw new IllegalStateException("Cannot encode with UTF-8", ex);
}
}
}
公共类身份验证器实现ClientRequestFilter{
私有最终字符串用户;
私有最终字符串密码;
公共身份验证器(字符串用户、字符串密码){
this.user=用户;
this.password=密码;
}
公共无效筛选器(ClientRequestContext requestContext)引发IOException{
多值Map headers=requestContext.getHeaders();
最后一个字符串basicAuthentication=getBasicAuthentication();
标题。添加(“授权”,基本认证);
}
私有字符串getBasicAuthentication(){
字符串标记=this.user+:“+this.password;
试一试{
返回“Basic”+DatatypeConverter.printBase64Binary(token.getBytes(“UTF-8”);
}捕获(不支持DencodingException ex){
抛出新的IllegalStateException(“无法使用UTF-8编码”,ex);
}
}
}
有谁能给我举个例子,像我在上面使用HTTPClient一样,使用带有抢占式身份验证的UsernamePasswordCredentials?我一定是在谷歌上搜索所有错误的东西,因为我就是找不到一个例子
如果我的帖子非常糟糕,那么请在它被OP=)关闭之前让我知道。你好,你的问题一点也不糟糕!!事实上,我也有同样的问题。我的代码和您一样工作,希望将其迁移到JAX-RS2.0,但我收到了401个未经授权的错误。你能解决这个问题吗?谢谢。自从我在春天工作以来,我走了一条不同的路。已经很长时间了,我不再能够访问源代码,但我记得这个链接很有帮助。。。您好,我使用以下HttpAuthenticationFeature=HttpAuthenticationFeature.basic(用户名、用户密码)与JAX-RS(Jersey)合作;然后是ClientBuilder.newClient().register(功能)。这就成功了。更多关于