package cn.coreqi.social.config;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.social.config.annotation.EnableSocial;
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.social.security.SpringSocialConfigurer; import javax.sql.DataSource; @Configuration
@EnableSocial
public class SocialConfig extends SocialConfigurerAdapter { @Autowired
private DataSource dataSource; /**
*
* @param connectionFactoryLocator 作用是去根据条件去查找应该用那个connectionFactory,因为系统中可能有很多的connectionFactory。
* @return
*/
@Override
public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
//第三个参数的作用是把插入到数据库的数据进行加解密
JdbcUsersConnectionRepository jdbcUsersConnectionRepository = new JdbcUsersConnectionRepository(dataSource,connectionFactoryLocator, Encryptors.noOpText());
//jdbcUsersConnectionRepository.setTablePrefix(); //设置数据表的前缀
return jdbcUsersConnectionRepository;
} /**
* 声明后还需要加在SpringSecurity过滤器链上
* @return
*/
@Bean
public SpringSocialConfigurer coreqiSocialSecurityConfig(){
CoreqiSpringSocialConfig config = new CoreqiSpringSocialConfig();
config.signupUrl("/registry"); //当从业务系统中无法找到OAuth快捷登陆的用户,那么将用户引导到注册页面中
return config;
} //1.注册过程中如何拿到SpringSocial信息
//2.注册完成后如何把业务系统的用户ID传给SpringSocial
@Bean
public ProviderSignInUtils providerSignInUtils(ConnectionFactoryLocator connectionFactoryLocator){
return new ProviderSignInUtils(connectionFactoryLocator,getUsersConnectionRepository(connectionFactoryLocator));
}
}

 package cn.coreqi.social.qq.entities;

 public class SocialUserInfo {
private String providerId;
private String providerUserId;
private String nickname;
private String headimg; public String getProviderId() {
return providerId;
} public void setProviderId(String providerId) {
this.providerId = providerId;
} public String getProviderUserId() {
return providerUserId;
} public void setProviderUserId(String providerUserId) {
this.providerUserId = providerUserId;
} public String getNickname() {
return nickname;
} public void setNickname(String nickname) {
this.nickname = nickname;
} public String getHeadimg() {
return headimg;
} public void setHeadimg(String headimg) {
this.headimg = headimg;
}
}

 package cn.coreqi.controller;

 import cn.coreqi.social.qq.entities.SocialUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest; import javax.servlet.http.HttpServletRequest; @RestController
public class UserController { @Autowired
private ProviderSignInUtils providerSignInUtils; @PostMapping("/regist")
public void regist(User user,HttpServletRequest request){
//不管是注册用户还是绑定用户,都会拿到一个用户唯一标识
String userId = user.getUsername();
providerSignInUtils.doPostSignUp(userId,new ServletWebRequest(request));
} /**
*
* @return
*/
@GetMapping("/social/user")
public SocialUserInfo getSocialUserInfo(HttpServletRequest request){
SocialUserInfo userInfo = new SocialUserInfo();
Connection<?> connection = providerSignInUtils.getConnectionFromSession(new ServletWebRequest(request));
userInfo.setProviderId(connection.getKey().getProviderId());
userInfo.setProviderUserId(connection.getKey().getProviderUserId());
userInfo.setNickname(connection.getDisplayName());
userInfo.setHeadimg(connection.getImageUrl());
return userInfo;
}
}
05-06 09:44