-
Notifications
You must be signed in to change notification settings - Fork 0
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
π λ‘κ·Έμμ κΈ°λ₯ ꡬν #10
Changes from 6 commits
b784c2b
3d84b50
f38d088
feba8b9
ebd5e20
025a63c
4350cd3
46a9d0c
916008b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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 lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
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)); | ||
} | ||
} |
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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
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()); | ||
return template; | ||
} | ||
} |
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") | ||
KimTaeO marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public record RedisProperties ( | ||
String host, | ||
int port | ||
){ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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 deleteBlackList(String key) { | ||
return redisBlackListTemplate.delete(key); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μ΄ λ λΆλΆμ νμκ° μμκΉ μΆμ΅λλ€ λΈλ리μ€νΈμμ μμ νλλΌλ access tokenμ live timeμ΄ μΌλ§ λ¨μ§ μμκ³ RefreshTokenλ μμ λμκΈ° λλ¬Έμ loginμ νλκ² μ’μ보μ¬μ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ν΄λΉ λ©μλ μμ νμ΅λλ€ |
||
|
||
public boolean hasKeyBlackList(String key) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. λ©μλλͺ ... λκ° κ΅¬λ¦½λλ€ |
||
return redisBlackListTemplate.hasKey(key); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ¬κΈ°μ template.setEnableTransactionSupport(true); νμ λ€μμ Service ν΄λμ€μ @transactional μ΄λ Έν μ΄μ λΆμ¬μ£Όλκ² μ’μ보μ¬μ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
4350cd3
μΆκ°νμ΅λλ€
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@transactionμ Config λ©μλκ° μλλΌ LogOutServiceService ν΄λμ€μ λΆμ¬μ£ΌμΈμ!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
916008b
μμ νμ΅λλ€!