This commit is contained in:
2023-07-13 17:30:09 +08:00
parent 672f66b40e
commit 6ed1224410
373 changed files with 566 additions and 409 deletions

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>qiaoba-common</artifactId>
<groupId>com.qiaoba</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>qiaoba-common-web</artifactId>
<description>通用-Web模块(有controller的模块必须引入此模块)</description>
<dependencies>
<dependency>
<groupId>com.qiaoba</groupId>
<artifactId>qiaoba-common-base</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,86 @@
package com.qiaoba.common.web.advice;
import com.qiaoba.common.base.exception.ServiceException;
import com.qiaoba.common.base.result.AjaxResult;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.validation.ConstraintViolationException;
import java.util.Objects;
/**
* 全局异常处理
*
* @author ailanyin
* @version 1.0
* @since 2021/10/15 0015 下午 16:43
*/
@ControllerAdvice
public class ExceptionAdvice {
/**
* 非自定义异常
*
* @param e Exception
* @return AjaxResult
*/
@ExceptionHandler(value = Exception.class)
@ResponseBody
public AjaxResult error(Exception e) {
e.printStackTrace();
return AjaxResult.error(e.getMessage());
}
/**
* 自定义异常, 控制台不打印
*
* @param e ServiceException
* @return AjaxResult
*/
@ExceptionHandler(ServiceException.class)
@ResponseBody
public AjaxResult handlerServiceException(ServiceException e) {
return Objects.isNull(e.getErrorCode()) ? AjaxResult.error(e.getMessage()) : AjaxResult.error(e.getErrorCode(), e.getMessage());
}
/**
* 参数校验
*
* @param e MethodArgumentNotValidException
* @return AjaxResult
*/
@ExceptionHandler({MethodArgumentNotValidException.class})
@ResponseBody
public AjaxResult handlerValidException(MethodArgumentNotValidException e) {
return AjaxResult.error(Objects.isNull(e.getBindingResult().getFieldError()) ? e.getMessage() : e.getBindingResult().getFieldError().getDefaultMessage());
}
/**
* 参数校验
*
* @param e BindException
* @return AjaxResult
*/
@ExceptionHandler({BindException.class})
@ResponseBody
public AjaxResult handlerValidException(BindException e) {
return AjaxResult.error(Objects.isNull(e.getBindingResult().getFieldError()) ? e.getMessage() : e.getBindingResult().getFieldError().getDefaultMessage());
}
/**
* 参数校验
*
* @param e ConstraintViolationException
* @return AjaxResult
*/
@ExceptionHandler({ConstraintViolationException.class})
@ResponseBody
public AjaxResult handlerValidException(ConstraintViolationException e) {
return AjaxResult.error(e.getMessage());
}
}

View File

@ -0,0 +1,37 @@
package com.qiaoba.common.web.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
/**
* 全局跨域配置
*
* @author ailanyin
* @version 1.0
* @since 2021/10/15 0015 下午 16:43
*/
@Configuration
public class GlobalCorsConfig {
@Bean
public CorsFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
// 设置访问源地址
config.addAllowedOriginPattern("*");
// 设置访问源请求头
config.addAllowedHeader("*");
// 设置访问源请求方法
config.addAllowedMethod("*");
// 有效期 半小时
config.setMaxAge(1800L);
// 添加映射路径,拦截一切请求
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
// 返回新的CorsFilter
return new CorsFilter(source);
}
}

View File

@ -0,0 +1,46 @@
package com.qiaoba.common.web.config;
import cn.hutool.core.io.IoUtil;
import com.qiaoba.common.web.util.IpUtil;
import lombok.extern.slf4j.Slf4j;
import org.lionsoul.ip2region.xdb.Searcher;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.InputStream;
/**
* 离线IP设置
*
* @author ailanyin
* @version 1.0
* @since 2023/5/25 16:32
*/
@Slf4j
@Configuration
public class IpConfig {
@PostConstruct
public void dbToCache() {
InputStream inputStream = null;
try {
ClassPathResource classPathResource = new ClassPathResource("ip2region.xdb");
inputStream = classPathResource.getInputStream();
IpUtil.setSearcher(Searcher.newWithBuffer(IoUtil.read(inputStream).toByteArray()));
log.info("加载IP离线库到内存成功");
} catch (Exception e) {
e.printStackTrace();
log.error("加载IP离线库到内存失败, 请联系管理员!");
} finally {
IoUtil.close(inputStream);
}
}
@PreDestroy
public void close() {
IpUtil.closeSearcher();
log.info("IP离线库已关闭");
}
}

View File

@ -0,0 +1,45 @@
package com.qiaoba.common.web.util;
import cn.hutool.extra.servlet.ServletUtil;
import lombok.extern.slf4j.Slf4j;
import org.lionsoul.ip2region.xdb.Searcher;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
* IP工具类
*
* @author ailanyin
* @version 1.0
* @since 2023/5/25 15:49
*/
@Slf4j
public class IpUtil {
private static Searcher searcher;
public static void setSearcher(Searcher searcher) {
IpUtil.searcher = searcher;
}
public static String getIp(HttpServletRequest request) {
return ServletUtil.getClientIP(request);
}
public static String getIpAddr(String ip) {
try {
return searcher.search(ip);
} catch (Exception e) {
return "error ip";
}
}
public static void closeSearcher() {
try {
searcher.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,34 @@
package com.qiaoba.common.web.util;
import cn.hutool.http.ContentType;
import cn.hutool.json.JSONUtil;
import com.qiaoba.common.base.constant.BaseConstant;
import com.qiaoba.common.base.result.AjaxResult;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* ResponseUtil
*
* @author ailanyin
* @version 1.0
* @since 2023-04-25 22:48:43
*/
public class ResponseUtil {
public static void response(HttpServletResponse response, String msg) throws IOException {
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType(ContentType.JSON.getValue());
response.setCharacterEncoding(BaseConstant.UTF8);
PrintWriter writer = response.getWriter();
writer.write(msg);
writer.close();
}
public static void errorAuth(HttpServletResponse response, Integer code, String msg) throws IOException {
AjaxResult result = AjaxResult.error(code, msg);
response(response, JSONUtil.toJsonStr(result));
}
}

View File

@ -0,0 +1,29 @@
package com.qiaoba.common.web.util;
import org.springframework.util.AntPathMatcher;
/**
* UriUtil
*
* @author ailanyin
* @version 1.0
* @since 2023-06-11 12:01:18
*/
public class UriUtil {
private static final AntPathMatcher MATCH = new AntPathMatcher();
private UriUtil() {
}
/**
* 通配符匹配
*
* @param pattern 通配符
* @param path uri
* @return 结果
*/
public static boolean match(String pattern, String path) {
return MATCH.match(pattern, path);
}
}

View File

@ -0,0 +1,6 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.qiaoba.common.web.advice.ExceptionAdvice,\
com.qiaoba.common.web.config.IpConfig,\
com.qiaoba.common.web.config.GlobalCorsConfig