title: Programmatic authentication in Java EE 6
body: <p>is it possible to authenticate <em>programmatically</em> a user in Java EE 6?</p>
  
  <p>Let me explain with some more details:</p>
  
  <p>I've got an existing Java SE project with Servlets and hibernate; where I manage manually all the authentication and access control:</p>
  
  <pre><code>class Authenticator {
      int Id
      string username
  }
  
  Authenticator login(string username, string password) ;
  
  void doListData(Authenticator auth) {
      if (isLoggedIn(auth)) listData();
      else doListError
  }
  
  void doUpdateData (Authenticator auth) {
      if (isLoggedAsAdmin(auth)) updateData() ;
      else doListError();
  }
  
  void doListError () {
      listError() ;
  }
  </code></pre>
  
  <p>And Im integrating J2ee/jpa/servlet 3/... (Glassfish 3) in this project.</p>
  
  <p>I've seen anotations like :</p>
  
  <pre><code>@RolesAllowed ("viewer")
  void doListdata (...) {
      istData() ;
  }
  
  @RolesAllowed("admin")
  void doUpdateData (...) {
      updateData() ;
  }
  
  @PermotAll
  void dolisterror () {
      listerror() ;
  }
  </code></pre>
  
  <p>but <em>how</em> can I manually state, in login(), that my user is in the admin and/or viewer role?</p>