4

i search for the best way how i can make a user authentication in my JSF Webproject. I have found this very good Example from BalusC at stockoverflow (by the way Thank you BalusC). But i dont understand this System.

I have written a registration page. After the registration the user is into my database. But who can i use the j_security_check to check the the user from my database?

In my webproject i don't have a site for users and for non-users. When a user is loged in so he see more options for example he can post a text. Who can i check this? Can i save the User Class with the userdata at the login and check:

if(user != null){
 <h:inputText value="User Only Text" id="text"  />
}

But i think this way is not very elegant. Also my Question is what is the best way to make a user login and show the valid user more options on the webpage than a non-user.

Thank you and sorry for my bad english.

Community
  • 1
  • 1
ThreeFingerMark
  • 989
  • 3
  • 12
  • 21

2 Answers2

5

Just declare user as a session scoped managed bean and make use of the component's rendered attribute.

<h:inputText value="User Only Text" id="text" rendered="#{user != null}" />
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • And how do i say that the User Class is load into the session scoped after login? – ThreeFingerMark Feb 22 '10 at 12:42
  • Either using sort of `UserManager` session bean with `login()` method and `user` property so that you can use `#{userManager.user != null}`, or by doing `externalContext.getSessionMap().put("user", user)` in login method of a bean. – BalusC Feb 22 '10 at 12:48
3

But how can I use the j_security_check to check the the user from my database?

This article explains how to use HTTP FORM based authentication in conjunction with JDBC realms within Glassfish.

what is the best way to make a user login and show the valid user more options on the webpage than a non-user.

Basically, you need to check if the user has the appropriate role to see the "options". Have a look at ExternalContext#isUserInRole(String)

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • @Pascal: Would you like to say something about when it is good to move from container based security to alternatives like shiro or others? See more focussed question here:http://stackoverflow.com/questions/7782720/when-to-move-from-container-managed-security-to-alternatives-like-apache-shiro – Rajat Gupta Oct 16 '11 at 06:38