JASPIC Wildfly 9使用会话验证请求

kno*_*noe 5 java session ejb jaspic wildfly

基于这个Jaspic示例,我为以下validateRequest方法编写了以下方法ServerAuthModule:

public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject,
        Subject serviceSubject) throws AuthException {

    boolean authenticated = false;
    final HttpServletRequest request = 
                      (HttpServletRequest) messageInfo.getRequestMessage();
    final String token = request.getParameter("token");
    TokenPrincipal principal = (TokenPrincipal) request.getUserPrincipal();

    Callback[] callbacks = new Callback[] {
            new CallerPrincipalCallback(clientSubject, (TokenPrincipal) null) };

    if (principal != null) {
        callbacks = new Callback[] { 
                new CallerPrincipalCallback(clientSubject, principal) };
        authenticated = true;
    } else {
        if (token != null && token.length() == Constants.tokenLength) {
            try {
                principal = fetchUser(token);
            } catch (final Exception e) {
                throw (AuthException) new AuthException().initCause(e);
            }
            callbacks = new Callback[]
                        { 
                             new CallerPrincipalCallback(clientSubject, principal),
                             new GroupPrincipalCallback(clientSubject,
                                                        new String[] { "aRole" })
                        };
            messageInfo.getMap().put("javax.servlet.http.registerSession", "TRUE");
            authenticated = true;
        }
    }

    if (authenticated) {
        try {
            handler.handle(callbacks);
        } catch (final Exception e) {
            throw (AuthException) new AuthException().initCause(e);
        }
        return SUCCESS;
    }

    return AuthStatus.SEND_FAILURE;
}
Run Code Online (Sandbox Code Playgroud)

对于ejb @RolesAllowed("aRole")的第一次调用,这可以正常工作,但是对于下一次调用,这根本不起作用.Wildfly通过以下错误消息拒绝它:

ERROR [org.jboss.as.ejb3.invocation] (default task-4) WFLYEJB0034: EJB Invocation 
    failed on component TestEJB for method public java.lang.String 
    com.jaspic.security.TestEJB.getPrincipalName():
    javax.ejb.EJBAccessException: WFLYSEC0027: Invalid User
Run Code Online (Sandbox Code Playgroud)

如果我猜对了,错误发生在: wilfly的源代码的org.jboss.as.security.service.SimpleSecurityManager 第367行,由于第405行,其中credential被检查,但似乎是null.

这似乎在Wildfly 8/9/10CR中是相同的(其他版本未经测试).

我不确定,如果我做错了,或者这是与https://issues.jboss.org/browse/WFLY-4626相同的错误 ?它是一个错误,还是预期的行为?

Uux*_*Uux 7

这听起来像是一个错误,因为调用者身份(调用者/组Principals)似乎在后续的Web调用中保留,而不是保留在EJB容器中.我自己的JASPIC类(在GlassFish 4.1上正常运行)在WildFly 9.0.2.Final和10.0.0.CR4上与原始Servlet和SLSB一起使用时失败的原因相同,即使后者已标记@PermitAll.

因为我自己不熟悉WildFly安全内部,所以我无法在这方面为您提供帮助.除非你能得到这个补丁,唯一SAM级的解决方法我能想到的暂时将不会使用javax.servlet.http.registerSession那个看似触发问题回调财产,而是有CallbackHandler寄存器都来电Principal 每个其组validateRequest(...)调用.如果适用于您的使用案例,您可能希望将该信息附加到该用例,HttpSession以便加快该过程; 否则从头开始重复.所以,例如:

public class Sam implements ServerAuthModule {

    // ...

    @Override
    public AuthStatus validateRequest(MessageInfo mi, Subject client, Subject service) throws AuthException {
        boolean authenticated = false;
        boolean attachAuthnInfoToSession = false;
        final String callerSessionKey = "authn.caller";
        final String groupsSessionKey = "authn.groups";
        final HttpServletRequest req = (HttpServletRequest) mi.getRequestMessage();
        TokenPrincipal tp = null;
        String[] groups = null;
        String token = null;
        HttpSession hs = req.getSession(false);
        if (hs != null) {
            tp = (TokenPrincipal) hs.getAttribute(callerSessionKey);
            groups = (String[]) hs.getAttribute(groupsSessionKey);
        }
        Callback[] callbacks = null;
        if (tp != null) {
            callbacks = new Callback[] { new CallerPrincipalCallback(client, tp), new GroupPrincipalCallback(client, groups) };
            authenticated = true;
        }
        else if (isValid(token = req.getParameter("token"))) {
            tp = newTokenPrincipal(token);
            groups = fetchGroups(tp);
            callbacks = new Callback[] { new CallerPrincipalCallback(client, tp), new GroupPrincipalCallback(client, groups) };
            authenticated = true;
            attachAuthnInfoToSession = true;
        }
        if (authenticated) {
            try {
                handler.handle(callbacks);
                if (attachAuthnInfoToSession && ((hs = req.getSession(false)) != null)) {
                    hs.setAttribute(callerSessionKey, tp);
                    hs.setAttribute(groupsSessionKey, groups);
                }
            }
            catch (IOException | UnsupportedCallbackException e) {
                throw (AuthException) new AuthException().initCause(e);
            }
            return AuthStatus.SUCCESS;
        }
        return AuthStatus.SEND_FAILURE;
    }

    // ...

    @Override
    public void cleanSubject(MessageInfo mi, Subject subject) throws AuthException {
        // ...
        // just to be safe
        HttpSession hs = ((HttpServletRequest) mi.getRequestMessage()).getSession(false);
        if (hs != null) {
            hs.invalidate();
        }
    }

    private boolean isValid(String token) {
        // whatever
        return ((token != null) && (token.length() == 10));
    }

    private TokenPrincipal newTokenPrincipal(String token) {
        // whatever
        return new TokenPrincipal(token);
    }

    private String[] fetchGroups(TokenPrincipal tp) {
        // whatever
        return new String[] { "aRole" };
    }

}
Run Code Online (Sandbox Code Playgroud)

我在上述WildFly版本中以上述方式测试了上述内容(即使用单个Servlet引用单个SLSB标记@DeclareRoles/方法级别@RolesAllowed),它似乎按预期工作.显然,我不能保证这种方法不会以其他意想不到的方式失败.


也可以看看:

  • 它对我不起作用,因为在`web.xml` ssl加密被强制执行,但不存在.谢谢!您的答案是我将使用的解决方法. (2认同)
  • @knoe和Uux; 我终于找到了一些时间来解决这个问题并向JBoss报告.请参阅https://issues.jboss.org/browse/WFLY-6579我使用修改后的JBoss内部类创建了一个临时补丁:https://github.com/omnifaces/omnisecurity-jaspic-undertow/blob/master/的src/main/JAVA /组织/ omnifaces /安全/ JASPICAuthenticationMechanismX.java#L230 (2认同)