增加全局异常处理
parent
702b2dd7fe
commit
dc871d5baf
|
@ -77,18 +77,18 @@
|
|||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.33</version>
|
||||
</dependency>
|
||||
<!-- Mybatis-plus 配置 -->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<version>3.5.4.1</version>
|
||||
</dependency>
|
||||
<!-- Mybatis 依赖配置 -->
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>2.2.2</version>
|
||||
</dependency>
|
||||
<!-- Pagehelper -->
|
||||
<dependency>
|
||||
<groupId>com.github.pagehelper</groupId>
|
||||
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
||||
<version>1.4.1</version>
|
||||
</dependency>
|
||||
<!-- Hibernate Validator -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
@ -104,6 +104,13 @@
|
|||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringBoot 测试框架 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
package com.muyu.common.config;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: Mybatis-Plus配置
|
||||
* @Date 2024-1-10 下午 05:12
|
||||
*/
|
||||
@Configuration
|
||||
public class MybatisPlusConfig {
|
||||
|
||||
public MybatisPlusConfig () {
|
||||
System.out.println("初始化-----------");
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加分页插件
|
||||
*/
|
||||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
|
||||
return interceptor;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package com.muyu.common.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.muyu.common.domain.request.user.UserAddReq;
|
||||
import com.muyu.common.utils.IdUtils;
|
||||
import com.muyu.common.utils.SecurityUtils;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 用户信息
|
||||
* @Date 2024-1-10 下午 03:35
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@TableName(value = "user_info")
|
||||
public class UserInfo {
|
||||
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
@TableField(value = "user_name")
|
||||
private String userName;
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
private String password;
|
||||
/**
|
||||
* 盐
|
||||
*/
|
||||
private String salt;
|
||||
|
||||
public static UserInfo addBuild (UserAddReq userAddReq) {
|
||||
String salt = IdUtils.genId();
|
||||
String passwordSalt = SecurityUtils.encryptPassword(userAddReq.getPassword(), salt);
|
||||
return UserInfo.builder()
|
||||
.userName(userAddReq.getUserName())
|
||||
.password(passwordSalt)
|
||||
.salt(salt)
|
||||
.build();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.muyu.common.domain.request.user;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Pattern;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 用户添加请求对象
|
||||
* @Date 2024-1-10 下午 08:02
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class UserAddReq {
|
||||
|
||||
@NotEmpty(message = "用户名称不可为空")
|
||||
@Pattern(regexp = "(?=.*[A-Za-z]\\d).{6,16}$", message = "用户名称不合法,因为6-16位的大小写英文和数字组成")
|
||||
private String userName;
|
||||
|
||||
@NotEmpty(message = "用户密码不可为空")
|
||||
@Pattern(regexp = "(?=.*[A-Za-z]\\d).{8,16}$", message = "用户密码不合法,因为8-16位的大小写英文和数字组成")
|
||||
private String password;
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.muyu.common.handler;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.muyu.common.result.Result;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.validation.ObjectError;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 全局异常处理
|
||||
* @Date 2024-1-10 下午 08:49
|
||||
*/
|
||||
@RestControllerAdvice
|
||||
@Configuration
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(value = MethodArgumentNotValidException.class)
|
||||
public Result<String> runtimeException (MethodArgumentNotValidException exception) {
|
||||
return Result.error(
|
||||
JSONObject.toJSONString(
|
||||
exception.getBindingResult().getAllErrors()
|
||||
.stream()
|
||||
.map(ObjectError::getDefaultMessage)
|
||||
.toList()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.muyu.common.utils;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: ID生成工具类
|
||||
* @Date 2024-1-10 下午 08:26
|
||||
*/
|
||||
public class IdUtils {
|
||||
|
||||
/**
|
||||
* 生成UUID
|
||||
* @return UUID
|
||||
*/
|
||||
public static String genId(){
|
||||
return UUID.randomUUID().toString().replace("-", "");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package com.muyu.common.utils;
|
||||
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Base64;
|
||||
|
||||
/**
|
||||
* 安全服务工具类
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class SecurityUtils {
|
||||
|
||||
|
||||
/**
|
||||
* 生成BCryptPasswordEncoder密码
|
||||
*
|
||||
* @param password 密码
|
||||
*
|
||||
* @return 加密字符串
|
||||
*/
|
||||
public static String encryptPassword (String password, String salt) {
|
||||
return encryptMD5(password, salt);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断密码是否相同
|
||||
*
|
||||
* @param rawPassword 真实密码
|
||||
* @param encodedPassword 加密后字符
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
public static boolean matchesPassword (String rawPassword, String salt, String encodedPassword) {
|
||||
return encryptMD5(rawPassword, salt).equals(encodedPassword);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算字符串的MD5加密值,并返回Base64编码的字符串。
|
||||
* @param password 要加密的字符串
|
||||
* @return 加密后的Base64编码字符串
|
||||
*/
|
||||
public static String encryptMD5(String password, String salt) {
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
md.update((password + salt).getBytes()); // 加盐处理
|
||||
byte[] digest = md.digest();
|
||||
return Base64.getEncoder().encodeToString(digest);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,9 +1,13 @@
|
|||
package com.muyu.system;
|
||||
|
||||
import com.muyu.common.config.MybatisPlusConfig;
|
||||
import com.muyu.common.handler.GlobalExceptionHandler;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
|
@ -12,6 +16,8 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
|
|||
@SpringBootApplication
|
||||
@EnableFeignClients( basePackages = {"com.muyu.**"})
|
||||
@EnableDiscoveryClient
|
||||
@MapperScan("com.muyu.system.mapper")
|
||||
@Import({MybatisPlusConfig.class, GlobalExceptionHandler.class})
|
||||
public class SystemApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SystemApplication.class);
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
package com.muyu.system.controller;
|
||||
|
||||
import com.muyu.common.domain.UserInfo;
|
||||
import com.muyu.common.domain.request.user.UserAddReq;
|
||||
import com.muyu.common.result.Result;
|
||||
import com.muyu.system.service.UserInfoService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 用户控制层
|
||||
* @Date 2024-1-10 下午 07:57
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
public class UserInfoController {
|
||||
|
||||
@Autowired
|
||||
private UserInfoService userInfoService;
|
||||
|
||||
/**
|
||||
* 添加用户
|
||||
* @return
|
||||
*/
|
||||
@PostMapping()
|
||||
public Result<String> add(@RequestBody @Validated UserAddReq userAddReq){
|
||||
userInfoService.save(
|
||||
UserInfo.addBuild(userAddReq)
|
||||
);
|
||||
return Result.success();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.muyu.system.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.muyu.common.domain.UserInfo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 用户信息plus持久层
|
||||
* @Date 2024-1-10 下午 04:57
|
||||
*/
|
||||
public interface UserInfoMapper extends BaseMapper<UserInfo> {
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.muyu.system.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.muyu.common.domain.UserInfo;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 用户业务层
|
||||
* @Date 2024-1-10 下午 07:41
|
||||
*/
|
||||
public interface UserInfoService extends IService<UserInfo> {
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.muyu.system.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.muyu.common.domain.UserInfo;
|
||||
import com.muyu.system.mapper.UserInfoMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 用户业务实现类
|
||||
* @Date 2024-1-10 下午 07:42
|
||||
*/
|
||||
@Service
|
||||
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
|
||||
implements UserInfoService {
|
||||
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.muyu.system.mapper.UserInfoMapper">
|
||||
|
||||
</mapper>
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
package com.muyu.test;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.muyu.common.domain.UserInfo;
|
||||
import com.muyu.system.SystemApplication;
|
||||
import com.muyu.system.mapper.UserInfoMapper;
|
||||
import com.muyu.system.service.UserInfoService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 用户测试
|
||||
* @Date 2024-1-10 下午 03:40
|
||||
*/
|
||||
@SpringBootTest(classes = SystemApplication.class)
|
||||
public class UserInfoTest {
|
||||
|
||||
@Autowired
|
||||
private UserInfoMapper userInfoMapper;
|
||||
|
||||
@Autowired
|
||||
private UserInfoService userInfoService;
|
||||
|
||||
@Test
|
||||
public void userInfoSave(){
|
||||
userInfoMapper.insert(UserInfo.builder()
|
||||
.userName("张三5")
|
||||
.password("123456")
|
||||
.salt("1245")
|
||||
.build());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void userInfoListTest(){
|
||||
IPage<UserInfo> page = new Page<>(1,2);
|
||||
LambdaQueryWrapper<UserInfo> userInfoQuery = new LambdaQueryWrapper<>();
|
||||
userInfoQuery.like(UserInfo::getUserName, "张三");
|
||||
userInfoQuery.gt(UserInfo::getId, 5);
|
||||
List<UserInfo> userInfoList = userInfoMapper.selectList(page, userInfoQuery);
|
||||
System.out.println(page.getTotal());
|
||||
System.out.println(userInfoList);
|
||||
}
|
||||
@Test
|
||||
public void userInfoGetOneTest(){
|
||||
// userInfoPlusMapper.selecoid
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue