title: Spring : Autowired object conflicted between users
body: <p>I am creating a backend application using spring boot, whose users authenticate using a JWT.</p>
  <p>when I validate the user's jwt (using a filter), I parse its content into an object that I named &quot;authentication&quot;, containing the user's id, name, role...</p>
  <p>after creating this object with user data, I autowire it, so that I use it to any class (service, controller...)</p>
  <p>Most of the time I have no problem with it. but sometimes I find that this object contains another user's information.</p>
  <p>I thought that each user has access to an object containing his information, and if this object does not contain information then he will have an empty object, and not an object of the other users</p>
  <p>Can this reasoning create a conflict? and how to solve it?</p>
  <p>This is my filter class</p>
  <pre><code>@Component
  class AuthenticationFilter : Filter, Ordered
  {
      @Autowired
      private lateinit var jwtUtils : JwtUtils
      
      @Autowired
      private lateinit var beanFactory : AutowireCapableBeanFactory
      
      @Autowired
      private lateinit var authentication : Authentication
      
      
      
      override fun doFilter(request : ServletRequest, response : ServletResponse, chain : FilterChain)
      {
          if (request is HttpServletRequest)
          {
              val authFromHeader : Authentication? = getAuthenticatedUser(request)
              
              if (authFromHeader != null)
              {
                  authentication.id = authFromHeader.id
                  authentication.name = authFromHeader.name
                  authentication.role = authFromHeader.role
                  authentication.level = authFromHeader.level
                  
                  beanFactory.autowireBean(authentication)
              }
          }
          
          chain.doFilter(request, response)
      }
      
      
      private fun getAuthenticatedUser(request : HttpServletRequest) : Authentication?
      {
          return try
          {
              val jwtToken : String? = request.getHeader(HttpHeaders.AUTHORIZATION)
              
              jwtUtils.getAuthentication(jwtToken)!!
          }
          catch (exception : Exception)
          {
              null
          }
      }
      
      
      override fun getOrder() : Int
      {
          return Ordered.HIGHEST_PRECEDENCE
      }
  }
  </code></pre>