Search code examples
springspring-securityj-security-check

j_username cannot be accessed using spring_security_check


I created a login page using spring_security_check.
Here:

<form name='f' action="/sec_test/j_spring_security_check" method='POST'>

    <table>
        <tr>
            <td>User:</td>
            <td><input type="text" name="j_username" value=''>
            </td>
        </tr>
        <tr>
            <td>Password:</td>
            <td><input type="password" name="j_password" />
            </td>
        </tr>
        <tr>
            <td colspan='2'><input name="submit" type="submit"
                value="submit" />
            </td>
        </tr>
        <tr>
            <td colspan='2'><input name="reset" type="reset" />
            </td>
        </tr>
    </table>
</form>

But I can not have access to j_username afterward.

I've tried:

* request.getParameter("j_username")
* request.getAttribute("j_username")
* request.getAttribute("j_username")
* request.getUserPrincipal().getName()
* request.getRemoteUser()

From all of them, all I'm getting is null!

Any idea what to do?


Solution

  • Spring does authentication for you based on type of the authentication configured in your configuration file. Once authentication is successful it redirects to the login success url(say /login.htm). Because it redirects to /login.htm, you will not get any request parameter (username/user role) unless explicitly set in request in overriden authentication filter.

    After successful authentication spring stores the authenticated information in security context, including user roles. You can access this information from SecurityContext. Refer - link