-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
12 changed files
with
276 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,6 @@ out/ | |
|
||
### VS Code ### | ||
.vscode/ | ||
|
||
### security | ||
./src/main/resources/application-oauth.yml |
54 changes: 54 additions & 0 deletions
54
src/main/java/com/gaaji/auth/applicationservice/CustomeOAuth2UserService.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,54 @@ | ||
package com.gaaji.auth.applicationservice; | ||
|
||
import com.gaaji.auth.domain.Auth; | ||
import com.gaaji.auth.repository.AuthRepository; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService; | ||
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest; | ||
import org.springframework.security.oauth2.client.userinfo.OAuth2UserService; | ||
import org.springframework.security.oauth2.core.OAuth2AuthenticationException; | ||
import org.springframework.security.oauth2.core.user.DefaultOAuth2User; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
import org.springframework.stereotype.Service; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class CustomeOAuth2UserService implements OAuth2UserService<OAuth2UserRequest, OAuth2User> { | ||
private final AuthRepository authRepository; | ||
|
||
|
||
@Override | ||
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException { | ||
OAuth2UserService<OAuth2UserRequest, OAuth2User> delegate = new DefaultOAuth2UserService(); | ||
OAuth2User oAuth2User = delegate.loadUser(userRequest); | ||
|
||
String registrationId = userRequest.getClientRegistration().getRegistrationId(); | ||
String userNameAttributeName = userRequest.getClientRegistration().getProviderDetails().getUserInfoEndpoint().getUserNameAttributeName(); | ||
|
||
|
||
OAuthAttributes attributes = OAuthAttributes.of(registrationId, userNameAttributeName, oAuth2User.getAttributes()); | ||
|
||
|
||
|
||
Auth user = saveOrUpdate(attributes); | ||
|
||
Map<String, Object> attribute = new HashMap<>(); | ||
attribute.put("id",user.getAuthIdForToken()); | ||
|
||
|
||
return new DefaultOAuth2User(Collections.emptyList(), | ||
attribute, | ||
attributes.getNameAttributeKey()); | ||
} | ||
|
||
private Auth saveOrUpdate(OAuthAttributes attributes) { | ||
Auth user = authRepository.findByPlatformInfo(attributes.getPlatformType(), attributes.getEmail()) // <- 조회 없으면 save 후 반환, 있으면 반환, | ||
.orElse(authRepository.save(attributes.getPlatformType(), attributes.getEmail())); | ||
|
||
return user; | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/com/gaaji/auth/applicationservice/OAuthAttributes.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,67 @@ | ||
package com.gaaji.auth.applicationservice; | ||
|
||
import com.gaaji.auth.domain.PlatformType; | ||
import java.util.Map; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import org.springframework.security.core.userdetails.User; | ||
|
||
@Getter | ||
public class OAuthAttributes { | ||
private Map<String, Object> attributes; | ||
private String nameAttributeKey; | ||
|
||
private String email; | ||
|
||
private PlatformType platformType; | ||
|
||
@Builder | ||
public OAuthAttributes(Map<String, Object> attributes, String nameAttributeKey, String email, PlatformType platformType) { | ||
this.attributes = attributes; | ||
this.nameAttributeKey = nameAttributeKey; | ||
this.email = email; | ||
this.platformType = platformType; | ||
} | ||
|
||
public static OAuthAttributes of(String registrationId, String userNameAttributeName, Map<String, Object> attributes) { | ||
if (registrationId.equals("kakao")) { | ||
return ofKakao(userNameAttributeName, attributes); | ||
} else if (registrationId.equals("naver")) { | ||
return ofNaver(userNameAttributeName,attributes); | ||
} | ||
// TODO Facebook 찾아보기 | ||
return ofGoogle(userNameAttributeName, attributes); | ||
} | ||
private static OAuthAttributes ofKakao(String userNameAttributeName, Map<String, Object> attributes) { | ||
Map<String, Object> kakao_account = (Map<String, Object>) attributes.get("kakao_account"); // 카카오로 받은 데이터에서 계정 정보가 담긴 kakao_account 값을 꺼낸다. | ||
|
||
return OAuthAttributes.builder() | ||
.email((String) kakao_account.get("email")) | ||
.platformType(PlatformType.KAKAO) | ||
.attributes(attributes) | ||
.nameAttributeKey(userNameAttributeName) | ||
.build(); | ||
} | ||
private static OAuthAttributes ofNaver(String userNameAttributeName, Map<String, Object> attributes) { | ||
Map<String, Object> response = (Map<String, Object>) attributes.get("response"); // 네이버가지로 profile(nickname, image_url.. 등) 정보가 담긴 값을 꺼낸다. | ||
|
||
return OAuthAttributes.builder() | ||
.email((String) response.get("email")) | ||
.platformType(PlatformType.NAVER) | ||
.attributes(attributes) | ||
.nameAttributeKey(userNameAttributeName) | ||
.build(); | ||
} | ||
private static OAuthAttributes ofGoogle(String userNameAttributeName, Map<String, Object> attributes) { | ||
return OAuthAttributes.builder() | ||
.email((String) attributes.get("email")) | ||
.platformType(PlatformType.GOOGLE) | ||
.attributes(attributes) | ||
.nameAttributeKey(userNameAttributeName) | ||
.build(); | ||
} | ||
|
||
public User toEntity() { | ||
return null; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,50 @@ | ||
package com.gaaji.auth.config; | ||
|
||
import com.gaaji.auth.applicationservice.CustomeOAuth2UserService; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
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.builders.AuthenticationManagerBuilder; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
import org.springframework.security.web.authentication.AuthenticationFailureHandler; | ||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
import org.springframework.web.cors.CorsConfiguration; | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
@RequiredArgsConstructor | ||
public class SecurityConfig { | ||
|
||
private final CustomeOAuth2UserService customOAuth2UserService; | ||
|
||
|
||
@Bean | ||
public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception { | ||
|
||
return httpSecurity | ||
.csrf().disable() | ||
.headers().frameOptions().disable() | ||
.and() | ||
.authorizeRequests() | ||
.antMatchers("/**/**").permitAll() | ||
.antMatchers("/**").authenticated() | ||
.anyRequest().authenticated() | ||
.and() | ||
|
||
.oauth2Login() | ||
.userInfoEndpoint() | ||
.userService(customOAuth2UserService) | ||
.and() | ||
.and() | ||
.build(); | ||
|
||
|
||
} | ||
} | ||
|
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
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
16 changes: 16 additions & 0 deletions
16
src/main/java/com/gaaji/auth/repository/AuthRepository.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,16 @@ | ||
package com.gaaji.auth.repository; | ||
|
||
import com.gaaji.auth.domain.Auth; | ||
import com.gaaji.auth.domain.PlatformType; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
public interface AuthRepository { | ||
Auth save(PlatformType type, String email); | ||
|
||
Optional<Auth> findByPlatformInfo(PlatformType type, String email); | ||
|
||
default String nextId(){ | ||
return UUID.randomUUID().toString(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/gaaji/auth/repository/AuthRepositoryImpl.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,26 @@ | ||
package com.gaaji.auth.repository; | ||
|
||
import com.gaaji.auth.domain.Auth; | ||
import com.gaaji.auth.domain.PlatformType; | ||
import java.util.Optional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@RequiredArgsConstructor | ||
@Repository | ||
public class AuthRepositoryImpl implements AuthRepository{ | ||
|
||
private final JpaAuthRepository jpaAuthRepository; | ||
|
||
@Override | ||
public Auth save(PlatformType type, String email) { | ||
return jpaAuthRepository.save(Auth.signUp(this.nextId(), type, email)); | ||
} | ||
|
||
@Override | ||
public Optional<Auth> findByPlatformInfo(PlatformType type, String email) { | ||
return jpaAuthRepository.findAuthByPlatformInfo_PlatformEmailAndPlatformInfo_PlatformType(type, email); | ||
} | ||
|
||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/gaaji/auth/repository/JpaAuthRepository.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,14 @@ | ||
package com.gaaji.auth.repository; | ||
|
||
import com.gaaji.auth.domain.Auth; | ||
import com.gaaji.auth.domain.AuthId; | ||
import com.gaaji.auth.domain.PlatformType; | ||
import java.util.Optional; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface JpaAuthRepository extends JpaRepository<Auth, AuthId> { | ||
|
||
Optional<Auth> findAuthByPlatformInfo_PlatformEmailAndPlatformInfo_PlatformType(PlatformType type, String platformEmail); | ||
|
||
Optional<Auth> findAuthByPlatformInfo(PlatformType type, String platformEmail); | ||
} |