-
Notifications
You must be signed in to change notification settings - Fork 13
[Spring Core] 김성현 과제 제출입니다. #5
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -35,3 +35,6 @@ out/ | |
|
|
||
| ### VS Code ### | ||
| .vscode/ | ||
|
|
||
| ### 보안을 위한 제거 ### | ||
| application.yml | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package com.example.demo.config; | ||
|
|
||
| import com.example.demo.security.JwtAuthenticationFilter; | ||
| import com.example.demo.security.JwtTokenProvider; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.http.HttpMethod; | ||
| import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
| import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; | ||
| import org.springframework.security.config.annotation.web.configurers.FormLoginConfigurer; | ||
| import org.springframework.security.config.annotation.web.configurers.HttpBasicConfigurer; | ||
| import org.springframework.security.config.http.SessionCreationPolicy; | ||
| import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
| import org.springframework.security.crypto.password.PasswordEncoder; | ||
| import org.springframework.security.web.SecurityFilterChain; | ||
| import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
|
|
||
| @Configuration | ||
| public class SecurityConfig { | ||
|
|
||
| private final JwtTokenProvider jwtTokenProvider; | ||
|
|
||
| public SecurityConfig(JwtTokenProvider jwtTokenProvider) { | ||
| this.jwtTokenProvider = jwtTokenProvider; | ||
| } | ||
|
|
||
| @Bean | ||
| public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { | ||
| return http | ||
| .formLogin(FormLoginConfigurer::disable) | ||
| .httpBasic(HttpBasicConfigurer::disable) | ||
| .csrf(AbstractHttpConfigurer::disable) | ||
| .sessionManagement((session) -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) | ||
| .authorizeHttpRequests((authorizeHttpRequests) -> | ||
| authorizeHttpRequests | ||
| .requestMatchers(HttpMethod.POST,"/members").permitAll() | ||
| .requestMatchers("/members/login").permitAll() | ||
| .anyRequest().authenticated() | ||
| ) | ||
| .addFilterBefore(new JwtAuthenticationFilter(jwtTokenProvider), UsernamePasswordAuthenticationFilter.class) | ||
| .build(); | ||
| } | ||
|
|
||
| @Bean | ||
| public PasswordEncoder passwordEncoder() { | ||
| return new BCryptPasswordEncoder(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.example.demo.controller.dto.request; | ||
|
|
||
| public record MemberLoginRequest( | ||
| String email, | ||
| String password | ||
| ) { | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.example.demo.controller.dto.response; | ||
|
|
||
| public record LoginResponse( | ||
| String message, | ||
| String token | ||
| ) { | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.example.demo.exception; | ||
|
|
||
| public interface BaseExceptionType { | ||
|
|
||
| int getErrorCode(); | ||
| int getHttpStatus(); | ||
| String getErrorMessage(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package com.example.demo.exception; | ||
|
|
||
| public enum BoardExceptionType implements BaseExceptionType { | ||
|
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. BaseExceptionType, BoardExceptionType, CommonExceptionType을 enum으로 나눠서 선언한 이유가 궁금합니다!
Author
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. 프로젝트를 개발하는 과정에서 지속적으로 확장이 일어날 것이고, 이를 고려했을 때 도메인 별로 에러코드를 다르게하고 다른 enum에서 관리함으로써 유지보수성을 높일 수 있다고 생각했습니다. |
||
|
|
||
| NOT_FOUND_BOARD(1200, 404, "존재하지 않는 게시판입니다"), | ||
| BOARD_HAS_POSTS(1201, 400, "게시판에 게시물이 존재합니다"); | ||
|
|
||
| private int errorCode; | ||
| private int httpStatus; | ||
| private String errorMessage; | ||
| BoardExceptionType(int errorCode, int httpStatus, String errorMessage) { | ||
| this.errorCode = errorCode; | ||
| this.httpStatus = httpStatus; | ||
| this.errorMessage = errorMessage; | ||
| } | ||
|
|
||
| @Override | ||
| public int getErrorCode() { | ||
| return errorCode; | ||
| } | ||
|
|
||
| @Override | ||
| public int getHttpStatus() { | ||
| return httpStatus; | ||
| } | ||
|
|
||
| @Override | ||
| public String getErrorMessage() { | ||
| return errorMessage; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package com.example.demo.exception; | ||
|
|
||
| public enum CommonExceptionType implements BaseExceptionType{ | ||
|
|
||
| BAD_REQUEST_NULL_VALUE(1000, 400, "요청 중 null 값이 존재합니다"); | ||
|
|
||
| private int errorCode; | ||
| private int httpStatus; | ||
| private String errorMessage; | ||
|
|
||
| CommonExceptionType(int errorCode, int httpStatus, String errorMessage) { | ||
| this.errorCode = errorCode; | ||
| this.httpStatus = httpStatus; | ||
| this.errorMessage = errorMessage; | ||
| } | ||
|
|
||
| @Override | ||
| public int getErrorCode() { | ||
| return errorCode; | ||
| } | ||
|
|
||
| @Override | ||
| public int getHttpStatus() { | ||
| return httpStatus; | ||
| } | ||
|
|
||
| @Override | ||
| public String getErrorMessage() { | ||
| return errorMessage; | ||
| } | ||
| } |
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.
DB에 위임 굳~
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.
감사합니다!