我正在使用Apache CFX编写到HP Quality Center的REST连接器。我希望在向服务器发出请求时使用CFX基础设施进行抢占式身份验证
HP Quality Center使用基于Basic的身份验证机制。为了进行身份验证,需要向http:///qcbin/authentication-point/authenticate发送一个带有标准基本身份验证头的get请求。然后,服务器返回一个cookie ("LWSSO"),该cookie必须包含在所有后续请求中。在身份验证之前从服务器请求资源将导致包含身份验证点URI (例如,LWSSO realm="http://:80/qcbin/authentication-point)的带有WWW-Authenticate报头的401。
理想情况下,我希望创建一个cookie或拦截器来处理身份验证,方法是截取401响应,解析出WWW-Authenticate头部,并在为所有后续请求缓存HttpAuthProvider之前在身份验证点URI上执行请求。
这将允许我使用工厂模式创建一个干净的基于代理的API。例如:
public QualityCenter create(String url, String username, String password) {
JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
bean.setAddress(url);
bean.setUsername(username);
bean.setPassword(password);
bean.setServiceClass(QualityCenter.class);
// TODO: Setup authentication modules here that use AuthPolicy for credentials.
return bean.create(QualityCenter.class);
}
我只是不知道这是不是可能的,在哪里实现这个功能最好。
发布于 2014-12-03 22:00:33
我最终在没有使用Apache CXF的情况下解决了这个问题--而是选择了Jersey。
我通过创建两个JAXRS过滤器实现了这一点。第一个过滤器截取401来自服务器的响应,遵循"WWW-Authenticate“报头中返回的认证点地址,并执行基本认证。然后将原始请求重放到服务器。
这个等式的第二部分是另一个处理会话cookie维护的过滤器。因此,当重放初始请求时,身份验证cookie存在。
这两个过滤器如下所示:
class AuthenticationFilter implements ClientResponseFilter {
private final String username;
private final String password;
public AuthenticationFilter(String username, String password) {
this.username = username;
this.password = password;
}
@Override
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
if (responseContext.getStatus() == Response.Status.UNAUTHORIZED.getStatusCode()) {
String header = responseContext.getHeaders().getFirst(WWW_AUTHENTICATE);
// Check if we have been given the authentication redirect go-ahead.
if (!header.startsWith("LWSSO")) {
return;
}
String authUri = header.substring(13, header.length() - 1);
Client client = ClientBuilder.newClient(requestContext.getConfiguration());
String credentials = "Basic " + Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
Response response = client.target(authUri).path("authenticate").request(MediaType.TEXT_PLAIN_TYPE).header(AUTHORIZATION, credentials).get();
if (response.getStatus() == Response.Status.OK.getStatusCode()) {
URI uri = requestContext.getUri();
MediaType mediaType = requestContext.getMediaType();
String method = requestContext.getMethod();
NewCookie cookie = response.getCookies().get("LWSSO_COOKIE_KEY");
MultivaluedMap<String, Object> headers = requestContext.getHeaders();
headers.remove(WWW_AUTHENTICATE);
Invocation.Builder builder = requestContext.getClient().target(uri).request(mediaType).headers(headers).cookie(cookie);
Invocation invocation;
if (requestContext.getEntity() != null) {
invocation = builder.build(method, Entity.entity(
requestContext.getEntity(), mediaType));
} else {
invocation = builder.build(method);
}
Response replayed = invocation.invoke();
responseContext.setStatus(replayed.getStatus());
responseContext.setStatusInfo(replayed.getStatusInfo());
if (replayed.hasEntity()) {
responseContext.setEntityStream(replayed
.readEntity(InputStream.class));
}
responseContext.getHeaders().clear();
responseContext.getHeaders()
.putAll(replayed.getStringHeaders());
}
}
}}
class SessionFilter implements ClientRequestFilter, ClientResponseFilter {
private final Map<String, NewCookie> cookies = new HashMap<String, NewCookie>();
@Override
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
cookies.putAll(responseContext.getCookies());
}
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
for(NewCookie cookie: cookies.values()) {
requestContext.getHeaders().add("Cookie", cookie);
}
}
}
https://stackoverflow.com/questions/27181664
复制相似问题