-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0d3faa0
commit 6aae646
Showing
26 changed files
with
804 additions
and
325 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
55 changes: 0 additions & 55 deletions
55
src/main/java/com/example/solutionchallenge/common/config/HttpSecurityConfig.java
This file was deleted.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
src/main/java/com/example/solutionchallenge/common/config/SecurityConfig.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,56 @@ | ||
package com.example.solutionchallenge.common.config; | ||
|
||
import com.example.solutionchallenge.common.config.exception.ExceptionHandlerFilter; | ||
import com.example.solutionchallenge.enums.UserRole; | ||
import com.example.solutionchallenge.filter.JwtFilter; | ||
import com.example.solutionchallenge.service.JwtTokenService; | ||
import com.example.solutionchallenge.service.UserService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer; | ||
import org.springframework.security.config.http.SessionCreationPolicy; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
|
||
import static org.springframework.security.config.Customizer.withDefaults; | ||
|
||
@RequiredArgsConstructor | ||
@Configuration | ||
public class SecurityConfig { | ||
private final JwtTokenService jwtTokenService; | ||
private final UserService userService; | ||
|
||
@Bean | ||
public AuthenticationManager authenticationManager( | ||
final AuthenticationConfiguration authenticationConfiguration) throws Exception { | ||
return authenticationConfiguration.getAuthenticationManager(); | ||
} | ||
|
||
@Bean | ||
public SecurityFilterChain configure(final HttpSecurity http) throws Exception { | ||
return http.cors(withDefaults()) | ||
.csrf((csrf) -> csrf.disable()) | ||
.authorizeHttpRequests((authorize) -> authorize | ||
.requestMatchers("/login/**", "/token/refresh").permitAll() | ||
.requestMatchers("/user/**").hasAuthority(UserRole.USER.getRole()) | ||
.anyRequest().authenticated()) | ||
.sessionManagement((session) -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) | ||
.formLogin(httpSecurityFormLoginConfigurer -> httpSecurityFormLoginConfigurer.disable()) // 로그인 폼 미사용 | ||
.httpBasic(httpSecurityHttpBasicConfigurer -> httpSecurityHttpBasicConfigurer.disable()) // http basic 미사용 | ||
.addFilterBefore(new JwtFilter(jwtTokenService, userService), UsernamePasswordAuthenticationFilter.class) // JWT Filter 추가 | ||
.addFilterBefore(new ExceptionHandlerFilter(), JwtFilter.class) // Security Filter 에서 CustomException 사용하기 위해 추가 | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public WebSecurityCustomizer webSecurityCustomizer(){ | ||
// 아래 url은 filter 에서 제외 | ||
return web -> | ||
web.ignoring() | ||
.requestMatchers("/login/**", "/token/refresh"); | ||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
...in/java/com/example/solutionchallenge/common/config/exception/ExceptionHandlerFilter.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,38 @@ | ||
package com.example.solutionchallenge.common.config.exception; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
|
||
public class ExceptionHandlerFilter extends OncePerRequestFilter { | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, | ||
FilterChain filterChain) throws ServletException, IOException { | ||
|
||
try { | ||
filterChain.doFilter(request, response); | ||
} catch (ApiException ex) { | ||
setErrorResponse(ex.getErrorCode().getHttpStatus(), response, ex); | ||
} catch (Exception ex) { | ||
setErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, response, ex); | ||
} | ||
} | ||
|
||
public void setErrorResponse(HttpStatus status, HttpServletResponse response, Throwable ex) throws IOException { | ||
logger.error("[ExceptionHandlerFilter] errMsg : " + ex.getMessage()); | ||
|
||
response.setStatus(status.value()); | ||
response.setContentType("application/json; charset=UTF-8"); | ||
|
||
response.getWriter().write( | ||
new ErrorResponse(ex.getMessage()) | ||
.convertToJson() | ||
); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...in/java/com/example/solutionchallenge/common/config/exception/GlobalExceptionHandler.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,40 @@ | ||
package com.example.solutionchallenge.common.config.exception; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
|
||
@ControllerAdvice | ||
public class GlobalExceptionHandler { | ||
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class); | ||
@ExceptionHandler(ApiException.class) | ||
public ResponseEntity<ErrorResponse> handleCustomException(ApiException ex) { | ||
logger.error("[CustomException] errCode : " + ex.getErrorCode()); | ||
logger.error("[CustomException] errMsg : " + ex.getMessage()); | ||
return new ResponseEntity( | ||
new ErrorResponse(ex.getMessage()), | ||
ex.getErrorCode().getHttpStatus() | ||
); | ||
} | ||
|
||
@ExceptionHandler(RuntimeException.class) | ||
public ResponseEntity<ErrorResponse> handleRuntimeException(RuntimeException ex) { | ||
logger.error("[RuntimeException] errMsg : " + ex.getMessage()); | ||
return new ResponseEntity( | ||
new ErrorResponse(ex.getMessage()), | ||
HttpStatus.INTERNAL_SERVER_ERROR | ||
); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
public ResponseEntity<ErrorResponse> handleException(RuntimeException ex) { | ||
logger.error("[Exception] errMsg : " + ex.getMessage()); | ||
return new ResponseEntity( | ||
new ErrorResponse(ex.getMessage()), | ||
HttpStatus.INTERNAL_SERVER_ERROR | ||
); | ||
} | ||
} |
37 changes: 0 additions & 37 deletions
37
src/main/java/com/example/solutionchallenge/controller/LoginController.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.