-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from GSM-MSG/feature/9-logout-function
🔀 로그아웃 기능 구현
- Loading branch information
Showing
10 changed files
with
166 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/com/gcms/v3/domain/auth/service/LogoutService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.gcms.v3.domain.auth.service; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
|
||
public interface LogoutService { | ||
void execute(HttpServletRequest request); | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/gcms/v3/domain/auth/service/impl/LogoutServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.gcms.v3.domain.auth.service.impl; | ||
|
||
import com.gcms.v3.domain.auth.domain.entity.RefreshToken; | ||
import com.gcms.v3.domain.auth.domain.repository.RefreshTokenRepository; | ||
import com.gcms.v3.domain.auth.exception.UserNotFoundException; | ||
import com.gcms.v3.domain.auth.service.LogoutService; | ||
import com.gcms.v3.domain.user.domain.entity.User; | ||
import com.gcms.v3.domain.user.util.UserUtil; | ||
import com.gcms.v3.global.redis.RedisUtil; | ||
import com.gcms.v3.global.security.jwt.JwtTokenProvider; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
@Transactional | ||
public class LogoutServiceImpl implements LogoutService { | ||
|
||
private final UserUtil userUtil; | ||
private final RefreshTokenRepository refreshTokenRepository; | ||
private final JwtTokenProvider jwtTokenProvider; | ||
private final RedisUtil redisUtil; | ||
|
||
public void execute(HttpServletRequest request) { | ||
String accessToken = jwtTokenProvider.resolveToken(request); | ||
|
||
User user = userUtil.getCurrentUser(); | ||
|
||
RefreshToken refreshToken = refreshTokenRepository.findByEmail(user.getEmail()) | ||
.orElseThrow(UserNotFoundException::new); | ||
|
||
refreshTokenRepository.delete(refreshToken); | ||
|
||
redisUtil.setBlackList(accessToken, "access_token", jwtTokenProvider.getExpiration(accessToken)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.gcms.v3.domain.user.util; | ||
|
||
import com.gcms.v3.domain.auth.exception.UserNotFoundException; | ||
import com.gcms.v3.domain.user.domain.entity.User; | ||
import com.gcms.v3.domain.user.domain.repository.UserRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class UserUtil { | ||
|
||
private final UserRepository userRepository; | ||
|
||
public User getCurrentUser() { | ||
String email = SecurityContextHolder.getContext().getAuthentication().getName(); | ||
return userRepository.findByEmail(email) | ||
.orElseThrow(UserNotFoundException::new); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.gcms.v3.global.redis; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class RedisConfig { | ||
private final RedisProperties redisProperties; | ||
|
||
@Bean | ||
public RedisConnectionFactory redisConnectionFactory() { | ||
return new LettuceConnectionFactory(redisProperties.host(), redisProperties.port()); | ||
} | ||
|
||
@Bean | ||
public RedisTemplate<String, Object> redisTemplate() { | ||
RedisTemplate<String, Object> template = new RedisTemplate<>(); | ||
template.setValueSerializer(new StringRedisSerializer()); | ||
template.setHashValueSerializer(new StringRedisSerializer()); | ||
template.setConnectionFactory(redisConnectionFactory()); | ||
template.setEnableTransactionSupport(true); | ||
return template; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/gcms/v3/global/redis/RedisProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.gcms.v3.global.redis; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
@ConfigurationProperties(prefix = "spring.data.redis") | ||
public record RedisProperties ( | ||
String host, | ||
int port | ||
){ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.gcms.v3.global.redis; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class RedisUtil { | ||
|
||
private final RedisTemplate<String, Object> redisBlackListTemplate; | ||
|
||
public boolean checkExistsValue(String value) { | ||
return !value.equals("false"); | ||
} | ||
|
||
public void setBlackList(String key, Object o, Long milliSeconds) { | ||
redisBlackListTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(o.getClass())); | ||
redisBlackListTemplate.opsForValue().set(key, o, milliSeconds, TimeUnit.MILLISECONDS); | ||
} | ||
|
||
public Object getBlackList(String key) { | ||
return redisBlackListTemplate.opsForValue().get(key); | ||
} | ||
|
||
public boolean hasKeyBlackList(String key) { | ||
return redisBlackListTemplate.hasKey(key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters