根据remote_user/session属性在facelet中显示用户个人资料 [英] display user profile in facelet based on remote_user/session attribute

查看:79
本文介绍了根据remote_user/session属性在facelet中显示用户个人资料的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Facelet页面上显示存储在Oracle中的User Profile数据.但是我不知道如何从托管bean中的Session中指定用户ID(以下@PostConstruct中的"myUserId").

I'm trying to display User Profile data stored in Oracle on a facelet page. But I don't know how to specify the user id from the Session in my managed bean ("myUserId" in the @PostConstruct below).

receiving email? #{member.receive_email} <br/>
signed waiver? #{member.signed_waiver} <br/>

托管Bean/Backing Bean(不确定正确的术语)

@SessionScoped
public class MemberProfile implements Serializable {

@Inject
private EntityManager em;
private Member member;

@Produces
@Named
public Member getMember() {
  return member;
}

@PostConstruct
public void getMemberData() {
  CriteriaBuilder cb = em.getCriteriaBuilder();
  CriteriaQuery<Member> criteria = cb.createQuery(Member.class);
  Root<Member> _member = criteria.from(Member.class);

  criteria.select(_member);
  criteria.where(cb.equal(_member.get("userid"), "myUserId"));
  member = em.createQuery(criteria).getSingleResult();
}

当我对用户ID进行硬编码时,它可以工作,但是我想查询已登录的用户.好吧,没有人真正登录到Web应用程序只是他们的PC.在我公司,IT部门已将Apache设置为使用Kerberose和Active Directory,并将其发送给JBoss.所以我有一个通过request.getAttribute("REMOTE_USER")

When I hardcode the user ID it works but I want to query the user logged in. Well, no one really logs in to the web app just their PC. At my company, IT has set up Apache to use Kerberose and Active Directory and sends that to JBoss. So I have a user id through request.getAttribute("REMOTE_USER")

我有一个servlet过滤器,该过滤器读取request.getAttribute("REMOTE_USER")并设置对象User,该对象在httpsession中具有基本员工数据(电话,电子邮件,姓名等).现在,成员资料来自另一个Oracle表中的员工滑雪俱乐部应用程序.

I have a servlet filter which reads the request.getAttribute("REMOTE_USER") and sets an object User which has basic employee data (phone, email, name, etc..) in a httpsession. The member profile is now from another Oracle table for an employee ski club application.

在facelet中,我可以这样访问用户数据:

In the facelet I can access the user data like this:

Hello: ${STKUserSession.fname }&nbsp;${STKUserSession.lname }&nbsp;(${STKUserSession.userId})   

那么我该如何在上面的查询构建器中利用此STKUserSession.userId.也许我无法弄清楚,因为我完全使用了错误的方法

So how do I utilize this STKUserSession.userId in the query builder above. Maybe I can't figure it out because I am using the wrong approach altogether

基于下面的答案,建议这样做,但是我不确定这是最干净的方法.

Based on the answer below, the suggestion is to do this, but I'm not so sure this is the cleanest approach.

     HttpServletRequest httpRequest = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
     HttpSession session = httpRequest.getSession();
     STKUser authenticatedUser = (STKUser) session.getAttribute("STKUserSession");
     String autheticatedBadge = authenticatedUser.getBadge();
     criteria.where(cb.equal(_member.get("badge"), autheticatedBadge));

推荐答案

这就是我要做的:

1)为STKUser对象创建一个holder对象:

1) create a holder object for STKUser object:

@Named("STKUserSession")
@SessionScoped
public class STKUserSession implements Serializable {
    private STKUser instance;

    @PostConstruct
    public void init() {
        instance = new STKUser(Faces.getRequestAttribute("REMOTE_USER"));
    }
    // facade methods for STKUser
}

2)使用STKUserSession对象获取您的个人资料:

2) Use the STKUserSession object to get your profile:

@Named
@SessionScoped
public class MemberProfile implements Serializable {
    @Inject
    private STKUserSession user;

    @PersistenceContext
    private transient EntityManager em;

    private Member member;

    @PostConstruct
    public void init() {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Member> cq = cb.createQuery(Member.class);
        Root<Member> root = cq.from(Member.class);

        cq.where(cb.equal(root.get("badge"), user.getBadge()));
        member = em.createQuery(cq).getSingleResult();
    }
}

请注意,这是演示代码:您可能希望对缺少的徽章进行错误检查,捕获从getSingleResult()抛出的异常(或使用getResultList()).它还使用 omnifaces 快捷方式.

Note that this is demo code: you probably want error checking for missing badges, catching the exceptions thrown from getSingleResult() (or use getResultList()). It also uses the omnifaces shortcut method(s).

还要检查周期:如果将MemberProfile注入到STKUserSession所使用的bean中,您的CDI实现将报错.

Also, check for cycles: your CDI implementation will complain if you inject MemberProfile into a bean used by STKUserSession.

这篇关于根据remote_user/session属性在facelet中显示用户个人资料的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆