title: How can i get currently authenticated user Principal with spring security and Redis in spring boot application
body: <p>I want to secure my services with spring security and Json Web Token(JWT) and also  store logged users in redis with spring session.So when user sends login request  i authenticate user and i send generated token back like below:</p>
  
  <pre><code>public ResponseEntity&lt;?&gt; createAuthenticationToken(@RequestBody JwtAuthenticationRequest authenticationRequest, Device device) throws AuthenticationException {
  
      // Perform the security
      final Authentication authentication = authenticationManager.authenticate(
              new UsernamePasswordAuthenticationToken(
                      authenticationRequest.getUsername(),
                      authenticationRequest.getPassword()
              )
      );
      SecurityContextHolder.getContext().setAuthentication(authentication);
  
  
      final UserDetails userDetails = userDetailsService.loadUserByUsername(authenticationRequest.getUsername());
      final String token = jwtTokenUtil.generateToken(userDetails, device);
  
      // Return the token
      return ResponseEntity.ok(new JwtAuthenticationResponse(token));
  }
  </code></pre>
  
  <p>And also  i add that user to  session(backed Redis session):</p>
  
  <pre><code>HttpSession session = request.getSession(false);
      if (session != null) {
          session.setMaxInactiveInterval(30 * 600);
          JwtUser user = (JwtUser) authentication.getPrincipal();
          session.setAttribute("user", user);
  
      }
  </code></pre>
  
  <p>After i check redis , i see user session already set to redis.But when i receive next attempt from user it became <strong>anonymousUser</strong> , ideally it shouldn't.Reason behind this problem is incoming JSESSIONID is invalid somehow.</p>
  
  <pre><code>        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
  </code></pre>
  
  <p>I see <a href="https://i.stack.imgur.com/LfKI4.png" rel="nofollow noreferrer">debug screen</a> Principal returns anonymousUser.</p>
  
  <p>Also i try as follows:</p>
  
  <pre><code>@RequestMapping(value = "/username", method = RequestMethod.GET)
  @ResponseBody
  public String currentUserNameSimple(HttpServletRequest request) {
      Principal principal = request.getUserPrincipal();
      return principal.getName();
  }
  </code></pre>
  
  <p>But  principal is null.</p>
  
  <p>My redis config:</p>
  
  <pre><code>@Configuration
  @EnableRedisHttpSession 
  public class RedisConfig implements BeanClassLoaderAware {
  
  private ClassLoader loader;
  
  
  @Bean(name = { "defaultRedisSerializer", "springSessionDefaultRedisSerializer" })
  public RedisSerializer&lt;Object&gt; springSessionDefaultRedisSerializer() {
      return new GenericJackson2JsonRedisSerializer(objectMapper());
  
  }
  
  @Bean
  public HttpSessionStrategy httpSessionStrategy() {
          return new HeaderHttpSessionStrategy(); 
  }
  
  @Bean
  JedisConnectionFactory jedisConnectionFactory() {
      JedisConnectionFactory jedisConFactory = new JedisConnectionFactory();
      jedisConFactory.setHostName("localhost");
      jedisConFactory.setPort(6379);
      return jedisConFactory;
  }
  
  ObjectMapper objectMapper() {
      ObjectMapper mapper = new ObjectMapper();
      mapper.registerModules(SecurityJackson2Modules.getModules(this.loader));
      return mapper;
  }
  
  public void setBeanClassLoader(ClassLoader classLoader) {
      this.loader = classLoader;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Maven dependencies:</p>
  
  <pre><code>&lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-mail&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-security&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-configuration-processor&lt;/artifactId&gt;
          &lt;optional&gt;true&lt;/optional&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;
          &lt;version&gt;1.4.3.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --&gt;
  
  
  
  
      &lt;!-- Password Validation --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.passay&lt;/groupId&gt;
          &lt;artifactId&gt;passay&lt;/artifactId&gt;
          &lt;version&gt;${passay.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
  
  
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.mobile&lt;/groupId&gt;
          &lt;artifactId&gt;spring-mobile-device&lt;/artifactId&gt;
          &lt;version&gt;1.1.4.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!--JWT --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;io.jsonwebtoken&lt;/groupId&gt;
          &lt;artifactId&gt;jjwt&lt;/artifactId&gt;
          &lt;version&gt;0.7.0&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;az.com.cybernet.utilities&lt;/groupId&gt;
          &lt;artifactId&gt;utilities&lt;/artifactId&gt;
          &lt;version&gt;0.0.1&lt;/version&gt;
          &lt;scope&gt;compile&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;az.com.cybernet.beans&lt;/groupId&gt;
          &lt;artifactId&gt;disieibeans&lt;/artifactId&gt;
          &lt;version&gt;0.0.1&lt;/version&gt;
          &lt;scope&gt;compile&lt;/scope&gt;
      &lt;/dependency&gt;
  
  
  
      &lt;!-- DB dependencies --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.postgresql&lt;/groupId&gt;
          &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
          &lt;version&gt;9.4-1200-jdbc41&lt;/version&gt;
      &lt;/dependency&gt;
  
  
  
  
      &lt;dependency&gt;
          &lt;groupId&gt;javax.el&lt;/groupId&gt;
          &lt;artifactId&gt;el-api&lt;/artifactId&gt;
          &lt;version&gt;2.2&lt;/version&gt;
      &lt;/dependency&gt;
  
  
      &lt;dependency&gt;
          &lt;groupId&gt;net.logstash.logback&lt;/groupId&gt;
          &lt;artifactId&gt;logstash-logback-encoder&lt;/artifactId&gt;
          &lt;version&gt;4.8&lt;/version&gt;
      &lt;/dependency&gt;
  
  
  
      &lt;!-- REDIS --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.session&lt;/groupId&gt;
          &lt;artifactId&gt;spring-session-data-redis&lt;/artifactId&gt;
          &lt;version&gt;2.0.0.BUILD-SNAPSHOT&lt;/version&gt;
          &lt;type&gt;pom&lt;/type&gt;
      &lt;/dependency&gt;
          &lt;dependency&gt;
          &lt;groupId&gt;redis.clients&lt;/groupId&gt;
          &lt;artifactId&gt;jedis&lt;/artifactId&gt;
          &lt;version&gt;2.5.1&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
          &lt;artifactId&gt;spring-security-core&lt;/artifactId&gt;
          &lt;version&gt;4.2.0.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
  
  
  
  
  &lt;/dependencies&gt;
  </code></pre>
  
  <p>Without Redis everything is works good, But with redis i cannot get currently authenticated user's principal</p>