refactor: 获取用户认证凭证信息方法命名合理调整
This commit is contained in:
@@ -5,7 +5,7 @@ import cn.hutool.core.util.StrUtil;
|
|||||||
import com.youlai.boot.common.constant.RedisConstants;
|
import com.youlai.boot.common.constant.RedisConstants;
|
||||||
import com.youlai.boot.core.security.exception.CaptchaValidationException;
|
import com.youlai.boot.core.security.exception.CaptchaValidationException;
|
||||||
import com.youlai.boot.core.security.model.SysUserDetails;
|
import com.youlai.boot.core.security.model.SysUserDetails;
|
||||||
import com.youlai.boot.core.security.model.AuthCredentials;
|
import com.youlai.boot.core.security.model.UserAuthCredentials;
|
||||||
import com.youlai.boot.system.service.UserService;
|
import com.youlai.boot.system.service.UserService;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
@@ -49,14 +49,14 @@ public class SmsAuthenticationProvider implements AuthenticationProvider {
|
|||||||
String inputVerifyCode = (String) authentication.getCredentials();
|
String inputVerifyCode = (String) authentication.getCredentials();
|
||||||
|
|
||||||
// 根据手机号获取用户信息
|
// 根据手机号获取用户信息
|
||||||
AuthCredentials authCredentials = userService.getAuthCredentialsByMobile(mobile);
|
UserAuthCredentials userAuthCredentials = userService.getAuthCredentialsByMobile(mobile);
|
||||||
|
|
||||||
if (authCredentials == null) {
|
if (userAuthCredentials == null) {
|
||||||
throw new UsernameNotFoundException("用户不存在");
|
throw new UsernameNotFoundException("用户不存在");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查用户状态是否有效
|
// 检查用户状态是否有效
|
||||||
if (ObjectUtil.notEqual(authCredentials.getStatus(), 1)) {
|
if (ObjectUtil.notEqual(userAuthCredentials.getStatus(), 1)) {
|
||||||
throw new DisabledException("用户已被禁用");
|
throw new DisabledException("用户已被禁用");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,7 +72,7 @@ public class SmsAuthenticationProvider implements AuthenticationProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 构建认证后的用户详情信息
|
// 构建认证后的用户详情信息
|
||||||
SysUserDetails userDetails = new SysUserDetails(authCredentials);
|
SysUserDetails userDetails = new SysUserDetails(userAuthCredentials);
|
||||||
|
|
||||||
// 创建已认证的 SmsAuthenticationToken
|
// 创建已认证的 SmsAuthenticationToken
|
||||||
return SmsAuthenticationToken.authenticated(
|
return SmsAuthenticationToken.authenticated(
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
|
|||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.youlai.boot.core.security.model.SysUserDetails;
|
import com.youlai.boot.core.security.model.SysUserDetails;
|
||||||
import com.youlai.boot.core.security.model.AuthCredentials;
|
import com.youlai.boot.core.security.model.UserAuthCredentials;
|
||||||
import com.youlai.boot.system.service.UserService;
|
import com.youlai.boot.system.service.UserService;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import me.chanjar.weixin.common.error.WxErrorException;
|
import me.chanjar.weixin.common.error.WxErrorException;
|
||||||
@@ -63,27 +63,27 @@ public class WechatAuthenticationProvider implements AuthenticationProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 根据微信 OpenID 查询用户信息
|
// 根据微信 OpenID 查询用户信息
|
||||||
AuthCredentials authCredentials = userService.getAuthCredentialsByOpenId(openId);
|
UserAuthCredentials userAuthCredentials = userService.getAuthCredentialsByOpenId(openId);
|
||||||
|
|
||||||
if (authCredentials == null) {
|
if (userAuthCredentials == null) {
|
||||||
// TODO: 用户不存在则注册,这里需要获取用户手机号并与现有用户绑定
|
// TODO: 用户不存在则注册,这里需要获取用户手机号并与现有用户绑定
|
||||||
userService.registerOrBindWechatUser(openId);
|
userService.registerOrBindWechatUser(openId);
|
||||||
|
|
||||||
// 再次查询用户信息,确保用户注册成功
|
// 再次查询用户信息,确保用户注册成功
|
||||||
authCredentials = userService.getAuthCredentialsByOpenId(openId);
|
userAuthCredentials = userService.getAuthCredentialsByOpenId(openId);
|
||||||
if (authCredentials == null) {
|
if (userAuthCredentials == null) {
|
||||||
throw new UsernameNotFoundException("用户注册失败,请稍后重试");
|
throw new UsernameNotFoundException("用户注册失败,请稍后重试");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查用户状态是否有效
|
// 检查用户状态是否有效
|
||||||
if (ObjectUtil.notEqual(authCredentials.getStatus(), 1)) {
|
if (ObjectUtil.notEqual(userAuthCredentials.getStatus(), 1)) {
|
||||||
throw new DisabledException("用户已被禁用");
|
throw new DisabledException("用户已被禁用");
|
||||||
}
|
}
|
||||||
// 这里因为已经根据 code 从微信小程序获取到 openid 不需要再经过系统认证,所以直接生成
|
// 这里因为已经根据 code 从微信小程序获取到 openid 不需要再经过系统认证,所以直接生成
|
||||||
|
|
||||||
// 构建认证后的用户详情信息
|
// 构建认证后的用户详情信息
|
||||||
SysUserDetails userDetails = new SysUserDetails(authCredentials);
|
SysUserDetails userDetails = new SysUserDetails(userAuthCredentials);
|
||||||
|
|
||||||
// 创建已认证的 WeChatAuthenticationToken
|
// 创建已认证的 WeChatAuthenticationToken
|
||||||
return WechatAuthenticationToken.authenticated(
|
return WechatAuthenticationToken.authenticated(
|
||||||
|
|||||||
@@ -64,9 +64,9 @@ public class SysUserDetails implements UserDetails {
|
|||||||
/**
|
/**
|
||||||
* 构造函数:根据用户认证信息初始化用户详情对象
|
* 构造函数:根据用户认证信息初始化用户详情对象
|
||||||
*
|
*
|
||||||
* @param user 用户认证信息对象 {@link AuthCredentials}
|
* @param user 用户认证信息对象 {@link UserAuthCredentials}
|
||||||
*/
|
*/
|
||||||
public SysUserDetails(AuthCredentials user) {
|
public SysUserDetails(UserAuthCredentials user) {
|
||||||
this.userId = user.getUserId();
|
this.userId = user.getUserId();
|
||||||
this.username = user.getUsername();
|
this.username = user.getUsername();
|
||||||
this.password = user.getPassword();
|
this.password = user.getPassword();
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import java.util.Set;
|
|||||||
* @since 2022/10/22
|
* @since 2022/10/22
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class AuthCredentials {
|
public class UserAuthCredentials {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户ID
|
* 用户ID
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package com.youlai.boot.core.security.service;
|
package com.youlai.boot.core.security.service;
|
||||||
|
|
||||||
import com.youlai.boot.core.security.model.SysUserDetails;
|
import com.youlai.boot.core.security.model.SysUserDetails;
|
||||||
import com.youlai.boot.core.security.model.AuthCredentials;
|
import com.youlai.boot.core.security.model.UserAuthCredentials;
|
||||||
import com.youlai.boot.system.service.UserService;
|
import com.youlai.boot.system.service.UserService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -33,11 +33,11 @@ public class SysUserDetailsService implements UserDetailsService {
|
|||||||
@Override
|
@Override
|
||||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||||
try {
|
try {
|
||||||
AuthCredentials authCredentials = userService.getAuthCredentialsByUsername(username);
|
UserAuthCredentials userAuthCredentials = userService.getAuthCredentialsByUsername(username);
|
||||||
if (authCredentials == null) {
|
if (userAuthCredentials == null) {
|
||||||
throw new UsernameNotFoundException(username);
|
throw new UsernameNotFoundException(username);
|
||||||
}
|
}
|
||||||
return new SysUserDetails(authCredentials);
|
return new SysUserDetails(userAuthCredentials);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// 记录异常日志
|
// 记录异常日志
|
||||||
log.error("认证异常:{}", e.getMessage());
|
log.error("认证异常:{}", e.getMessage());
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package com.youlai.boot.system.service;
|
|||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
import com.youlai.boot.common.model.Option;
|
import com.youlai.boot.common.model.Option;
|
||||||
import com.youlai.boot.core.security.model.AuthCredentials;
|
import com.youlai.boot.core.security.model.UserAuthCredentials;
|
||||||
import com.youlai.boot.system.model.dto.CurrentUserDTO;
|
import com.youlai.boot.system.model.dto.CurrentUserDTO;
|
||||||
import com.youlai.boot.system.model.dto.UserExportDTO;
|
import com.youlai.boot.system.model.dto.UserExportDTO;
|
||||||
import com.youlai.boot.system.model.entity.User;
|
import com.youlai.boot.system.model.entity.User;
|
||||||
@@ -69,10 +69,10 @@ public interface UserService extends IService<User> {
|
|||||||
* 根据用户名获取认证信息
|
* 根据用户名获取认证信息
|
||||||
*
|
*
|
||||||
* @param username 用户名
|
* @param username 用户名
|
||||||
* @return {@link AuthCredentials}
|
* @return {@link UserAuthCredentials}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
AuthCredentials getAuthCredentialsByUsername(String username);
|
UserAuthCredentials getAuthCredentialsByUsername(String username);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -166,10 +166,10 @@ public interface UserService extends IService<User> {
|
|||||||
* 根据 openid 获取用户认证信息
|
* 根据 openid 获取用户认证信息
|
||||||
*
|
*
|
||||||
* @param username 用户名
|
* @param username 用户名
|
||||||
* @return {@link AuthCredentials}
|
* @return {@link UserAuthCredentials}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
AuthCredentials getAuthCredentialsByOpenId(String username);
|
UserAuthCredentials getAuthCredentialsByOpenId(String username);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据微信 OpenID 注册或绑定用户
|
* 根据微信 OpenID 注册或绑定用户
|
||||||
@@ -182,9 +182,9 @@ public interface UserService extends IService<User> {
|
|||||||
* 根据手机号获取用户认证信息
|
* 根据手机号获取用户认证信息
|
||||||
*
|
*
|
||||||
* @param mobile 手机号
|
* @param mobile 手机号
|
||||||
* @return {@link AuthCredentials}
|
* @return {@link UserAuthCredentials}
|
||||||
*/
|
*/
|
||||||
AuthCredentials getAuthCredentialsByMobile(String mobile);
|
UserAuthCredentials getAuthCredentialsByMobile(String mobile);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ import com.youlai.boot.system.converter.UserConverter;
|
|||||||
import com.youlai.boot.system.enums.DictCodeEnum;
|
import com.youlai.boot.system.enums.DictCodeEnum;
|
||||||
import com.youlai.boot.system.mapper.UserMapper;
|
import com.youlai.boot.system.mapper.UserMapper;
|
||||||
import com.youlai.boot.system.model.bo.UserBO;
|
import com.youlai.boot.system.model.bo.UserBO;
|
||||||
import com.youlai.boot.core.security.model.AuthCredentials;
|
import com.youlai.boot.core.security.model.UserAuthCredentials;
|
||||||
import com.youlai.boot.system.model.dto.CurrentUserDTO;
|
import com.youlai.boot.system.model.dto.CurrentUserDTO;
|
||||||
import com.youlai.boot.system.model.dto.UserExportDTO;
|
import com.youlai.boot.system.model.dto.UserExportDTO;
|
||||||
import com.youlai.boot.system.model.entity.DictItem;
|
import com.youlai.boot.system.model.entity.DictItem;
|
||||||
@@ -189,57 +189,57 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据用户名获取认证信息
|
* 根据用户名获取认证凭证信息
|
||||||
*
|
*
|
||||||
* @param username 用户名
|
* @param username 用户名
|
||||||
* @return 用户认证信息 {@link AuthCredentials}
|
* @return 用户认证凭证信息 {@link UserAuthCredentials}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public AuthCredentials getAuthCredentialsByUsername(String username) {
|
public UserAuthCredentials getAuthCredentialsByUsername(String username) {
|
||||||
AuthCredentials authCredentials = this.baseMapper.getAuthCredentialsByUsername(username);
|
UserAuthCredentials userAuthCredentials = this.baseMapper.getAuthCredentialsByUsername(username);
|
||||||
if (authCredentials != null) {
|
if (userAuthCredentials != null) {
|
||||||
Set<String> roles = authCredentials.getRoles();
|
Set<String> roles = userAuthCredentials.getRoles();
|
||||||
// 获取最大范围的数据权限
|
// 获取最大范围的数据权限
|
||||||
Integer dataScope = roleService.getMaximumDataScope(roles);
|
Integer dataScope = roleService.getMaximumDataScope(roles);
|
||||||
authCredentials.setDataScope(dataScope);
|
userAuthCredentials.setDataScope(dataScope);
|
||||||
}
|
}
|
||||||
return authCredentials;
|
return userAuthCredentials;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据 openid 获取用户认证信息
|
* 根据 openid 获取用户认证信息
|
||||||
*
|
*
|
||||||
* @param openid 微信
|
* @param openid 微信 OpenId
|
||||||
* @return {@link AuthCredentials}
|
* @return {@link UserAuthCredentials}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public AuthCredentials getAuthCredentialsByOpenId(String openid) {
|
public UserAuthCredentials getAuthCredentialsByOpenId(String openid) {
|
||||||
AuthCredentials authCredentials = this.baseMapper.getAuthCredentialsByOpenId(openid);
|
UserAuthCredentials userAuthCredentials = this.baseMapper.getAuthCredentialsByOpenId(openid);
|
||||||
if (authCredentials != null) {
|
if (userAuthCredentials != null) {
|
||||||
Set<String> roles = authCredentials.getRoles();
|
Set<String> roles = userAuthCredentials.getRoles();
|
||||||
// 获取最大范围的数据权限
|
// 获取最大范围的数据权限
|
||||||
Integer dataScope = roleService.getMaximumDataScope(roles);
|
Integer dataScope = roleService.getMaximumDataScope(roles);
|
||||||
authCredentials.setDataScope(dataScope);
|
userAuthCredentials.setDataScope(dataScope);
|
||||||
}
|
}
|
||||||
return authCredentials;
|
return userAuthCredentials;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据手机号获取用户认证信息
|
* 根据手机号获取用户认证凭证信息
|
||||||
*
|
*
|
||||||
* @param mobile 手机号
|
* @param mobile 手机号
|
||||||
* @return {@link AuthCredentials}
|
* @return {@link UserAuthCredentials}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public AuthCredentials getAuthCredentialsByMobile(String mobile) {
|
public UserAuthCredentials getAuthCredentialsByMobile(String mobile) {
|
||||||
AuthCredentials authCredentials = this.baseMapper.getAuthCredentialsByMobile(mobile);
|
UserAuthCredentials userAuthCredentials = this.baseMapper.getAuthCredentialsByMobile(mobile);
|
||||||
if (authCredentials != null) {
|
if (userAuthCredentials != null) {
|
||||||
Set<String> roles = authCredentials.getRoles();
|
Set<String> roles = userAuthCredentials.getRoles();
|
||||||
// 获取最大范围的数据权限
|
// 获取最大范围的数据权限
|
||||||
Integer dataScope = roleService.getMaximumDataScope(roles);
|
Integer dataScope = roleService.getMaximumDataScope(roles);
|
||||||
authCredentials.setDataScope(dataScope);
|
userAuthCredentials.setDataScope(dataScope);
|
||||||
}
|
}
|
||||||
return authCredentials;
|
return userAuthCredentials;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -286,7 +286,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
|||||||
|
|
||||||
List<UserExportDTO> exportUsers = this.baseMapper.listExportUsers(queryParams);
|
List<UserExportDTO> exportUsers = this.baseMapper.listExportUsers(queryParams);
|
||||||
if (CollectionUtil.isNotEmpty(exportUsers)) {
|
if (CollectionUtil.isNotEmpty(exportUsers)) {
|
||||||
//获取角色的字典数据
|
//获取性别的字典项
|
||||||
Map<String, String> genderMap = dictItemService.list(
|
Map<String, String> genderMap = dictItemService.list(
|
||||||
new LambdaQueryWrapper<DictItem>().eq(DictItem::getDictCode,
|
new LambdaQueryWrapper<DictItem>().eq(DictItem::getDictCode,
|
||||||
DictCodeEnum.GENDER.getValue())
|
DictCodeEnum.GENDER.getValue())
|
||||||
@@ -372,7 +372,6 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
|||||||
return this.updateById(entity);
|
return this.updateById(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 修改用户密码
|
* 修改用户密码
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -119,7 +119,7 @@
|
|||||||
</select>
|
</select>
|
||||||
|
|
||||||
<!-- 用户认证信息映射 -->
|
<!-- 用户认证信息映射 -->
|
||||||
<resultMap id="AuthCredentialsMap" type="com.youlai.boot.core.security.model.AuthCredentials">
|
<resultMap id="AuthCredentialsMap" type="com.youlai.boot.core.security.model.UserAuthCredentials">
|
||||||
<id property="userId" column="userId" jdbcType="BIGINT"/>
|
<id property="userId" column="userId" jdbcType="BIGINT"/>
|
||||||
<result property="username" column="username" jdbcType="VARCHAR"/>
|
<result property="username" column="username" jdbcType="VARCHAR"/>
|
||||||
<result property="password" column="password" jdbcType="VARCHAR"/>
|
<result property="password" column="password" jdbcType="VARCHAR"/>
|
||||||
|
|||||||
Reference in New Issue
Block a user