【发布时间】:2016-01-08 18:06:51
【问题描述】:
我有一个 EJB 3.0 会话 Bean,包括一个“Hello World”远程接口和一个“HelloWorldBean”会话Bean。现在我尝试为它们添加一些安全性。这是我的 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,客户端就可以工作。希望有人能帮帮我,谢谢。
【问题讨论】:
标签: java security jakarta-ee jboss ejb