Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JWT 로그인 완료 #10

Merged
merged 13 commits into from
Jul 21, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(Login): Redis에 JWT 정보를 기록
limehee committed Jul 20, 2024
commit caeb33291735c2578f20f4a0884f3c7ba8a8c5aa
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.stempo.api.domain.application.service;

import com.stempo.api.domain.domain.model.RedisToken;
import com.stempo.api.domain.domain.model.Role;
import com.stempo.api.domain.presentation.dto.response.TokenInfo;

public interface RedisTokenService {

RedisToken findByAccessToken(String token);

RedisToken findByRefreshToken(String token);

void saveToken(String id, Role role, TokenInfo tokenInfo);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.stempo.api.domain.application.service;

import com.stempo.api.domain.domain.model.RedisToken;
import com.stempo.api.domain.domain.model.Role;
import com.stempo.api.domain.domain.repository.RedisTokenRepository;
import com.stempo.api.domain.presentation.dto.response.TokenInfo;
import com.stempo.api.global.auth.exception.TokenNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class RedisTokenServiceImpl implements RedisTokenService {

private final RedisTokenRepository redisTokenRepository;

@Override
public RedisToken findByAccessToken(String token) {
return redisTokenRepository.findByAccessToken(token)
.orElseThrow(() -> new TokenNotFoundException("존재하지 않는 토큰입니다."));
}

@Override
public RedisToken findByRefreshToken(String token) {
return redisTokenRepository.findByRefreshToken(token)
.orElseThrow(() -> new TokenNotFoundException("존재하지 않는 토큰입니다."));
}

@Override
public void saveToken(String id, Role role, TokenInfo tokenInfo) {
RedisToken redisToken = RedisToken.create(id, role, tokenInfo);
redisTokenRepository.save(redisToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.stempo.api.domain.domain.model;

import com.stempo.api.domain.presentation.dto.response.TokenInfo;
import jakarta.persistence.Column;
import jakarta.persistence.Id;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.index.Indexed;

@Getter
@Setter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@RedisHash(value = "refresh", timeToLive = 60 * 60 * 24 * 14)
public class RedisToken {

@Id
@Column(name = "user_id")
private String id;

private Role role;

@Indexed
private String accessToken;

@Indexed
private String refreshToken;

public static RedisToken create(String id, Role role, TokenInfo tokenInfo) {
return RedisToken.builder()
.id(id)
.role(role)
.accessToken(tokenInfo.getAccessToken())
.refreshToken(tokenInfo.getRefreshToken())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.stempo.api.domain.domain.repository;

import com.stempo.api.domain.domain.model.RedisToken;

import java.util.Optional;

public interface RedisTokenRepository {

Optional<RedisToken> findByAccessToken(String token);

Optional<RedisToken> findByRefreshToken(String token);

void save(RedisToken redisToken);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.stempo.api.domain.persistence.repository;

import com.stempo.api.domain.domain.model.RedisToken;
import org.springframework.data.repository.CrudRepository;

import java.util.Optional;

public interface RedisTokenJpaRepository extends CrudRepository<RedisToken, String> {

Optional<RedisToken> findByAccessToken(String token);

Optional<RedisToken> findByRefreshToken(String token);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.stempo.api.domain.persistence.repository;

import com.stempo.api.domain.domain.model.RedisToken;
import com.stempo.api.domain.domain.repository.RedisTokenRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
@RequiredArgsConstructor
public class RedisTokenRepositoryImpl implements RedisTokenRepository {

private final RedisTokenJpaRepository repository;

@Override
public Optional<RedisToken> findByAccessToken(String token) {
return repository.findByAccessToken(token);
}

@Override
public Optional<RedisToken> findByRefreshToken(String token) {
return repository.findByRefreshToken(token);
}

@Override
public void save(RedisToken redisToken) {
repository.save(redisToken);
}
}