From 65dd24289bc83e50f8756e488be9fe7c0cd0c229 Mon Sep 17 00:00:00 2001 From: Iamasoldier <1272745473@qq.com> Date: Fri, 21 Feb 2025 11:56:33 +0800 Subject: [PATCH] =?UTF-8?q?fix[=E7=94=A8=E6=88=B7=E4=B8=AD=E5=BF=83]?= =?UTF-8?q?=EF=BC=9A=E4=BC=98=E5=8C=96=E7=99=BB=E5=BD=95=E7=AD=89=E5=BD=95?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/controller/SysUserController.java | 6 +- .../central/user/service/ISysUserService.java | 14 ++--- .../user/service/impl/SysUserServiceImpl.java | 3 +- .../token/TencentAuthenticationToken.java | 10 +++- .../com/central/common/feign/UserService.java | 4 +- .../fallback/UserServiceFallbackFactory.java | 4 +- .../oauth/service/ZltUserDetailsService.java | 2 +- .../service/impl/UserDetailServiceImpl.java | 4 +- .../TencentAuthenticationConverter.java | 21 ++----- .../TencentAuthenticationProvider.java | 59 ++++++++++++++++--- zlt-uaa/src/main/resources/bootstrap.yml | 8 +-- 11 files changed, 87 insertions(+), 48 deletions(-) diff --git a/zlt-business/user-center/src/main/java/com/central/user/controller/SysUserController.java b/zlt-business/user-center/src/main/java/com/central/user/controller/SysUserController.java index 9fb06e5d..aec732d3 100644 --- a/zlt-business/user-center/src/main/java/com/central/user/controller/SysUserController.java +++ b/zlt-business/user-center/src/main/java/com/central/user/controller/SysUserController.java @@ -121,10 +121,10 @@ public class SysUserController { * * @param openId openId */ - @GetMapping(value = "/users-anon/wxOpenId", params = "openId") + @GetMapping(value = "/users-anon/wxOpenId", params = {"openId","mobile"}) @Operation(summary = "根据WxOpenId查询用户") - public SysUser findByWxOpenId(String openId) throws Exception { - return appUserService.findByWxOpenid(openId); + public SysUser findByWxOpenId(String openId,String mobile) throws Exception { + return appUserService.findByWxOpenid(openId,mobile); } @GetMapping("/users/{id}") diff --git a/zlt-business/user-center/src/main/java/com/central/user/service/ISysUserService.java b/zlt-business/user-center/src/main/java/com/central/user/service/ISysUserService.java index 9c985164..aab205a1 100644 --- a/zlt-business/user-center/src/main/java/com/central/user/service/ISysUserService.java +++ b/zlt-business/user-center/src/main/java/com/central/user/service/ISysUserService.java @@ -1,15 +1,15 @@ package com.central.user.service; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import com.central.common.service.ISuperService; -import com.central.user.model.SysUserExcel; import com.central.common.model.PageResult; import com.central.common.model.Result; import com.central.common.model.SysRole; import com.central.common.model.SysUser; +import com.central.common.service.ISuperService; +import com.central.user.model.SysUserExcel; + +import java.util.List; +import java.util.Map; +import java.util.Set; /** * @author zlt @@ -29,7 +29,7 @@ public interface ISysUserService extends ISuperService { SysUser findByMobile(String username); - SysUser findByWxOpenid(String wxOpenid) throws Exception; + SysUser findByWxOpenid(String wxOpenid,String mobile) throws Exception; /** * 通过SysUser 把roles和permissions也查询出来 diff --git a/zlt-business/user-center/src/main/java/com/central/user/service/impl/SysUserServiceImpl.java b/zlt-business/user-center/src/main/java/com/central/user/service/impl/SysUserServiceImpl.java index 734fac82..ddc746bf 100644 --- a/zlt-business/user-center/src/main/java/com/central/user/service/impl/SysUserServiceImpl.java +++ b/zlt-business/user-center/src/main/java/com/central/user/service/impl/SysUserServiceImpl.java @@ -71,7 +71,7 @@ public class SysUserServiceImpl extends SuperServiceImpl } @Override - public SysUser findByWxOpenid(String wxOpenid) throws Exception { + public SysUser findByWxOpenid(String wxOpenid,String mobile) throws Exception { SysUser sysUser = this.selectByOpenId(wxOpenid); if (ObjectUtils.isEmpty(sysUser)) { sysUser = new SysUser(); @@ -80,6 +80,7 @@ public class SysUserServiceImpl extends SuperServiceImpl sysUser.setType(UserType.APP.name()); sysUser.setPassword(wxOpenid); sysUser.setEnabled(Boolean.TRUE); + sysUser.setMobile(mobile); } String username = sysUser.getUsername(); boolean result = super.saveOrUpdateIdempotency(sysUser, lock diff --git a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/token/TencentAuthenticationToken.java b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/token/TencentAuthenticationToken.java index 6a0a2837..168b3bda 100644 --- a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/token/TencentAuthenticationToken.java +++ b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/token/TencentAuthenticationToken.java @@ -18,15 +18,21 @@ import java.util.Collection; public class TencentAuthenticationToken extends BaseAuthenticationToken { public static final AuthorizationGrantType GRANT_TYPE = new AuthorizationGrantType("tencent"); private final Object principal; + private final String encryptedData; + private final String iv; - public TencentAuthenticationToken(String code) { + public TencentAuthenticationToken(String code, String encryptedData, String iv) { super(GRANT_TYPE); this.principal = code; + this.encryptedData = encryptedData; + this.iv = iv; super.setAuthenticated(true); } - public TencentAuthenticationToken(UserDetails user, Collection authorities) { + public TencentAuthenticationToken(UserDetails user, Collection authorities, String encryptedData, String iv) { super(authorities); this.principal = user; + this.encryptedData = encryptedData; + this.iv = iv; } } diff --git a/zlt-commons/zlt-common-core/src/main/java/com/central/common/feign/UserService.java b/zlt-commons/zlt-common-core/src/main/java/com/central/common/feign/UserService.java index aa68e520..63834b4e 100644 --- a/zlt-commons/zlt-common-core/src/main/java/com/central/common/feign/UserService.java +++ b/zlt-commons/zlt-common-core/src/main/java/com/central/common/feign/UserService.java @@ -56,8 +56,8 @@ public interface UserService { * * @param openId openId */ - @GetMapping(value = "/users-anon/wxOpenId", params = "openId") - SysUser findByWxOpenId(@RequestParam("openId") String openId); + @GetMapping(value = "/users-anon/wxOpenId", params = {"openId","mobile"}) + SysUser findByWxOpenId(@RequestParam("openId") String openId, @RequestParam("mobile") String mobile); /** diff --git a/zlt-commons/zlt-common-core/src/main/java/com/central/common/feign/fallback/UserServiceFallbackFactory.java b/zlt-commons/zlt-common-core/src/main/java/com/central/common/feign/fallback/UserServiceFallbackFactory.java index 805179c9..dd05fb22 100644 --- a/zlt-commons/zlt-common-core/src/main/java/com/central/common/feign/fallback/UserServiceFallbackFactory.java +++ b/zlt-commons/zlt-common-core/src/main/java/com/central/common/feign/fallback/UserServiceFallbackFactory.java @@ -3,8 +3,8 @@ package com.central.common.feign.fallback; import com.central.common.feign.UserService; import com.central.common.model.SysRole; import com.central.common.model.SysUser; -import org.springframework.cloud.openfeign.FallbackFactory; import lombok.extern.slf4j.Slf4j; +import org.springframework.cloud.openfeign.FallbackFactory; import java.util.Collections; import java.util.List; @@ -45,7 +45,7 @@ public class UserServiceFallbackFactory implements FallbackFactory } @Override - public SysUser findByWxOpenId(String openId) { + public SysUser findByWxOpenId(String openId,String mobile) { log.error("通过wxOpenId查询用户异常:{}", openId, throwable); return new SysUser(); } diff --git a/zlt-uaa/src/main/java/com/central/oauth/service/ZltUserDetailsService.java b/zlt-uaa/src/main/java/com/central/oauth/service/ZltUserDetailsService.java index fca81721..1914d151 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/service/ZltUserDetailsService.java +++ b/zlt-uaa/src/main/java/com/central/oauth/service/ZltUserDetailsService.java @@ -36,5 +36,5 @@ public interface ZltUserDetailsService extends UserDetailsService { * 根据微信用户id/openId查询用户 * @param wxOpenId 用户id/openId */ - UserDetails loadUserByWxOpenId(String wxOpenId) throws UsernameNotFoundException; + UserDetails loadUserByWxOpenId(String wxOpenId,String mobile) throws UsernameNotFoundException; } diff --git a/zlt-uaa/src/main/java/com/central/oauth/service/impl/UserDetailServiceImpl.java b/zlt-uaa/src/main/java/com/central/oauth/service/impl/UserDetailServiceImpl.java index 50307170..e8a3ec69 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/service/impl/UserDetailServiceImpl.java +++ b/zlt-uaa/src/main/java/com/central/oauth/service/impl/UserDetailServiceImpl.java @@ -51,8 +51,8 @@ public class UserDetailServiceImpl implements ZltUserDetailsService { } @Override - public UserDetails loadUserByWxOpenId(String wxOpenId) throws UsernameNotFoundException { - SysUser sysUser = userService.findByWxOpenId(wxOpenId); + public UserDetails loadUserByWxOpenId(String wxOpenId,String mobile) throws UsernameNotFoundException { + SysUser sysUser = userService.findByWxOpenId(wxOpenId,mobile); checkUser(sysUser); return LoginUserUtils.getLoginAppUser(sysUser); } diff --git a/zlt-uaa/src/main/java/com/central/oauth/support/tencent/TencentAuthenticationConverter.java b/zlt-uaa/src/main/java/com/central/oauth/support/tencent/TencentAuthenticationConverter.java index acc70897..21ed5853 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/support/tencent/TencentAuthenticationConverter.java +++ b/zlt-uaa/src/main/java/com/central/oauth/support/tencent/TencentAuthenticationConverter.java @@ -1,30 +1,17 @@ package com.central.oauth.support.tencent; -import com.alibaba.fastjson.JSONObject; -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.central.oauth.exception.CustomOAuth2AuthenticationException; import com.central.oauth.support.base.BaseAuthenticationConverter; import com.central.oauth.utils.OAuthEndpointUtils; import com.central.oauth2.common.token.BaseAuthenticationToken; import com.central.oauth2.common.token.TencentAuthenticationToken; -import org.springframework.beans.BeanUtils; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; -import org.springframework.http.ResponseEntity; import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; -import org.springframework.util.Assert; import org.springframework.util.MultiValueMap; -import org.springframework.web.client.RestTemplate; -import java.util.Date; -import java.util.HashMap; import java.util.List; -import java.util.Map; /** - * @author: zlt - * @date: 2023/11/18 + * @author: lsl + * @date: 2024/12/18 *

* Blog: https://zlt2000.gitee.io * Github: https://github.com/zlt2000 @@ -44,7 +31,9 @@ public class TencentAuthenticationConverter extends BaseAuthenticationConverter @Override protected BaseAuthenticationToken getToken(MultiValueMap parameters) { String code = OAuthEndpointUtils.getParam(parameters, OAuth2ParameterNames.CODE); - return new TencentAuthenticationToken(code); + String encryptedData = OAuthEndpointUtils.getParam(parameters,"encryptedData"); + String iv = OAuthEndpointUtils.getParam(parameters,"iv"); + return new TencentAuthenticationToken(code, encryptedData, iv); } diff --git a/zlt-uaa/src/main/java/com/central/oauth/support/tencent/TencentAuthenticationProvider.java b/zlt-uaa/src/main/java/com/central/oauth/support/tencent/TencentAuthenticationProvider.java index 1e0fb0eb..c3f3f3ea 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/support/tencent/TencentAuthenticationProvider.java +++ b/zlt-uaa/src/main/java/com/central/oauth/support/tencent/TencentAuthenticationProvider.java @@ -1,7 +1,7 @@ package com.central.oauth.support.tencent; +import com.alibaba.fastjson.JSONObject; import com.central.oauth.exception.CustomOAuth2AuthenticationException; -import com.central.oauth.service.IValidateCodeService; import com.central.oauth.service.impl.UserDetailServiceFactory; import com.central.oauth.support.base.BaseAuthenticationProvider; import com.central.oauth2.common.properties.SecurityProperties; @@ -9,7 +9,10 @@ import com.central.oauth2.common.token.TencentAuthenticationToken; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.Getter; import lombok.Setter; -import org.springframework.http.*; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.userdetails.UserDetails; @@ -19,9 +22,13 @@ import org.springframework.security.oauth2.server.authorization.OAuth2Authorizat import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenGenerator; import org.springframework.util.Assert; import org.springframework.util.LinkedMultiValueMap; -import org.springframework.web.client.RestTemplate; import org.springframework.util.MultiValueMap; -import java.util.HashMap; +import org.springframework.web.client.RestTemplate; + +import javax.crypto.Cipher; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; +import java.util.Base64; import java.util.Map; /** @@ -60,11 +67,17 @@ public class TencentAuthenticationProvider extends BaseAuthenticationProvider { TencentAuthenticationToken authToken = (TencentAuthenticationToken) authentication; String code = (String) authToken.getPrincipal(); - Map wxLoginResult = getWxOpenId(code); + String encryptedData = authToken.getEncryptedData(); + String iv = authToken.getIv(); + + Map wxLoginResult = getWxSessionResult(code); UserDetails userDetails; try { - userDetails = userDetailsServiceFactory.getService(authToken).loadUserByWxOpenId(wxLoginResult.get("openid").toString()); + String openId = wxLoginResult.get("openid").toString(); + String sessionKey = wxLoginResult.get("session_key").toString(); + String mobile = decryptPhoneNumber(encryptedData,sessionKey,iv); + userDetails = userDetailsServiceFactory.getService(authToken).loadUserByWxOpenId(openId,mobile); } catch (AuthenticationException e) { throw new CustomOAuth2AuthenticationException(e.getMessage()); @@ -73,7 +86,7 @@ public class TencentAuthenticationProvider extends BaseAuthenticationProvider { throw new CustomOAuth2AuthenticationException("用户登录信息待处理,请重新登录"); } - return new TencentAuthenticationToken(userDetails, userDetails.getAuthorities()); + return new TencentAuthenticationToken(userDetails, userDetails.getAuthorities(),encryptedData, iv); } @Override @@ -87,7 +100,7 @@ public class TencentAuthenticationProvider extends BaseAuthenticationProvider { * @param code 小程序code * @return */ - private Map getWxOpenId(String code) { + private Map getWxSessionResult(String code) { if (code == null || code.isEmpty()) { throw new CustomOAuth2AuthenticationException("Code cannot be null or empty"); @@ -119,4 +132,34 @@ public class TencentAuthenticationProvider extends BaseAuthenticationProvider { } } + /** + * 解析手机号 + * @param encryptedData + * @param sessionKey + * @param iv + * @return + */ + public String decryptPhoneNumber(String encryptedData, String sessionKey, String iv) { + try { + // Base64解码 + byte[] encryptedDataBytes = Base64.getDecoder().decode(encryptedData); + byte[] sessionKeyBytes = Base64.getDecoder().decode(sessionKey); + byte[] ivBytes = Base64.getDecoder().decode(iv); + + // AES解密 + Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); + SecretKeySpec keySpec = new SecretKeySpec(sessionKeyBytes, "AES"); + IvParameterSpec ivSpec = new IvParameterSpec(ivBytes); + cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); + byte[] decryptedBytes = cipher.doFinal(encryptedDataBytes); + + // 解析解密后的数据 + String decryptedData = new String(decryptedBytes, "UTF-8"); + JSONObject jsonObject = JSONObject.parseObject(decryptedData); + return jsonObject.getString("purePhoneNumber"); // 获取手机号 + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } } diff --git a/zlt-uaa/src/main/resources/bootstrap.yml b/zlt-uaa/src/main/resources/bootstrap.yml index f24da648..aff9b849 100644 --- a/zlt-uaa/src/main/resources/bootstrap.yml +++ b/zlt-uaa/src/main/resources/bootstrap.yml @@ -8,10 +8,10 @@ server: spring: application: name: uaa-server -# cloud: -# nacos: -# discovery: -# ip: 124.222.128.40 + cloud: + nacos: + discovery: + ip: 124.222.128.40 encrypt: key-store: