1

I'm able to pass the username from JSF to managed bean e.g. this way:

<script type="text/javascript" >
  function onLoad(){
    document.getElementById("form:user").value = "#{sessionScope['username']}";}
  window.onload = onLoad;
</script>

<h:inputHidden id="user" value="#{bean.username}"/>

Is it possible to get it directly using Java method? I've tried something like:

public String getCurrentUserName()
{
  String name = "";
  FacesContext facesContext = FacesContext.getCurrentInstance();
  ExternalContext externalContext = facesContext.getExternalContext();

  if (externalContext.getUserPrincipal() != null) {
    name = externalContext.getUserPrincipal().getName(); // null
  }
  return name;
}

or:

facesContext.getExternalContext().getRemoteUser();       // null
facesContext.getExternalContext().isUserInRole("pps");   // null

But user is always null.. what am doing wrong?

UPDATE (creation a session container):

public String login() {
  ...
  FacesContext context = FacesContext.getCurrentInstance();
  session = (HttpSession) context.getExternalContext().getSession(true);
  session.setAttribute("id", user.getId());
  session.setAttribute("username", user.getName());
  ...
gaffcz
  • 3,469
  • 14
  • 68
  • 108

1 Answers1

4
#{sessionScope['username']}

This basically prints the session attribute with the name "username". Something like the following in raw Java code (if you're familiar with the basic Servlet API):

response.getWriter().print(session.getAttribute("username"));

If this part works, then you are definitely not using container managed authentication at all and thus the container managed user principal and user role getters definitely won't return anything.

You can access a session attribute just the same way as you access a session scoped JSF managed bean (they are under the covers namely also stored as session attributes!):

@ManagedProperty("#{username}")
private String username; // +setter

or, of course, the clumsy way:

String username = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("username");

See also:


Unrelated to the concrete question: I highly doubt the usefulness of that hidden input field and that piece of JS. Why are you passing a variable which is already present in the server side back to the server side? Are you sure you really need to do it this way?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you, it's very helpful as usually.. I'm doing it that way simply because I've learned it at the starting with JSF, and it was enough until now. Which method would you recommend me then? – gaffcz Aug 27 '12 at 11:59
  • It's your choice. Container managed authentication has done most the work for you, but the configuration possibilities are fairly limited. If you surely need more configuration freedom, you'd have to look for a 3rd party authentication framework (Spring Security, Apache Shiro, etc), or to homegrow yourself (setting `User` as session attribute which is checked by a servlet filter on certain URLs, etc, exactly as you're apparently doing now). – BalusC Aug 27 '12 at 12:04
  • And why do you think the `FacesContext.getCurrentInstance()....` method is clumsy? And the second thing, I've tried the `ManagedProperty` method, but I have to set the `@ManagedProperty("#{userManager.username}")` and also set the property for `username` in `userManager` bean, is it correct? – gaffcz Aug 27 '12 at 12:49
  • It is not necessarily at its own clumsy, but it is clumsy when compared to the simpler and more abstract `@ManagedProperty` way. As to the other bean, I have no idea, this detail is missing in the initial question. I just gave an answer which exactly suits the initial question. – BalusC Aug 27 '12 at 12:52