✨ 设置自定义用户查询Service
This commit is contained in:
parent
219412c68c
commit
8b6dba3749
|
@ -0,0 +1,44 @@
|
||||||
|
package com.spring.step2.config;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||||
|
import com.spring.step2.context.BaseContext;
|
||||||
|
import org.apache.ibatis.reflection.MetaObject;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置MP在修改和新增时的操作
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class MyBatisPlusFieldConfig implements MetaObjectHandler {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用mp做添加操作时候,这个方法执行
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void insertFill(MetaObject metaObject) {
|
||||||
|
// 设置属性值
|
||||||
|
this.strictInsertFill(metaObject, "isDeleted", Integer.class, 0);
|
||||||
|
this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
|
||||||
|
this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
|
||||||
|
if (BaseContext.getUsername() != null) {
|
||||||
|
this.setFieldValByName("createUser", BaseContext.getUserId(), metaObject);
|
||||||
|
this.setFieldValByName("updateUser", BaseContext.getUserId(), metaObject);
|
||||||
|
} else {
|
||||||
|
this.setFieldValByName("createUser", 0L, metaObject);
|
||||||
|
this.setFieldValByName("updateUser", BaseContext.getUserId(), metaObject);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用mp做修改操作时候,这个方法执行
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void updateFill(MetaObject metaObject) {
|
||||||
|
if (BaseContext.getUserId() != null) {
|
||||||
|
this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
|
||||||
|
this.setFieldValByName("updateUser", BaseContext.getUserId(), metaObject);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package com.spring.step2.config;
|
package com.spring.step2.config.web;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonParser;
|
import com.fasterxml.jackson.core.JsonParser;
|
||||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.spring.step2.config.web;
|
||||||
|
|
||||||
|
import com.spring.step2.context.BaseContext;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class ThreadLocalCleanupInterceptor implements HandlerInterceptor {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||||
|
BaseContext.removeUser();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
package com.spring.step2.config.web;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class WebConfig implements WebMvcConfigurer {
|
||||||
|
|
||||||
|
private final ThreadLocalCleanupInterceptor threadLocalCleanupInterceptor;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
|
registry.addInterceptor(threadLocalCleanupInterceptor);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RestTemplate restTemplate() {
|
||||||
|
return new RestTemplate();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
package com.spring.step2.context;
|
||||||
|
|
||||||
|
|
||||||
|
public class BaseContext {
|
||||||
|
private static final ThreadLocal<Long> userId = new ThreadLocal<>();
|
||||||
|
private static final ThreadLocal<String> username = new ThreadLocal<>();
|
||||||
|
|
||||||
|
// 用户id相关
|
||||||
|
public static Long getUserId() {
|
||||||
|
return userId.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setUserId(Long _userId) {
|
||||||
|
userId.set(_userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getUsername() {
|
||||||
|
return username.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setUsername(String _username) {
|
||||||
|
username.set(_username);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void removeUser() {
|
||||||
|
username.remove();
|
||||||
|
userId.remove();
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,8 +6,6 @@ import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
|
@ -15,9 +13,6 @@ import java.time.LocalDateTime;
|
||||||
@Schema(name = "UserDTO对象", title = "用户", description = "用户的DTO对象")
|
@Schema(name = "UserDTO对象", title = "用户", description = "用户的DTO对象")
|
||||||
public class UserDto {
|
public class UserDto {
|
||||||
|
|
||||||
@Schema(name = "id", title = "主键")
|
|
||||||
private String id;
|
|
||||||
|
|
||||||
@Schema(name = "username", title = "用户名")
|
@Schema(name = "username", title = "用户名")
|
||||||
private String username;
|
private String username;
|
||||||
|
|
||||||
|
@ -27,19 +22,4 @@ public class UserDto {
|
||||||
@Schema(name = "email", title = "邮箱")
|
@Schema(name = "email", title = "邮箱")
|
||||||
private String email;
|
private String email;
|
||||||
|
|
||||||
@Schema(name = "createTime", title = "创建时间")
|
|
||||||
private LocalDateTime createTime;
|
|
||||||
|
|
||||||
@Schema(name = "updateTime", title = "更新时间")
|
|
||||||
private LocalDateTime updateTime;
|
|
||||||
|
|
||||||
@Schema(name = "createUser", title = "创建用户")
|
|
||||||
private Long createUser;
|
|
||||||
|
|
||||||
@Schema(name = "updateUser", title = "更新用户")
|
|
||||||
private Long updateUser;
|
|
||||||
|
|
||||||
@Schema(name = "isDeleted", title = "是否被删除")
|
|
||||||
private Boolean isDeleted;
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
package com.spring.step2.security.service;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
import com.spring.step2.domain.entity.UserEntity;
|
||||||
|
import com.spring.step2.mapper.UserMapper;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.security.core.userdetails.User;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||||
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class DbUserDetailService implements UserDetailsService {
|
||||||
|
|
||||||
|
private final UserMapper userMapper;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||||
|
// 查询当前用户
|
||||||
|
QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
|
||||||
|
wrapper.lambda().eq(UserEntity::getUsername, username);
|
||||||
|
UserEntity userEntity = userMapper.selectOne(wrapper);
|
||||||
|
|
||||||
|
// 判断当前用户是否存在
|
||||||
|
if (userEntity == null) {
|
||||||
|
throw new UsernameNotFoundException("用户不存在");
|
||||||
|
}
|
||||||
|
|
||||||
|
return User.builder().username(userEntity.getUsername())
|
||||||
|
.password(userEntity.getPassword())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,35 +1,27 @@
|
||||||
package com.spring.step2.security.service;
|
package com.spring.step2.security.service;
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
// @Service
|
||||||
import org.springframework.security.core.userdetails.User;
|
// @RequiredArgsConstructor
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
// public class InMemoryUserDetailsService implements UserDetailsService {
|
||||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
//
|
||||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
// private final PasswordEncoder passwordEncoder;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
//
|
||||||
import org.springframework.stereotype.Service;
|
// @Override
|
||||||
|
// public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||||
@Service
|
// // 1. 这里应该根据username从数据库或其他存储中查询用户信息
|
||||||
@RequiredArgsConstructor
|
// // 以下是模拟数据,实际应用中应从数据库查询
|
||||||
public class InMemoryUserDetailsService implements UserDetailsService {
|
//
|
||||||
|
// // 2. 如果用户不存在,抛出UsernameNotFoundException
|
||||||
private final PasswordEncoder passwordEncoder;
|
// if (!"bunny".equalsIgnoreCase(username)) {
|
||||||
|
// throw new UsernameNotFoundException("User not found: " + username);
|
||||||
@Override
|
// }
|
||||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
//
|
||||||
// 1. 这里应该根据username从数据库或其他存储中查询用户信息
|
// // 3. 构建UserDetails对象返回
|
||||||
// 以下是模拟数据,实际应用中应从数据库查询
|
// return User.builder()
|
||||||
|
// .username(username) // 使用传入的用户名
|
||||||
// 2. 如果用户不存在,抛出UsernameNotFoundException
|
// .password(passwordEncoder.encode("123456")) // 密码应该已经加密存储,这里仅为示例
|
||||||
if (!"bunny".equalsIgnoreCase(username)) {
|
// .roles("USER") // 角色会自动添加ROLE_前缀
|
||||||
throw new UsernameNotFoundException("User not found: " + username);
|
// .authorities("read", "write") // 添加具体权限
|
||||||
}
|
// .build();
|
||||||
|
// }
|
||||||
// 3. 构建UserDetails对象返回
|
// }
|
||||||
return User.builder()
|
|
||||||
.username(username) // 使用传入的用户名
|
|
||||||
.password(passwordEncoder.encode("123456")) // 密码应该已经加密存储,这里仅为示例
|
|
||||||
.roles("USER") // 角色会自动添加ROLE_前缀
|
|
||||||
.authorities("read", "write") // 添加具体权限
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -12,6 +12,7 @@ import com.spring.step2.mapper.UserMapper;
|
||||||
import com.spring.step2.service.UserService;
|
import com.spring.step2.service.UserService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
@ -31,6 +32,8 @@ import java.util.List;
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {
|
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {
|
||||||
|
|
||||||
|
private final PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* * 用户 服务实现类
|
* * 用户 服务实现类
|
||||||
*
|
*
|
||||||
|
@ -59,6 +62,12 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> impleme
|
||||||
public void addUser(UserDto dto) {
|
public void addUser(UserDto dto) {
|
||||||
UserEntity user = new UserEntity();
|
UserEntity user = new UserEntity();
|
||||||
BeanUtils.copyProperties(dto, user);
|
BeanUtils.copyProperties(dto, user);
|
||||||
|
|
||||||
|
// 设置用户密码
|
||||||
|
String password = user.getPassword();
|
||||||
|
String encodePassword = passwordEncoder.encode(password);
|
||||||
|
user.setPassword(encodePassword);
|
||||||
|
|
||||||
save(user);
|
save(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@ import com.spring.step2.mapper.UsersMapper;
|
||||||
import com.spring.step2.service.UsersService;
|
import com.spring.step2.service.UsersService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
@ -31,6 +32,8 @@ import java.util.List;
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class UsersServiceImpl extends ServiceImpl<UsersMapper, UsersEntity> implements UsersService {
|
public class UsersServiceImpl extends ServiceImpl<UsersMapper, UsersEntity> implements UsersService {
|
||||||
|
|
||||||
|
private final PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* * 服务实现类
|
* * 服务实现类
|
||||||
*
|
*
|
||||||
|
@ -57,9 +60,15 @@ public class UsersServiceImpl extends ServiceImpl<UsersMapper, UsersEntity> impl
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void addUsers(UsersDto dto) {
|
public void addUsers(UsersDto dto) {
|
||||||
UsersEntity users = new UsersEntity();
|
UsersEntity user = new UsersEntity();
|
||||||
BeanUtils.copyProperties(dto, users);
|
BeanUtils.copyProperties(dto, user);
|
||||||
save(users);
|
|
||||||
|
// 设置用户密码
|
||||||
|
String password = user.getPassword();
|
||||||
|
String encodePassword = passwordEncoder.encode(password);
|
||||||
|
user.setPassword(encodePassword);
|
||||||
|
|
||||||
|
save(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -27,9 +27,6 @@
|
||||||
from t_user
|
from t_user
|
||||||
<where>
|
<where>
|
||||||
is_deleted = 0
|
is_deleted = 0
|
||||||
<if test="dto.id != null and dto.id != ''">
|
|
||||||
and id like CONCAT('%',#{dto.id},'%')
|
|
||||||
</if>
|
|
||||||
<if test="dto.username != null and dto.username != ''">
|
<if test="dto.username != null and dto.username != ''">
|
||||||
and username like CONCAT('%',#{dto.username},'%')
|
and username like CONCAT('%',#{dto.username},'%')
|
||||||
</if>
|
</if>
|
||||||
|
|
Loading…
Reference in New Issue