fix[用户中心]:优化登录等录逻辑
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
+5
-16
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
+51
-8
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user