title: User authentication in Java EE with enum
body: <p>I was thinking about using Java EE without any framework (eg. Spring), if possible, to perform user authentication. I use PG, JPA, JSF, Java EE, Glassfish and EJB in my project.</p>
  
  <p>In Glassfish web.xml would do it for me. Problem is, that I found a lot of possibilities to do it with <strong>3 tables</strong> - USER, USER_IN_GROUP and GROUP, and none to do it with <strong>enum role and just 1 table</strong> - USER (with role as a column), which should be imo much easier and lighter.</p>
  
  <p>Role enum like:</p>
  
  <pre><code>public enum Role {
     User, Admin;
  }
  </code></pre>
  
  <p>User entity like:</p>
  
  <pre><code>public class User implements Serializable {
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     private Long id;
  
     private String nick;
     private String pass;
  
     @Enumerated(EnumType.STRING)
     private Role roles;
  
     //Getters and Setters
  }
  </code></pre>
  
  <p>In <em>web.xml</em> I use Basic method to test credentials.</p>
  
  <pre><code>&lt;login-config&gt;
      &lt;auth-method&gt;BASIC&lt;/auth-method&gt;
      &lt;realm-name&gt;issuetrack-realm&lt;/realm-name&gt;
  &lt;/login-config&gt;
  &lt;security-role&gt;
      &lt;description/&gt;
      &lt;role-name&gt;User&lt;/role-name&gt;
  &lt;/security-role&gt;
  &lt;security-role&gt;
      &lt;description/&gt;
      &lt;role-name&gt;Admin&lt;/role-name&gt;
  &lt;/security-role&gt;
  </code></pre>
  
  <p>The Realm using <strong>JDBCRealm</strong> looks like this:
  <a href="http://i.stack.imgur.com/6b5sn.png" rel="nofollow">Realm in Glassfish</a></p>
  
  <p>After I try to login it says: <strong>Warning: WEB9102: Web Login Failed: com.sun.enterprise.security.auth.login.common.LoginException: Login failed: Security Exception</strong>. </p>
  
  <p>Any suggestions?</p>
  
  <hr>
  
  <p>Login failed: Security Exception was just a problem in setting of the realm. I don't secure my password in db and I left Digest Algorithm empty = SHA-256.</p>
  
  <p>The real problem is with roles there. I can't get in my app with this setting because there is a nickname principal instead of groups name (REALM setting from db).</p>