【问题标题】:javaee : @Resource annotation not injecting JNDI resourcejavaee : @Resource 注解不注入 JNDI 资源
【发布时间】:2015-04-03 20:59:41
【问题描述】:

这是我的环境设置:
1. Apache Tomcat EE 1.6.2(Java EE 6 认证)
2. Java 1.7.0_65
3. JAAS

A) WEB-INF/context.xml

<?xml version="1.0" encoding="UTF-8"?>
<context>
<!-- Use JAASRealm -->
<Realm className="org.apache.catalina.realm.JAASRealm" appName="JAASLoginModule"
    userClassNames="com.jaas.login.module.UserPrincipal" roleClassNames="com.jaas.login.module.RolePrincipal" />

<Resource name="ldapRes" 
          auth="Application"
          type="org.apache.directory.ldap.client.api.LdapNetworkConnection"
          factory="com.ldap.factory.services.LdapContextFactory"
          singleton="true" 
          java.naming.provider.url="ldap://127.0.0.1:389"
          java.naming.security.authentication="simple"
          java.naming.security.principal="cn=admin,dc=example,dc=com,dc=au"
          java.naming.security.credentials="a0120733@OPENLDAP" />
</context>

B) web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">

:

<resource-ref>
    <description>
        LDAP Resource
    </description>
    <res-ref-name>
        ldapResRef
    </res-ref-name>
    <lookup-name>
        java:comp/env/ldapRes
    </lookup-name>
  </resource-ref>

</web-app>

C) LdapContextFactory 类

public class LdapContextFactory implements ObjectFactory {

public Object getObjectInstance(Object obj, Name name, Context nameCtx,
        Hashtable<?, ?> environment) throws Exception {

    System.out.println("getObjectInstance");
    Hashtable<Object, String> configurations = new Hashtable<Object, String>();
    Reference reference = (Reference) obj;
    Enumeration<RefAddr> references = reference.getAll();

    while (references.hasMoreElements()) {
        RefAddr address = references.nextElement();
        String type = address.getType();
        String content = (String) address.getContent();

        switch (type) {
        case Context.PROVIDER_URL:
            configurations.put(Context.PROVIDER_URL, content);
            break;
        case Context.SECURITY_AUTHENTICATION:
            configurations.put(Context.SECURITY_AUTHENTICATION, content);
            break;
        case Context.SECURITY_PRINCIPAL:
            configurations.put(Context.SECURITY_PRINCIPAL, content);
            break;
        case Context.SECURITY_CREDENTIALS:
            configurations.put(Context.SECURITY_CREDENTIALS, content);
            break;
        default:
            break;
        }
    }

    int startIndexIP = ((String) configurations.get(Context.PROVIDER_URL))
            .lastIndexOf("//") + 2;
    int lastIndexIP = ((String) configurations.get(Context.PROVIDER_URL))
            .lastIndexOf(':');
    String ldapHost = ((String) configurations.get(Context.PROVIDER_URL))
            .substring(startIndexIP, lastIndexIP);
    int ldapPort = Integer.parseInt(((String) configurations
            .get(Context.PROVIDER_URL)).substring(lastIndexIP + 1));

    return new LdapNetworkConnection(ldapHost, ldapPort);

}

}

D) JAASLoginModule 类

public class JAASLoginModule implements LoginModule {
 :
public boolean login() throws LoginException {
    if (callbackHandler == null)
        throw new LoginException("no handler");

    NameCallback nameCall = new NameCallback("username: ");
    PasswordCallback passCall = new PasswordCallback("password: ", false);
    try {
        callbackHandler.handle(new Callback[]{nameCall, passCall});
    } catch (UnsupportedCallbackException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println(nameCall.getName());
    System.out.println(passCall.getPassword());

    LdapConnection connection = new LdapResourceConnection().getLdapConnection();

    try {
        System.out.println(connection);
          :

 }

E) LdapResourceConnection 类

public class LdapResourceConnection {

private LdapNetworkConnection ldapConnection;

public LdapConnection getLdapConnection() {
    System.out.println("GET ..");
    return ldapConnection;
}

@Resource(lookup="java:comp/env/ldapResRef")
public void setLdapConnection(LdapNetworkConnection ldapConnection) {
    System.out.println("SET ..");
    this.ldapConnection = ldapConnection;
}

}

问题:
在登录模块上

LdapConnection connection = new LdapResourceConnection().getLdapConnection();

这无法运行 LdapResourceConnection setLdapConnection() 方法。我在这个命令上得到的只是“GET ..”被打印出来,而不是“SET ..”。

请帮忙。
谢谢。

【问题讨论】:

    标签: jakarta-ee annotations tomcat7 jndi java-ee-7


    【解决方案1】:

    您的问题是您自己创建了LdapResourceConnection,因此容器(应用程序服务器/CDI)无法注入任何东西。

    为了能够使用注入到 bean 中,这个 bean 应该由容器管理(创建/处置)。

    要在您的登录模块上更正此问题,您应该注入 LdapResourceConnection

    @Inject
    LdapResourceConnection ldapResourceConnection;
    ...
    LdapConnection connection = ldapResourceConnection.getLdapConnection()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-15
      • 1970-01-01
      • 2011-02-24
      • 1970-01-01
      • 2017-11-04
      • 2021-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多