Skip to content

Commit

Permalink
6-7
Browse files Browse the repository at this point in the history
  • Loading branch information
jojozhai committed Sep 10, 2017
1 parent 306048d commit 575e8a0
Show file tree
Hide file tree
Showing 16 changed files with 188 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
*
*/
package com.imooc.security.app;

/**
* @author zhailiang
*
*/
public class AppSecretException extends RuntimeException {

public AppSecretException(String msg){
super(msg);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
*
*/
package com.imooc.security.app;

import javax.servlet.http.HttpServletRequest;

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 com.imooc.security.app.social.AppSingUpUtils;
import com.imooc.security.core.support.SocialUserInfo;

/**
* @author zhailiang
*
*/
@RestController
public class AppSecurityController {

@Autowired
private ProviderSignInUtils providerSignInUtils;

@Autowired
private AppSingUpUtils appSingUpUtils;

@GetMapping("/social/signUp")
@ResponseStatus(HttpStatus.UNAUTHORIZED)
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());

appSingUpUtils.saveConnectionData(new ServletWebRequest(request), connection.createData());

return userInfo;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.social.security.SpringSocialConfigurer;

import com.imooc.security.app.social.openid.OpenIdAuthenticationSecurityConfig;
import com.imooc.security.app.authentication.openid.OpenIdAuthenticationSecurityConfig;
import com.imooc.security.core.authentication.mobile.SmsCodeAuthenticationSecurityConfig;
import com.imooc.security.core.properties.SecurityConstants;
import com.imooc.security.core.properties.SecurityProperties;
Expand Down Expand Up @@ -74,7 +74,7 @@ public void configure(HttpSecurity http) throws Exception {
securityProperties.getBrowser().getSignUpUrl(),
securityProperties.getBrowser().getSession().getSessionInvalidUrl(),
securityProperties.getBrowser().getSignOutUrl(),
"/user/regist")
"/user/regist", "/social/signUp")
.permitAll()
.anyRequest()
.authenticated()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
*
*/
package com.imooc.security.app;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import com.imooc.security.core.social.ImoocSpringSocialConfigurer;

/**
* @author zhailiang
*
*/
@Component
public class SpringSocialConfigurerPostProcessor implements BeanPostProcessor {

/* (non-Javadoc)
* @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization(java.lang.Object, java.lang.String)
*/
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}

/* (non-Javadoc)
* @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization(java.lang.Object, java.lang.String)
*/
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if(StringUtils.equals(beanName, "imoocSocialSecurityConfig")){
ImoocSpringSocialConfigurer config = (ImoocSpringSocialConfigurer)bean;
config.signupUrl("/social/signUp");
return config;
}
return bean;
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
*/
package com.imooc.security.app.social.openid;
package com.imooc.security.app.authentication.openid;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
*/
package com.imooc.security.app.social.openid;
package com.imooc.security.app.authentication.openid;

import java.util.HashSet;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
*/
package com.imooc.security.app.social.openid;
package com.imooc.security.app.authentication.openid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
*/
package com.imooc.security.app.social.openid;
package com.imooc.security.app.authentication.openid;

import java.util.Collection;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
*
*/
/**
* @author zhailiang
*
*/
package com.imooc.security.app.authentication.openid;
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
*
*/
package com.imooc.security.app.social;

import java.util.concurrent.TimeUnit;

import org.apache.commons.lang.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 com.imooc.security.app.AppSecretException;

/**
* @author zhailiang
*
*/
@Component
public class AppSingUpUtils {

@Autowired
private RedisTemplate<Object, Object> redisTemplate;

@Autowired
private UsersConnectionRepository usersConnectionRepository;

@Autowired
private ConnectionFactoryLocator connectionFactoryLocator;

public void saveConnectionData(WebRequest request, ConnectionData connectionData) {
redisTemplate.opsForValue().set(getKey(request), connectionData, 10, TimeUnit.MINUTES);
}

public void doPostSignUp(WebRequest request, String userId) {
String key = getKey(request);
if(!redisTemplate.hasKey(key)){
throw new AppSecretException("无法找到缓存的用户社交账号信息");
}
ConnectionData connectionData = (ConnectionData) redisTemplate.opsForValue().get(key);
Connection<?> connection = connectionFactoryLocator.getConnectionFactory(connectionData.getProviderId())
.createConnection(connectionData);
usersConnectionRepository.createConnectionRepository(userId).addConnection(connection);

redisTemplate.delete(key);
}

private String getKey(WebRequest request) {
String deviceId = request.getHeader("deviceId");
if (StringUtils.isBlank(deviceId)) {
throw new AppSecretException("设备id参数不能为空");
}
return "imooc:security:social.connect." + deviceId;
}

}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import com.imooc.security.browser.support.SocialUserInfo;
import com.imooc.security.core.properties.SecurityConstants;
import com.imooc.security.core.properties.SecurityProperties;
import com.imooc.security.core.support.SimpleResponse;
import com.imooc.security.core.support.SocialUserInfo;

/**
* @author zhailiang
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
*/
package com.imooc.security.browser.support;
package com.imooc.security.core.support;

/**
* @author zhailiang
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.fasterxml.jackson.annotation.JsonView;
import com.imooc.dto.User;
import com.imooc.dto.UserQueryCondition;
import com.imooc.security.app.social.AppSingUpUtils;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
Expand All @@ -46,12 +47,16 @@ public class UserController {
@Autowired
private ProviderSignInUtils providerSignInUtils;

@Autowired
private AppSingUpUtils appSingUpUtils;

@PostMapping("/regist")
public void regist(User user, HttpServletRequest request) {

//不管是注册用户还是绑定用户,都会拿到一个用户唯一标识。
String userId = user.getUsername();
providerSignInUtils.doPostSignUp(userId, new ServletWebRequest(request));
//providerSignInUtils.doPostSignUp(userId, new ServletWebRequest(request));
appSingUpUtils.doPostSignUp(new ServletWebRequest(request), userId);
}

@GetMapping("/me")
Expand Down

0 comments on commit 575e8a0

Please sign in to comment.