Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8365135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:35:16+00:00 2026-06-09T12:35:16+00:00

I have a jdbc-realm configured on Glassfish and i am trying to implement authentication

  • 0

I have a jdbc-realm configured on Glassfish and i am trying to implement authentication for users with multiple roles. I am using JPA to manage the database tables, JDK 1.7, Glassfish3+, and EclipseLink (JPA 2.0).

I already can successfully login using FORM based authentication, but as a logged-in user i dont have any of the defined roles assigned.

My entity mapping on the relevant classes is

PersonAccount.java:

@Entity
@Table(name="PersonAccount") 
public class PersonAccount implements Serializable {

@Id
@NotNull
private String userName;       
private String passwordHash;        

@ManyToMany(cascade={CascadeType.PERSIST})
private Set<UserRole> userRoles = new HashSet<UserRole>();;

...

UserRole.java:

@Entity
@Table(name="UserRole") 
public class UserRole extends NamedEntity implements Serializable {

@Enumerated(EnumType.ORDINAL)
private persons.util.RoleType roleType = RoleType.StudentRole;

@ManyToMany(mappedBy="userRoles", cascade=
    {CascadeType.PERSIST, CascadeType.REMOVE})
private Set<PersonAccount> personAccounts = new HashSet<PersonAccount>(); 

...

where the base class NamedEntity defines a primary key “id” and a value “name”, and “PersonAccount” itself is assigned to a “Person”.

When creating the first (master admin) account “admin admin” the content of my database looks like this:

PERSONACCOUNT:
.USERNAME = admin
.PASSWORDHASH = 8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918
.PERSON_ID = 220002

PERSONACCOUNT_USERROLE:
.PERSONACCOUNTS_USERNAME admin
.USERROLES_ID = 220001

USERROLE:
.ID = 220001
.NAME = AdminRole
.ROLETYPE = 3

After creating the account i can login with form-based authentication. The principal-name returned is “admin”, but none of the user-roles “AdminRole”, “FacultyRole”, “TeacherRole” or “StudentRole” is assigned. This i can verify by trying to access one of the security protected pages.

Here the configurations of my application and glassfish:

web.xml:

<login-config>
    <auth-method>FORM</auth-method>
    <realm-name>klops</realm-name>
    <form-login-config>
        <form-login-page>/login.xhtml</form-login-page>
        <form-error-page>/login.xhtml?error=true</form-error-page>
    </form-login-config>
</login-config>
<security-role>
    <description>Administrators</description>
    <role-name>AdminRole</role-name>
</security-role>
<security-role>
    <description>Faculty</description>
    <role-name>FacultyRole</role-name>
</security-role>    
<security-role>
    <description>Teachers</description>
    <role-name>TeacherRole</role-name>
</security-role>
<security-role>
    <description>Students</description>
    <role-name>StudentRole</role-name>
</security-role>

glassfish-web.xml

<security-role-mapping>
    <principal-name>admin</principal-name>      
    <role-name>AdminRole</role-name>
    <group-name>AdminRole</group-name>
</security-role-mapping>
<security-role-mapping>
    <principal-name>teacher</principal-name>
    <role-name>TeacherRole</role-name>
    <group-name>TeacherRole</group-name>
</security-role-mapping>
<security-role-mapping>
    <role-name>StudentRole</role-name>
    <group-name>StudentRole</group-name>
    <principal-name>student</principal-name>
</security-role-mapping>
<security-role-mapping>
    <principal-name>faculty</principal-name>
    <role-name>FacultyRole</role-name>
    <group-name>FacultyRole</group-name>
</security-role-mapping>

And finally, the realm configuration:

    <auth-realm name="myjdbcrealm" classname="com.sun.enterprise.security.auth.realm.jdbc.JDBCRealm">
      <property name="jaas-context" value="jdbcRealm"></property>
      <property name="password-column" value="passwordhash"></property>
      <property name="assign-groups" value="AdminRole,FacultyRole,TeacherRole,StudentRole"></property>
      <property name="datasource-jndi" value="jdbc/__default"></property>
      <property name="user-table" value="personaccount"></property>
      <property name="group-table" value="userrole"></property>
      <property name="user-name-column" value="username"></property>
      <property name="group-name-column" value="name"></property>
      <property name="digest-algorithm" value="SHA-256"></property>
    </auth-realm>

I am kind of baffled by the fact that some of the information is found (i can only login with the created account, and only with the correct password), but when im logged in, the user context does not seem to possess any roles.

I have searched everywhere and tried for nearly a day now, but i cannot find the error here.
It would even help to be able to list the roles a connected user has at the moment, but it seems the only way to check for a role is by knowing its name using

FacesContext.getCurrentInstance().getExternalContext().
    isUserInRole("AdminRole"));

Help on this problem would be greatly appreciated 🙂

Thanks,

Patrick

EDIT:

I created a view USER_ACCOUNTS which contains UserRole.name as UserName and PersonAccount_Userrole.username as UserName and changed the realm mapping accordingly, but i still get the same results.
If the username equals the principal-name in the security role mapping, the user has all of the 4 roles. If i remove the principal-name from the mapping the user has no roles at all after logging in.

Now tables contents and configuration are:

PERSONACCOUNT: (Table)
.USERNAME = admin
.PASSWORDHASH = 8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918
.PERSON_ID = 220002

USER_ACCOUNTS: (View)
.USERNAME = admin
.ROLENAME = AdminRole

and

  <property name="password-column" value="passwordhash"></property>
  <property name="assign-groups" value="AdminRole,FacultyRole,TeacherRole,StudentRole"></property>
  <property name="user-table" value="personaccount"></property>
  <property name="group-table" value="user_accounts"></property>
  <property name="user-name-column" value="username"></property>
  <property name="group-name-column" value="rolename"></property>
  <property name="digest-algorithm" value="SHA-256"></property>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:35:17+00:00Added an answer on June 9, 2026 at 12:35 pm

    Given your auth-realm definition (which I’m assuming is valid for the moment), I’d expect to see two tables (with minimum information similar to):

    CREATE TABLE `personaccount` (
      `username` varchar(32) NOT NULL,
      `passwordhash` varchar(64) DEFAULT NULL,
      PRIMARY KEY (`username`)
    );
    
    CREATE TABLE `userrole` (
      `username` varchar(32) NOT NULL,
      `name` varchar(32) DEFAULT NULL, -- role name
      PRIMARY KEY (`username`,`name`) -- allow multiple roles per user
    );
    

    Note that the username column has the same (column) name in both tables.


    I see that you have three tables:

    PERSONACCOUNT
    PERSONACCOUNT_USERROLE
    USERROLE
    

    If you want to preserve that structure, then create a view to map the PERSONACCOUNT_USERROLE and USERROLE tables into something looks like the group table shown above, and make that view your group-table.

    I can confirm that this view-based strategy works with Glassfish 3.1.X and MySQL; it may not be portable to other app-servers.


    Not sure what you’re trying to accomplish with your security-role-mapping including the principal name, other than possibly trying to work around the missing roles from the JDBC Realm.


    Likewise, the purpose of the realm property assign-groups is to establish a set of groups that are always defined for all users. I gather from your later comments this behavior was unexpected; this is apparently the cause.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Glassfish server in production which uses JDBC Realm for authentication. It
I have tried setting up a JDBC Authentication Realm as follows 1-I configured a
I have been busy setting up authentication, a JDBC realm in particular, on GlassFish
I have a java-application using JDBC for database interaction. I want to do a
I have a JRuby project that connects to an Oracle database using JDBC via
I am trying to connect to Oracle DB using JDBC. I have put ojdbc.jar
In pom.xml I have (taken from http://struberg.wordpress.com/2012/05/10/using-jpa-in-real-projects-part-1/ ): <properties> <openjpa.sql.action>refresh</openjpa.sql.action> <database.driver.name>org.postgresql.Driver</database.driver.name> <database.connection.url>jdbc:postgresql://myURL</database.connection.url> <database.user>user</database.user> <database.password>password</database.password>
I have some problem in my JDBC code. I am trying to connect through
I have been trying to get the JDBC portion of the Extension library to
I have multiple BIRT reports that obtains the data from the same jdbc data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.