fix[用户中心]:优化登录等录逻辑

This commit is contained in:
2025-02-21 11:56:33 +08:00
parent cdb35876a0
commit 65dd24289b
11 changed files with 87 additions and 48 deletions
@@ -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}")
@@ -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> {
SysUser findByMobile(String username);
SysUser findByWxOpenid(String wxOpenid) throws Exception;
SysUser findByWxOpenid(String wxOpenid,String mobile) throws Exception;
/**
* 通过SysUser 把roles和permissions也查询出来
@@ -71,7 +71,7 @@ public class SysUserServiceImpl extends SuperServiceImpl<SysUserMapper, SysUser>
}
@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<SysUserMapper, SysUser>
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
@@ -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<? extends GrantedAuthority> authorities) {
public TencentAuthenticationToken(UserDetails user, Collection<? extends GrantedAuthority> authorities, String encryptedData, String iv) {
super(authorities);
this.principal = user;
this.encryptedData = encryptedData;
this.iv = iv;
}
}
@@ -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);
/**
@@ -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<UserService>
}
@Override
public SysUser findByWxOpenId(String openId) {
public SysUser findByWxOpenId(String openId,String mobile) {
log.error("通过wxOpenId查询用户异常:{}", openId, throwable);
return new SysUser();
}
@@ -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;
}
@@ -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);
}
@@ -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
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
@@ -44,7 +31,9 @@ public class TencentAuthenticationConverter extends BaseAuthenticationConverter
@Override
protected BaseAuthenticationToken getToken(MultiValueMap<String, String> 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);
}
@@ -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<String,Object> wxLoginResult = getWxOpenId(code);
String encryptedData = authToken.getEncryptedData();
String iv = authToken.getIv();
Map<String,Object> 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<String, Object> getWxOpenId(String code) {
private Map<String, Object> 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;
}
}
}
+4 -4
View File
@@ -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: