1

我在 apache tomee 中部署了一个 EJB,但是无论谁在 Web 模块中经过身份验证,context.getCallerPrincipal().getName();总是返回“ ”用户。guest在我的 JSF 托管 bean 中正确捕获了正确的用户。

EJB

@Stateless()
@DeclareRoles(value = {"ROLE_USER"})
public class StudentFacade extends AbstractFacade<Student> {

    @Resource
    private SessionContext context;
    @PersistenceContext(unitName = "tomeePU")
    private EntityManager em;

    public StudentFacade() {
        super(Student.class);
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRED)

    public void save(Student student) {

        System.out.println("Username in EJB "+context.getCallerPrincipal().getName());
        //prints 'guest' user
        em.persist(student);
    }

    @TransactionAttribute(TransactionAttributeType.SUPPORTS)
    public Student getById(Integer id) {
        return em.find(Student.class, id);
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
}

jsf托管bean中的方法

 public void save(AjaxBehaviorEvent ajaxBehaviorEvent) {
        try {
            studentFacade.save(student);
            Principal p = JsfUtil.getCurrentHttpRequest().getUserPrincipal();
            System.out.println("UserName Is " + p.getName());//Works fine
            setStudent(new Student());
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e.getMessage());
        }

有哪些额外的 tomee 配置可以将正确的 Principal 传播到 ejb?谢谢。

4

0 回答 0