-
Notifications
You must be signed in to change notification settings - Fork 5
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 #103 from youcastle03/weekly/10
Feat: blackList구현
- Loading branch information
Showing
14 changed files
with
194 additions
and
16 deletions.
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
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,57 @@ | ||
package jeje.work.aeatbe.config; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
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.RedisStandaloneConfiguration; | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
@Configuration | ||
@EnableRedisRepositories | ||
public class RedisConfig { | ||
|
||
@Value("${spring.data.redis.host}") | ||
private String host; | ||
|
||
@Value("${spring.data.redis.port}") | ||
private int port; | ||
|
||
@Value("${spring.data.redis.password}") | ||
private String password; | ||
|
||
@Value("${spring.data.redis.username}") | ||
private String username; | ||
|
||
@Bean | ||
public RedisConnectionFactory redisConnectionFactory() { | ||
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(host, port); | ||
config.setUsername(username); | ||
config.setPassword(password); | ||
return new LettuceConnectionFactory(config); | ||
|
||
} | ||
|
||
|
||
@Bean | ||
public RedisTemplate<String, Object> redisTemplate() { | ||
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); | ||
redisTemplate.setConnectionFactory(redisConnectionFactory()); | ||
|
||
redisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
redisTemplate.setValueSerializer(new StringRedisSerializer()); | ||
|
||
redisTemplate.setHashKeySerializer(new StringRedisSerializer()); | ||
redisTemplate.setHashValueSerializer(new StringRedisSerializer()); | ||
|
||
redisTemplate.setDefaultSerializer(new StringRedisSerializer()); | ||
|
||
return redisTemplate; | ||
} | ||
|
||
|
||
|
||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package jeje.work.aeatbe.entity; | ||
|
||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.redis.core.RedisHash; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@RedisHash(value = "blackList" , timeToLive = 3600) | ||
public class BlackList { | ||
|
||
@Id | ||
private String token; | ||
|
||
private String userId; | ||
} |
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
9 changes: 9 additions & 0 deletions
9
src/main/java/jeje/work/aeatbe/repository/BlackListRepository.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,9 @@ | ||
package jeje.work.aeatbe.repository; | ||
|
||
import java.util.Optional; | ||
import jeje.work.aeatbe.entity.BlackList; | ||
import org.springframework.data.repository.CrudRepository; | ||
|
||
public interface BlackListRepository extends CrudRepository<BlackList, String> { | ||
|
||
} |
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,52 @@ | ||
package jeje.work.aeatbe.service; | ||
|
||
import java.util.Optional; | ||
import jeje.work.aeatbe.entity.BlackList; | ||
import jeje.work.aeatbe.repository.BlackListRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class TokenService { | ||
|
||
private final BlackListRepository blackListRepository; | ||
private final RedisTemplate<String, Object> redisTemplate; | ||
|
||
/** | ||
* 로그아웃된 엑세스 토큰을 블랙 리스트에 등록 | ||
* @param accessToken | ||
* @param userId | ||
*/ | ||
public void addBlackList (String accessToken, String userId){ | ||
accessToken = removePrefix(accessToken); | ||
BlackList blackList = new BlackList(accessToken, userId); | ||
blackListRepository.save(blackList); | ||
} | ||
|
||
/** | ||
* 토큰의 Bearer prefix 제거 | ||
* @param token | ||
* @return prefix없는 순수 토큰 | ||
*/ | ||
public String removePrefix(String token){ | ||
if(token.startsWith("Bearer ")){ | ||
return token.substring(7); | ||
} | ||
return token; | ||
} | ||
|
||
/** | ||
* 블랙시스트에 있는지 확인 | ||
* @param accessToken | ||
* @return 유무 | ||
*/ | ||
public boolean isInBlackList(String accessToken){ | ||
Optional<BlackList> blackList = blackListRepository.findById(accessToken); | ||
|
||
return !blackList.isEmpty(); | ||
} | ||
|
||
|
||
} |
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
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