Search code examples
javaspring-securitytomcat8

How to properly extend WebAuthenticationDetails?


Building Spring Boot application, deploying(by copying to webapps folder while Tomcat is down) to local Tomcat8. Always get an error:

No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

As I understand, problems are while instantiating bean with WebAuthenticationDetails extending:

    @Component
    public class AuthDetails extends WebAuthenticationDetails{

        private final AuthTarget authTarget;

        public AuthDetails(HttpServletRequest request) {
            super(request);
            this.authTarget = AuthTarget.valueOf(request.getParameter("target"));
        }

        public AuthTarget getAuthTarget(){
            return this.authTarget;
        }
    }

It cannot provide HttpServletRequest for bean constructing, but I don't know how to evade it.

Tried to add RequestContextListener, in xml or as implementation(and marking as @WebListener), no effect.

Out of ideas, how to fix it. Tried example from here: https://github.com/Baeldung/spring-security-registration , no changes - same error at the similar place.

Any help is greatly welcome.


Solution

  • The key was simple: I should define filter bean explicitly in security configuration extends WebSecurityConfigurerAdapter:

    @Bean
    AuthFilter authFilter() throws Exception{
        AuthFilter authFilter = new AuthFilter();
        return authFilter;
    }
    

    instead of autowiring it.