Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case insensitive check for role in HttpServletRequest

The javax.servlet.http.HttpServletRequest class has a method called isUserInRole. I use this to check if a user has, for example, the admin role. However, that method is case sensitive. So, if the role in the request was Admin or ADMIN, then isUserInRole("admin") would be false. I use the isUserInRole method in a number of places accross multiple applications to check for a number of different roles.

Is there a way to achieve the isUserInRole functionality case-insensitively that does not require checking each different possible case combination with isUserInRole?

like image 797
Andrew Mairose Avatar asked Dec 09 '15 17:12

Andrew Mairose


People also ask

Is request getHeader case sensitive?

The docs for getHeader(String) state: The header name is case insensitive.

Is request getParameter case sensitive?

Parameter names are case sensitive so, for example, request. get- Parameter("Param1") and request. getParameter("param1") are not interchangeable. The values supplied to getParameter and getParameterValues are case sensitive.

What is Httpservlet request?

The HttpServletRequest provides methods for accessing parameters of a request. The type of the request determines where the parameters come from. In most implementations, a GET request takes the parameters from the query string, while a POST request takes the parameters from the posted arguments.

What is HttpServletRequest and HttpServletResponse?

The HttpServletRequest object can be used to retrieve incoming HTTP request headers and form data. The HttpServletResponse object can be used to set the HTTP response headers (e.g., content-type) and the response message body.


2 Answers

You could implement a filter that wraps requests using a HttpServletRequestWrapper - implement your HttpServletRequestWrapper to override the isUserInRole() method to make it case-insensitive (eg, configure all roles in upper-case, test role params by converting to upper-case).

A quick search will find plenty of HTTPServletRequestWrapper examples...

like image 147
MattR Avatar answered Nov 09 '22 04:11

MattR


http://docs.oracle.com/javaee/6/tutorial/doc/gjiie.html

Just map multiple role names to the admin role:

<servlet>
    <security-role-ref>
        <role-name>admin</role-name>
        <role-link>admin</role-link>
    </security-role-ref>
    <security-role-ref>
        <role-name>Admin</role-name>
        <role-link>admin</role-link>
    </security-role-ref>
</servlet>

<security-role>
    <role-name>admin</role-name>
</security-role>
like image 3
isak gilbert Avatar answered Nov 09 '22 04:11

isak gilbert