feat(平台管理):新增小程序的管理,多个小程序登录unionId管理
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.central.oauth.service;
|
||||
|
||||
import com.central.common.model.MiniAppConfig;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
@@ -37,4 +38,11 @@ public interface ZltUserDetailsService extends UserDetailsService {
|
||||
* @param wxOpenId 用户id/openId
|
||||
*/
|
||||
UserDetails loadUserByWxOpenId(String wxOpenId,String mobile,String unionId) throws UsernameNotFoundException;
|
||||
|
||||
/**
|
||||
* 根据appId获取APP
|
||||
* @param appId
|
||||
* @return
|
||||
*/
|
||||
MiniAppConfig loadMiniAppConfig(String appId);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.central.oauth.service.impl;
|
||||
|
||||
import com.central.common.constant.SecurityConstants;
|
||||
import com.central.common.feign.UserService;
|
||||
import com.central.common.model.MiniAppConfig;
|
||||
import com.central.common.model.SysUser;
|
||||
import com.central.common.utils.LoginUserUtils;
|
||||
import com.central.oauth.service.ZltUserDetailsService;
|
||||
@@ -57,6 +58,11 @@ public class UserDetailServiceImpl implements ZltUserDetailsService {
|
||||
return LoginUserUtils.getLoginAppUser(sysUser);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MiniAppConfig loadMiniAppConfig(String appId) {
|
||||
return userService.getByAppid(appId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserDetails loadUserByMobile(String mobile) {
|
||||
SysUser sysUser = userService.findByMobile(mobile);
|
||||
|
||||
+2
-1
@@ -33,7 +33,8 @@ public class TencentAuthenticationConverter extends BaseAuthenticationConverter
|
||||
String code = OAuthEndpointUtils.getParam(parameters, OAuth2ParameterNames.CODE);
|
||||
String encryptedData = OAuthEndpointUtils.getParam(parameters,"encryptedData");
|
||||
String iv = OAuthEndpointUtils.getParam(parameters,"iv");
|
||||
return new TencentAuthenticationToken(code, encryptedData, iv);
|
||||
String appId = OAuthEndpointUtils.getParam(parameters,"appId");
|
||||
return new TencentAuthenticationToken(code, encryptedData, iv, appId);
|
||||
}
|
||||
|
||||
|
||||
|
||||
+14
-5
@@ -1,6 +1,7 @@
|
||||
package com.central.oauth.support.tencent;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.central.common.model.MiniAppConfig;
|
||||
import com.central.oauth.exception.CustomOAuth2AuthenticationException;
|
||||
import com.central.oauth.service.impl.UserDetailServiceFactory;
|
||||
import com.central.oauth.support.base.BaseAuthenticationProvider;
|
||||
@@ -71,8 +72,16 @@ public class TencentAuthenticationProvider extends BaseAuthenticationProvider {
|
||||
String code = (String) authToken.getPrincipal();
|
||||
String encryptedData = authToken.getEncryptedData();
|
||||
String iv = authToken.getIv();
|
||||
String appid = authToken.getAppId(); // 前端需传递
|
||||
|
||||
Map<String,Object> wxLoginResult = getWxSessionResult(code);
|
||||
// 1. 根据appid查找配置
|
||||
MiniAppConfig config = userDetailsServiceFactory.getService(authToken).loadMiniAppConfig(appid);
|
||||
if (config == null) {
|
||||
throw new CustomOAuth2AuthenticationException("不支持的appid");
|
||||
}
|
||||
|
||||
// 2. 用查到的appid/secret调用微信接口
|
||||
Map<String,Object> wxLoginResult = getWxSessionResult(code, config.getAppid(), config.getSecret());
|
||||
|
||||
UserDetails userDetails;
|
||||
try {
|
||||
@@ -90,7 +99,7 @@ public class TencentAuthenticationProvider extends BaseAuthenticationProvider {
|
||||
|
||||
throw new CustomOAuth2AuthenticationException("用户登录信息待处理,请重新登录");
|
||||
}
|
||||
return new TencentAuthenticationToken(userDetails, userDetails.getAuthorities(),encryptedData, iv);
|
||||
return new TencentAuthenticationToken(userDetails, userDetails.getAuthorities(),encryptedData, iv,appid);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -104,7 +113,7 @@ public class TencentAuthenticationProvider extends BaseAuthenticationProvider {
|
||||
* @param code 小程序code
|
||||
* @return
|
||||
*/
|
||||
private Map<String, Object> getWxSessionResult(String code) {
|
||||
private Map<String, Object> getWxSessionResult(String code, String appid, String secret) {
|
||||
|
||||
if (code == null || code.isEmpty()) {
|
||||
throw new CustomOAuth2AuthenticationException("Code cannot be null or empty");
|
||||
@@ -112,8 +121,8 @@ public class TencentAuthenticationProvider extends BaseAuthenticationProvider {
|
||||
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
|
||||
params.add("appid", securityProperties.getTencent().getAppId());
|
||||
params.add("secret", securityProperties.getTencent().getAppSecret());
|
||||
params.add("appid", appid);
|
||||
params.add("secret", secret);
|
||||
params.add("js_code", code);
|
||||
params.add("grant_type", "authorization_code");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user