Authentication EJB2授权相关的简单程序(java.lang.SecurityException:User:manager,身份验证失败。)

Authentication EJB2授权相关的简单程序(java.lang.SecurityException:User:manager,身份验证失败。),authentication,ejb,weblogic,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&

我已经编写了以下文件 -------------------------------

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
-----------------

这可能有几个原因:

isUserInRole("Manager");
1)
Weblogic登录域中不存在
User:manager
。在这种情况下,请检查Weblogic中的
用户/组设置

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

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

对于ex:

isUserInRole("Manager");
此代码不起作用,因为它没有引用部署描述符中定义的任何
。检查小写、大写、精确的字符。因为
isUserInRole
区分大小写


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

能否验证用户凭据是否正确?如果是,您可以尝试在WLS实例“localhost:7001”上启用ATN调试,并检查身份验证失败的原因。请从客户端共享完整的堆栈跟踪