Announcement Announcement Module
Collapse
No announcement yet.
How can I get username in the spring MVC Controller? Page Title Module
Move Remove Collapse
This topic is closed
X
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How can I get username in the spring MVC Controller?

    Now I am using acegi security, but the spring MVC controller could not get the correct username.

    Login.jsp

    <form method="POST" action="/iadvisezou/j_acegi_security_check">


    Login:

    <input type="text" name="j_username">



    Password:

    <input type="password" name="j_password">



    <input type="submit" value="Login">
    <input type="reset" value="Reset">



    </form>

    afterlogin.java

    protected Object formBackingObject(HttpServletRequest request)
    throws IadvisezouException{
    System.out.println("problemid="+id);
    String username = request.getRemoteUser();
    System.out.println("username ="+username);

    But the output username is always null, how can I fix this problem?
    Thanks a lot!

  • #2
    If you want to use the standard request methods you have to add the requestWrapper in your filter chain and <bean id="requestWrapper" class="net.sf.acegisecurity.wrapper.ContextHolderA wareRequestFilter"/>
    in your context file.
    ContextHolderAwareRequestFilter wrapps the request so you can use the standard isUserInRole(), getRemoteUser() and getUserPrincipal() methods.

    regards

    Comment


    • #3
      Just another related note: You can also get the Authentication object from the SecurityContextHolder... which binds the object to a ThreadLocal.

      I have used this in cases where I need to capture user information in business services.. for things like Auditing, etc...


      Cheers,
      Mark

      Comment

      Working...
      X