今度、FacebookLOGINを具現します。
まず、developers.facebook.com/apps/create/に接続します。
そして、APPIDとパスワードをAPPICATIONPROPERTIESに入れます。
これから、Facebook Buttonを追加します。
<button class="btn btn-primary" ><a href="/oauth2/authorization/facebook" style="text-decoration: none; color: white" >Facebook Login</a></button>
そして、一つでProviderを具現してinterfaceを追加します。
package com.rbwsn.oauth.provider;
public interface OAuth2UserInfo {
String getProviderId();
String getProvider();
String getEmail();
String getName();
}
GoogleにはproviderIDがSUBで入ったからSUBで
FacebookにはProviderIdがIDで入るからIDでもらいます。
package com.rbwsn.oauth.provider;
public interface OAuth2UserInfo {
String getProviderId();
String getProvider();
String getEmail();
String getName();
}
package com.rbwsn.oauth.provider;
import java.util.Map;
public class GoogleOAuth2UserInfo implements OAuth2UserInfo{
private Map<String,Object> attributes;
public GoogleOAuth2UserInfo(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");
}
}
package com.rbwsn.oauth.provider;
import java.util.Map;
public class FacebookOAuth2UserInfo implements OAuth2UserInfo{
private Map<String,Object> attributes;
public FacebookOAuth2UserInfo(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");
}
}
これを具現するPrincipalOauth2UserSerivceも修正します。
package com.rbwsn.oauth;
import com.rbwsn.auth.SecurityDetails;
import com.rbwsn.constant.Role;
import com.rbwsn.controller.IndexController;
import com.rbwsn.entity.User;
import com.rbwsn.oauth.provider.FacebookOAuth2UserInfo;
import com.rbwsn.oauth.provider.GoogleOAuth2UserInfo;
import com.rbwsn.oauth.provider.OAuth2UserInfo;
import com.rbwsn.repository.UserRepository;
import com.rbwsn.service.UserService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
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 org.springframework.transaction.annotation.Transactional;
import javax.servlet.RequestDispatcher;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
@Service
@Transactional
public class PrincipalOauth2UserService extends DefaultOAuth2UserService {
@Autowired
private UserRepository userRepository;
@Autowired
private PasswordEncoder passwordEncoder;
@SneakyThrows
@Override
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
OAuth2User oAuth2User = super.loadUser(userRequest);
OAuth2UserInfo oAuth2UserInfo = null;
if(userRequest.getClientRegistration().getRegistrationId().equals("google")){
System.out.println("Google Login");
oAuth2UserInfo = new GoogleOAuth2UserInfo(oAuth2User.getAttributes());
}else if(userRequest.getClientRegistration().getRegistrationId().equals("facebook")){
System.out.println("Facebook Login");
oAuth2UserInfo = new FacebookOAuth2UserInfo(oAuth2User.getAttributes());
}else{
System.out.println("null");
}
String provider = oAuth2UserInfo.getProvider();
String providerId = oAuth2UserInfo.getProviderId();
String email = oAuth2UserInfo.getEmail();
String name = oAuth2UserInfo.getName();
String password = passwordEncoder.encode("oauth1234");
User user = userRepository.findByEmail(email);
if (user == null) {
user = User.builder()
.username(name)
.provider(provider)
.password(password)
.email(email)
.role(Role.ROLE_USER)
.provider_id(providerId)
.build();
userRepository.save(user);
}
return new SecurityDetails(user, oAuth2User.getAttributes());
}
}