1

In tomcat-users.xml is defined user and roles:

<user username="admin" password="admin" roles="user,admin,APP_ADMIN"/>
  <role rolename="user"/>
  <role rolename="APP_ADMIN"/>
  <role rolename="admin"/>

and application security is defined as:

<security-constraint>
        <web-resource-collection>
                <web-resource-name>Dynamic pages</web-resource-name>
                <url-pattern>*.jsp</url-pattern>
        </web-resource-collection>
        <auth-constraint>
                <description>These are the roles who have access.</description>
                <role-name>*</role-name>
        </auth-constraint>
        <user-data-constraint>
                <description></description>
                <transport-guarantee>NONE</transport-guarantee>
        </user-data-constraint>

But when I login as admin into application it gives me always HTTP 403 not authorized.
I checked roles with JSP scriplet:

out.write(request.getUserPrincipal().toString()); 

And it prints:

User username="admin", roles="user,admin,APP_ADMIN"

But when i check isUserInRole:

out.write(request.isUserInRole("APP_ADMIN") ? "Yep" : "nope");

Gets:

nope

Tomcat version is 7.0.55

JIV
  • 813
  • 2
  • 12
  • 30

2 Answers2

2

1: You might have to define the roles in the web.xml. See this SO Question Why do I list security roles in web.xml when they're in jdbcRealm database?.

2: The wildcard '*' in the role-name could be causing trouble. Maybe give it a try with role-name 'user' and see if it works.

For a wildcard as role name you have to enable allRolesMode:

This attribute controls how the special role name * is handled when processing authorization constraints in web.xml. By default, the specification compliant value of strict is used which means that the user must be assigned one of the roles defined in web.xml. The alternative values are authOnly which means that the user must be authenticated but no check is made for assigned roles and strictAuthOnly which means that the user must be authenticated and no check will be made for assigned roles unless roles are defined in web.xml in which case the user must be assigned at least one of those roles.

See the Tomcat docs for more: https://tomcat.apache.org/tomcat-7.0-doc/config/realm.html

Community
  • 1
  • 1
jHilscher
  • 1,810
  • 2
  • 25
  • 29
0

Finally i found the problem, i just had to replace lines in server.xml:

<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
             resourceName="UserDatabase"/>

with these:

<Realm className="org.apache.catalina.realm.LockOutRealm">
               <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
  </Realm> 

Now I don't know why UserDatabaseRealm doesn't work without LockOutRealm wrapper, strange but its working now ...

JIV
  • 813
  • 2
  • 12
  • 30