@Test public void createAccessToken() { final String accessToken = jwtTool.createAccessToken(expectedEmail, expectedRole); System.out.println(accessToken); String actualEmail = Jwts.parser() .setSigningKey(jwtTool.getAccessTokenKey()) .parseClaimsJws(accessToken) .getBody() .getSubject(); assertEquals(expectedEmail, actualEmail); @SuppressWarnings({"unchecked, rawtype"}) List<String> authorities = (List<String>) Jwts.parser() .setSigningKey(jwtTool.getAccessTokenKey()) .parseClaimsJws(accessToken) .getBody() .get(AUTHORITIES); assertEquals(expectedRole, ROLE.valueOf(authorities.get(0))); }
public String createAccessToken(String email, ROLE role) { Claims claims = Jwts.claims().setSubject(email); claims.put(AUTHORITIES, Collections.singleton(role.name())); Date now = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(now); calendar.add(Calendar.MINUTE, accessTokenValidTimeInMinutes); return Jwts.builder() .setClaims(claims) .setIssuedAt(now) .setExpiration(calendar.getTime()) .signWith(SignatureAlgorithm.HS256, accessTokenKey) .compact(); }
JwtTool { public String createAccessToken(String email, ROLE role) { Claims claims = Jwts.claims().setSubject(email); claims.put(AUTHORITIES, Collections.singleton(role.name())); Date now = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(now); calendar.add(Calendar.MINUTE, accessTokenValidTimeInMinutes); return Jwts.builder() .setClaims(claims) .setIssuedAt(now) .setExpiration(calendar.getTime()) .signWith(SignatureAlgorithm.HS256, accessTokenKey) .compact(); } }
JwtTool { public String createAccessToken(String email, ROLE role) { Claims claims = Jwts.claims().setSubject(email); claims.put(AUTHORITIES, Collections.singleton(role.name())); Date now = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(now); calendar.add(Calendar.MINUTE, accessTokenValidTimeInMinutes); return Jwts.builder() .setClaims(claims) .setIssuedAt(now) .setExpiration(calendar.getTime()) .signWith(SignatureAlgorithm.HS256, accessTokenKey) .compact(); } @Autowired  JwtTool(@Value("${accessTokenValidTimeInMinutes}") Integer accessTokenValidTimeInMinutes,
                   @Value("${refreshTokenValidTimeInMinutes}") Integer refreshTokenValidTimeInMinutes,
                   @Value("${tokenKey}") String accessTokenKey); }
JwtTool { public String createAccessToken(String email, ROLE role) { Claims claims = Jwts.claims().setSubject(email); claims.put(AUTHORITIES, Collections.singleton(role.name())); Date now = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(now); calendar.add(Calendar.MINUTE, accessTokenValidTimeInMinutes); return Jwts.builder() .setClaims(claims) .setIssuedAt(now) .setExpiration(calendar.getTime()) .signWith(SignatureAlgorithm.HS256, accessTokenKey) .compact(); } @Autowired  JwtTool(@Value("${accessTokenValidTimeInMinutes}") Integer accessTokenValidTimeInMinutes,
                   @Value("${refreshTokenValidTimeInMinutes}") Integer refreshTokenValidTimeInMinutes,
                   @Value("${tokenKey}") String accessTokenKey); String createAccessToken(String email, ROLE role); String createRefreshToken(User user); String getEmailOutOfAccessToken(String token); boolean isTokenValid(String token, String tokenKey); String getAccessTokenKey(); String getTokenFromHttpServletRequest(HttpServletRequest servletRequest); String generateTokenKey(); }
JwtTool { public String createAccessToken(String email, ROLE role) { Claims claims = Jwts.claims().setSubject(email); claims.put(AUTHORITIES, Collections.singleton(role.name())); Date now = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(now); calendar.add(Calendar.MINUTE, accessTokenValidTimeInMinutes); return Jwts.builder() .setClaims(claims) .setIssuedAt(now) .setExpiration(calendar.getTime()) .signWith(SignatureAlgorithm.HS256, accessTokenKey) .compact(); } @Autowired  JwtTool(@Value("${accessTokenValidTimeInMinutes}") Integer accessTokenValidTimeInMinutes,
                   @Value("${refreshTokenValidTimeInMinutes}") Integer refreshTokenValidTimeInMinutes,
                   @Value("${tokenKey}") String accessTokenKey); String createAccessToken(String email, ROLE role); String createRefreshToken(User user); String getEmailOutOfAccessToken(String token); boolean isTokenValid(String token, String tokenKey); String getAccessTokenKey(); String getTokenFromHttpServletRequest(HttpServletRequest servletRequest); String generateTokenKey();  }