docs: 설명 추가

This commit is contained in:
kimjunseo
2021-07-27 13:54:35 +09:00
parent f1eed120de
commit f5398a277f
4 changed files with 26 additions and 27 deletions

View File

@@ -17,8 +17,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override @Override
protected void configure(HttpSecurity http) throws Exception { protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable() http.csrf().disable() // h2 console 접속을 위해
.headers().frameOptions().disable() .headers().frameOptions().disable() // h2 console 접속을 위해
.and() .and()
.oauth2Login() // OAuth2 로그인 설정 시작점 .oauth2Login() // OAuth2 로그인 설정 시작점
.userInfoEndpoint() // OAuth2 로그인 성공 이후 사용자 정보를 가져올 때 설정 담당 .userInfoEndpoint() // OAuth2 로그인 성공 이후 사용자 정보를 가져올 때 설정 담당

View File

@@ -31,14 +31,14 @@ public class OAuthService implements OAuth2UserService<OAuth2UserRequest, OAuth2
OAuth2User oAuth2User = delegate.loadUser(userRequest); // OAuth 서비스(github, google, naver)에서 가져온 유저 정보를 담고있음 OAuth2User oAuth2User = delegate.loadUser(userRequest); // OAuth 서비스(github, google, naver)에서 가져온 유저 정보를 담고있음
String registrationId = userRequest.getClientRegistration() String registrationId = userRequest.getClientRegistration()
.getRegistrationId(); // OAuth 서비스 이름(github, naver, google) .getRegistrationId(); // OAuth 서비스 이름(ex. github, naver, google)
String userNameAttributeName = userRequest.getClientRegistration().getProviderDetails() String userNameAttributeName = userRequest.getClientRegistration().getProviderDetails()
.getUserInfoEndpoint().getUserNameAttributeName(); .getUserInfoEndpoint().getUserNameAttributeName(); // OAuth 로그인 시 키(pk)가 되는 값
Map<String, Object> attributes = oAuth2User.getAttributes(); Map<String, Object> attributes = oAuth2User.getAttributes(); // OAuth 서비스의 유저 정보들
UserProfile userProfile = OAuthAttributes.extract(registrationId, attributes); UserProfile userProfile = OAuthAttributes.extract(registrationId, attributes); // registrationId에 따라 유저 정보를 통해 공통된 UserProfile 객체로 만들어 줌
Member member = saveOrUpdate(userProfile); Member member = saveOrUpdate(userProfile); // DB에 저장
return new DefaultOAuth2User( return new DefaultOAuth2User(
Collections.singleton(new SimpleGrantedAuthority(member.getRoleKey())), Collections.singleton(new SimpleGrantedAuthority(member.getRoleKey())),
@@ -48,7 +48,7 @@ public class OAuthService implements OAuth2UserService<OAuth2UserRequest, OAuth2
private Member saveOrUpdate(UserProfile userProfile) { private Member saveOrUpdate(UserProfile userProfile) {
Member member = memberRepository.findByOauthId(userProfile.getOauthId()) Member member = memberRepository.findByOauthId(userProfile.getOauthId())
.map(m -> m.update(userProfile.getName(), userProfile.getEmail(), userProfile.getImageUrl())) .map(m -> m.update(userProfile.getName(), userProfile.getEmail(), userProfile.getImageUrl())) // OAuth 서비스 사이트에서 유저 정보 변경이 있을 수 있기 때문에 우리 DB에도 update
.orElse(userProfile.toMember()); .orElse(userProfile.toMember());
return memberRepository.save(member); return memberRepository.save(member);
} }

View File

@@ -4,24 +4,23 @@ spring:
client: client:
registration: registration:
github: github:
client-id: "6c34d9a6903231c5a301" client-id: 6c34d9a6903231c5a301
client-secret: "비밀키" client-secret: 비밀키
scope: "name,email,avatar_url" scope: name,email,avatar_url
google: google:
client-id: "54767115914-gcla0mork6h3156h4qcutjerm0mdf4fu.apps.googleusercontent.com" client-id: 54767115914-gcla0mork6h3156h4qcutjerm0mdf4fu.apps.googleusercontent.com
client-secret: "비밀키" client-secret: 비밀키
scope: "profile,email" scope: profile,email
# 네이버는 spring security가 기본적을 제공해주지 않기 때문에 github, google과 달리 많은 정보를 적어줘야한다.
naver: naver:
client-id: "sCfhQHgPVQFFf8RTGjVe" client-id: sCfhQHgPVQFFf8RTGjVe
client-secret: "비밀키" client-secret: 비밀키
redirect-uri: "{baseUrl}/{action}/oauth2/code/{registrationId}" redirect-uri: "{baseUrl}/{action}/oauth2/code/{registrationId}"
authorization_grant_type: "authorization_code" authorization_grant_type: authorization_code
scope: "name,email,profile_image" scope: name,email,profile_image
client-name: "Naver" client-name: Naver
provider: provider:
naver: naver:
authorization_uri: "https://nid.naver.com/oauth2.0/authorize" authorization_uri: https://nid.naver.com/oauth2.0/authorize
token_uri: "https://nid.naver.com/oauth2.0/token" token_uri: https://nid.naver.com/oauth2.0/token
user-info-uri: "https://openapi.naver.com/v1/nid/me" user-info-uri: https://openapi.naver.com/v1/nid/me
user_name_attribute: "response" user_name_attribute: response

View File

@@ -1,9 +1,9 @@
spring: spring:
profiles: profiles:
include: "oauth" include: oauth
datasource: datasource:
url: "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE" url: jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
name: "sa" name: sa
password: password:
h2: h2:
console: console: