-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor/47 회원 API리팩토링 #50
Changes from all commits
002cca9
5a0cad4
9487cb7
41f7ba5
98197b1
dd3b30d
60ab451
8c40463
76237ea
03b7628
0f194a9
547e251
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.gamegoo.apiPayload.exception.handler; | ||
|
||
import org.springframework.security.core.AuthenticationException; | ||
|
||
public class CustomUserException extends AuthenticationException { | ||
public CustomUserException(String msg) { | ||
super(msg); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.gamegoo.apiPayload.exception.handler; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.gamegoo.apiPayload.ApiResponse; | ||
import com.gamegoo.apiPayload.code.status.ErrorStatus; | ||
import io.jsonwebtoken.JwtException; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
public class JWTExceptionHandlerFilter extends OncePerRequestFilter { | ||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { | ||
try { | ||
filterChain.doFilter(request, response); | ||
} catch (JwtException e) { | ||
|
||
if (Objects.equals(e.getMessage(), "Token expired")) { | ||
setErrorResponse(response, ErrorStatus.TOKEN_EXPIRED); | ||
} else if (Objects.equals(e.getMessage(), "Token null")) { | ||
setErrorResponse(response, ErrorStatus.TOKEN_NULL); | ||
} else if (Objects.equals(e.getMessage(), "No Member")) { | ||
setErrorResponse(response, ErrorStatus.MEMBER_NOT_FOUND); | ||
} else { | ||
setErrorResponse(response, ErrorStatus.INVALID_TOKEN); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private void setErrorResponse(HttpServletResponse response, ErrorStatus errorStatus) throws IOException { | ||
// 에러 응답 생성하기 | ||
ApiResponse<Object> apiResponse = ApiResponse.onFailure(errorStatus.getCode(), errorStatus.getMessage(), null); | ||
response.setStatus(errorStatus.getHttpStatus().value()); | ||
response.setContentType("application/json"); | ||
response.setCharacterEncoding("UTF-8"); | ||
new ObjectMapper().writeValue(response.getWriter(), apiResponse); | ||
} | ||
} | ||
|
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
package com.gamegoo.config; | ||
|
||
import com.gamegoo.jwt.JWTFilter; | ||
import com.gamegoo.jwt.JWTUtil; | ||
import com.gamegoo.security.LoginFilter; | ||
import com.gamegoo.apiPayload.exception.handler.JWTExceptionHandlerFilter; | ||
import com.gamegoo.filter.JWTFilter; | ||
import com.gamegoo.filter.LoginFilter; | ||
import com.gamegoo.service.member.CustomUserDetailService; | ||
import com.gamegoo.util.JWTUtil; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
|
@@ -18,17 +21,14 @@ | |
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
|
||
@Configuration | ||
@EnableWebSecurity | ||
@RequiredArgsConstructor | ||
public class SecurityConfig { | ||
private final AuthenticationConfiguration authenticationConfiguration; | ||
private final JWTUtil jwtUtil; | ||
private final CustomUserDetailService customUserDetailService; | ||
|
||
public SecurityConfig(AuthenticationConfiguration authenticationConfiguration, JWTUtil jwtUtil) { | ||
this.authenticationConfiguration = authenticationConfiguration; | ||
this.jwtUtil = jwtUtil; | ||
} | ||
|
||
@Bean | ||
public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration) throws Exception { | ||
|
@@ -37,8 +37,9 @@ public AuthenticationManager authenticationManager(AuthenticationConfiguration c | |
|
||
@Bean | ||
public JWTFilter jwtFilter() { | ||
List<String> excludedPaths = Arrays.asList("/swagger-ui/", "/v3/api-docs", "/api/member/join/local", "/api/member/login/local", "/api/member/email"); | ||
return new JWTFilter(jwtUtil, excludedPaths); | ||
List<String> excludedPaths = Arrays.asList("/api/member/join", "/api/member/login", "/api/member/email"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "/swagger-ui/", "/v3/api-docs" 이 uri도 다시 추가 부탁드립니다..! dev랑 충돌 해결하면서 지워진 것 같아요 |
||
return new JWTFilter(jwtUtil, excludedPaths, customUserDetailService); | ||
|
||
} | ||
|
||
@Bean | ||
|
@@ -49,13 +50,15 @@ public BCryptPasswordEncoder bCryptPasswordEncoder() { | |
@Bean | ||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { | ||
http | ||
|
||
.csrf(AbstractHttpConfigurer::disable) | ||
.formLogin(AbstractHttpConfigurer::disable) | ||
.httpBasic(AbstractHttpConfigurer::disable) | ||
|
||
.authorizeHttpRequests((auth) -> auth | ||
.antMatchers("/api/member/join/local", "/api/member/login/local", "/api/member/email").permitAll() | ||
.antMatchers("/", "/swagger-ui/**", "/v3/api-docs/**").permitAll() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분도 다시 추가 부탁드려요! |
||
.antMatchers("/api/member/join", "/api/member/login", "/api/member/email/**").permitAll() | ||
.anyRequest().authenticated()) | ||
.addFilterBefore(new JWTExceptionHandlerFilter(), UsernamePasswordAuthenticationFilter.class) | ||
.addFilterAt(new LoginFilter(authenticationManager(authenticationConfiguration), jwtUtil), UsernamePasswordAuthenticationFilter.class) | ||
.addFilterBefore(jwtFilter(), LoginFilter.class) | ||
.sessionManagement((session) -> session | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.gamegoo.controller.member; | ||
|
||
import com.gamegoo.apiPayload.ApiResponse; | ||
import com.gamegoo.dto.member.EmailCodeDTO; | ||
import com.gamegoo.dto.member.EmailDTO; | ||
import com.gamegoo.dto.member.JoinDTO; | ||
import com.gamegoo.service.member.AuthService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/member") | ||
@Slf4j | ||
public class AuthController { | ||
private final AuthService authService; | ||
|
||
@PostMapping("/join") | ||
@Operation(summary = "회원가입 API 입니다.", description = "API for join") | ||
public ApiResponse<Object> joinMember(@RequestBody JoinDTO joinDTO) { | ||
authService.joinMember(joinDTO); | ||
return ApiResponse.onSuccess("회원가입에 성공했습니다."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기서 가입된 회원에 대한 정보(id, email)등을 응답으로 주는 것도 좋을 것 같아요..! |
||
} | ||
|
||
@PostMapping("/email/send") | ||
@Operation(summary = "이메일 인증코드 전송 API 입니다.", description = "API for sending email") | ||
public ApiResponse<Object> sendEmail(@RequestBody EmailDTO emailDTO) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 응답을 단순 string으로 줄 때는 여기도 ApiResponse으로 지정해주는 게 더 좋을 것 같아요! |
||
String email = emailDTO.getEmail(); | ||
authService.sendEmail(email); | ||
return ApiResponse.onSuccess("인증 이메일을 발송했습니다."); | ||
} | ||
|
||
@PostMapping("/email/verify") | ||
@Operation(summary = "이메일 인증코드 검증 API 입니다.", description = "API for email verification") | ||
public ApiResponse<Object> verifyEmail(@RequestBody EmailCodeDTO emailCodeDTO) { | ||
String email = emailCodeDTO.getEmail(); | ||
String code = emailCodeDTO.getCode(); | ||
authService.verifyEmail(email, code); | ||
return ApiResponse.onSuccess("인증코드 검증에 성공했습니다."); | ||
} | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 filter는 apiPayload.exception.handler 패키지보다 그냥 com.gamegoo.filter 패키지에 있는게 찾기 편할 것 같아요!