javamysqlauthenticationtomcatjdbcrealm

JDBC Tomcat auth 403 after login / role not taken in account


I have set up my environnement like follows but after the login it says access denied like if the role wasn't set up correctly.

Here some infos:

web.xml

    <security-role>
        <role-name>USER</role-name>
    </security-role>
    <security-role>
        <role-name>ADMIN</role-name>
    </security-role>
     <!--<security-role>
    <role-name>*</role-name>
    </security-role>-->
    
    <security-constraint>
        <display-name>IndexPage</display-name>
        <web-resource-collection>
            <web-resource-name>start</web-resource-name>
            <url-pattern>/pages/protected/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
        <!--<role-name>*</role-name> -->
            <role-name>USER</role-name>
            <role-name>ADMIN</role-name>
        </auth-constraint>
    </security-constraint>
    
    <login-config>
        <auth-method>BASIC</auth-method>
    </login-config>

server.xml

<Engine defaultHost="localhost" name="Catalina">
<Realm className="org.apache.catalina.realm.JDBCRealm"
connectionURL="jdbc:mysql://localhost:3306/jsfdb?user=root"
driverName="com.mysql.jdbc.Driver"
roleNameCol="role_name" 
userCredCol="password"
userNameCol="username1"
userRoleTable="user_role"
userTable="user"
/>

Database

database

user data

user table

user_role data

user role table


Solution

  • The problem was that in the username field from the user_role table the value the primary key of the user table was. Expected is the actual username so I fixed it in changing the PK from id to the username. The db looks like this now:

    db

    God I hate Java