Skip to content

Commit

Permalink
fix: user cache. fix: use jwt expire.
Browse files Browse the repository at this point in the history
  • Loading branch information
ballade0d committed Oct 19, 2024
1 parent 64a2f39 commit 7ce80d0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,23 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.util.Base64;

@Component
public class JWTService {
private final ObjectMapper objectMapper;

private final Algorithm algorithm;
private final JWTVerifier verifier;
private final Long expire;
private final ObjectMapper objectMapper;

public JWTService(@Value("${app.auth.jwt.secret}") String secret, ObjectMapper objectMapper) {
public JWTService(@Value("${app.auth.jwt.secret}") String secret,
@Value("${app.auth.jwt.expire}") Long expire,
ObjectMapper objectMapper) {
this.algorithm = Algorithm.HMAC256(secret);
this.verifier = JWT.require(algorithm).build();
this.expire = expire;
this.objectMapper = objectMapper;
}

Expand All @@ -31,6 +37,7 @@ public record Payload<T>(T value) {
@SneakyThrows
public String generate(Payload<?> payload) {
return JWT.create().withPayload(objectMapper.writeValueAsString(payload))
.withExpiresAt(Instant.now().plusSeconds(expire))
.sign(algorithm);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;
import java.util.Map;

@Configuration
@EnableCaching
@Slf4j
Expand All @@ -42,10 +39,6 @@ public CacheManager cacheManager() {
.prefixCacheNameWith(prefix + ":");
return RedisCacheManager.builder(factory)
.cacheDefaults(configuration)
.withInitialCacheConfigurations(Map.of(
"user", RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofDays(365))
))
.build();
}

Expand Down
42 changes: 42 additions & 0 deletions src/test/java/fun/sast/evento/lark/UserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package fun.sast.evento.lark;

import fun.sast.evento.lark.api.security.Permission;
import fun.sast.evento.lark.domain.event.entity.User;
import fun.sast.evento.lark.infrastructure.auth.JWTService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cache.CacheManager;

@SpringBootTest
public class UserTest {

@Autowired
JWTService jwtService;
@Autowired
CacheManager cacheManager;

@Test
void generateUser(){
User user = new User();
user.setUserId("B00000000");
user.setPermission(Permission.LOGIN.getNum());

String token = jwtService.generate(new JWTService.Payload<>(user));
System.out.println(token);

cacheManager.getCache("user").put("B00000000", token);
}

@Test
void generateAdmin(){
User user = new User();
user.setUserId("B00000001");
user.setPermission(Permission.ADMIN.getNum());

String token = jwtService.generate(new JWTService.Payload<>(user));
System.out.println(token);

cacheManager.getCache("user").put("B00000001", token);
}
}

0 comments on commit 7ce80d0

Please sign in to comment.