鉴权完成后如何拿到SpringSocial的用户信息
Posted fanqisoft
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鉴权完成后如何拿到SpringSocial的用户信息相关的知识,希望对你有一定的参考价值。
⒈
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.social.connect.Connection; 6 import org.springframework.social.connect.web.ProviderSignInUtils; 7 import org.springframework.web.bind.annotation.GetMapping; 8 import org.springframework.web.bind.annotation.RestController; 9 import org.springframework.web.context.request.ServletWebRequest; 10 11 import javax.servlet.http.HttpServletRequest; 12 13 @RestController 14 public class UserController { 15 16 17 @Autowired 18 private ProviderSignInUtils providerSignInUtils; 19 20 /** 21 * 22 * @return 23 */ 24 @GetMapping("/social/user") 25 public SocialUserInfo getSocialUserInfo(HttpServletRequest request){ 26 SocialUserInfo userInfo = new SocialUserInfo(); 27 Connection<?> connection = providerSignInUtils.getConnectionFromSession(new ServletWebRequest(request)); 28 userInfo.setProviderId(connection.getKey().getProviderId()); 29 userInfo.setProviderUserId(connection.getKey().getProviderUserId()); 30 userInfo.setNickname(connection.getDisplayName()); 31 userInfo.setHeadimg(connection.getImageUrl()); 32 return userInfo; 33 } 34 }
以上是关于鉴权完成后如何拿到SpringSocial的用户信息的主要内容,如果未能解决你的问题,请参考以下文章