Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After searching the Web and trying many different ways, here's what I'd suggest for Java EE 6 authentication:</p> <h2>Set up the security realm:</h2> <p>In my case, I had the users in the database. So I followed this blog post to create a JDBC Realm that could authenticate users based on username and MD5-hashed passwords in my database table:</p> <p><a href="http://blog.gamatam.com/2009/11/jdbc-realm-setup-with-glassfish-v3.html" rel="noreferrer">http://blog.gamatam.com/2009/11/jdbc-realm-setup-with-glassfish-v3.html</a></p> <p>Note: the post talks about a user and a group table in the database. I had a User class with a UserType enum attribute mapped via javax.persistence annotations to the database. I configured the realm with the same table for users and groups, using the userType column as the group column and it worked fine.</p> <h2>Use form authentication:</h2> <p>Still following the above blog post, configure your web.xml and sun-web.xml, but instead of using BASIC authentication, use FORM (actually, it doesn't matter which one you use, but I ended up using FORM). Use the standard HTML , not the JSF .</p> <p>Then use BalusC's tip above on lazy initializing the user information from the database. He suggested doing it in a managed bean getting the principal from the faces context. I used, instead, a stateful session bean to store session information for each user, so I injected the session context:</p> <pre><code> @Resource private SessionContext sessionContext; </code></pre> <p>With the principal, I can check the username and, using the EJB Entity Manager, get the User information from the database and store in my <code>SessionInformation</code> EJB.</p> <h2>Logout:</h2> <p>I also looked around for the best way to logout. The best one that I've found is using a Servlet:</p> <pre><code> @WebServlet(name = "LogoutServlet", urlPatterns = {"/logout"}) public class LogoutServlet extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(false); // Destroys the session for this user. if (session != null) session.invalidate(); // Redirects back to the initial page. response.sendRedirect(request.getContextPath()); } } </code></pre> <p>Although my answer is really late considering the date of the question, I hope this helps other people that end up here from Google, just like I did.</p> <p>Ciao,</p> <p>Vítor Souza</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload