-
Notifications
You must be signed in to change notification settings - Fork 487
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
188 additions
and
56 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
imooc-security-app/src/main/java/com/imooc/security/app/AppSecretException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
imooc-security-app/src/main/java/com/imooc/security/app/AppSecurityController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...ecurity-app/src/main/java/com/imooc/security/app/SpringSocialConfigurerPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...al/openid/OpenIdAuthenticationFilter.java → ...on/openid/OpenIdAuthenticationFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../openid/OpenIdAuthenticationProvider.java → .../openid/OpenIdAuthenticationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...d/OpenIdAuthenticationSecurityConfig.java → ...d/OpenIdAuthenticationSecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ial/openid/OpenIdAuthenticationToken.java → ...ion/openid/OpenIdAuthenticationToken.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...security-app/src/main/java/com/imooc/security/app/authentication/openid/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* | ||
*/ | ||
/** | ||
* @author zhailiang | ||
* | ||
*/ | ||
package com.imooc.security.app.authentication.openid; |
61 changes: 61 additions & 0 deletions
61
imooc-security-app/src/main/java/com/imooc/security/app/social/AppSingUpUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
31 changes: 0 additions & 31 deletions
31
...n/java/com/imooc/security/app/social/impl/AppSocialAuthenticationFilterPostProcessor.java
This file was deleted.
Oops, something went wrong.
8 changes: 0 additions & 8 deletions
8
imooc-security-app/src/main/java/com/imooc/security/app/social/impl/package-info.java
This file was deleted.
Oops, something went wrong.
8 changes: 0 additions & 8 deletions
8
imooc-security-app/src/main/java/com/imooc/security/app/social/openid/package-info.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...urity/browser/support/SocialUserInfo.java → ...security/core/support/SocialUserInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters