페이스북 개발자 앱 등록
https://growth-coder.tistory.com/141
1. 라이브러리 등록
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
2.application.yml 설정
spring: security: oauth2: client: registration: facebook: client-id: client-secret: scope: - public_profile - email
3. SecurityConfig 설정
@RequiredArgsConstructor @EnableWebSecurity @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { pivate final OAuth2DetailsService oAuth2DetailsService; @Override protected void configure(HttpSecurity http) throws Exception { http. ~ .and() .oauth2Login() .userInfoEndpoint() .userService(oAuth2DetailsService); }
4. OAuth2DetailsService
import java.util.Map; import java.util.UUID; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService; import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest; import org.springframework.security.oauth2.core.OAuth2AuthenticationException; import org.springframework.security.oauth2.core.user.OAuth2User; import org.springframework.stereotype.Service; import com.cos.photogramstart.config.auth.PrincipalDetails; import com.cos.photogramstart.domain.user.User; import com.cos.photogramstart.domain.user.UserRepository; @Service public class OAuth2DetailsService extends DefaultOAuth2UserService{ @Autowired private UserRepository userRepository; @Override public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException { System.out.println(" OAuth 2 페이스북 로그인 " +userRequest.getAccessToken()); OAuth2User oauth2User=super.loadUser(userRequest); Map<String,Object> userInfo= oauth2User.getAttributes(); String username="facebook_"+(String)userInfo.get("id"); String password= new BCryptPasswordEncoder().encode(UUID.randomUUID().toString()); String name=(String)userInfo.get("name"); String email=(String)userInfo.get("email"); System.out.println("************ oAuth2User.getAttributes : " + oauth2User.getAttributes()); System.out.println("************ username : " + username); System.out.println("************ password : " + password); System.out.println("************ name : " + name); System.out.println("************ email : " + email); User userEntity=userRepository.findByUsername(username); if(userEntity==null) { User user =User.builder() .username(username) .password(password) .email(email) .name(name) .role("ROLE_USER") .build(); return new PrincipalDetails(userRepository.save(user), oauth2User.getAttributes()); }else { //페이스북으로 이미 회원가입이 되어 있는경우 return new PrincipalDetails(userEntity, oauth2User.getAttributes()); } } }
5. login.jsp
<!-- Oauth 소셜로그인 --> <div class="login__facebook"> <button onclick="javascript:location.href='/oauth2/authorization/facebook'"> <i class="fab fa-facebook-square"></i> <span>Facebook으로 로그인</span> </button> </div> <!-- Oauth 소셜로그인end -->
PrincipalDetails
import java.util.ArrayList; import java.util.Collection; import java.util.Map; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.oauth2.core.user.OAuth2User; import com.cos.photogramstart.domain.user.User; public class PrincipalDetails implements UserDetails , OAuth2User{ private static final long serialVersionUID = 3351489579645764340L; private User user; private Map<String, Object> attributes; public PrincipalDetails(User user) { this.user=user; } public PrincipalDetails(User user, Map<String, Object> attributes) { this.attributes=attributes; this.user=user; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } //권한:한개가 아닐 수 있음.(3개 이상의 권한) @Override public Collection<? extends GrantedAuthority> getAuthorities() { Collection<GrantedAuthority> collector=new ArrayList<>(); // collector.add(new GrantedAuthority() { // // @Override // public String getAuthority() { // return user.getRole(); // } // }); collector.add(()-> user.getRole()); return collector; } @Override public String getPassword() { return user.getPassword(); } @Override public String getUsername() { return user.getUsername(); } @Override public boolean isAccountNonExpired() { return true; } @Override public boolean isAccountNonLocked() { return true; } @Override public boolean isCredentialsNonExpired() { return true; } @Override public boolean isEnabled() { return true; } @Override public Map<String, Object> getAttributes() { return attributes; } @Override public String getName() { return (String)attributes.get("name"); } }
소스 :
https://github.com/braverokmc79/spring-boot-jpa-web-release
댓글 ( 4)
댓글 남기기