Skip to content

Commit

Permalink
[BE] 6차 데모데이 배포를 진행한다. (#860)
Browse files Browse the repository at this point in the history
  • Loading branch information
JINU-CHANG authored Oct 22, 2024
2 parents 3004105 + d88f3ed commit 2edbe43
Show file tree
Hide file tree
Showing 140 changed files with 3,079 additions and 1,160 deletions.
12 changes: 11 additions & 1 deletion .github/workflows/backend-dev-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,18 @@ jobs:
with:
name: bang-ggood-be-develop-jar

- name: Create unhealth_flag file
run: echo "unhealth" | sudo tee /etc/nginx/sites-available/unhealth_flag.txt > /dev/null


- name: Sleep for 30 seconds
run: sleep 30

- name: Turn off the server 8080 if runs
run: sudo fuser -k -n tcp 8080 || true

- name: Start server
run: sudo nohup java -jar -Dspring.profiles.active=dev -Duser.timezone=Asia/Seoul ./backend/bang-ggood/build/libs/*SNAPSHOT.jar > /home/ubuntu/actions-runner/server.log 2>&1 &

- name: Delete unhealth_flag file
run: sudo rm /etc/nginx/sites-available/unhealth_flag.txt
Binary file added backend/bang-ggood/.DS_Store
Binary file not shown.
8 changes: 6 additions & 2 deletions backend/bang-ggood/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ dependencies {
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-aop'
implementation 'org.projectlombok:lombok'
implementation 'com.mysql:mysql-connector-j'
implementation 'com.opencsv:opencsv:5.9'

implementation 'io.jsonwebtoken:jjwt-api:0.11.2'
implementation 'io.jsonwebtoken:jjwt-impl:0.11.2'
implementation 'io.jsonwebtoken:jjwt-gson:0.11.2'
implementation 'com.mysql:mysql-connector-j'
implementation 'com.opencsv:opencsv:5.9'

annotationProcessor 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
Expand Down
Binary file added backend/bang-ggood/src/.DS_Store
Binary file not shown.
Binary file added backend/bang-ggood/src/main/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public ArticleController(ArticleService articleService) {
public ResponseEntity<Void> createArticle(@AuthRequiredPrincipal User user,
@Valid @RequestBody ArticleCreateRequest request) {
Long id = articleService.createArticle(request);
return ResponseEntity.created(URI.create("articles/" + id)).build();
return ResponseEntity.created(URI.create("/article/" + id)).build();
}

@GetMapping("/articles/{id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.util.Objects;

import static lombok.AccessLevel.PROTECTED;

@Getter
@NoArgsConstructor(access = PROTECTED)
@Entity
public class Article extends BaseEntity {

Expand All @@ -32,33 +38,6 @@ public Article(String title, String content, String keyword, String summary, Str
this.thumbnail = thumbnail;
}

protected Article() {
}

public Long getId() {
return id;
}

public String getTitle() {
return title;
}

public String getContent() {
return content;
}

public String getKeyword() {
return keyword;
}

public String getSummary() {
return summary;
}

public String getThumbnail() {
return thumbnail;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ default Article getById(Long id) {

@Query("SELECT a FROM Article a " +
"WHERE a.deleted = false " +
"ORDER BY a.createdAt DESC ")
"ORDER BY a.createdAt DESC, a.id DESC")
List<Article> findLatestArticles();

@Modifying(flushAutomatically = true, clearAutomatically = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@

import com.bang_ggood.article.domain.Article;
import com.bang_ggood.article.dto.request.ArticleCreateRequest;
import com.bang_ggood.article.dto.response.ArticlesResponse;
import com.bang_ggood.article.dto.response.ArticleResponse;
import com.bang_ggood.article.dto.response.ArticlesResponse;
import com.bang_ggood.article.dto.response.ArticlesResponses;
import com.bang_ggood.article.repository.ArticleRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@RequiredArgsConstructor
@Service
public class ArticleService {

private final ArticleRepository articleRepository;

public ArticleService(ArticleRepository articleRepository) {
this.articleRepository = articleRepository;
}

@Transactional
public long createArticle(ArticleCreateRequest request) {
Article article = request.toEntity();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.bang_ggood.auth.config;

import com.bang_ggood.auth.controller.CookieResolver;
import com.bang_ggood.auth.controller.cookie.CookieResolver;
import com.bang_ggood.auth.service.AuthService;
import com.bang_ggood.global.exception.BangggoodException;
import com.bang_ggood.global.exception.ExceptionCode;
import com.bang_ggood.user.domain.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.core.MethodParameter;
Expand Down Expand Up @@ -35,12 +33,8 @@ public User resolveArgument(MethodParameter parameter, ModelAndViewContainer mav
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) {
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();

// TODO 리팩토링
if (request.getCookies() == null || cookieResolver.isTokenNotExist(request.getCookies())) {
throw new BangggoodException(ExceptionCode.AUTHENTICATION_TOKEN_EMPTY);
}

String token = cookieResolver.extractAccessToken(request.getCookies());
cookieResolver.checkLoginRequired(request);
String token = cookieResolver.extractAccessToken(request);
return authService.getAuthUser(token);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.bang_ggood.auth.config;

import com.bang_ggood.auth.controller.CookieResolver;
import com.bang_ggood.auth.controller.cookie.CookieResolver;
import com.bang_ggood.auth.service.AuthService;
import com.bang_ggood.user.domain.User;
import jakarta.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -33,11 +33,11 @@ public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer m
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) {
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();

if (request.getCookies() == null || cookieResolver.isTokenNotExist(request.getCookies())) {
if (cookieResolver.isTokenEmpty(request)) {
return authService.assignGuestUser();
}

String token = cookieResolver.extractAccessToken(request.getCookies());
String token = cookieResolver.extractAccessToken(request);
return authService.getAuthUser(token);
}
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,51 @@
package com.bang_ggood.auth.controller;

import com.bang_ggood.auth.config.AuthRequiredPrincipal;
import com.bang_ggood.auth.controller.cookie.CookieProvider;
import com.bang_ggood.auth.controller.cookie.CookieResolver;
import com.bang_ggood.auth.dto.request.LocalLoginRequestV1;
import com.bang_ggood.auth.dto.request.OauthLoginRequest;
import com.bang_ggood.auth.dto.request.RegisterRequestV1;
import com.bang_ggood.auth.dto.response.AuthTokenResponse;
import com.bang_ggood.auth.dto.response.TokenExistResponse;
import com.bang_ggood.auth.service.AuthService;
import com.bang_ggood.user.domain.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseCookie;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import java.net.URI;

@RequiredArgsConstructor
@RestController
public class AuthController {

private final AuthService authService;
private final CookieProvider cookieProvider;
private final CookieResolver cookieResolver;

public AuthController(AuthService authService, CookieProvider cookieProvider, CookieResolver cookieResolver) {
this.authService = authService;
this.cookieProvider = cookieProvider;
this.cookieResolver = cookieResolver;
@PostMapping("/v1/local-auth/register")
public ResponseEntity<Void> register(@Valid @RequestBody RegisterRequestV1 request) {
Long userId = authService.register(request);
return ResponseEntity.created(URI.create("/v1/local-auth/register/" + userId)).build();
}

@DeleteMapping("/v1/withdraw")
public ResponseEntity<Void> withdraw(@AuthRequiredPrincipal User user) {
authService.withdraw(user);
return ResponseEntity.noContent().build();
}

@PostMapping("/oauth/login")
public ResponseEntity<Void> login(@Valid @RequestBody OauthLoginRequest request) {
AuthTokenResponse response = authService.login(request);
public ResponseEntity<Void> oauthLogin(@Valid @RequestBody OauthLoginRequest request) {
AuthTokenResponse response = authService.oauthLogin(request);

ResponseCookie accessTokenCookie = cookieProvider.createAccessTokenCookie(response.accessToken());
ResponseCookie refreshTokenCookie = cookieProvider.createRefreshTokenCookie(response.refreshToken());
Expand All @@ -41,14 +56,29 @@ public ResponseEntity<Void> login(@Valid @RequestBody OauthLoginRequest request)
.build();
}

@PostMapping("/oauth/logout")
@PostMapping("/v1/local-auth/login")
public ResponseEntity<Void> localLogin(@Valid @RequestBody LocalLoginRequestV1 request) {
AuthTokenResponse response = authService.localLogin(request);

ResponseCookie accessTokenCookie = cookieProvider.createAccessTokenCookie(response.accessToken());
ResponseCookie refreshTokenCookie = cookieProvider.createRefreshTokenCookie(response.refreshToken());

return ResponseEntity.ok()
.header(HttpHeaders.SET_COOKIE, accessTokenCookie.toString())
.header(HttpHeaders.SET_COOKIE, refreshTokenCookie.toString())
.build();
}

@PostMapping("/v1/logout")
public ResponseEntity<Void> logout(@AuthRequiredPrincipal User user,
HttpServletRequest httpServletRequest) {
String accessToken = cookieResolver.extractAccessToken(httpServletRequest.getCookies());
String refreshToken = cookieResolver.extractRefreshToken(httpServletRequest.getCookies());
String accessToken = cookieResolver.extractAccessToken(httpServletRequest);
String refreshToken = cookieResolver.extractRefreshToken(httpServletRequest);

authService.logout(accessToken, refreshToken, user);
ResponseCookie deletedAccessTokenCookie = cookieProvider.deleteAccessTokenCookie(accessToken);
ResponseCookie deletedRefreshTokenCookie = cookieProvider.deleteRefreshTokenCookie(refreshToken);

ResponseCookie deletedAccessTokenCookie = cookieProvider.deleteAccessTokenCookie();
ResponseCookie deletedRefreshTokenCookie = cookieProvider.deleteRefreshTokenCookie();

return ResponseEntity.noContent()
.header(HttpHeaders.SET_COOKIE, deletedAccessTokenCookie.toString())
Expand All @@ -57,13 +87,35 @@ public ResponseEntity<Void> logout(@AuthRequiredPrincipal User user,
}

@PostMapping("/accessToken/reissue")
public ResponseEntity<Void> reIssueAccessToken(HttpServletRequest httpServletRequest) {
String refreshToken = cookieResolver.extractRefreshToken(httpServletRequest.getCookies());
String accessToken = authService.reIssueAccessToken(refreshToken);
public ResponseEntity<Void> reissueAccessToken(HttpServletRequest httpServletRequest) {
cookieResolver.checkLoginRequired(httpServletRequest);

String refreshToken = cookieResolver.extractRefreshToken(httpServletRequest);
String accessToken = authService.reissueAccessToken(refreshToken);

ResponseCookie accessTokenCookie = cookieProvider.createAccessTokenCookie(accessToken);
return ResponseEntity.ok()
.header(HttpHeaders.SET_COOKIE, accessTokenCookie.toString())
.build();
}

@GetMapping("/token-exist")
public ResponseEntity<TokenExistResponse> check(HttpServletRequest httpServletRequest) {
boolean isAccessTokenExist = !cookieResolver.isAccessTokenEmpty(httpServletRequest);
boolean isRefreshTokenExist = !cookieResolver.isRefreshTokenEmpty(httpServletRequest);

TokenExistResponse tokenExistResponse = TokenExistResponse.from(isAccessTokenExist, isRefreshTokenExist);
return ResponseEntity.ok(tokenExistResponse);
}

@DeleteMapping("/token")
public ResponseEntity<Void> deleteToken() {
ResponseCookie deletedAccessTokenCookie = cookieProvider.deleteAccessTokenCookie();
ResponseCookie deletedRefreshTokenCookie = cookieProvider.deleteRefreshTokenCookie();

return ResponseEntity.noContent()
.header(HttpHeaders.SET_COOKIE, deletedAccessTokenCookie.toString())
.header(HttpHeaders.SET_COOKIE, deletedRefreshTokenCookie.toString())
.build();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.bang_ggood.auth.controller;
package com.bang_ggood.auth.controller.cookie;

import com.bang_ggood.auth.service.JwtTokenProperties;
import com.bang_ggood.auth.service.jwt.JwtTokenProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseCookie;
import org.springframework.stereotype.Component;
Expand All @@ -9,8 +9,9 @@
@Component
public class CookieProvider {

public static final String ACCESS_TOKEN_COOKIE_NAME = "accessToken";
public static final String REFRESH_TOKEN_COOKIE_NAME = "refreshToken";
protected static final String ACCESS_TOKEN_COOKIE_NAME = "accessToken";
protected static final String REFRESH_TOKEN_COOKIE_NAME = "refreshToken";

private final JwtTokenProperties jwtTokenProperties;
private final String domain;

Expand Down Expand Up @@ -46,11 +47,11 @@ private ResponseCookie createCookie(String tokenName, String token, long expired
.build();
}

public ResponseCookie deleteAccessTokenCookie(String token) {
public ResponseCookie deleteAccessTokenCookie() {
return deleteCookie(ACCESS_TOKEN_COOKIE_NAME);
}

public ResponseCookie deleteRefreshTokenCookie(String token) {
public ResponseCookie deleteRefreshTokenCookie() {
return deleteCookie(REFRESH_TOKEN_COOKIE_NAME);
}

Expand Down
Loading

0 comments on commit 2edbe43

Please sign in to comment.