JwtTokenService
@Service
@RequiredArgsConstructor
public class JwtTokenService {
public static final long EXPIRATION_TIME = 3_600_000;
private final Key key = Keys.secretKeyFor(SignatureAlgorithm.HS256);
public Date getExpiration(long expirationTime) {
return new Date(new Date().getTime() + expirationTime);
}
public String createToken(String userid, Date expiration) {
return Jwts.builder()
.setSubject(userid)
.signWith(key)
.setExpiration(expiration)
.compact();
}
public String createDefaultToken(String userid) {
return createToken(userid, getExpiration(EXPIRATION_TIME));
}
public String validateToken(String token) {
try {
return Jwts.parserBuilder()
.setSigningKey(key).build()
.parseClaimsJws(token)
.getBody()
.getSubject();
} catch (Exception e) {
return null;
}
}
}
JwtTokenServiceTest
@Slf4j
@SpringBootTest
class JwtTokenServiceTest {
@Autowired
public JwtTokenService jwtTokenService;
@Test
public void token() {
String token = jwtTokenService.createDefaultToken("userid");
log.info(token);
String userid = jwtTokenService.validateToken(token);
log.info(userid);
}
}
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.6</version>
</parent>
<properties>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<springboot.version>3.0.6</springboot.version>
<lombok.version>1.18.26</lombok.version>
<jjwt.version>0.11.5</jjwt.version>
<junit.version>5.10.0-M1</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>${jjwt.version}</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>${jjwt.version}</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>${jjwt.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${springboot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>