我有一个EJB 3.0 Sesion Bean,包括一个“Hello World”远程接口和一个“HelloWorldBean”SessionBean。现在我尝试为它们添加一些安全性。这是我的SessionBean:
@Stateful
@SecurityDomain("other")
@DeclareRoles("ejb")
@PermitAll
public class HelloWorldBean implements HelloWorld {
@Resource
private SessionContext context;
/**
* Default constructor.
*/
public HelloWorldBean() {
// TODO Auto-generated constructor stub
}
@Override
@PermitAll
public String sayHello() throws Exception {
String s = this.toString();
s = s + " Security: "+context.isCallerInRole("ejb") + " "+context.getCallerPrincipal().getName();
return s;
}
}
我使用@PermitAll
来测试应用程序,它可以正常运行。如果我留下上面的注释,我会得到一个异常,告诉我我不允许调用方法sayHello()
。方法getCallerPrincipal().getName()
始终返回“匿名”。以下类是我的独立客户端。
public class EjbAccess3_1_v2 {
public static void main(String[] args) throws Exception {
String providerUrl = "remote://localhost:4447";
final String appName = "NewApplication";
final String moduleName = "EJB3.1";
final String distinctName = "";
final String beanName = "HelloWorldBean";
final String viewClassName = HelloWorld.class.getName();
final String ejbPath= appName + "/" + moduleName + "/" + beanName + "!" + viewClassName;
Properties environment = new Properties();
environment.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
environment.put(Context.PROVIDER_URL, providerUrl);
environment.put("jboss.naming.client.ejb.context", true);
environment.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
environment.put(Context.SECURITY_PRINCIPAL, "ejb");
environment.put(Context.SECURITY_CREDENTIALS, "password");
InitialContext initialContext = new InitialContext(environment);
HelloWorld remote = (HelloWorld) initialContext.lookup(ejbPath);
System.out.println("EJB V3.1 v2");
System.out.println(remote.sayHello());
}
}
我正在使用JBoss 6作为应用程序服务器并创建了一个应用程序用户“ejb”。只要我不使用任何安全性或使用SessionBean类中的@PermitAll
和我想要调用的方法,客户端就会工作。我希望有人可以帮助我,谢谢。
答案 0 :(得分:0)
您的用户名为" ejb"和一个名为" ejb"的角色。如果用户和角色尚未相互映射,则需要map them 1st somehow。
如果已映射,则需要使用注释
@RolesAllowed({"ejb"})
关于here等类或方法。