2016-12-16 3 views
0

webサービス用にcamel cxfを使用していて、soap UIからのヒットの証明書を検証したいと考えています。私はcxfインターセプタを使用していますが、どの資格情報が検証されているかを投稿しますが、Soap UIレスポンスでjson/xml形式でエラーメッセージを出力できません。Camel CXFを使用したSoapHeaderAuthenticationのレスポンスメッセージロギングの問題

以下
<cxf:rsServer id="testingService" 
    address="http://127.0.0.1:8183/test/myWebserviceEndPoint" 
    serviceClass="com.test.SerFac"> 
    <cxf:providers> 
     <bean class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider" /> 
    </cxf:providers> 
    <cxf:inInterceptors> 
      <bean class="org.apache.cxf.interceptor.LoggingInInterceptor" /> 
      <bean class="com.test.BasicAuthAuthorizationInterceptor" /> 
    </cxf:inInterceptors> 
    <cxf:outInterceptors> 
      <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" /> 
    </cxf:outInterceptors> 
</cxf:rsServer> 

がBasicAuthAuthorizationInterceptor.java

public class BasicAuthAuthorizationInterceptor extends SoapHeaderInterceptor { 

protected Logger log = Logger.getLogger(getClass()); 

/** Map of allowed users to this system with their corresponding passwords. */ 
private Map<String,String> users; 

@Required 
public void setUsers(Map<String, String> users) { 
    this.users = users; 
} 

@Override public void handleMessage(Message message) throws Fault { 
    // This is set by CXF 
    AuthorizationPolicy policy = message.get(AuthorizationPolicy.class); 

    // If the policy is not set, the user did not specify credentials 
    // A 401 is sent to the client to indicate that authentication is required 
    if (policy == null) { 
     if (log.isDebugEnabled()) { 
      log.debug("User attempted to log in with no credentials"); 
     } 
     sendErrorResponse(message, HttpURLConnection.HTTP_UNAUTHORIZED); 
     return; 
    } 

    if (log.isDebugEnabled()) { 
     log.debug("Logging in use: " + policy.getUserName()); 
    } 

    // Verify the password 
    String realPassword = users.get(policy.getUserName()); 
    if (realPassword == null || !realPassword.equals(policy.getPassword())) { 
     log.error("Invalid username or password for user: " + policy.getUserName()); 

     sendErrorResponse(message, HttpURLConnection.HTTP_FORBIDDEN); 
    } 
} 

private void sendErrorResponse(Message message, int responseCode) { 
    Message outMessage = getOutMessage(message); 
    outMessage.put(Message.RESPONSE_CODE, responseCode); 

    // Set the response headers 
    Map<String, List<String>> responseHeaders = 
     (Map<String, List<String>>)message.get(Message.PROTOCOL_HEADERS); 
    if (responseHeaders != null) { 
     responseHeaders.put("WWW-Authenticate", Arrays.asList(new String[]{"Basic realm=realm"})); 
     responseHeaders.put("Content-Length", Arrays.asList(new String[]{"0"})); 
    } 
    message.getInterceptorChain().abort(); 
    try { 
     getConduit(message).prepare(outMessage); 
     close(outMessage); 
    } catch (IOException e) { 
     log.warn(e.getMessage(), e); 
    } 
} 

private Message getOutMessage(Message inMessage) { 
    Exchange exchange = inMessage.getExchange(); 
    Message outMessage = exchange.getOutMessage(); 
    if (outMessage == null) { 
     Endpoint endpoint = exchange.get(Endpoint.class); 
     outMessage = endpoint.getBinding().createMessage(); 
     exchange.setOutMessage(outMessage); 
    } 
    outMessage.putAll(inMessage); 
    return outMessage; 
} 

private Conduit getConduit(Message inMessage) throws IOException { 
    Exchange exchange = inMessage.getExchange(); 
    EndpointReferenceType target = exchange.get(EndpointReferenceType.class); 
    Conduit conduit = 
     exchange.getDestination().getBackChannel(inMessage, null, target); 
    exchange.setConduit(conduit); 
    return conduit; 
} 

private void close(Message outMessage) throws IOException { 
    OutputStream os = outMessage.getContent(OutputStream.class); 
    os.flush(); 
    os.close(); 
} 

}石鹸UIから

応答である: - - :

は、以下の私の青写真である

XML形式で: -

生で
<xml/> 

: -

HTTP/1.1 403 Forbidden 
Accept-Encoding: gzip,deflate 
Authorization: Basic fLaWs3dvcmQ6cGFzc3dvcmQx 
Content-Length: 0 
Content-Type: application/json 
Host: localhost:8183 
User-Agent: Apache-HttpClient/4.1.1 (java 1.5) 
WWW-Authenticate: Basic realm=realm 
Server: Jetty(8.1.17.v20150415) 

誰も私が間違った資格情報を与える場合は、石鹸のUIでのエラーメッセージを印刷する方法を助けることができますか?

+0

たぶん私は何かをしないのですが、どこで認証エラーが発生した場合には、 'Fault'を投げていますか?また、なぜあなたのサブクラスのどこにでもその実装を使用していない場合は 'SoapHeaderInterceptor'を拡張しますか?それともあなたですか? – Ralf

+0

Thanks Ralf、AbstractPhaseInterceptor でSoapHeaderAuthenticationの代わりにFaultを試して、与えられたようにxml応答を得ました。しかし、私はそれをJSON形式に変換することはできません。 Json形式でFaultエラーメッセージを出力することは可能ですか? – Som

+0

フォルトフォルト=新しいフォルト( "ユーザーの無効なユーザー名またはパスワード:" + policy.getUserName()、java.util.logging.Logger.getGlobal()); fault.setStatusCode(403); 障害をスローします。 – Som

答えて

関連する問題