EJB2授权相关的简单程序(java.lang.SecurityException:User:manager,无法通过身份验证。)

时间:2013-05-17 17:02:35

标签: authentication ejb weblogic

我写了以下文件     -------------------------------

ejb-jar.xml
-------------

<?xml version="1.0"?>

<!DOCTYPE ejb-jar PUBLIC 
'-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' 
'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>

<ejb-jar>
  <enterprise-beans>
    <session>
     <ejb-name>HelloEJB2</ejb-name>

     <home>com.jlcindia.ejb2.hello.HelloHome</home>
     <remote>com.jlcindia.ejb2.hello.HelloRemote</remote>
     <ejb-class>com.jlcindia.ejb2.hello.HelloBean</ejb-class>
     <session-type>Stateless</session-type>
     <transaction-type>Container</transaction-type>
     <security-role-ref>
     <role-name>managers</role-name>
     <role-link>manager</role-link>
     </security-role-ref>

     <security-role-ref>
     <role-name>students</role-name>
     <role-link>student</role-link>
     </security-role-ref>

     <security-role-ref>
     <role-name>administrators</role-name>
     <role-link>administrator</role-link>
     </security-role-ref>

     </session>
     </enterprise-beans>
     <assembly-descriptor>
     <security-role>
     <role-name>manager</role-name>
     </security-role>

     <security-role>
     <role-name>student</role-name>
     </security-role>

     <security-role>
     <role-name>administrator</role-name>
     </security-role>
     </assembly-descriptor>

</ejb-jar>

weblogic-ejb-jar.xml (using weblogic 8)
----------------------
<?xml version="1.0"?>

<!DOCTYPE weblogic-ejb-jar PUBLIC 
'-//BEA Systems, Inc.//DTD WebLogic 8.1.0 EJB//EN'
'http://www.bea.com/servers/wls810/dtd/weblogic-ejb-jar.dtd'>

<weblogic-ejb-jar>

  <weblogic-enterprise-bean>

  <ejb-name>HelloEJB2</ejb-name>
  <jndi-name>JLCHelloHomeJNDI2</jndi-name>
  </weblogic-enterprise-bean>

<security-role-assignment>
<role-name>manager</role-name>
<principal-name>managers</principal-name>
</security-role-assignment>
</weblogic-ejb-jar>

HelloHome.java
-----------------
package com.jlcindia.ejb2.hello;
import java.rmi.RemoteException;


import javax.ejb.*;

public interface HelloHome extends EJBHome{
    public HelloRemote create()throws CreateException,RemoteException;
}


HelloRemote.java
----------------
package com.jlcindia.ejb2.hello;
import java.rmi.RemoteException;

import javax.ejb.*;


public interface HelloRemote extends EJBObject{
    public String getMessage(String name)throws RemoteException;
    public void balance()throws RemoteException;
    public void updateAccount()throws RemoteException;

}


HelloBean.java
------------
package com.jlcindia.ejb2.hello;

import java.rmi.RemoteException;

import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

public class HelloBean implements SessionBean{

    SessionContext sc;
    public void ejbCreate()throws EJBException,RemoteException{
        System.out.println("HelloBean-ejbCreate()");
    }

    public void ejbActivate() throws EJBException, RemoteException {
        System.out.println("HelloBean-ejbActivate()");

    }

    public void ejbPassivate() throws EJBException, RemoteException {
        System.out.println("HelloBean-ejbPassivate()");

    }

    public void ejbRemove() throws EJBException, RemoteException {
        System.out.println("HelloBean-ejbRemove()");

    }

    public void setSessionContext(SessionContext sc) throws EJBException,
            RemoteException {
        System.out.println("HelloBean-setSessionContext()");
        this.sc=sc;

    }
    public String getMessage(String name){
        String msg="Hello!"+name+"welcome to EJB2 with weblogic8";
        System.out.println(msg);
        return msg;
    }

    public void balance(){
        if(sc.isCallerInRole("managers")||sc.isCallerInRole("cashiers"))
        System.out.println("inside balance");
        else{
            System.out.println("not manager or administrator for balance");
        }
    }


    public void updateAccount(){
        if(sc.isCallerInRole("administrators"))
        System.out.println("update account");
        else{
            System.out.println("not administrators for updatation");
        }
    }

}



HelloClient.java
-------------
package com.jlcindia.ejb2.hello;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;

public class HelloClient {
    public static void main(String[] args) {
        try{
            Properties p=new Properties();
            p.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
            p.put(Context.PROVIDER_URL, "t3://localhost:7001");
            p.put(Context.SECURITY_PRINCIPAL, "manager");
            p.put(Context.SECURITY_CREDENTIALS, "manager");
            Context ctx=new InitialContext(p);
            Object obj=ctx.lookup("JLCHelloHomeJNDI2");
            HelloHome home=(HelloHome)obj;
            HelloRemote hello=home.create();
            String msg=hello.getMessage("srinivas");
            hello.updateAccount();
            hello.balance();
            System.out.println(msg);

        }catch(Exception e){
            e.printStackTrace();
        }
    }

}



and I m using weblogic 8
after deploying and 
runnig the HelloClient



m getting the following Exception
-----------------

javax.naming.AuthenticationException。根异常是java.lang.SecurityException:User:manager,无法进行身份验证.at weblogic.common.internal.RMIBootServiceImpl.authenticate(RMIBootServiceImpl.java:95)         在weblogic.common.internal.RMIBootServiceImpl_WLSkel.invoke(未知来源)         在weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:466)         在weblogic.rmi.internal.BasicServerRef $ 1.run(BasicServerRef.java:409)         在weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:353)         在weblogic.security.service.SecurityManager.runAs(SecurityManager.java:144)         在weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.java:404)         在weblogic.rmi.internal.BasicExecuteRequest.execute(BasicExecuteRequest.java:30)         在weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:197)         在weblogic.kernel.ExecuteThread.run(ExecuteThread.java:170)

Please tell me the solution for this n thanks in adv.

1 个答案:

答案 0 :(得分:0)

这可能有以下几个原因:

1) User: manager中不存在Weblogic login realms。在这种情况下,请检查 Weblogic 中的Users/Groups设置。

2)在部署之前或之后,您没有将User: manager映射到部署描述符中定义的任何roles。检查已部署的应用程序,看看User: manager是否映射到任何提供的角色。

3)您的代码引用了错误的角色名称。

例如:

isUserInRole("Manager");

此代码不起作用,因为它不引用部署描述符中定义的任何<role-name>。检查小写大写完全 字符。因为isUserInRole区分大小写。

注意:请发布执行角色检查的代码,并发布可能的完整错误堆栈跟踪。