【问题标题】:CDI injection in JASPIC ServerAuthModule doesn't workJASPIC ServerAuthModule 中的 CDI 注入不起作用
【发布时间】:2018-10-26 13:31:09
【问题描述】:

在 Java 10 上使用 Wildfly 11 Final。

我有 JASPIC 实现,它本身就可以正常工作。我需要将它连接到数据库,以便我可以在 public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject, Subject serviceSubject) throws AuthException {...} 中进行身份验证。但是,@Resource(mappedName="java:jboss/datasources/someDB") javax.sql.DataSource db; 始终是 null,对于 db

CDI 对同一 WAR 存档中的任意 Servlet 类按预期工作 - 如果我将确切的行放在 Servlet 类中,它会给出一个 DataSource 对象。

我通过private static InitialContext ic; 手动查找来解决这个问题,但我需要 CDI 来处理其他全局事务。有没有人遇到过这样的问题? WEB-INF/beans.xmlbeans 元素中有一个bean-discovery-mode="all" 属性。

public class SrvAuthModuleImpl implements ServerAuthModule {

@Resource(mappedName="java:jboss/datasources/someDB") javax.sql.DataSource db; //always null
private static InitialContext ic; //workaround via manual lookup

private CallbackHandler handler;
private Class<?>[] supportedMessageTypes = new Class[] {HttpServletRequest.class, HttpServletResponse.class};

@Override
public void initialize(MessagePolicy requestPolicy, MessagePolicy responsePolicy, CallbackHandler handler, @SuppressWarnings("rawtypes") java.util.Map options) throws AuthException {
    this.handler = handler;
}

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

    try (
        Connection c = getDb().getConnection(); //works
        // Connection c = db.getConnection(); //db is null, exception thrown
    ) {

    } catch (SQLException ex) {

    }

    return AuthStatus.SUCCESS;
}

@Override
public Class<?>[] getSupportedMessageTypes() {return supportedMessageTypes;}
@Override
public AuthStatus secureResponse(MessageInfo messageInfo, Subject serviceSubject) throws AuthException {return AuthStatus.SEND_SUCCESS;}
@Override
public void cleanSubject(MessageInfo messageInfo, Subject subject) throws AuthException {}

private static javax.sql.DataSource getDb() {
    try {
        if (ic == null) {
            synchronized(TestServerAuthModule.class) {
                if (ic == null) {
                    ic = new javax.naming.InitialContext();
                }
            }
        }
        return (javax.sql.DataSource)ic.lookup("java:jboss/datasources/someDB");
    } catch (Exception ex) {
        return null;
    }
}
}

【问题讨论】:

    标签: jakarta-ee wildfly cdi jaspic


    【解决方案1】:

    这是意料之中的。 JASPIC 比 Resource Injection 或 CDI 更早,并且都不支持。

    在 Java EE 7 中,您可以使用类似

    CDI.current().select(MyBean.class).get()
    

    在非托管上下文中获取 CDI bean。

    另一个选项是 Soteria,它是 Java EE 8 安全 API 的参考实现,它也适用于 WildFly 11。

    【讨论】:

      猜你喜欢
      • 2016-07-07
      • 2012-07-29
      • 2014-08-30
      • 1970-01-01
      • 2012-04-30
      • 2014-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多