Java JAX-RS+EJB,EJB中的SecurityContext为空,在WildFly 10.1上
我第一次开发了一个JavaEE应用程序,它试图使用ejb安全特性。 我正在使用WildFly 10.1。 我已经创建了一个Jdbc安全域,并配置了一个基于表单的登录。对web方法和url路径的访问以及登录工作权限可防止对未经授权的方法的访问,并在登录后授权访问 我有一组实现Jax-rsrest接口的bean,还有一组实现应用程序业务逻辑的ejb无状态bean 以下是jboss-web.xml和web.xml的截图:Java JAX-RS+EJB,EJB中的SecurityContext为空,在WildFly 10.1上,java,security,ejb,jax-rs,wildfly,Java,Security,Ejb,Jax Rs,Wildfly,我第一次开发了一个JavaEE应用程序,它试图使用ejb安全特性。 我正在使用WildFly 10.1。 我已经创建了一个Jdbc安全域,并配置了一个基于表单的登录。对web方法和url路径的访问以及登录工作权限可防止对未经授权的方法的访问,并在登录后授权访问 我有一组实现Jax-rsrest接口的bean,还有一组实现应用程序业务逻辑的ejb无状态bean 以下是jboss-web.xml和web.xml的截图: <jboss-web> <security-domai
<jboss-web>
<security-domain>myDomain</security-domain>
</jboss-web>
以及注入EJB的片段,我需要通过编程方式访问安全上下文和主体信息:
@DeclareRoles({"administrator","operator","user"})
@PermitAll
@Stateless
public class myFacade {
@PersistenceContext(unitName = "myPersistencePU")
private EntityManager em;
@Context SecurityContext securityContext;
@Resource SecurityContext sc; //I have tried both :-(
public DataStuff find(Object id) {
//Here I get a NullPointerException, tried both sc and securitycontext
String username = securityContext.getUserPrincipal().getName();
if(username.equals("gino"){
return null;
}
return getEntityManager().find(entityClass, id);
}
}
我尝试过使用@DeclareRoles和不使用@PermitAll,但是securityContext和sc变量都是空的。也许我错过了一些东西,但我知道安全信息在bean调用中神奇地移动
问题
如何将安全上下文从Jax-RS类传播到
ejb bean?
安全信息是否像我预期的那样自动管理?或
我是否需要改进或添加其他jboss-?.xml配置文件?
或
我必须更改调用Jax-RS bean中的某些内容才能
将安全信息传播到被调用的bean?或
还是我做错了什么?
先谢谢你
关于我已经找到了答案,问题已经被问到了 SecurityContext仅适用于JAX-RSbean,您需要将一个EJBContext对象代替SecurityContext对象注入到其他java bean中。 您也可以使用SessionContext对象,但EJBContext接口类似于SecurityContext接口。以下是工作版本:
@DeclareRoles({"administrator","operator","user"})
@PermitAll
@Stateless
public class myFacade {
@PersistenceContext(unitName = "myPersistencePU")
private EntityManager em;
@Resource EJBContext securityContext;
public DataStuff find(Object id) {
//Now the securityContext is != null :-D
String username = securityContext.getCallerPrincipal().getName();
if(username.equals("gino"){
return null;
}
return getEntityManager().find(entityClass, id);
}
}
这个问题是下面解决方案的来源,我在源代码中添加了一些有用的细节,希望有用。仅回答您的前两个要点,容器负责传播安全上下文,您通常不需要关心这一点。
@DeclareRoles({"administrator","operator","user"})
@PermitAll
@Stateless
public class myFacade {
@PersistenceContext(unitName = "myPersistencePU")
private EntityManager em;
@Context SecurityContext securityContext;
@Resource SecurityContext sc; //I have tried both :-(
public DataStuff find(Object id) {
//Here I get a NullPointerException, tried both sc and securitycontext
String username = securityContext.getUserPrincipal().getName();
if(username.equals("gino"){
return null;
}
return getEntityManager().find(entityClass, id);
}
}
@DeclareRoles({"administrator","operator","user"})
@PermitAll
@Stateless
public class myFacade {
@PersistenceContext(unitName = "myPersistencePU")
private EntityManager em;
@Resource EJBContext securityContext;
public DataStuff find(Object id) {
//Now the securityContext is != null :-D
String username = securityContext.getCallerPrincipal().getName();
if(username.equals("gino"){
return null;
}
return getEntityManager().find(entityClass, id);
}
}