Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- dockerinstall
- 스프링익셉션처리
- 스프링부트서버에사진전송
- 출처 노마드코더
- 출처 코딩셰프
- 인스타클론
- 스프링구독
- ssh도커설치
- 스프링부트구독취소
- 스프링부트중복예외처리
- 멀티폼
- 스프링사진
- 스프링부트사진올리기
- vm도커설치하는법
- springboot_exception_handler
- 출처 따배도
- 스프링사진업로드
- centos도커설치
- 출처 문어박사
- 스프링부트
- 서버에도커설치
- 우분투도커설치
- 스프링부트팔로잉
- 도커설치하는법
- WAS웹서버
- 출처 메타코딩
- 스프링이미지업로드
- 파이썬sort
- 스프링부트팔로우취소
- 스프링부트api
Archives
- Today
- Total
MakerHyeon
[SpringBoot] Security 페이스북 로그인 (security facebook login) 본문
SpringBoot/Security
[SpringBoot] Security 페이스북 로그인 (security facebook login)
유쾌한고등어 2023. 1. 23. 14:26● Security 페이스북 로그인 (security facebook login)
- loginForm.html에 페이스북 로그인 UI추가
<a href="/oauth2/authorization/google">구글 로그인</a>
<a href="/oauth2/authorization/facebook">페이스북 로그인</a>
<a href="/joinForm">회원가입을 아직 하지 않으셨나요?</a>
- application.yml 에 아이디,시크릿키 추가
facebook:
client-id: 57755337384710656
client-secret: 6c474e4db28a061ad83cf137c8a9e95e56
scope:
- email
- public_profile
- 구글 , 페이스북 로그인 분리 구현을 위해 OAuth2UserInfo interface 생성
public interface OAuth2UserInfo {
String getProviderId();
String getProvider();
String getEmail();
String getName();
}
- GoogleUserInfo 파일 생성
public class GoogleUserInfo implements OAuth2UserInfo{
private Map<String,Object> attributes;
public GoogleUserInfo(Map<String,Object> attributes){
this.attributes = attributes;
}
@Override
public String getProviderId() {
return (String) attributes.get("sub");
}
@Override
public String getProvider() {
return "google";
}
@Override
public String getEmail() {
return (String) attributes.get("email");
}
@Override
public String getName() {
return (String) attributes.get("name");
}
}
- FacebookUserInfo 파일 생성
public class FacebookUserInfo implements OAuth2UserInfo{
private Map<String,Object> attributes;
public FacebookUserInfo(Map<String,Object> attributes){
this.attributes = attributes;
}
@Override
public String getProviderId() {
return (String) attributes.get("id");
}
@Override
public String getProvider() {
return "facebook";
}
@Override
public String getEmail() {
return (String) attributes.get("email");
}
@Override
public String getName() {
return (String) attributes.get("name");
}
}
- 구글,페이스북 loadUser 분리구현
// PrincipalOauth2UserService의 loadUser함수
OAuth2User oAuth2User = super.loadUser(userRequest);
// oAuth2User =oAuth2User.getAttributes();
// 회원가입 진행
OAuth2UserInfo oAuth2UserInfo = null;
if(userRequest.getClientRegistration().getRegistrationId().equals("google")){
oAuth2UserInfo = new GoogleUserInfo(oAuth2User.getAttributes());
}else if(userRequest.getClientRegistration().getRegistrationId().equals("facebook")){
oAuth2UserInfo = new FacebookUserInfo(oAuth2User.getAttributes());
}else{
System.out.println("구글/페이스북만지원한다...!");
}
// String provider = userRequest.getClientRegistration().getRegistrationId();
// String providerId = oAuth2User.getAttribute("sub");
// String username = provider+"-"+providerId; // google_103234234234534543534
// String password = bCryptPasswordEncoder.encode("겟인데어");
// String email = oAuth2User.getAttribute("email");
// String role = "ROLE_USER";
String provider = oAuth2UserInfo.getProvider(); // google
String providerId = oAuth2UserInfo.getProviderId();
String username = provider+"-"+providerId; // google_103234234234534543534
String password = bCryptPasswordEncoder.encode("겟인데어");
String email = oAuth2User.getAttribute("email");
String role = "ROLE_USER";
User userEntity = userRepository.findByUsername(username);
if(userEntity == null){ // 이미 회원가입이 되어있을때 에러
userEntity = User.builder()
.username(username)
.password(password)
.email(email)
.role(role)
.provider(provider)
.providerId(providerId)
.build();
userRepository.save(userEntity);
}else{
System.out.println("로그인을 이미 한 적이 있습니다.");
}
return new PrincipalDetails(userEntity,oAuth2User.getAttributes());
'SpringBoot > Security' 카테고리의 다른 글
[SpringSecurity] Form Login 인증 (0) | 2023.04.03 |
---|---|
[SpringBoot] Security 네이버 로그인 (0) | 2023.01.23 |
[SpringBoot] Security 구글 로그인 (0) | 2023.01.21 |
[SpringBoot] Security 권한 부분 설정 (0) | 2023.01.20 |
[SpringBoot] Security 회원가입과 로그인 (0) | 2023.01.20 |
Comments