Skip to content

Commit

Permalink
Merge pull request #740 from GraudationProject2023/YongHyeon
Browse files Browse the repository at this point in the history
Yong hyeon
  • Loading branch information
dydgus1052 authored Sep 25, 2023
2 parents cd9b7a1 + 52b27d9 commit b23d333
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 16 deletions.
6 changes: 6 additions & 0 deletions src/main/java/GraduationProject/TripPlannerZ/RedisConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public RedisConnectionFactory redisConnectionFactory() {
return redisTemplate;
}

@Bean
public RedisTemplate<?, ?> blackList() {
RedisTemplate<byte[], byte[]> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory((redisConnectionFactory()));
return redisTemplate;
}


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package GraduationProject.TripPlannerZ.config;

import GraduationProject.TripPlannerZ.util.RedisUtil;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
Expand All @@ -16,6 +17,7 @@
public class JwtAuthFilter extends OncePerRequestFilter {

private final UserAuthProvider userAuthProvider;
private final RedisUtil redisUtil;

@Override
protected void doFilterInternal(HttpServletRequest request,
Expand All @@ -29,6 +31,12 @@ protected void doFilterInternal(HttpServletRequest request,
String[] elements = header.split(" ");
System.out.println("elements[1] = " + elements[1]);

if (elements.length == 2 && "Bearer".equals(elements[0])) {
if (redisUtil.existBlackList(elements[1])) {
throw new RuntimeException("유효하지 않은 토큰 입니다.");
}
}

// key가 Bearer이고 value가 멤버의 토큰값
if (elements.length == 2 && "Bearer".equals(elements[0])) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package GraduationProject.TripPlannerZ.config;

import GraduationProject.TripPlannerZ.util.RedisUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -17,13 +18,14 @@ public class SecurityConfig {

private final UserAuthenticationEntryPoint userAuthenticationEntryPoint;
private final UserAuthProvider userAuthProvider;
private final RedisUtil redisUtil;

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.exceptionHandling().authenticationEntryPoint(userAuthenticationEntryPoint)
.and()
.addFilterBefore(new JwtAuthFilter(userAuthProvider), BasicAuthenticationFilter.class)
.addFilterBefore(new JwtAuthFilter(userAuthProvider, redisUtil), BasicAuthenticationFilter.class)
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@

import GraduationProject.TripPlannerZ.config.UserAuthProvider;
import GraduationProject.TripPlannerZ.domain.MemberPreference;
import GraduationProject.TripPlannerZ.dto.member.Credential;
import GraduationProject.TripPlannerZ.dto.member.MemberDto;
import GraduationProject.TripPlannerZ.dto.member.MemberRegister;
import GraduationProject.TripPlannerZ.dto.member.*;
import GraduationProject.TripPlannerZ.domain.Member;
import GraduationProject.TripPlannerZ.dto.member.ChangeMemberInfo;
import GraduationProject.TripPlannerZ.delete.MemberLogin;
import GraduationProject.TripPlannerZ.dto.member.MemberTrip;
import GraduationProject.TripPlannerZ.dto.member.MyPage;
import GraduationProject.TripPlannerZ.service.*;


import GraduationProject.TripPlannerZ.service.TripService;
import GraduationProject.TripPlannerZ.sseEmitter.SseEmitterService;
import GraduationProject.TripPlannerZ.util.RedisUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
Expand All @@ -38,12 +34,10 @@
public class MemberController {

private final MemberService memberService;
private final LoginService loginService;
private final TripService tripService;
private final MemberPreferenceService memberPreferenceService;
private final UserAuthProvider userAuthProvider;
private final SseEmitterService sseEmitterService;
private final PartyService partyService;
private final AuthService authService;



Expand Down Expand Up @@ -81,12 +75,10 @@ public SseEmitter subscribe() {

}

@GetMapping("/members/logout")
public void logout(HttpServletRequest request) {
HttpSession session = request.getSession(false);

if (session != null)
session.invalidate();
@PostMapping("/members/logout")
public void logout(@RequestBody BlackList blackList) {
System.out.println("blackList.getToken() = " + blackList.getToken());
authService.logout(blackList.getToken());
}

@GetMapping("/members/tripInfo")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package GraduationProject.TripPlannerZ.dto.member;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class BlackList {

private String token;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package GraduationProject.TripPlannerZ.service;

import GraduationProject.TripPlannerZ.util.RedisUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class AuthService {

private final RedisUtil redisUtil;

public void logout(String accessToken) {
redisUtil.setBlackList(accessToken, "BlackList", 3_600_000);
}
}
11 changes: 11 additions & 0 deletions src/main/java/GraduationProject/TripPlannerZ/util/RedisUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
public class RedisUtil {

private final StringRedisTemplate redisTemplate;
private final StringRedisTemplate blackList;

public String getData(String key) {
ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
Expand All @@ -27,6 +28,12 @@ public void setDataExpire(String key, String value, long duration) {
valueOperations.set(key, value, expireDuration);
}

public void setBlackList(String key, String value, long duration) {
ValueOperations<String, String> valueOperations = blackList.opsForValue();
Duration expireDuration = Duration.ofSeconds(duration);
valueOperations.set(key, "BlackList", expireDuration);
}

public void deleteData(String key) {
// 데이터 삭제
redisTemplate.delete(key);
Expand All @@ -36,4 +43,8 @@ public boolean existData(String key) {
return Boolean.TRUE.equals(redisTemplate.hasKey(key));
}

public boolean existBlackList(String key) {
return Boolean.TRUE.equals(blackList.hasKey(key));
}

}

0 comments on commit b23d333

Please sign in to comment.