【问题标题】:Axis2 WS-Security Running on Java EE Application Server在 Java EE 应用服务器上运行的 Axis2 WS-Security
【发布时间】:2012-07-22 15:25:32
【问题描述】:

我在应用服务器(如 JBoss、WebSphere 和 Weblogic)中运行了 Axis2 Web 服务,直到现在我在请求中传递用户详细信息并在处理之前对用户进行身份验证。

下一步是我想将身份验证位委托给 Java EE 应用服务器,一旦通过身份验证,应用服务器应该传递我将用作上下文来执行请求的 UserPrinciple。

我不确定我的问题是否正确?我想我将 WebContainer 身份验证与 WS-Security 的东西混合在一起。

任何人都可以通过一些文档为我指明正确的方向,我可以将其作为启动指南。

【问题讨论】:

  • 让我改写一下...我想使用axis2 Web服务并希望进行身份验证并将RPINCIPLE传递给我的服务,然后我将使用它来进一步设置我的上下文等...问候
  • 谁能帮我解决这个问题,因为我找到的所有示例都只是解释了如何使用回调来处理用户名和密码,但我找不到任何有关设置用户的信息或然后我可以稍后使用的原则....有人可以帮忙...谢谢

标签: jboss axis2 ws-security websphere-7 weblogic11g


【解决方案1】:

您也可以获取以下格式的 SOAP 标头中传递的用户名。在授权之前,您可以在操作中访问它。您可能不需要 TestRequestContext 类来存储用户名。如果有人不同意,请告诉我。

MessageContext msgContext = MessageContext.getCurrentMessageContext();                   
// Getting HttpServletRequest from Message Context 
String username = (String)msgContext 
                           .getProperty(RampartMessageData.USERNAME);

【讨论】:

    【解决方案2】:

    好的,我尝试了一个解决方案,它在一定程度上奏效了。以下是详细信息;

    使用以下 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 中,我只是使用“身份验证”选项卡来设置用户名和密码并调用?在调用之前我是否需要设置其他任何东西。

    另外,使用 ThreadLocal 的方法是正确的,或者我也可以通过其他方式访问原理?

    谢谢。

    【讨论】:

    • 我也通过从 InflowSecurity 的 节点中删除“时间戳”对异常进行了排序。 SoapUI 没有在标头中添加时间戳信息,因此它失败了。现在一切正常。
    猜你喜欢
    • 2012-12-25
    • 2011-07-13
    • 2012-07-11
    • 2012-08-09
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多