-
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 #13 from local-mood/chore/12-env
Feat: ํ์ API ์์ฑ
- Loading branch information
Showing
25 changed files
with
1,007 additions
and
18 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.ceos.vote.auth; | ||
|
||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
|
||
import java.lang.annotation.*; | ||
|
||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : user") | ||
public @interface CurrentUser { | ||
} |
109 changes: 109 additions & 0 deletions
109
src/main/java/com/ceos/vote/auth/controller/AuthController.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,109 @@ | ||
package com.ceos.vote.auth.controller; | ||
|
||
import com.ceos.vote.auth.service.AuthService; | ||
import com.ceos.vote.common.dto.NormalResponseDto; | ||
import com.ceos.vote.auth.jwt.entity.TokenDto; | ||
import com.ceos.vote.auth.jwt.entity.LoginRequestDto; | ||
import com.ceos.vote.domain.member.dto.MemberRequestDto; | ||
|
||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.boot.web.server.Cookie; | ||
import org.springframework.http.*; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping("/app/auth") | ||
@RequiredArgsConstructor | ||
public class AuthController { | ||
|
||
private final long COOKIE_EXPIRATION = 7776000; // 90์ผ | ||
|
||
private final AuthService authService; | ||
|
||
// ํ์๊ฐ์ | ||
@PostMapping("/signup") | ||
public ResponseEntity<NormalResponseDto> join(@RequestBody @Valid MemberRequestDto requestDto) { | ||
authService.joinMember(requestDto); | ||
return ResponseEntity.ok(NormalResponseDto.success()); | ||
} | ||
|
||
// ๋ก๊ทธ์ธ | ||
@PostMapping("/login") | ||
public ResponseEntity<?> login(@RequestBody LoginRequestDto loginRequest) { | ||
|
||
TokenDto tokenDto = authService.login(loginRequest); | ||
|
||
HttpCookie httpCookie = ResponseCookie.from("refresh-token", tokenDto.getRefreshToken()) | ||
.maxAge(COOKIE_EXPIRATION) | ||
.httpOnly(true) | ||
.secure(true) | ||
.sameSite(Cookie.SameSite.NONE.attributeValue()) //์๋ํํฐ ์ฟ ํค ์ฌ์ฉ ํ์ฉ | ||
.build(); | ||
return ResponseEntity.ok() | ||
.header(HttpHeaders.SET_COOKIE, httpCookie.toString()) | ||
.header(HttpHeaders.AUTHORIZATION, "Bearer " + tokenDto.getAccessToken()) | ||
.build(); | ||
} | ||
|
||
// ํ ํฐ ์ ํจ์ฑ ๊ฒ์ฌ | ||
@PostMapping("/validate") | ||
public ResponseEntity<?> validate(@RequestHeader("Authorization") String requestAccessToken) { | ||
if (!authService.validate(requestAccessToken)) { | ||
return ResponseEntity.ok().build(); | ||
} else { | ||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); | ||
} | ||
} | ||
|
||
// ํ ํฐ ์ฌ๋ฐ๊ธ | ||
@PostMapping("/reissue") | ||
public ResponseEntity<?> reissue(@CookieValue(name = "refresh-token") String requestRefreshToken, | ||
@RequestHeader("Authorization") String requestAccessToken) { | ||
|
||
TokenDto newAuthToken = authService.reissue(requestAccessToken, requestRefreshToken); | ||
|
||
if (newAuthToken != null) { | ||
// ์๋ก์ด ํ ํฐ ๋ฐ๊ธ, ๋ฐํ | ||
ResponseCookie responseCookie = ResponseCookie.from("refresh-token", newAuthToken.getRefreshToken()) | ||
.maxAge(COOKIE_EXPIRATION) | ||
.httpOnly(true) | ||
.secure(true) | ||
.sameSite(Cookie.SameSite.NONE.attributeValue()) | ||
.build(); | ||
return ResponseEntity.status(HttpStatus.OK) | ||
.header(HttpHeaders.SET_COOKIE, responseCookie.toString()) | ||
.header(HttpHeaders.AUTHORIZATION, "Bearer " + newAuthToken.getAccessToken()) | ||
.build(); | ||
} else { | ||
// Refresh Token์ด ํ์ทจ ๊ฐ๋ฅํ ๋ ์ฟ ํค ์ญ์ ํ๊ณ ์ฌ๋ก๊ทธ์ธ | ||
ResponseCookie responseCookie = ResponseCookie.from("refresh-token", "") | ||
.maxAge(0) | ||
.path("/") | ||
.build(); | ||
return ResponseEntity | ||
.status(HttpStatus.UNAUTHORIZED) | ||
.header(HttpHeaders.SET_COOKIE, responseCookie.toString()) | ||
.build(); | ||
} | ||
} | ||
|
||
// ๋ก๊ทธ์์ | ||
@PostMapping("/logout") | ||
public ResponseEntity<NormalResponseDto> logout(@RequestHeader("Authorization") String requestAccessToken) { | ||
|
||
// Access Token์ ๋ฌดํจํํ์ฌ ๋ก๊ทธ์์ ์ฒ๋ฆฌ | ||
authService.logout(requestAccessToken); | ||
|
||
ResponseCookie responseCookie = ResponseCookie.from("refresh-token", "") | ||
.maxAge(0) | ||
.path("/") | ||
.build(); | ||
|
||
|
||
return ResponseEntity.status(HttpStatus.OK) | ||
.header(HttpHeaders.SET_COOKIE, responseCookie.toString()) | ||
.build(); | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/ceos/vote/auth/exception/JwtAccessDeniedHandler.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,22 @@ | ||
package com.ceos.vote.auth.exception; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.security.web.access.AccessDeniedHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
|
||
@Component | ||
public class JwtAccessDeniedHandler implements AccessDeniedHandler { | ||
|
||
@Override | ||
public void handle(HttpServletRequest request, HttpServletResponse response, | ||
AccessDeniedException accessDeniedException) throws IOException { | ||
|
||
// ํ์ํ ๊ถํ ์์ด ์ ๊ทผ์ 403 error | ||
response.sendError(HttpServletResponse.SC_FORBIDDEN); | ||
|
||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/ceos/vote/auth/exception/JwtAuthenticationEntryPoint.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,20 @@ | ||
package com.ceos.vote.auth.exception; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.AuthenticationEntryPoint; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
|
||
@Component | ||
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint { | ||
|
||
@Override | ||
public void commence(HttpServletRequest request, HttpServletResponse response, | ||
AuthenticationException authenticationException) throws IOException { | ||
// ์ ํจํ์ง ์์ ์๊ฒฉ์ฆ๋ช ์ผ ๋ 401 error | ||
response.sendError(HttpServletResponse.SC_UNAUTHORIZED); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/ceos/vote/auth/jwt/entity/LoginRequestDto.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,16 @@ | ||
package com.ceos.vote.auth.jwt.entity; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
public class LoginRequestDto { | ||
private String email; | ||
|
||
private String password; | ||
} |
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 com.ceos.vote.auth.jwt.entity; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Builder | ||
public class TokenDto { | ||
|
||
private String accessToken; | ||
private String refreshToken; | ||
|
||
public TokenDto(String accessToken, String refreshToken) { | ||
this.accessToken = accessToken; | ||
this.refreshToken = refreshToken; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/com/ceos/vote/auth/jwt/filter/JwtAuthenticationFilter.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,52 @@ | ||
package com.ceos.vote.auth.jwt.filter; | ||
|
||
import com.ceos.vote.auth.jwt.provider.JwtTokenProvider; | ||
import com.ceos.vote.exception.ErrorCode; | ||
import com.ceos.vote.exception.CeosException; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
|
||
|
||
@RequiredArgsConstructor | ||
public class JwtAuthenticationFilter extends OncePerRequestFilter { | ||
|
||
private final JwtTokenProvider jwtTokenProvider; | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, | ||
FilterChain filterChain) throws IOException, ServletException { | ||
// AT ์ถ์ถ | ||
String token = resolveToken(request); | ||
|
||
try { | ||
// ์ ํจ๊ธฐ๊ฐ๋ง ์ ์ธํ๊ณ ์ ์ํ ํฐ์ธ์ง ๊ฒ์ฌ | ||
if (token != null && jwtTokenProvider.validateToken(token)) { | ||
Authentication authentication = jwtTokenProvider.getAuthentication(token); | ||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
} | ||
} catch (UsernameNotFoundException e) { // ํ์ ๋ชป ์ฐพ์ ๋ | ||
throw new CeosException(ErrorCode.MEMBER_NOT_FOUND); | ||
} | ||
|
||
// ๋ค์ ํํฐ๋ก ์ด๋ | ||
filterChain.doFilter(request, response); | ||
} | ||
|
||
// request header์์ ํ ํฐ ์ถ์ถ | ||
public String resolveToken(HttpServletRequest request) { | ||
String bearerToken = request.getHeader("Authorization"); | ||
if (bearerToken != null && bearerToken.startsWith("Bearer ")) { | ||
return bearerToken.substring(7); | ||
} | ||
return null; | ||
} | ||
} |
Oops, something went wrong.