Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring: how to pass objects from filters to controllers

I'm trying to add a Filter that creates an object that is then to be used inside a controller in a Spring Boot application.

The idea is to use the Filter as a "centralized" generator of this object - that is request-specific and useful only in a controller. I've tried to use the HttpServletRequest request.getSession().setAttribute method: I can access my object in the controller, but then it will be (clearly) added to the session.

Are the Filters the right way to do so? If yes, where can I keep the temporary object generated by the filter to be used by the controllers?

like image 926
Roberto Avatar asked Feb 16 '16 22:02

Roberto


People also ask

What kinds of processing can you do in a filter vs Interceptor?

Filters can modify inbound and outbound requests and responses including modification of headers, entity and other request/response parameters. Interceptors are used primarily for modification of entity input and output streams. You can use interceptors for example to zip and unzip output and input entity streams.

What is the difference between interceptor and filter?

Filter is related to the Servlet API and HandlerIntercepter is a Spring specific concept. Interceptors will only execute after Filters. Fine-grained pre-processing tasks are suitable for HandlerInterceptors (authorization checks, etc.)

How do you use Spring Filters?

There are three ways to add your filter, Annotate your filter with one of the Spring stereotypes such as @Component. Register a @Bean with Filter type in Spring @Configuration. Register a @Bean with FilterRegistrationBean type in Spring @Configuration.


2 Answers

Why Don't you use a Bean with the @Scope('request')

@Component
@Scope(value="request", proxyMode= ScopedProxyMode.TARGET_CLASS)
class UserInfo {
   public String getPassword() {
      return password;
   }

   public void setPassword(String password) {
      this.password = password;
   }

   private String password;
}

and then you can Autowireed this bean in both filter and controller to do setting and getting of data.

lifecycle of this UserInfo bean is only exisits within the request so once the http request is done then it terminates the instance as well

like image 99
imal hasaranga perera Avatar answered Sep 22 '22 12:09

imal hasaranga perera


you can use ServletRequest.setAttribute(String name, Object o);

for example

@RestController
@EnableAutoConfiguration
public class App {

    @RequestMapping("/")
    public String index(HttpServletRequest httpServletRequest) {
        return (String) httpServletRequest.getAttribute(MyFilter.passKey);
    }

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @Component
    public static class MyFilter implements Filter {

        public static String passKey = "passKey";

        private static String passValue = "hello world";

        @Override
        public void init(FilterConfig filterConfig) throws ServletException {

        }

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                throws IOException, ServletException {
            request.setAttribute(passKey, passValue);
            chain.doFilter(request, response);
        }

        @Override
        public void destroy() {

        }
    }
}
like image 44
wcong Avatar answered Sep 20 '22 12:09

wcong