【问题标题】:How to use JNDI with WAS (Web Spehere Application Server 8.5.5)如何将 JNDI 与 WAX 结合使用(Websphere Application Server 8.5.5)
【发布时间】:2015-06-18 15:34:01
【问题描述】:

之前没有真正使用过WebSphere Application Server (8.5.5),我也没有任何实际的JNDI 经验,我什至只使用Shiro 几天。 我仍然需要为 JNDI 获取一个自定义 Shiro-realm,以便应用程序和 Shiro 共享该领域的一个公共实例(并且该领域可以通过注入访问 ejb 资源)

到目前为止,我是这样做的:

WEB-INF/shiro.ini

[main]
realmFactory = org.apache.shiro.realm.jndi.JndiRealmFactory
realmFactory.jndiNames = realms/ShiroRealm
...

一个自定义的 Shiro-Realm(现在只是模拟)

import javax.faces.bean.SessionScoped;
@SessionScoped
public class MockRealm extends AuthorizingRealm implements Serializable {

  @Inject public UserMB user;

  @Override protected AuthenticationInfo doGetAuthenticationInfo(...) ...
  @Override protected AuthorizationInfo doGetAuthorizationInfo(...) ...
}

自定义 CredentialsMatcher

public class MockCredentialsMatcher implements CredentialsMatcher {

  @Override public boolean doCredentialsMatch(...) ...
}

我有一个 shiro-startup 类如下

@Singleton
@Startup
public class ShiroStartup {

  @Inject
  private MockRealm realm;

  @PostConstruct
  public void setup() {
    Hashtable<String, String> env = new Hashtable<String, String>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
    InitialContext ic = new InitialContext(env);

    try {
    Object obj = ic.lookup("realms/ShiroRealm"); // This is expected to fail when the application is published
        } catch (NamingException ne) {
            this.realm.setCredentialsMatcher(new MockCredentialsMatcher());
            ic.rebind("realms/ShiroRealm", this.realm);
            System.out.println("Bound: realms/ShiroRealm";
        }
    } catch (NamingException e) {
        e.printStackTrace();
    }
  }
}

我之前发现一篇帖子建议以这种方式桥接注入作用域,实际上这是可行的,但仅当此代码在 GlassFish (v4) 上运行时才有效。 我很确定这也适用于 WAS 8.5.5,但无法解决此错误。 (对某人来说可能微不足道,但是...)

我得到的错误是抱怨找不到/无法使用 jndi 名称“realms/ShiroRealm”。 (我稍后会发布确切的异常,目前根本无法运行服务器) 我还没有找到应该如何给出这个名字(这开始需要时间,即使我希望这种信息很容易找到),所以我在这里发帖希望有人能给我建议。

(注意:我在这里只复制了我认为相关的部分。在 GlassFish 上运行时调试代码时,我会在预期的所有正确位置得到命中)

// 更新(为了完整性,虽然我认为这些与这个问题无关)
beans.xml的内容

<?xml version="1.0" encoding="UTF-8"?>
<beans
  xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://docs.jboss.org/cdi/beans_1_0.xsd">
  <interceptors>
    <class>com.example.interceptor.ShiroSecuredInterceptor</class>
  </interceptors>
</beans>


ejb-jar.xml的内容

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd"
  version="3.1">
<interceptors>
  <interceptor>
    <interceptor-class>com.example.interceptor.ShiroSecuredInterceptor</interceptor-class>
    </interceptor>
  </interceptors>
  <assembly-descriptor>
    <interceptor-binding>
      <ejb-name>*</ejb-name>
    <interceptor-class>com.example.interceptor.ShiroSecuredInterceptor</interceptor-class>
    </interceptor-binding>
  </assembly-descriptor>
</ejb-jar>

web.xml的内容

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID"
  version="3.1">
<display-name>shiroTest</display-name>
<context-param>
  <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
  <param-value>true</param-value>
</context-param>
<welcome-file-list>
  <welcome-file>Login.xhtml</welcome-file>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <listener>
    <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
  </listener>
  <filter>
    <filter-name>shiroFilter</filter-name>
    <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
    </filter>
  <filter-mapping>
    <filter-name>shiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>ERROR</dispatcher>
  </filter-mapping>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
  </servlet-mapping>
</web-app>

// 更新
终于让服务器重新上线并能够抛出异常

 [4/17/15 12:16:22:053 EEST] 00000043 SystemErr     R javax.naming.NameNotFoundException: Context: securityoffNode01Cell/nodes/securityoff/servers/server1, name: realms/ShiroRealm: First component in name realms/ShiroRealm not found. [Root exception is org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0]

如果完整的堆栈跟踪很重要,我将它放在 pastebin 上一段时间。 http://pastebin.com/sANAqCJL

(意识到我用来在 GlassFish 上测试此配置的项目是一个简单的“战争”项目,其中部署在 WAS 上的实际实现已经发布。这些服务器之间可能相关,也可能不相关。)

// 更新 2 读完后(还没有完全,还在阅读和测试): http://www-01.ibm.com/support/knowledgecenter/SS7JFU_7.0.0/com.ibm.websphere.express.iseries.doc/info/iseriesexp/ae/cejb_bindingsejbfp.html

我尝试使用名称:“ejblocal:MockRealm”。 好吧,这似乎没问题。然而继续同样的问题,现在出现以下异常。

[4/17/15 13:17:47:536 EEST] 000000ac webapp        E com.ibm.ws.webcontainer.webapp.WebApp logServletError SRVE0293E: [Servlet Error]-[com.ibm.ws.webcontainer.extension.DefaultExtensionProcessor]: java.lang.IllegalStateException: Unable to look up realm with jndi name 'ejblocal:MockRealm'.

看起来 shiro.ini 中使用的 jndi 名称仍需要修复其他内容,但“ejbLocal:MockRealm”或“简单的“MockRealm”不行。 我希望尽快找到解决方案,然后我会发布一个真正的答案。

【问题讨论】:

标签: jndi shiro websphere-8


【解决方案1】:

您是否尝试过删除您在此处传递的“env”参数?

//InitialContext ic = new InitialContext(env); //OLD
InitialContext ic = new InitialContext(); //NEW

原因是当 Shiro 尝试查找“realms/ShiroRealm”时,它会使用没有环境参数的 InitialContext。

如果您定义的环境是必需的,您必须创建一个 RealmFactory,为 Shiro 提供相同的属性。

package com.acme.realm.jndi;

import java.util.Properties;

import javax.naming.Context;

public class WASJndiRealmFactory extends JndiRealmFactory {

    @Override
    public void setJndiNames (String commaDelimited) throws IllegalStateException {
        Properties p = new Properties();
        p.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
        setJndiEnvironment(p);
        super.setJndiNames(commaDelimited);
    }
}

那么您将不得不更新您的“shiro.ini”以引用新的 RealmFactory。

[main]
realmFactory = com.acme.realm.jndi.WASJndiRealmFactory

【讨论】:

    【解决方案2】:

    我偶然浏览了这个有点过时的问题。 我无法配置 Shiro 以使其与 WAS 一起使用。相反,我手动将两个“范围”所需的公共数据放入 JNDI 上下文中。 这样我就可以在 Shiro 中查找实际应用程序中需要的东西。
    有了这个解决方案,ShiroStartup 就不再需要了。

    【讨论】:

      猜你喜欢
      • 2018-08-22
      • 2016-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-21
      • 1970-01-01
      • 1970-01-01
      • 2015-05-19
      相关资源
      最近更新 更多