-
Notifications
You must be signed in to change notification settings - Fork 0
Error #13
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
Merged
Merged
Error #13
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
src/main/java/com/example/redunm/config/PasswordConfig.java
This file contains hidden or 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,15 @@ | ||
| package com.example.redunm.config; | ||
|
|
||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
| import org.springframework.security.crypto.password.PasswordEncoder; | ||
|
|
||
| @Configuration | ||
| public class PasswordConfig { | ||
|
|
||
| @Bean | ||
| public PasswordEncoder passwordEncoder() { | ||
| return new BCryptPasswordEncoder(); | ||
| } | ||
| } |
63 changes: 37 additions & 26 deletions
63
src/main/java/com/example/redunm/config/SecurityConfig.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,58 +1,69 @@ | ||
| package com.example.redunm.config; | ||
|
|
||
| import com.example.redunm.filter.JsonAuthenticationFilter; | ||
| import com.example.redunm.service.UserService; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.security.authentication.AuthenticationManager; | ||
| import org.springframework.security.config.Customizer; | ||
| import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; | ||
| import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
| import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
| import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; | ||
| 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.config.annotation.authentication.configuration.AuthenticationConfiguration; | ||
| import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
|
|
||
| @Configuration | ||
| @EnableWebSecurity | ||
| @EnableMethodSecurity | ||
| public class SecurityConfig { | ||
|
|
||
| private final UserService userService; | ||
|
|
||
| @Autowired | ||
| public SecurityConfig(UserService userService) { | ||
| this.userService = userService; | ||
| } | ||
|
|
||
| @Bean | ||
| public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { | ||
| http | ||
| .csrf(AbstractHttpConfigurer::disable) // CSRF 비활성화 | ||
| .cors(Customizer.withDefaults()) // 기본 CORS 설정 | ||
| public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception { | ||
| return authenticationConfiguration.getAuthenticationManager(); | ||
| } | ||
|
|
||
| @Bean | ||
| public SecurityFilterChain securityFilterChain(HttpSecurity http, AuthenticationManager authManager) throws Exception { | ||
| http | ||
| .csrf(csrf -> csrf.disable()) // CSRF 비활성화 | ||
| .cors(Customizer.withDefaults()) | ||
| .sessionManagement(session -> session | ||
| .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED) | ||
| ) | ||
| .authorizeHttpRequests(auth -> auth | ||
| .requestMatchers( | ||
| "/api/auth/signup/**", // 회원가입 관련 요청 허용 | ||
| "/api/auth/login/**", // 로그인 관련 요청 허용 | ||
| "/api/cart/**", // 장바구니 API 요청 허용 | ||
| "/api/data-models/**", // 데이터 모델 API 요청 허용 | ||
| "/css/**", // 정적 리소스 허용 | ||
| "/api/auth/signup/**", | ||
| "/api/auth/login/**", | ||
| "/api/cart/**", | ||
| "/api/data-models/**", | ||
| "/css/**", | ||
| "/js/**", | ||
| "/models/**" | ||
| ).permitAll() | ||
| .anyRequest().authenticated() // 그 외의 요청은 인증 필요 | ||
| .anyRequest().authenticated() | ||
| ) | ||
|
|
||
| .formLogin(form -> form | ||
| .loginPage("/login") // 커스텀 로그인 페이지 | ||
| .loginProcessingUrl("/api/auth/login") // 로그인 요청 URL | ||
| .defaultSuccessUrl("/home", true) // 로그인 성공 후 이동 URL | ||
| .logout(logout -> logout | ||
| .logoutUrl("/api/auth/logout") | ||
| .logoutSuccessUrl("/") | ||
| .permitAll() | ||
| ) | ||
| .formLogin(form -> form.disable()); | ||
|
|
||
| .logout(logout -> logout | ||
| .logoutUrl("/api/auth/logout") // 로그아웃 URL | ||
| .logoutSuccessUrl("/") // 로그아웃 성공 후 이동 URL | ||
| .permitAll() | ||
| ); | ||
| JsonAuthenticationFilter jsonAuthenticationFilter = new JsonAuthenticationFilter("/api/auth/login", authManager, userService); | ||
| http.addFilterBefore(jsonAuthenticationFilter, UsernamePasswordAuthenticationFilter.class); | ||
|
|
||
| return http.build(); | ||
| } | ||
|
|
||
| @Bean | ||
| public PasswordEncoder passwordEncoder() { | ||
| return new BCryptPasswordEncoder(); | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,24 +1,17 @@ | ||
| package com.example.redunm.config; | ||
|
|
||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.web.servlet.config.annotation.CorsRegistry; | ||
| import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
| import org.springframework.web.servlet.config.annotation.*; | ||
|
|
||
| @Configuration | ||
| public class WebConfig { | ||
| public class WebConfig implements WebMvcConfigurer { | ||
|
|
||
| @Bean | ||
| public WebMvcConfigurer corsConfigurer() { | ||
| return new WebMvcConfigurer() { | ||
| @Override | ||
| public void addCorsMappings(CorsRegistry registry) { | ||
| registry.addMapping("/**") | ||
| .allowedOrigins("http://192.168.0.20:3000", "http://localhost:3000") | ||
| .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") | ||
| .allowedHeaders("*") | ||
| .allowCredentials(true); | ||
| } | ||
| }; | ||
| @Override | ||
| public void addCorsMappings(CorsRegistry registry) { | ||
| registry.addMapping("/**") | ||
| .allowedOrigins("http://localhost:3000") | ||
| .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") | ||
| .allowedHeaders("*") | ||
| .allowCredentials(true); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
63 changes: 63 additions & 0 deletions
63
src/main/java/com/example/redunm/filter/JsonAuthenticationFilter.java
This file contains hidden or 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,63 @@ | ||
| package com.example.redunm.filter; | ||
|
|
||
| import com.example.redunm.login.LoginRequest; | ||
| import com.example.redunm.service.UserService; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import jakarta.servlet.FilterChain; | ||
| import jakarta.servlet.ServletException; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
| import org.springframework.security.core.Authentication; | ||
| import org.springframework.security.core.AuthenticationException; | ||
| import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter; | ||
| import org.springframework.security.authentication.AuthenticationManager; | ||
| import org.springframework.security.core.context.SecurityContextHolder; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class JsonAuthenticationFilter extends AbstractAuthenticationProcessingFilter { | ||
|
|
||
| private final ObjectMapper objectMapper = new ObjectMapper(); | ||
| private final UserService userService; | ||
|
|
||
| public JsonAuthenticationFilter(String defaultFilterProcessesUrl, AuthenticationManager authenticationManager, UserService userService) { | ||
| super(defaultFilterProcessesUrl); | ||
| setAuthenticationManager(authenticationManager); | ||
| this.userService = userService; | ||
| } | ||
|
|
||
| @Override | ||
| public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) | ||
| throws AuthenticationException, IOException, ServletException { | ||
|
|
||
| if (!request.getMethod().equals("POST")) { | ||
| throw new ServletException("Authentication method not supported: " + request.getMethod()); | ||
| } | ||
|
|
||
| LoginRequest loginRequest = objectMapper.readValue(request.getInputStream(), LoginRequest.class); | ||
|
|
||
| UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken( | ||
| loginRequest.getEmail(), | ||
| loginRequest.getPassword() | ||
| ); | ||
|
|
||
| return getAuthenticationManager().authenticate(authRequest); | ||
| } | ||
|
|
||
| @Override | ||
| protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, | ||
| Authentication authResult) throws IOException, ServletException { | ||
| SecurityContextHolder.getContext().setAuthentication(authResult); | ||
| response.setContentType("application/json"); | ||
| response.getWriter().write("{\"message\": \"로그인 성공\"}"); | ||
| } | ||
|
|
||
| @Override | ||
| protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, | ||
| AuthenticationException failed) throws IOException, ServletException { | ||
| response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); | ||
| response.setContentType("application/json"); | ||
| response.getWriter().write("{\"error\": \"이메일 또는 비밀번호가 잘못되었습니다.\"}"); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🛠️ Refactor suggestion
Return an empty collection instead of
null.Returning
nullmight lead toNullPointerExceptionin certain Spring Security setups.📝 Committable suggestion