完善微信登录功能
This commit is contained in:
+3
-3
@@ -5,7 +5,7 @@ package com.micro.fast.security.core.social.wechat.config;
|
||||
|
||||
import com.micro.fast.security.core.master.SecurityProperties;
|
||||
import com.micro.fast.security.core.social.wechat.WeChatSocialProperties;
|
||||
import com.micro.fast.security.core.social.wechat.connet.WeixinConnectionFactory;
|
||||
import com.micro.fast.security.core.social.wechat.connet.WeChatConnectionFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.social.SocialAutoConfigurerAdapter;
|
||||
@@ -20,7 +20,7 @@ import org.springframework.social.connect.ConnectionFactory;
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(prefix = "ms.security.social.weChat", name = "app-id")
|
||||
public class WeixinAutoConfiguration extends SocialAutoConfigurerAdapter {
|
||||
public class WeChatAutoConfiguration extends SocialAutoConfigurerAdapter {
|
||||
|
||||
@Autowired
|
||||
private SecurityProperties securityProperties;
|
||||
@@ -28,7 +28,7 @@ public class WeixinAutoConfiguration extends SocialAutoConfigurerAdapter {
|
||||
@Override
|
||||
protected ConnectionFactory<?> createConnectionFactory() {
|
||||
WeChatSocialProperties weChatConfig = securityProperties.getSocial().getWeChat();
|
||||
return new WeixinConnectionFactory(weChatConfig.getProviderId(), weChatConfig.getAppId(),
|
||||
return new WeChatConnectionFactory(weChatConfig.getProviderId(), weChatConfig.getAppId(),
|
||||
weChatConfig.getAppSecret());
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -10,16 +10,16 @@ import org.springframework.social.oauth2.AccessGrant;
|
||||
* 所以在这里继承了标准AccessGrant,添加了openId字段,作为对微信access_token信息的封装。
|
||||
* @author lsy
|
||||
*/
|
||||
public class WeixinAccessGrant extends AccessGrant {
|
||||
public class WeChatAccessGrant extends AccessGrant {
|
||||
|
||||
|
||||
private String openId;
|
||||
|
||||
public WeixinAccessGrant() {
|
||||
public WeChatAccessGrant() {
|
||||
super("");
|
||||
}
|
||||
|
||||
public WeixinAccessGrant(String accessToken, String scope, String refreshToken, Long expiresIn) {
|
||||
public WeChatAccessGrant(String accessToken, String scope, String refreshToken, Long expiresIn) {
|
||||
super(accessToken, scope, refreshToken, expiresIn);
|
||||
}
|
||||
|
||||
+3
-3
@@ -13,13 +13,13 @@ import org.springframework.social.connect.UserProfile;
|
||||
* 微信 api适配器,将微信 api的数据模型转为spring social的标准模型。
|
||||
* @author lsy
|
||||
*/
|
||||
public class WeixinAdapter implements ApiAdapter<WeChat> {
|
||||
public class WeChatAdapter implements ApiAdapter<WeChat> {
|
||||
|
||||
private String openId;
|
||||
|
||||
public WeixinAdapter() {}
|
||||
public WeChatAdapter() {}
|
||||
|
||||
public WeixinAdapter(String openId){
|
||||
public WeChatAdapter(String openId){
|
||||
this.openId = openId;
|
||||
}
|
||||
|
||||
+7
-8
@@ -14,18 +14,16 @@ import org.springframework.social.oauth2.OAuth2ServiceProvider;
|
||||
|
||||
/**
|
||||
* 微信连接工厂
|
||||
*
|
||||
*
|
||||
* @author lsy
|
||||
*/
|
||||
public class WeixinConnectionFactory extends OAuth2ConnectionFactory<WeChat> {
|
||||
public class WeChatConnectionFactory extends OAuth2ConnectionFactory<WeChat> {
|
||||
|
||||
/**
|
||||
* @param appId
|
||||
* @param appSecret
|
||||
*/
|
||||
public WeixinConnectionFactory(String providerId, String appId, String appSecret) {
|
||||
super(providerId, new WeixinServiceProvider(appId, appSecret), new WeixinAdapter());
|
||||
public WeChatConnectionFactory(String providerId, String appId, String appSecret) {
|
||||
super(providerId, new WeChatServiceProvider(appId, appSecret), new WeChatAdapter());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -33,8 +31,9 @@ public class WeixinConnectionFactory extends OAuth2ConnectionFactory<WeChat> {
|
||||
*/
|
||||
@Override
|
||||
protected String extractProviderUserId(AccessGrant accessGrant) {
|
||||
if(accessGrant instanceof WeixinAccessGrant) {
|
||||
return ((WeixinAccessGrant)accessGrant).getOpenId();
|
||||
//如果是微信登录就直接返回 openId
|
||||
if(accessGrant instanceof WeChatAccessGrant) {
|
||||
return ((WeChatAccessGrant)accessGrant).getOpenId();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -61,7 +60,7 @@ public class WeixinConnectionFactory extends OAuth2ConnectionFactory<WeChat> {
|
||||
}
|
||||
|
||||
private ApiAdapter<WeChat> getApiAdapter(String providerUserId) {
|
||||
return new WeixinAdapter(providerUserId);
|
||||
return new WeChatAdapter(providerUserId);
|
||||
}
|
||||
|
||||
private OAuth2ServiceProvider<WeChat> getOAuth2ServiceProvider() {
|
||||
+3
-3
@@ -23,7 +23,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
* 完成微信的OAuth2认证流程的模板类。国内厂商实现的OAuth2每个都不同, spring默认提供的OAuth2Template适应不了,只能针对每个厂商自己微调。
|
||||
* @author lsy
|
||||
*/
|
||||
public class WeixinOAuth2Template extends OAuth2Template {
|
||||
public class WeChatOAuth2Template extends OAuth2Template {
|
||||
|
||||
private String clientId;
|
||||
|
||||
@@ -35,7 +35,7 @@ public class WeixinOAuth2Template extends OAuth2Template {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
public WeixinOAuth2Template(String clientId, String clientSecret, String authorizeUrl, String accessTokenUrl) {
|
||||
public WeChatOAuth2Template(String clientId, String clientSecret, String authorizeUrl, String accessTokenUrl) {
|
||||
super(clientId, clientSecret, authorizeUrl, accessTokenUrl);
|
||||
setUseParametersForClientAuthentication(true);
|
||||
this.clientId = clientId;
|
||||
@@ -93,7 +93,7 @@ public class WeixinOAuth2Template extends OAuth2Template {
|
||||
throw new RuntimeException("获取access token失败, errcode:"+errcode+", errmsg:"+errmsg);
|
||||
}
|
||||
|
||||
WeixinAccessGrant accessToken = new WeixinAccessGrant(
|
||||
WeChatAccessGrant accessToken = new WeChatAccessGrant(
|
||||
MapUtils.getString(result, "access_token"),
|
||||
MapUtils.getString(result, "scope"),
|
||||
MapUtils.getString(result, "refresh_token"),
|
||||
+3
-3
@@ -14,7 +14,7 @@ import org.springframework.social.oauth2.AbstractOAuth2ServiceProvider;
|
||||
* @author zhailiang
|
||||
*
|
||||
*/
|
||||
public class WeixinServiceProvider extends AbstractOAuth2ServiceProvider<WeChat> {
|
||||
public class WeChatServiceProvider extends AbstractOAuth2ServiceProvider<WeChat> {
|
||||
|
||||
/**
|
||||
* 微信获取授权码的url
|
||||
@@ -29,8 +29,8 @@ public class WeixinServiceProvider extends AbstractOAuth2ServiceProvider<WeChat>
|
||||
* @param appId
|
||||
* @param appSecret
|
||||
*/
|
||||
public WeixinServiceProvider(String appId, String appSecret) {
|
||||
super(new WeixinOAuth2Template(appId, appSecret,URL_AUTHORIZE,URL_ACCESS_TOKEN));
|
||||
public WeChatServiceProvider(String appId, String appSecret) {
|
||||
super(new WeChatOAuth2Template(appId, appSecret,URL_AUTHORIZE,URL_ACCESS_TOKEN));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user