2

Axis2 Webサービスをアプリケーションサーバー(JBoss、WebSphere、Weblogicなど)で実行しています。これまで、リクエスト内でユーザーの詳細を渡し、処理する前にユーザーを認証しています。

次のステップは、認証ビットをJava EEアプリケーションサーバーに委任することです。認証されると、アプリケーションサーバーは、要求を実行するためのコンテキストとして使用するUserPrincipleを渡す必要があります。

正しく質問したかどうかわかりませんか?WebContainer認証とWS-Securityのものを混ぜ合わせていると思います。

誰かが私がスタートアップガイドとして参照できるいくつかのドキュメントで正しい方向を教えてくれますか?

4

2 に答える 2

0

以下の形式でも、SOAP ヘッダーで渡されたユーザー名を取得できます。承認する前に、操作でこれにアクセスできます。ユーザー名を保存するために TestRequestContext クラスを必要としない場合があります。同意しない場合はお知らせください。

MessageContext msgContext = MessageContext.getCurrentMessageContext();                   
// Getting HttpServletRequest from Message Context 
String username = (String)msgContext 
                           .getProperty(RampartMessageData.USERNAME);
于 2015-12-01T16:41:08.773 に答える
0

OK、私は解決策を試しましたが、ある程度は機能しました。詳細は次のとおりです。

次のservices.xmlでTestServiceを作成しました

<serviceGroup>
    <service name="TestWebService" scope="application" targetNamespace="http://TestServiceWs"> 
        <schema schemaNamespace="http://TestServiceWs"/>
        <description>Test POJO Axis2 AAR deployment</description>
        <parameter name="ServiceClass">test.TestServiceWS</parameter>
        <messageReceivers>
            <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
                class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
            <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
                class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
        </messageReceivers>
    </service>

    <module ref="rampart" />

    <parameter name="InflowSecurity">
      <action>
        <items>UsernameToken Timestamp</items>
        <passwordCallbackClass>test.PWHandlerServer</passwordCallbackClass>
      </action>
    </parameter>
</serviceGroup>

PWHandlerServer.java を実装

package test;

import java.io.IOException;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;

import org.apache.ws.security.WSPasswordCallback;

public class PWHandlerServer implements CallbackHandler {

    // the username and password we expect incoming WS calls to use
    private String user = "wsuser";
    private String pwd = "wspwd";

    public void handle (Callback[] callbacks) throws IOException, UnsupportedCallbackException {
        for (int i = 0; i < callbacks.length; i++) {
            if (callbacks[i] instanceof WSPasswordCallback) {
                WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];
                System.out.println("identifier: "+pc.getIdentifer()+", usage: "+pc.getUsage());

                if (pc.getUsage() == WSPasswordCallback.USERNAME_TOKEN) {
                    // for passwords sent in digest mode we need to provide the password,
                    // because the original one can't be un-digested from the message

                    // we can throw either of the two Exception types if authentication fails
                    if (! user.equals(pc.getIdentifer()))
                        throw new IOException("unknown user: "+pc.getIdentifer());

                    // this will throw an exception if the passwords don't match
                    pc.setPassword(pwd);

                } else if (pc.getUsage() == WSPasswordCallback.USERNAME_TOKEN_UNKNOWN) {
                    // for passwords sent in cleartext mode we can compare passwords directly

                    if (! user.equals(pc.getIdentifer()))
                        throw new IOException("unknown user: "+pc.getIdentifer());

                    // we can throw either of the two Exception types if authentication fails
                    if (! pwd.equals(pc.getPassword()))
                        throw new IOException("password incorrect for user: "+pc.getIdentifer());
                }
                // If everything is OK then set the context and move on
                TestRequestContext ctx = new TestRequestContext(pc.getIdentifer());
                TestRequestContext.setRequestContext(ctx);

            } else {
                throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
            }
        }
    }
}

TestRequestContext の実装

package test;


class TestRequestContext {
    private final static ThreadLocal<TestRequestContext> currentContext = new ThreadLocal<TestRequestContext>();

    public static void setRequestContext(TestRequestContext context) {
        currentContext.set(context);
    }

    public static TestRequestContext getRequestContext() {
        return currentContext.get();
    }

    public static void clearRequestContext() {
        currentContext.remove();
    }

    private String userPrincipal;

    public TestRequestContext(String principal) {
        this.userPrincipal = principal;
    }

    public String getUserPrincipal(){
        return this.userPrincipal;
    }
}

これで、Web サービス クラスで TestRequestContext.getUserPricipal() にアクセスし、それをトランザクション用に DB に渡し、セキュリティ コンテキストを切り替えることができるはずです。唯一の問題は、SoapUI を介してサービスにアクセスしようとすると、次の例外が発生することです。

    15:07:34,924 INFO  [STDOUT] identifier: wsuser, usage: 5
15:08:48,081 INFO  [STDOUT] [ERROR] WSDoAllReceiver: security processing failed (actions mismatch)
org.apache.axis2.AxisFault: WSDoAllReceiver: security processing failed (actions mismatch)
        at org.apache.rampart.handler.WSDoAllReceiver.processBasic(WSDoAllReceiver.java:344)
        at org.apache.rampart.handler.WSDoAllReceiver.processMessage(WSDoAllReceiver.java:86)
        at org.apache.rampart.handler.WSDoAllHandler.invoke(WSDoAllHandler.java:72)
        at org.apache.axis2.engine.Phase.invoke(Phase.java:318)
        at org.apache.axis2.engine.AxisEngine.invoke(AxisEngine.java:254)
        at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:160)
        at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:173)
        at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:144)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
        at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:182)
        at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
        at java.lang.Thread.run(Thread.java:619)

SoapUI 内で、'Auth' タブを使用してユーザー名とパスワードを設定し、呼び出していますか? 呼び出しの前に何か他のものを設定する必要がありますか?

また、 ThreadLocal を使用するアプローチは正しいですか、それとも他の方法でも原則にアクセスできますか?

ありがとう。

于 2012-07-26T14:21:51.267 に答える