-
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 #4 from KAKAO-TOUR-API-CONTEST/develop
FEAT : add a headerInterceptor
- Loading branch information
Showing
19 changed files
with
287 additions
and
123 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,6 @@ public class AppConfig { | |
public RestTemplate restTemplate() { | ||
return new RestTemplate(); | ||
} | ||
|
||
|
||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/com/example/ai_jeju/config/HeaderCheckInterceptor.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,50 @@ | ||
package com.example.ai_jeju.config; | ||
|
||
import com.example.ai_jeju.jwt.TokenProvider; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
@Component | ||
public class HeaderCheckInterceptor implements HandlerInterceptor { | ||
|
||
private final static String HEADER_AUTHORIZATION = "Authorization"; | ||
private final static String TOKEN_PREFIX = "Bearer "; | ||
@Autowired | ||
private TokenProvider tokenProvider; | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | ||
// 예시로 "X-Custom-Header"라는 헤더를 확인 | ||
String headerValue = request.getHeader("access-token"); | ||
|
||
String authorizationHeader = request.getHeader(HEADER_AUTHORIZATION); | ||
|
||
// 가져온 값에서 접두사 제거 | ||
String token = getAccesToken(authorizationHeader); | ||
|
||
// 가져온 토큰이 유효한지 확인하고, 유효한 때는 인증정보 설정한다. | ||
System.out.println("토큰 유효성 검사"+tokenProvider.validToken(token)); | ||
|
||
|
||
// 헤더가 올바르면 요청을 진행 | ||
return true; | ||
} | ||
|
||
private String getAccesToken(String authorizationHeader){ | ||
System.out.println("get Access Token 실행"); | ||
|
||
if(authorizationHeader != null && authorizationHeader.startsWith(TOKEN_PREFIX)){ | ||
|
||
System.out.println("get Access Token : " + authorizationHeader.substring(TOKEN_PREFIX.length())); | ||
|
||
return authorizationHeader.substring(TOKEN_PREFIX.length()); | ||
} | ||
else{ | ||
System.out.println("Access Token 없음"); | ||
} | ||
return null; | ||
} | ||
} |
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,19 @@ | ||
package com.example.ai_jeju.config; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
public class WebConfig implements WebMvcConfigurer { | ||
|
||
@Autowired | ||
private HeaderCheckInterceptor headerCheckInterceptor; | ||
|
||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
// 모든 경로에 대해 인터셉터를 적용 (필요에 따라 경로를 지정할 수 있음) | ||
registry.addInterceptor(headerCheckInterceptor).addPathPatterns("/**"); | ||
} | ||
} |
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
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,40 @@ | ||
package com.example.ai_jeju.domain; | ||
|
||
|
||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
@Table(name="play") | ||
@NoArgsConstructor(access= AccessLevel.PROTECTED) //기본생성자 | ||
@Getter | ||
@Entity | ||
@AllArgsConstructor // 모든 필드를 초기화하는 생성자 | ||
@Builder // 빌더 패턴 | ||
public class Play { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "playId", updatable = false, unique = true) | ||
int playId; | ||
|
||
@Column(name = "playName", updatable = false) | ||
String playName; | ||
|
||
@Column(name = "latitude", updatable = false) | ||
double latitude; | ||
|
||
@Column(name = "longitude", updatable = false) | ||
double longitude; | ||
|
||
@Column(name = "addr", updatable = false) | ||
String address; | ||
|
||
@Column(name = "dc", updatable = false) | ||
int chair; | ||
|
||
@Column(name = "nokidsZone", updatable = false) | ||
Boolean nokidsZone; | ||
|
||
@Column(name = "operation_time", updatable = false) | ||
int operationTime; | ||
} |
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 |
---|---|---|
|
@@ -36,4 +36,6 @@ public RefreshToken update(String newRefreshToken){ | |
} | ||
|
||
|
||
|
||
|
||
} |
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
29 changes: 29 additions & 0 deletions
29
src/main/java/com/example/ai_jeju/filter/HeaderCheckFilter.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,29 @@ | ||
package com.example.ai_jeju.filter; | ||
|
||
import java.io.IOException; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
public class HeaderCheckFilter extends OncePerRequestFilter { | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) | ||
throws ServletException, IOException { | ||
|
||
// 예시로 "X-Custom-Header"라는 헤더를 확인 | ||
String headerValue = request.getHeader("X-Custom-Header"); | ||
|
||
if (headerValue == null || !headerValue.equals("ExpectedValue")) { | ||
// 헤더가 없거나 예상된 값이 아닐 경우 요청을 차단 | ||
response.setStatus(HttpServletResponse.SC_BAD_REQUEST); | ||
response.getWriter().write("Missing or Invalid Header"); | ||
return; | ||
} | ||
|
||
// 헤더가 올바르면 다음 필터로 진행 | ||
filterChain.doFilter(request, response); | ||
} | ||
} |
Oops, something went wrong.