Spring: how to pass objects from filters to controllers

前端 未结 5 1287
被撕碎了的回忆
被撕碎了的回忆 2021-01-02 05:19

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 \"cen

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-02 05:42

    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

提交回复
热议问题