feat: 包结构调整,整合ip2region解析ip的所在区域

This commit is contained in:
Ray.Hao
2024-06-27 14:47:50 +08:00
parent a0530dc380
commit d13adf233d
32 changed files with 90 additions and 42 deletions

View File

@@ -1,27 +0,0 @@
package com.youlai.system.common.enums;
/**
* EasyCaptcha 验证码类型枚举
*
* @author haoxr
* @since 2.5.1
*/
public enum CaptchaTypeEnum {
/**
* 圆圈干扰验证码
*/
CIRCLE,
/**
* GIF验证码
*/
GIF,
/**
* 干扰线验证码
*/
LINE,
/**
* 扭曲干扰验证码
*/
SHEAR
}

View File

@@ -1,31 +0,0 @@
package com.youlai.system.common.enums;
import com.youlai.system.common.base.IBaseEnum;
import lombok.Getter;
/**
* 数据权限枚举
*
* @author haoxr
* @since 2.3.0
*/
@Getter
public enum DataScopeEnum implements IBaseEnum<Integer> {
/**
* value 越小,数据权限范围越大
*/
ALL(0, "所有数据"),
DEPT_AND_SUB(1, "部门及子部门数据"),
DEPT(2, "本部门数据"),
SELF(3, "本人数据");
private final Integer value;
private final String label;
DataScopeEnum(Integer value, String label) {
this.value = value;
this.label = label;
}
}

View File

@@ -1,28 +0,0 @@
package com.youlai.system.common.enums;
import com.youlai.system.common.base.IBaseEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
/**
* 性别枚举
*
* @author haoxr
* @since 2022/10/14
*/
@Getter
@Schema(enumAsRef = true)
public enum GenderEnum implements IBaseEnum<Integer> {
MALE(1, ""),
FEMALE (2, "");
private final Integer value;
private final String label;
GenderEnum(Integer value, String label) {
this.value = value;
this.label = label;
}
}

View File

@@ -1,28 +0,0 @@
package com.youlai.system.common.enums;
import com.youlai.system.common.base.IBaseEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
/**
* 日志类型枚举
*
* @author Ray
* @since 2.10.0
*/
@Schema(enumAsRef = true)
@Getter
public enum LogTypeEnum implements IBaseEnum<Integer> {
OPERATION(1, "操作日志"),
LOGIN (2, "登录日志");
private final Integer value;
private final String label;
LogTypeEnum(Integer value, String label) {
this.value = value;
this.label = label;
}
}

View File

@@ -1,35 +0,0 @@
package com.youlai.system.common.enums;
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.youlai.system.common.base.IBaseEnum;
import lombok.Getter;
/**
* 菜单类型枚举
*
* @author haoxr
* @since 2022/4/23 9:36
*/
public enum MenuTypeEnum implements IBaseEnum<Integer> {
NULL(0, null),
MENU(1, "菜单"),
CATALOG(2, "目录"),
EXTLINK(3, "外链"),
BUTTON(4, "按钮");
@Getter
@EnumValue // Mybatis-Plus 提供注解表示插入数据库时插入该值
private Integer value;
@Getter
// @JsonValue // 表示对枚举序列化时返回此字段
private String label;
MenuTypeEnum(Integer value, String label) {
this.value = value;
this.label = label;
}
}

View File

@@ -1,27 +0,0 @@
package com.youlai.system.common.enums;
import com.youlai.system.common.base.IBaseEnum;
import lombok.Getter;
/**
* 状态枚举
*
* @author haoxr
* @since 2022/10/14
*/
public enum StatusEnum implements IBaseEnum<Integer> {
ENABLE(1, "启用"),
DISABLE (0, "禁用");
@Getter
private Integer value;
@Getter
private String label;
StatusEnum(Integer value, String label) {
this.value = value;
this.label = label;
}
}

View File

@@ -1,38 +0,0 @@
package com.youlai.system.common.exception;
import com.youlai.system.common.result.IResultCode;
import lombok.Getter;
import org.slf4j.helpers.MessageFormatter;
/**
* 自定义业务异常
*
* @author Ray
* @since 2022/7/31
*/
@Getter
public class BusinessException extends RuntimeException {
public IResultCode resultCode;
public BusinessException(IResultCode errorCode) {
super(errorCode.getMsg());
this.resultCode = errorCode;
}
public BusinessException(String message, Throwable cause) {
super(message, cause);
}
public BusinessException(Throwable cause) {
super(cause);
}
public BusinessException(String message, Object... args) {
super(formatMessage(message, args));
}
private static String formatMessage(String message, Object... args) {
return MessageFormatter.arrayFormat(message, args).getMessage();
}
}

View File

@@ -1,219 +0,0 @@
package com.youlai.system.common.exception;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.youlai.system.common.result.Result;
import com.youlai.system.common.result.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.TypeMismatchException;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.NoHandlerFoundException;
import jakarta.servlet.ServletException;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import java.sql.SQLSyntaxErrorException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* 全局系统异常处理器
* <p>
* 调整异常处理的HTTP状态码丰富异常处理类型
*
* @author Gadfly
* @since 2020-02-25 13:54
**/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(BindException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public <T> Result<T> processException(BindException e) {
log.error("BindException:{}", e.getMessage());
String msg = e.getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining(""));
return Result.failed(ResultCode.PARAM_ERROR, msg);
}
/**
* RequestParam参数的校验
*
* @param e
* @param <T>
* @return
*/
@ExceptionHandler(ConstraintViolationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public <T> Result<T> processException(ConstraintViolationException e) {
log.error("ConstraintViolationException:{}", e.getMessage());
String msg = e.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.joining(""));
return Result.failed(ResultCode.PARAM_ERROR, msg);
}
/**
* RequestBody参数的校验
*
* @param e
* @param <T>
* @return
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public <T> Result<T> processException(MethodArgumentNotValidException e) {
log.error("MethodArgumentNotValidException:{}", e.getMessage());
String msg = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining(""));
return Result.failed(ResultCode.PARAM_ERROR, msg);
}
@ExceptionHandler(NoHandlerFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public <T> Result<T> processException(NoHandlerFoundException e) {
log.error(e.getMessage(), e);
return Result.failed(ResultCode.RESOURCE_NOT_FOUND);
}
/**
* MissingServletRequestParameterException
*/
@ExceptionHandler(MissingServletRequestParameterException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public <T> Result<T> processException(MissingServletRequestParameterException e) {
log.error(e.getMessage(), e);
return Result.failed(ResultCode.PARAM_IS_NULL);
}
/**
* MethodArgumentTypeMismatchException
*/
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public <T> Result<T> processException(MethodArgumentTypeMismatchException e) {
log.error(e.getMessage(), e);
return Result.failed(ResultCode.PARAM_ERROR, "类型错误");
}
/**
* ServletException
*/
@ExceptionHandler(ServletException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public <T> Result<T> processException(ServletException e) {
log.error(e.getMessage(), e);
return Result.failed(e.getMessage());
}
@ExceptionHandler(IllegalArgumentException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public <T> Result<T> handleIllegalArgumentException(IllegalArgumentException e) {
log.error("非法参数异常,异常原因:{}", e.getMessage(), e);
return Result.failed(e.getMessage());
}
@ExceptionHandler(JsonProcessingException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public <T> Result<T> handleJsonProcessingException(JsonProcessingException e) {
log.error("Json转换异常异常原因{}", e.getMessage(), e);
return Result.failed(e.getMessage());
}
/**
* HttpMessageNotReadableException
*/
@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public <T> Result<T> processException(HttpMessageNotReadableException e) {
log.error(e.getMessage(), e);
String errorMessage = "请求体不可为空";
Throwable cause = e.getCause();
if (cause != null) {
errorMessage = convertMessage(cause);
}
return Result.failed(errorMessage);
}
@ExceptionHandler(TypeMismatchException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public <T> Result<T> processException(TypeMismatchException e) {
log.error(e.getMessage(), e);
return Result.failed(e.getMessage());
}
@ExceptionHandler(BadSqlGrammarException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
public <T> Result<T> handleBadSqlGrammarException(BadSqlGrammarException e) {
log.error(e.getMessage(), e);
String errorMsg = e.getMessage();
if (StrUtil.isNotBlank(errorMsg) && errorMsg.contains("denied to user")) {
return Result.failed(ResultCode.FORBIDDEN_OPERATION);
} else {
return Result.failed(e.getMessage());
}
}
@ExceptionHandler(SQLSyntaxErrorException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
public <T> Result<T> processSQLSyntaxErrorException(SQLSyntaxErrorException e) {
log.error(e.getMessage(), e);
return Result.failed(e.getMessage());
}
@ExceptionHandler(BusinessException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public <T> Result<T> handleBizException(BusinessException e) {
log.error("biz exception: {}", e.getMessage());
if (e.getResultCode() != null) {
return Result.failed(e.getResultCode());
}
return Result.failed(e.getMessage());
}
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public <T> Result<T> handleException(Exception e) throws Exception{
// 将 Spring Security 异常继续抛出,以便交给自定义处理器处理
if (e instanceof AccessDeniedException
|| e instanceof AuthenticationException) {
throw e;
}
log.error("unknown exception: {}", e.getMessage());
e.printStackTrace();
return Result.failed(e.getLocalizedMessage());
}
/**
* 传参类型错误时,用于消息转换
*
* @param throwable 异常
* @return 错误信息
*/
private String convertMessage(Throwable throwable) {
String error = throwable.toString();
String regulation = "\\[\"(.*?)\"]+";
Pattern pattern = Pattern.compile(regulation);
Matcher matcher = pattern.matcher(error);
String group = "";
if (matcher.find()) {
String matchString = matcher.group();
matchString = matchString.replace("[", "").replace("]", "");
matchString = "%s字段类型错误".formatted(matchString.replaceAll("\"", ""));
group += matchString;
}
return group;
}
}

View File

@@ -1,7 +1,10 @@
package com.youlai.system.common.result;
/**
* @author haoxr
* 响应码接口
*
* @author Ray
* @since 2022/2/18
**/
public interface IResultCode {

View File

@@ -9,8 +9,8 @@ import java.util.List;
/**
* 分页响应结构体
*
* @author haoxr
* @since 2022/2/18 23:29
* @author Ray
* @since 2022/2/18
*/
@Data
public class PageResult<T> implements Serializable {

View File

@@ -7,7 +7,7 @@ import java.io.Serializable;
/**
* 统一响应结构体
*
* @author haoxr
* @author Ray
* @since 2022/1/30
**/
@Data

View File

@@ -7,9 +7,11 @@ import java.io.Serializable;
/**
* 响应码枚举
* <p>
* 参考阿里巴巴开发手册响应码规范
*
* @author haoxr
* @since 2020-06-23
* @author Ray
* @since 2020/6/23
**/
@AllArgsConstructor
@NoArgsConstructor
@@ -103,7 +105,7 @@ public enum ResultCode implements IResultCode, Serializable {
}
public static ResultCode getValue(String code){
public static ResultCode getValue(String code) {
for (ResultCode value : values()) {
if (value.getCode().equals(code)) {
return value;

View File

@@ -3,12 +3,20 @@ package com.youlai.system.common.util;
import cn.hutool.core.util.StrUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.lionsoul.ip2region.xdb.Searcher;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* IP工具类
* IP工具类
* <p>
* 获取客户端IP地址和IP地址对应的地理位置信息
* <p>
* 使用Nginx等反向代理软件 则不能通过request.getRemoteAddr()获取IP地址
* 如果使用了多级反向代理的话X-Forwarded-For的值并不止一个而是一串IP地址X-Forwarded-For中第一个非unknown的有效IP字符串则为真实IP地址
* </p>
*
* @author Ray
* @since 2.10.0
@@ -16,10 +24,13 @@ import java.net.UnknownHostException;
@Slf4j
public class IPUtils {
private static final String DB_PATH = "src/main/resources/data/ip2region.xdb";
/**
* 获取IP地址
* 使用Nginx等反向代理软件 则不能通过request.getRemoteAddr()获取IP地址
* 如果使用了多级反向代理的话X-Forwarded-For的值并不止一个而是一串IP地址X-Forwarded-For中第一个非unknown的有效IP字符串则为真实IP地址
*
* @param request HttpServletRequest对象
* @return 客户端IP地址
*/
public static String getIpAddr(HttpServletRequest request) {
String ip = null;
@@ -51,7 +62,7 @@ public class IPUtils {
log.error("IPUtils ERROR, {}", e.getMessage());
}
//使用代理则获取第一个IP地址
// 使用代理则获取第一个IP地址
if (StrUtil.isNotBlank(ip) && ip.indexOf(",") > 0) {
ip = ip.substring(0, ip.indexOf(","));
}
@@ -59,14 +70,15 @@ public class IPUtils {
return ip;
}
private static boolean checkIp(String ip) {
String unknown = "unknown";
return StrUtil.isEmpty(ip) || ip.isEmpty() || unknown.equalsIgnoreCase(ip);
return StrUtil.isEmpty(ip) || unknown.equalsIgnoreCase(ip);
}
/**
* 获取本机的IP地址
*
* @return 本机IP地址
*/
private static String getLocalAddr() {
try {
@@ -77,5 +89,28 @@ public class IPUtils {
return null;
}
/**
* 根据IP地址获取地理位置信息
*
* @param ip IP地址
* @return 地理位置信息
*/
public static String getRegion(String ip) {
Searcher searcher = null;
try {
searcher = Searcher.newWithFileOnly(DB_PATH);
return searcher.search(ip);
} catch (Exception e) {
log.error("IpRegionUtil ERROR, {}", e.getMessage());
return null;
} finally {
if (searcher != null) {
try {
searcher.close();
} catch (IOException e) {
log.error("IpRegionUtil close ERROR, {}", e.getMessage());
}
}
}
}
}