⒈
1 package cn.coreqi.social.config; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.context.annotation.Bean; 5 import org.springframework.context.annotation.Configuration; 6 import org.springframework.security.crypto.encrypt.Encryptors; 7 import org.springframework.social.config.annotation.EnableSocial; 8 import org.springframework.social.config.annotation.SocialConfigurerAdapter; 9 import org.springframework.social.connect.ConnectionFactoryLocator; 10 import org.springframework.social.connect.UsersConnectionRepository; 11 import org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository; 12 import org.springframework.social.connect.web.ProviderSignInUtils; 13 import org.springframework.social.security.SpringSocialConfigurer; 14 15 import javax.sql.DataSource; 16 17 @Configuration 18 @EnableSocial 19 public class SocialConfig extends SocialConfigurerAdapter { 20 21 @Autowired 22 private DataSource dataSource; 23 24 /** 25 * 26 * @param connectionFactoryLocator 作用是去根据条件去查找应该用那个connectionFactory,因为系统中可能有很多的connectionFactory。 27 * @return 28 */ 29 @Override 30 public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) { 31 //第三个参数的作用是把插入到数据库的数据进行加解密 32 JdbcUsersConnectionRepository jdbcUsersConnectionRepository = new JdbcUsersConnectionRepository(dataSource,connectionFactoryLocator, Encryptors.noOpText()); 33 //jdbcUsersConnectionRepository.setTablePrefix(); //设置数据表的前缀 34 return jdbcUsersConnectionRepository; 35 } 36 37 /** 38 * 声明后还需要加在SpringSecurity过滤器链上 39 * @return 40 */ 41 @Bean 42 public SpringSocialConfigurer coreqiSocialSecurityConfig(){ 43 CoreqiSpringSocialConfig config = new CoreqiSpringSocialConfig(); 44 config.signupUrl("/registry"); //当从业务系统中无法找到OAuth快捷登陆的用户,那么将用户引导到注册页面中 45 return config; 46 } 47 48 49 50 51 52 //1.注册过程中如何拿到SpringSocial信息 53 //2.注册完成后如何把业务系统的用户ID传给SpringSocial 54 @Bean 55 public ProviderSignInUtils providerSignInUtils(ConnectionFactoryLocator connectionFactoryLocator){ 56 return new ProviderSignInUtils(connectionFactoryLocator,getUsersConnectionRepository(connectionFactoryLocator)); 57 } 58 }
⒉
1 package cn.coreqi.social.qq.entities; 2 3 public class SocialUserInfo { 4 private String providerId; 5 private String providerUserId; 6 private String nickname; 7 private String headimg; 8 9 public String getProviderId() { 10 return providerId; 11 } 12 13 public void setProviderId(String providerId) { 14 this.providerId = providerId; 15 } 16 17 public String getProviderUserId() { 18 return providerUserId; 19 } 20 21 public void setProviderUserId(String providerUserId) { 22 this.providerUserId = providerUserId; 23 } 24 25 public String getNickname() { 26 return nickname; 27 } 28 29 public void setNickname(String nickname) { 30 this.nickname = nickname; 31 } 32 33 public String getHeadimg() { 34 return headimg; 35 } 36 37 public void setHeadimg(String headimg) { 38 this.headimg = headimg; 39 } 40 }
⒊
1 package cn.coreqi.controller; 2 3 import cn.coreqi.social.qq.entities.SocialUserInfo; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.security.core.userdetails.User; 6 import org.springframework.social.connect.Connection; 7 import org.springframework.social.connect.web.ProviderSignInUtils; 8 import org.springframework.web.bind.annotation.GetMapping; 9 import org.springframework.web.bind.annotation.PostMapping; 10 import org.springframework.web.bind.annotation.RestController; 11 import org.springframework.web.context.request.ServletWebRequest; 12 13 import javax.servlet.http.HttpServletRequest; 14 15 @RestController 16 public class UserController { 17 18 19 @Autowired 20 private ProviderSignInUtils providerSignInUtils; 21 22 @PostMapping("/regist") 23 public void regist(User user,HttpServletRequest request){ 24 //不管是注册用户还是绑定用户,都会拿到一个用户唯一标识 25 String userId = user.getUsername(); 26 providerSignInUtils.doPostSignUp(userId,new ServletWebRequest(request)); 27 } 28 29 /** 30 * 31 * @return 32 */ 33 @GetMapping("/social/user") 34 public SocialUserInfo getSocialUserInfo(HttpServletRequest request){ 35 SocialUserInfo userInfo = new SocialUserInfo(); 36 Connection<?> connection = providerSignInUtils.getConnectionFromSession(new ServletWebRequest(request)); 37 userInfo.setProviderId(connection.getKey().getProviderId()); 38 userInfo.setProviderUserId(connection.getKey().getProviderUserId()); 39 userInfo.setNickname(connection.getDisplayName()); 40 userInfo.setHeadimg(connection.getImageUrl()); 41 return userInfo; 42 } 43 }