app第三方账户绑定用户功能实现
This commit is contained in:
+49
@@ -0,0 +1,49 @@
|
||||
package com.micro.fast.security.app;
|
||||
|
||||
import com.micro.fast.security.app.social.AppSingUpUtils;
|
||||
import com.micro.fast.security.core.pojo.SocialUserInfo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
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.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* @author lsy
|
||||
*/
|
||||
@RestController
|
||||
public class AppSecurityController {
|
||||
|
||||
|
||||
@Autowired
|
||||
private AppSingUpUtils singUpUtils;
|
||||
|
||||
@Autowired
|
||||
private ProviderSignInUtils providerSignInUtils;
|
||||
|
||||
/**
|
||||
* 引导用户注册,关联第三方账户,把用户的信息存储到redis之中
|
||||
* 需要被授权
|
||||
* 在注册接口之,需要从redis之中获取数据进行注册
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/social/signUp")
|
||||
@ResponseStatus(HttpStatus.UNAUTHORIZED)
|
||||
public SocialUserInfo getSocialUserInfo(HttpServletRequest request){
|
||||
SocialUserInfo socialUserInfo = new SocialUserInfo();
|
||||
Connection<?> connectionFromSession = providerSignInUtils.getConnectionFromSession(new ServletWebRequest(request));
|
||||
socialUserInfo.setProviderId(connectionFromSession.getKey().getProviderId());
|
||||
socialUserInfo.setProviderUserId(connectionFromSession.getKey().getProviderUserId());
|
||||
socialUserInfo.setHeadingUrl(connectionFromSession.getDisplayName());
|
||||
socialUserInfo.setHeadingUrl(connectionFromSession.getImageUrl());
|
||||
singUpUtils.saveConnectionData(new ServletWebRequest(request),connectionFromSession.createData());
|
||||
return socialUserInfo;
|
||||
}
|
||||
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.micro.fast.security.app;
|
||||
|
||||
import com.micro.fast.security.core.social.MsSpringSocialConfigurer;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* spring中所有的bean初始化之前和初始化之后都要经过这两个方法
|
||||
* @author lsy
|
||||
*/
|
||||
@Component
|
||||
public class SpringSocialConfigurePostProcessor implements BeanPostProcessor{
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
||||
if (StringUtils.equals(beanName,"msSpringSocialConfigurer")){
|
||||
MsSpringSocialConfigurer configurer = (MsSpringSocialConfigurer)bean;
|
||||
//app第三方用户的注册
|
||||
configurer.signupUrl("/social/signUp");
|
||||
return configurer;
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
}
|
||||
+3
@@ -32,8 +32,10 @@ public class AuthorizationResourceConfig extends ResourceServerConfigurerAdapter
|
||||
|
||||
@Autowired
|
||||
private MsAuthenticationSuccessHandler msAuthenticationSuccessHandler;
|
||||
|
||||
@Autowired
|
||||
private SecurityProperties securityProperties;
|
||||
|
||||
/**
|
||||
* 短信验证码登录的配置
|
||||
*/
|
||||
@@ -83,6 +85,7 @@ public class AuthorizationResourceConfig extends ResourceServerConfigurerAdapter
|
||||
//用户注册或绑定的页面
|
||||
, securityProperties.getBrowser().getSignUpUrl()
|
||||
, "处理用户的注册绑定请求的url"
|
||||
,"/social/signUp"
|
||||
)
|
||||
.permitAll()
|
||||
//任何请求
|
||||
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package com.micro.fast.security.app.social;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.social.connect.Connection;
|
||||
import org.springframework.social.connect.ConnectionData;
|
||||
import org.springframework.social.connect.ConnectionFactoryLocator;
|
||||
import org.springframework.social.connect.UsersConnectionRepository;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* app第三方登录的工具类
|
||||
* @author lsy
|
||||
*/
|
||||
@Component
|
||||
public class AppSingUpUtils {
|
||||
|
||||
@Autowired
|
||||
private UsersConnectionRepository usersConnectionRepository;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<Object,Object> redisTemplate;
|
||||
|
||||
@Autowired
|
||||
private ConnectionFactoryLocator connectionFactoryLocator;
|
||||
|
||||
/**
|
||||
* 存储第三方账号关联信息
|
||||
* @param request
|
||||
* @param connectionData
|
||||
*/
|
||||
public void saveConnectionData(WebRequest request, ConnectionData connectionData){
|
||||
redisTemplate.opsForValue().set(getKey(request),connectionData,10, TimeUnit.MINUTES);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册第三方的用户信息
|
||||
* @param request
|
||||
* @param userId
|
||||
*/
|
||||
public void doPostSignUp(WebRequest request,String userId){
|
||||
String key = getKey(request);
|
||||
if (!redisTemplate.hasKey(key)){
|
||||
throw new RuntimeException("无法找到缓存的第三方用户信息");
|
||||
}
|
||||
ConnectionData connectionData = (ConnectionData)redisTemplate.opsForValue().get(getKey(request));
|
||||
Connection<?> connection = connectionFactoryLocator.getConnectionFactory(connectionData.getProviderId()).createConnection(connectionData);
|
||||
usersConnectionRepository.createConnectionRepository(userId).addConnection(connection);
|
||||
redisTemplate.delete(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建redis的key
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
protected String getKey(WebRequest request){
|
||||
String deviceId = request.getHeader("deviceId");
|
||||
if (StringUtils.isBlank(deviceId)){
|
||||
throw new RuntimeException("机器唯一标识参数不能为空");
|
||||
}
|
||||
return "ms:security:social.connect."+deviceId;
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
package com.micro.fast.security.browser.controller;
|
||||
|
||||
import com.micro.fast.security.browser.pojo.SocialUserInfo;
|
||||
import com.micro.fast.security.core.pojo.SocialUserInfo;
|
||||
import com.micro.fast.security.core.master.SecurityConstants;
|
||||
import com.micro.fast.security.core.master.SecurityProperties;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.micro.fast.security.browser.pojo;
|
||||
package com.micro.fast.security.core.pojo;
|
||||
|
||||
/**
|
||||
* 第三方登录时用户的信息
|
||||
+1
-1
@@ -64,7 +64,7 @@ public class SocialConfig extends SocialConfigurerAdapter{
|
||||
* springSocialconfigure的配置的bean
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
@Bean("msSpringSocialConfigurer")
|
||||
public SpringSocialConfigurer springSocialConfigurer(){
|
||||
SocialProperties social = securityProperties.getSocial();
|
||||
MsSpringSocialConfigurer msSpringSocialConfigurer = new MsSpringSocialConfigurer(social.getFilterProcessUrl());
|
||||
|
||||
Reference in New Issue
Block a user