-
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.
feat : filter로 header의 쿠키 validation
** yml 주석 처리 (일단 보류) ** 아직 확인해보지 않음
- Loading branch information
Showing
4 changed files
with
156 additions
and
71 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,64 @@ | ||
name: Server CD | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Set up JDK 18 | ||
uses: actions/setup-java@v4 | ||
with: | ||
java-version: "18" | ||
distribution: "adopt" | ||
|
||
- name: Run chmod to make gradlew executable | ||
run: chmod +x ./gradlew | ||
|
||
- name: Add application.properties | ||
run: | | ||
mkdir -p ./src/main/resources | ||
touch ./src/main/resources/application.properties | ||
echo "${{ secrets.APPLICATION }}" > ./src/main/resources/application.properties | ||
cat ./src/main/resources/application.properties | ||
- name: Build with Gradle | ||
run: ./gradlew clean build | ||
|
||
- name: Build docker image | ||
run: | | ||
sudo chmod 666 /var/run/docker.sock | ||
docker buildx build --no-cache --platform linux/amd64 -t ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }}:latest -f ./Dockerfile . | ||
- name: Login to Docker Hub | ||
uses: docker/login-action@v2 | ||
with: | ||
username: ${{ secrets.DOCKERHUB_USERNAME }} | ||
password: ${{ secrets.DOCKERHUB_TOKEN }} | ||
|
||
- name: Publish to Docker Hub | ||
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }}:latest | ||
|
||
- name: Connect to WAS & Execute Application | ||
uses: appleboy/ssh-action@v0.1.6 | ||
with: | ||
host: ${{ secrets.SERVER_HOST }} | ||
username: ${{ secrets.SERVER_USERNAME }} | ||
key: ${{ secrets.SERVER_KEY }} | ||
port: ${{ secrets.SERVER_PORT }} | ||
script: | | ||
sudo chmod 666 /var/run/docker.sock | ||
sudo chmod +x /usr/local/bin/docker-compose | ||
cd /home/${{ secrets.SERVER_USERNAME }} | ||
sudo docker-compose stop | ||
sudo docker-compose rm -f | ||
sudo docker-compose pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }}:latest | ||
docker-compose up -d | ||
sudo docker system prune -f | ||
echo "끝" | ||
#name: Server CD | ||
# | ||
#on: | ||
# push: | ||
# branches: | ||
# - main | ||
# | ||
#jobs: | ||
# build: | ||
# runs-on: ubuntu-latest | ||
# | ||
# steps: | ||
# - uses: actions/checkout@v4 | ||
# | ||
# - name: Set up JDK 18 | ||
# uses: actions/setup-java@v4 | ||
# with: | ||
# java-version: "18" | ||
# distribution: "adopt" | ||
# | ||
# - name: Run chmod to make gradlew executable | ||
# run: chmod +x ./gradlew | ||
# | ||
# - name: Add application.properties | ||
# run: | | ||
# mkdir -p ./src/main/resources | ||
# touch ./src/main/resources/application.properties | ||
# echo "${{ secrets.APPLICATION }}" > ./src/main/resources/application.properties | ||
# cat ./src/main/resources/application.properties | ||
# | ||
# - name: Build with Gradle | ||
# run: ./gradlew clean build | ||
# | ||
# - name: Build docker image | ||
# run: | | ||
# sudo chmod 666 /var/run/docker.sock | ||
# docker buildx build --no-cache --platform linux/amd64 -t ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }}:latest -f ./Dockerfile . | ||
# | ||
# - name: Login to Docker Hub | ||
# uses: docker/login-action@v2 | ||
# with: | ||
# username: ${{ secrets.DOCKERHUB_USERNAME }} | ||
# password: ${{ secrets.DOCKERHUB_TOKEN }} | ||
# | ||
# - name: Publish to Docker Hub | ||
# run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }}:latest | ||
# | ||
# - name: Connect to WAS & Execute Application | ||
# uses: appleboy/ssh-action@v0.1.6 | ||
# with: | ||
# host: ${{ secrets.SERVER_HOST }} | ||
# username: ${{ secrets.SERVER_USERNAME }} | ||
# key: ${{ secrets.SERVER_KEY }} | ||
# port: ${{ secrets.SERVER_PORT }} | ||
# script: | | ||
# sudo chmod 666 /var/run/docker.sock | ||
# sudo chmod +x /usr/local/bin/docker-compose | ||
# cd /home/${{ secrets.SERVER_USERNAME }} | ||
# sudo docker-compose stop | ||
# sudo docker-compose rm -f | ||
# sudo docker-compose pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }}:latest | ||
# docker-compose up -d | ||
# sudo docker system prune -f | ||
# echo "끝" |
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,19 @@ | ||
package oauth.config; | ||
|
||
import oauth.auth.JwtService; | ||
import org.springframework.boot.web.servlet.FilterRegistrationBean; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class FilterConfig { | ||
|
||
@Bean | ||
public FilterRegistrationBean<TokenValidationFilter> accessTokenValidationFilter(JwtService jwtService) { | ||
FilterRegistrationBean<TokenValidationFilter> registrationBean = new FilterRegistrationBean<>(); | ||
registrationBean.setFilter(new TokenValidationFilter(jwtService)); | ||
registrationBean.addUrlPatterns("/users/*"); // 필터를 적용할 URL 패턴 설정 | ||
return registrationBean; | ||
} | ||
} | ||
|
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,65 @@ | ||
package oauth.config; | ||
|
||
import jakarta.servlet.Filter; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.FilterConfig; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.ServletRequest; | ||
import jakarta.servlet.ServletResponse; | ||
import jakarta.servlet.http.Cookie; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import oauth.auth.JwtService; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
|
||
@RequiredArgsConstructor | ||
public class TokenValidationFilter implements Filter { | ||
|
||
private final JwtService jwtService; | ||
|
||
@Override | ||
public void init(FilterConfig filterConfig) throws ServletException { | ||
// 필터 초기화 작업이 필요하면 여기에 작성 | ||
} | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { | ||
|
||
HttpServletRequest httpRequest = (HttpServletRequest) request; | ||
HttpServletResponse httpResponse = (HttpServletResponse) response; | ||
|
||
// 쿠키에서 accessToken 가져오기 | ||
Cookie[] cookies = httpRequest.getCookies(); | ||
String accessToken = null; | ||
|
||
if (cookies != null) { | ||
accessToken = Arrays.stream(cookies) | ||
.filter(cookie -> "skt".equals(cookie.getName())) | ||
.map(Cookie::getValue) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
|
||
// 토큰이 존재하면 유효성 검사 수행 | ||
if (accessToken != null && jwtService.isValidToken(accessToken)) { | ||
String userId = jwtService.decodeAccessToken(accessToken); | ||
|
||
// 유효한 토큰인 경우 요청을 계속 처리 | ||
if (userId != null) { | ||
//TODO: destory에 뭘 넣어야 하는거지? | ||
return; | ||
} | ||
} | ||
|
||
// 유효하지 않은 토큰인 경우 401 에러 반환 | ||
httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid access token"); | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
// 필터 종료 작업이 필요하면 여기에 작성 | ||
} | ||
} |