title: How to get Refresh Token in spring boot using JWT
body: <p>I am doing spring security for REST API using JWT. I have already done with creating web token with expiry time and its working fine . I have set the time limit to 5 minutes . After 5 minutes, the token will get expired. This causes problem for me, so can anyone guide me how to solve this one by using refresh token, since I am very new to this concept.</p>
  
  <p>Here is my code..</p>
  
  <p>SpringSecurityConfiguration</p>
  
  <pre><code> @Bean
   public JwtAuthenticationTokenFilter authenticationTokenFilter() {
      JwtAuthenticationTokenFilter filter = new 
      JwtAuthenticationTokenFilter();
      filter.setAuthenticationManager(authenticationManager());
      filter.setAuthenticationSuccessHandler(new JwtSuccessHandler());
      return filter;
  }
  
  
  @Override
  protected void configure(HttpSecurity http) throws Exception {
  
      http.csrf().disable()
              .authorizeRequests().antMatchers("/admin/**").authenticated()
              .antMatchers("/admin/**").hasAnyAuthority("Admin")
              .and()
              .exceptionHandling().authenticationEntryPoint(entryPoint)
              .and()
              .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
  
      http.addFilterBefore(authenticationTokenFilter(), UsernamePasswordAuthenticationFilter.class);
      http.headers().cacheControl();
  
  }
  </code></pre>
  
  <p>TokenController</p>
  
  <pre><code>@RestController
  @RequestMapping("/token")
  public class TokenController {
  
  private JwtGenerator jwtGenerator;
  
  public TokenController(JwtGenerator jwtGenerator) {
      this.jwtGenerator = jwtGenerator;
  }
  
  @RequestMapping(method = RequestMethod.POST)
  public String generate(@RequestBody final User user) {
  
      return jwtGenerator.generate(user);
  
  }
  }
  </code></pre>
  
  <p>JwtGenerator</p>
  
  <pre><code>@Component
  public class JwtGenerator { 
  private Long expiration;
  private String secret = "youtube";
  static final String CLAIM_KEY_CREATED = "created";
  
  public String generate(User user) {
      Claims claims = Jwts.claims()
              .setSubject(user.getFirstName());
      claims.put("password", String.valueOf(user.getPassword()));
      //claims.put("role", jwtUser.getRole());
  
      return Jwts.builder()
              .setClaims(claims)
              .setExpiration(generateExpirationDate())
              .signWith(SignatureAlgorithm.HS512, "youtube")
              .compact();
  }
  
  private Date generateExpirationDate() {
      return new Date(System.currentTimeMillis()  + (5 * 60 * 1000));
  }
  }
  </code></pre>
  
  <p>JwtAuthenticationProvider</p>
  
  <pre><code>@Override
  protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken) throws AuthenticationException {
  
      JwtAuthenticationToken jwtAuthenticationToken = (JwtAuthenticationToken) usernamePasswordAuthenticationToken;
      String token = jwtAuthenticationToken.getToken();
  
      User user = validator.validate(token);
  
      if (user == null) {
          throw new RuntimeException("JWT Token is incorrect");
      }
      String firstname=user.getFirstName();
      User user1=userRepository.getRoleId(firstname);
      List&lt;GrantedAuthority&gt; grantedAuthorities = AuthorityUtils
              .commaSeparatedStringToAuthorityList(user1.getRole().getRoleName());
      return new JwtUserDetails(user.getFirstName(), user.getPassword(),
              token,
              grantedAuthorities);
  }
  
  @Override
  public boolean supports(Class&lt;?&gt; aClass) {
      return (JwtAuthenticationToken.class.isAssignableFrom(aClass));
  }
  </code></pre>
  
  <p>JwtValidator</p>
  
  <pre><code>@Component
  public class JwtValidator {
  
  
  private String secret = "youtube";
  
  public User validate(String token) {
  
      User user = null;
      try {
          Claims body = Jwts.parser()
                  .setSigningKey(secret)
                  .parseClaimsJws(token)
                  .getBody();
  
          user = new User();
          user.setFirstName(body.getSubject());
          user.setPassword((String) body.get("password"));
          //user.setRole((String) body.get("role"));
      }
      catch (Exception e) {
          System.out.println(e);
      }
  
      return user;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I am passing username and password to get the token.Thanks in advance</p>