fix[用户中心]:优化登录等录逻辑
This commit is contained in:
+3
-3
@@ -121,10 +121,10 @@ public class SysUserController {
|
|||||||
*
|
*
|
||||||
* @param openId openId
|
* @param openId openId
|
||||||
*/
|
*/
|
||||||
@GetMapping(value = "/users-anon/wxOpenId", params = "openId")
|
@GetMapping(value = "/users-anon/wxOpenId", params = {"openId","mobile"})
|
||||||
@Operation(summary = "根据WxOpenId查询用户")
|
@Operation(summary = "根据WxOpenId查询用户")
|
||||||
public SysUser findByWxOpenId(String openId) throws Exception {
|
public SysUser findByWxOpenId(String openId,String mobile) throws Exception {
|
||||||
return appUserService.findByWxOpenid(openId);
|
return appUserService.findByWxOpenid(openId,mobile);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/users/{id}")
|
@GetMapping("/users/{id}")
|
||||||
|
|||||||
+7
-7
@@ -1,15 +1,15 @@
|
|||||||
package com.central.user.service;
|
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.PageResult;
|
||||||
import com.central.common.model.Result;
|
import com.central.common.model.Result;
|
||||||
import com.central.common.model.SysRole;
|
import com.central.common.model.SysRole;
|
||||||
import com.central.common.model.SysUser;
|
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
|
* @author zlt
|
||||||
@@ -29,7 +29,7 @@ public interface ISysUserService extends ISuperService<SysUser> {
|
|||||||
|
|
||||||
SysUser findByMobile(String username);
|
SysUser findByMobile(String username);
|
||||||
|
|
||||||
SysUser findByWxOpenid(String wxOpenid) throws Exception;
|
SysUser findByWxOpenid(String wxOpenid,String mobile) throws Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过SysUser 把roles和permissions也查询出来
|
* 通过SysUser 把roles和permissions也查询出来
|
||||||
|
|||||||
+2
-1
@@ -71,7 +71,7 @@ public class SysUserServiceImpl extends SuperServiceImpl<SysUserMapper, SysUser>
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SysUser findByWxOpenid(String wxOpenid) throws Exception {
|
public SysUser findByWxOpenid(String wxOpenid,String mobile) throws Exception {
|
||||||
SysUser sysUser = this.selectByOpenId(wxOpenid);
|
SysUser sysUser = this.selectByOpenId(wxOpenid);
|
||||||
if (ObjectUtils.isEmpty(sysUser)) {
|
if (ObjectUtils.isEmpty(sysUser)) {
|
||||||
sysUser = new SysUser();
|
sysUser = new SysUser();
|
||||||
@@ -80,6 +80,7 @@ public class SysUserServiceImpl extends SuperServiceImpl<SysUserMapper, SysUser>
|
|||||||
sysUser.setType(UserType.APP.name());
|
sysUser.setType(UserType.APP.name());
|
||||||
sysUser.setPassword(wxOpenid);
|
sysUser.setPassword(wxOpenid);
|
||||||
sysUser.setEnabled(Boolean.TRUE);
|
sysUser.setEnabled(Boolean.TRUE);
|
||||||
|
sysUser.setMobile(mobile);
|
||||||
}
|
}
|
||||||
String username = sysUser.getUsername();
|
String username = sysUser.getUsername();
|
||||||
boolean result = super.saveOrUpdateIdempotency(sysUser, lock
|
boolean result = super.saveOrUpdateIdempotency(sysUser, lock
|
||||||
|
|||||||
+8
-2
@@ -18,15 +18,21 @@ import java.util.Collection;
|
|||||||
public class TencentAuthenticationToken extends BaseAuthenticationToken {
|
public class TencentAuthenticationToken extends BaseAuthenticationToken {
|
||||||
public static final AuthorizationGrantType GRANT_TYPE = new AuthorizationGrantType("tencent");
|
public static final AuthorizationGrantType GRANT_TYPE = new AuthorizationGrantType("tencent");
|
||||||
private final Object principal;
|
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);
|
super(GRANT_TYPE);
|
||||||
this.principal = code;
|
this.principal = code;
|
||||||
|
this.encryptedData = encryptedData;
|
||||||
|
this.iv = iv;
|
||||||
super.setAuthenticated(true);
|
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);
|
super(authorities);
|
||||||
this.principal = user;
|
this.principal = user;
|
||||||
|
this.encryptedData = encryptedData;
|
||||||
|
this.iv = iv;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,8 +56,8 @@ public interface UserService {
|
|||||||
*
|
*
|
||||||
* @param openId openId
|
* @param openId openId
|
||||||
*/
|
*/
|
||||||
@GetMapping(value = "/users-anon/wxOpenId", params = "openId")
|
@GetMapping(value = "/users-anon/wxOpenId", params = {"openId","mobile"})
|
||||||
SysUser findByWxOpenId(@RequestParam("openId") String openId);
|
SysUser findByWxOpenId(@RequestParam("openId") String openId, @RequestParam("mobile") String mobile);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+2
-2
@@ -3,8 +3,8 @@ package com.central.common.feign.fallback;
|
|||||||
import com.central.common.feign.UserService;
|
import com.central.common.feign.UserService;
|
||||||
import com.central.common.model.SysRole;
|
import com.central.common.model.SysRole;
|
||||||
import com.central.common.model.SysUser;
|
import com.central.common.model.SysUser;
|
||||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -45,7 +45,7 @@ public class UserServiceFallbackFactory implements FallbackFactory<UserService>
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SysUser findByWxOpenId(String openId) {
|
public SysUser findByWxOpenId(String openId,String mobile) {
|
||||||
log.error("通过wxOpenId查询用户异常:{}", openId, throwable);
|
log.error("通过wxOpenId查询用户异常:{}", openId, throwable);
|
||||||
return new SysUser();
|
return new SysUser();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,5 +36,5 @@ public interface ZltUserDetailsService extends UserDetailsService {
|
|||||||
* 根据微信用户id/openId查询用户
|
* 根据微信用户id/openId查询用户
|
||||||
* @param wxOpenId 用户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
|
@Override
|
||||||
public UserDetails loadUserByWxOpenId(String wxOpenId) throws UsernameNotFoundException {
|
public UserDetails loadUserByWxOpenId(String wxOpenId,String mobile) throws UsernameNotFoundException {
|
||||||
SysUser sysUser = userService.findByWxOpenId(wxOpenId);
|
SysUser sysUser = userService.findByWxOpenId(wxOpenId,mobile);
|
||||||
checkUser(sysUser);
|
checkUser(sysUser);
|
||||||
return LoginUserUtils.getLoginAppUser(sysUser);
|
return LoginUserUtils.getLoginAppUser(sysUser);
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-16
@@ -1,30 +1,17 @@
|
|||||||
package com.central.oauth.support.tencent;
|
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.support.base.BaseAuthenticationConverter;
|
||||||
import com.central.oauth.utils.OAuthEndpointUtils;
|
import com.central.oauth.utils.OAuthEndpointUtils;
|
||||||
import com.central.oauth2.common.token.BaseAuthenticationToken;
|
import com.central.oauth2.common.token.BaseAuthenticationToken;
|
||||||
import com.central.oauth2.common.token.TencentAuthenticationToken;
|
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.security.oauth2.core.endpoint.OAuth2ParameterNames;
|
||||||
import org.springframework.util.Assert;
|
|
||||||
import org.springframework.util.MultiValueMap;
|
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.List;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author: zlt
|
* @author: lsl
|
||||||
* @date: 2023/11/18
|
* @date: 2024/12/18
|
||||||
* <p>
|
* <p>
|
||||||
* Blog: https://zlt2000.gitee.io
|
* Blog: https://zlt2000.gitee.io
|
||||||
* Github: https://github.com/zlt2000
|
* Github: https://github.com/zlt2000
|
||||||
@@ -44,7 +31,9 @@ public class TencentAuthenticationConverter extends BaseAuthenticationConverter
|
|||||||
@Override
|
@Override
|
||||||
protected BaseAuthenticationToken getToken(MultiValueMap<String, String> parameters) {
|
protected BaseAuthenticationToken getToken(MultiValueMap<String, String> parameters) {
|
||||||
String code = OAuthEndpointUtils.getParam(parameters, OAuth2ParameterNames.CODE);
|
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;
|
package com.central.oauth.support.tencent;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.central.oauth.exception.CustomOAuth2AuthenticationException;
|
import com.central.oauth.exception.CustomOAuth2AuthenticationException;
|
||||||
import com.central.oauth.service.IValidateCodeService;
|
|
||||||
import com.central.oauth.service.impl.UserDetailServiceFactory;
|
import com.central.oauth.service.impl.UserDetailServiceFactory;
|
||||||
import com.central.oauth.support.base.BaseAuthenticationProvider;
|
import com.central.oauth.support.base.BaseAuthenticationProvider;
|
||||||
import com.central.oauth2.common.properties.SecurityProperties;
|
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 com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
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.Authentication;
|
||||||
import org.springframework.security.core.AuthenticationException;
|
import org.springframework.security.core.AuthenticationException;
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
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.security.oauth2.server.authorization.token.OAuth2TokenGenerator;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
import org.springframework.web.client.RestTemplate;
|
|
||||||
import org.springframework.util.MultiValueMap;
|
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;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -60,11 +67,17 @@ public class TencentAuthenticationProvider extends BaseAuthenticationProvider {
|
|||||||
TencentAuthenticationToken authToken = (TencentAuthenticationToken) authentication;
|
TencentAuthenticationToken authToken = (TencentAuthenticationToken) authentication;
|
||||||
|
|
||||||
String code = (String) authToken.getPrincipal();
|
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;
|
UserDetails userDetails;
|
||||||
try {
|
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) {
|
} catch (AuthenticationException e) {
|
||||||
throw new CustomOAuth2AuthenticationException(e.getMessage());
|
throw new CustomOAuth2AuthenticationException(e.getMessage());
|
||||||
@@ -73,7 +86,7 @@ public class TencentAuthenticationProvider extends BaseAuthenticationProvider {
|
|||||||
|
|
||||||
throw new CustomOAuth2AuthenticationException("用户登录信息待处理,请重新登录");
|
throw new CustomOAuth2AuthenticationException("用户登录信息待处理,请重新登录");
|
||||||
}
|
}
|
||||||
return new TencentAuthenticationToken(userDetails, userDetails.getAuthorities());
|
return new TencentAuthenticationToken(userDetails, userDetails.getAuthorities(),encryptedData, iv);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -87,7 +100,7 @@ public class TencentAuthenticationProvider extends BaseAuthenticationProvider {
|
|||||||
* @param code 小程序code
|
* @param code 小程序code
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private Map<String, Object> getWxOpenId(String code) {
|
private Map<String, Object> getWxSessionResult(String code) {
|
||||||
|
|
||||||
if (code == null || code.isEmpty()) {
|
if (code == null || code.isEmpty()) {
|
||||||
throw new CustomOAuth2AuthenticationException("Code cannot be null or empty");
|
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:
|
spring:
|
||||||
application:
|
application:
|
||||||
name: uaa-server
|
name: uaa-server
|
||||||
# cloud:
|
cloud:
|
||||||
# nacos:
|
nacos:
|
||||||
# discovery:
|
discovery:
|
||||||
# ip: 124.222.128.40
|
ip: 124.222.128.40
|
||||||
|
|
||||||
encrypt:
|
encrypt:
|
||||||
key-store:
|
key-store:
|
||||||
|
|||||||
Reference in New Issue
Block a user