0

Apache CFX를 사용하여 HP Quality Center에 REST 커넥터를 쓰고 있습니다. 서버 요청시 CFX 인프라를 사용하여 선점 인증을 수행하고 싶습니다.Apache CFX 기본 인증 HP Quality Center

HP Quality Center는 기본 기반 인증 메커니즘을 사용합니다. 인증을 위해 get 요청은 표준 기본 인증 헤더를 사용하여 http : /// qcbin/authentication-point/authenticate로 전송됩니다. 서버는 모든 후속 요청에 포함되어야하는 쿠키 ("LWSSO")를 반환합니다. 인증 전에 서버에서 자원을 요청하면 인증 지점 URI (예 : LWSSO 영역 = "http : // : 80/qcbin/authentication-point)가 포함 된 WWW-Authenticate 헤더가있는 401이됩니다.

이상적으로 말하면, 401 응답을 가로 채서 WWW-Authenticate 헤더를 구문 분석하고 모든 후속 요청에 대한 쿠키를 캐싱하기 전에 인증 포인트 URI에 대한 요청을 수행하여 인증을 처리하는 CFX HttpAuthProvider 또는 Interceptor를 만들려고합니다.

팩토리 패턴을 사용하여 깨끗한 프록시 기반 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); 
} 
)

나는 이것이 가능하고 기능을 구현하는 데 가장 적합한 곳인지 궁금하다.

답변

0

Apache CXF를 사용하지 않고이 문제를 해결했습니다. 대신 저지를 선택했습니다.

두 개의 JAXRS 필터를 생성하여이 작업을 수행했습니다. 이 첫 번째 필터는 서버에서 401 응답을 가로 채고 "WWW-Authenticate"헤더에 반환 된 인증 지점 주소를 따르고 기본 인증을 수행합니다. 원래 요청은 서버로 재생됩니다.

이 방정식의 두 번째 부분은 세션 쿠키 유지 관리를 처리하는 또 다른 필터입니다. 따라서 초기 요청이 재생되면 인증 쿠키가 제공됩니다.

두 개의 필터

는 다음과 같다 :

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);   
     } 
    } 

}