-
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 #8 from URECA-PODONG/User
feat/#7: 소셜로그인(kakao업로드)
- Loading branch information
Showing
19 changed files
with
622 additions
and
353 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
46 changes: 46 additions & 0 deletions
46
src/main/java/com/ureca/sole_paradise/user/config/CustomSuccessHandler.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,46 @@ | ||
package com.ureca.sole_paradise.user.config; | ||
|
||
import com.ureca.sole_paradise.user.db.dto.CustomOAuth2User; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.Cookie; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class CustomSuccessHandler extends SimpleUrlAuthenticationSuccessHandler { | ||
|
||
@Override | ||
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { | ||
|
||
//OAuth2User | ||
CustomOAuth2User customUserDetails = (CustomOAuth2User) authentication.getPrincipal(); | ||
|
||
//ROLE 추출 | ||
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities(); | ||
Iterator<? extends GrantedAuthority> iterator = authorities.iterator(); | ||
GrantedAuthority auth = iterator.next(); | ||
String role = auth.getAuthority(); | ||
|
||
// 04.14 - 비회원 상태일경우 가입 페이지로, 커스텀 필요 | ||
if (role.equals("ROLE_VALIDATE")) { | ||
response.setStatus(205); | ||
//회원가입 페이지 | ||
response.sendRedirect("http://localhost:5173/userRegister/:userId"); | ||
return; | ||
} | ||
|
||
response.sendRedirect("http://localhost:5173/userRegister/:userId"); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/ureca/sole_paradise/user/config/ReferencedException.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,17 @@ | ||
package com.ureca.sole_paradise.user.config; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
|
||
@ResponseStatus(HttpStatus.CONFLICT) | ||
public class ReferencedException extends RuntimeException { | ||
|
||
public ReferencedException() { | ||
super(); | ||
} | ||
|
||
public ReferencedException(final ReferencedWarning referencedWarning) { | ||
super(referencedWarning.toMessage()); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/ureca/sole_paradise/user/config/ReferencedWarning.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,31 @@ | ||
package com.ureca.sole_paradise.user.config; | ||
|
||
|
||
import java.util.ArrayList; | ||
import java.util.stream.Collectors; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
|
||
@Getter | ||
@Setter | ||
public class ReferencedWarning { | ||
|
||
private String key = null; | ||
private ArrayList<Object> params = new ArrayList<>(); | ||
|
||
public void addParam(final Object param) { | ||
params.add(param); | ||
} | ||
|
||
public String toMessage() { | ||
String message = key; | ||
if (!params.isEmpty()) { | ||
message += "," + params.stream() | ||
.map(Object::toString) | ||
.collect(Collectors.joining(",")); | ||
} | ||
return message; | ||
} | ||
|
||
} |
95 changes: 95 additions & 0 deletions
95
src/main/java/com/ureca/sole_paradise/user/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,95 @@ | ||
package com.ureca.sole_paradise.user.config; | ||
|
||
import com.ureca.sole_paradise.user.service.CustomOAuth2UserService; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
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.EnableWebSecurity; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint; | ||
import org.springframework.web.cors.CorsConfiguration; | ||
import org.springframework.web.cors.CorsConfigurationSource; | ||
|
||
import java.util.List; | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
@RequiredArgsConstructor | ||
public class SecurityConfig { | ||
|
||
//OAuth 로그인 | ||
private final CustomOAuth2UserService customOAuth2UserService; | ||
private final CustomSuccessHandler customSuccessHandler; | ||
|
||
//AuthenticationManager Bean 등록 | ||
@Bean | ||
public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration) throws Exception { | ||
|
||
return configuration.getAuthenticationManager(); | ||
} | ||
|
||
@Bean | ||
public BCryptPasswordEncoder bCryptPasswordEncoder() { | ||
|
||
return new BCryptPasswordEncoder(); | ||
} | ||
|
||
@Bean | ||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { | ||
|
||
http | ||
.cors((corsCustomizer -> corsCustomizer.configurationSource(new CorsConfigurationSource() { | ||
|
||
@Override | ||
public CorsConfiguration getCorsConfiguration(HttpServletRequest request) { | ||
|
||
CorsConfiguration config = new CorsConfiguration(); | ||
|
||
config.setAllowedOrigins(List.of("http://localhost:5173")); | ||
config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS")); | ||
config.setAllowCredentials(true); | ||
config.setAllowedHeaders(List.of("*")); | ||
config.setMaxAge(3600L); | ||
|
||
return config; | ||
} | ||
}))); | ||
|
||
//csrf disable | ||
http | ||
.csrf((auth) -> auth.disable()); | ||
|
||
//From 로그인 방식 disable | ||
http | ||
.formLogin((auth) -> auth.disable()); | ||
|
||
//http basic 인증 방식 disable | ||
http | ||
.httpBasic((auth) -> auth.disable()); | ||
|
||
// 04.14 작성 - 잠시 주석처리 | ||
// Oauth 소셜로그인 | ||
http | ||
.oauth2Login((oauth2) -> oauth2 | ||
.userInfoEndpoint((userInfoEndpointConfig) -> userInfoEndpointConfig | ||
.userService(customOAuth2UserService)) | ||
.successHandler(customSuccessHandler)); | ||
|
||
//경로별 인가 작업 | ||
http | ||
.authorizeHttpRequests((auth) -> auth | ||
.requestMatchers("/*", "/**").permitAll() | ||
// .requestMatchers("/api/**", "/api/*").permitAll() //개발 용 로그인 안했을때 postman 사용을 위해 | ||
.anyRequest().authenticated()) | ||
.exceptionHandling((exceptionConfig) -> | ||
exceptionConfig.authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/user/error"))); | ||
|
||
|
||
return http.build(); | ||
} | ||
} |
78 changes: 0 additions & 78 deletions
78
src/main/java/com/ureca/sole_paradise/user/controller/KakaoLoginController.java
This file was deleted.
Oops, something went wrong.
32 changes: 0 additions & 32 deletions
32
src/main/java/com/ureca/sole_paradise/user/controller/KakaoLoginPageController.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.