Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: request.getRemoteUser()

  1. #1
    Join Date
    Sep 2004
    Location
    Boston, US
    Posts
    130

    Default request.getRemoteUser()

    Ben,
    Can Ageci set the correct remote user on the request object on successful authentication?

    ie
    Code:
    request.getRemoteUser()
    should transparently return

    Code:
    ((net.sf.acegisecurity.providers.dao.User) auth.getPrincipal()).getUsername()
    Some third party libraries/filters like Clickstream use the standard HttpServletRequest API call getRemoteUser() to determine the remote user. It would be nice if the same API call works even when using Acegi authentication as it would make the Acegi authentication more spec compliant or more aligned with the Servlet Web Container authentication.

    I do see the difficulty implementing this since the HttpServletRequest interface does not have a setRemoteUser(..) and neither does Weblogic's' implementation of this interface however maybe you can come up with a solution like wrapping the container HttpServletRequest with a proxy in the Agegi filter.


    Thanks,
    Sanjiv

  2. #2
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    If you'd like to implement a HttpServletRequest wrapper, I'd be happy to add it to the source code. Personally I'd try one of the following though:

    (i) Modify code relying on HttpServletRequest.getRemoteUser(). The "principal source" should be pluggable in any decent library, or made pluggable by introduction of an interface. I'm sure projects like ClickStream would welcome an pluggable approach. If it's pluggable you can easily obtain the Authentication in the standard way from the ContextHolder.

    (ii) Use a container adapter. That way Acegi Security will be used for authentication via servlet spec security, and the container will thus populate the HttpServletRequest. Although I tend to stay away from container adapters as much as possible (difficult configuration, need to use the servlet spec to commence authentication based on URL requests, non-portable between web containers).

  3. #3
    Join Date
    Nov 2004
    Location
    Caracas/Venezuela
    Posts
    4

    Default

    Here is the HttpServletRequestWrapper

    Code:
    public class AcegiHttpServletRequestWrapper extends HttpServletRequestWrapper {
    
        public AcegiHttpServletRequestWrapper(HttpServletRequest request) {
            super(request);
        }
    
        public boolean isUserInRole(String role) {
            return isGranted(role);
        }
    
        public String getRemoteUser() {
    
            Authentication auth = getAuthentication();
    
            if (auth == null)
                return null;
    
            return ((net.sf.acegisecurity.providers.dao.User) auth.getPrincipal()).getUsername();
        }
    
        private Authentication getAuthentication() {
    
            if (ContextHolder.getContext() != null && ContextHolder.getContext() instanceof SecureContext) {
                return ((SecureContext) ContextHolder.getContext()).getAuthentication();
            }
    
            return null;
        }
    
        private boolean isGranted(String role) {
    
            Authentication auth = getAuthentication();
    
            if (auth == null)
                return false;
    
            for &#40;int i=0; i < auth.getAuthorities&#40;&#41;.length; i++&#41; &#123;
                if &#40;role.equals&#40;auth.getAuthorities&#40;&#41;&#91;i&#93;.getAuthority&#40;&#41;&#41;&#41;
                    return true;
            &#125;
    
            return false;
        &#125;
    &#125;
    Here is the filter

    Code:
    public class AcegiHttpServletRequestFilter implements Filter &#123;
    
        public void init&#40;FilterConfig filterConfig&#41; throws ServletException &#123;&#125;
    
        public void doFilter&#40;ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain&#41; throws IOException, ServletException &#123;
    
            HttpServletRequest request = &#40;HttpServletRequest&#41; servletRequest;
    
            if &#40;!&#40;request instanceof AcegiHttpServletRequestWrapper&#41;&#41; &#123;
                request = new AcegiHttpServletRequestWrapper&#40;request&#41;;
            &#125;
    
            filterChain.doFilter&#40;request, servletResponse&#41;;
        &#125;
    
        public void destroy&#40;&#41; &#123;&#125;
    &#125;
    And here is the web.xml:

    <filter>
    <filter-name>Acegi Http Servlet Request Filter</filter-name>
    <filter-class>packagename.AcegiHttpServletRequestFilter</filter-class>
    </filter>

    <filter-mapping>
    <filter-name>Acegi Http Servlet Request Filter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

  4. #4
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    Thanks Sanjiv, this is a really useful integration. I'll add it to Acegi Security CVS later on today.

  5. #5
    Join Date
    Sep 2004
    Location
    Boston, US
    Posts
    130

    Default

    Ben,
    Actually I just asked the question and never got to implement it.

    Thank 'paramosyermos' for the implementation.

    Looking forward to using this feature.

    Regards,
    Sanjiv

  6. #6
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    Ooops, thanks paramosyermos (could you send me a real name via email, so I can add you to the contributors?).

  7. #7
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    I've now added this to CVS (renamed a little), along with unit tests. See the net.sf.acegisecurity.ui.wrapper package.

  8. #8
    Join Date
    Aug 2004
    Location
    Denver
    Posts
    249

    Default

    Is this implemented in Acegi by default now? I don't see the "net.sf.acegisecurity.ui.wrapper" package.

  9. #9
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    Matt, it's still in CVS. You'll need to checkout. We really need to get a version 0.7.0 out sometime soon....

  10. #10
    Join Date
    Aug 2004
    Location
    Denver
    Posts
    249

    Default

    I was looking CVS - then I discovered there's more than one src tree: src and core/main/src. I'm guessing the "src" tree is deprecated? Thanks for the help, I'll try using the ContextHolderAwareRequestFilter to get request.getRemoteUser() to work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •