serv\web- 日志
This commit is contained in:
@@ -1,189 +0,0 @@
|
||||
package org.xyzh.common.utils;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* @description ServletUtil.java文件描述 Servlet工具类
|
||||
* @filename ServletUtil.java
|
||||
* @author yslg
|
||||
* @copyright xyzh
|
||||
* @since 2025-10-07
|
||||
*/
|
||||
public class ServletUtil {
|
||||
|
||||
private static final String TOKEN_PREFIX = "Bearer ";
|
||||
|
||||
/**
|
||||
* @description 获取当前请求对象
|
||||
* @return HttpServletRequest 当前请求对象,无当前请求时返回null
|
||||
* @author yslg
|
||||
* @since 2025-10-07
|
||||
*/
|
||||
public static HttpServletRequest getRequest() {
|
||||
ServletRequestAttributes requestAttributes =
|
||||
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
return requestAttributes != null ? requestAttributes.getRequest() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取当前响应对象
|
||||
* @return HttpServletResponse 当前响应对象,无当前请求时返回null
|
||||
* @author yslg
|
||||
* @since 2025-10-07
|
||||
*/
|
||||
public static HttpServletResponse getResponse() {
|
||||
ServletRequestAttributes requestAttributes =
|
||||
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
return requestAttributes != null ? requestAttributes.getResponse() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 从当前请求中获取token(无参方法)
|
||||
* @return String token值,未找到返回null
|
||||
* @author yslg
|
||||
* @since 2025-10-07
|
||||
*/
|
||||
public static String getToken() {
|
||||
HttpServletRequest request = getRequest();
|
||||
return request != null ? extractTokenFromRequest(request) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 从当前请求中获取指定请求头
|
||||
* @param headerName 请求头名称
|
||||
* @return String 请求头值,未找到返回null
|
||||
* @author yslg
|
||||
* @since 2025-10-07
|
||||
*/
|
||||
public static String getHeader(String headerName) {
|
||||
HttpServletRequest request = getRequest();
|
||||
return request != null ? request.getHeader(headerName) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 从当前请求中获取指定请求参数
|
||||
* @param paramName 参数名称
|
||||
* @return String 参数值,未找到返回null
|
||||
* @author yslg
|
||||
* @since 2025-10-07
|
||||
*/
|
||||
public static String getParameter(String paramName) {
|
||||
HttpServletRequest request = getRequest();
|
||||
return request != null ? request.getParameter(paramName) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取当前请求的客户端IP地址
|
||||
* @return String IP地址,未找到返回null
|
||||
* @author yslg
|
||||
* @since 2025-10-07
|
||||
*/
|
||||
public static String getClientIpAddress() {
|
||||
HttpServletRequest request = getRequest();
|
||||
if (request == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String ip = request.getHeader("X-Forwarded-For");
|
||||
if (NonUtils.isNotEmpty(ip) && !"unknown".equalsIgnoreCase(ip)) {
|
||||
// 多次反向代理后会有多个IP值,第一个为真实IP
|
||||
int index = ip.indexOf(',');
|
||||
if (index != -1) {
|
||||
return ip.substring(0, index);
|
||||
} else {
|
||||
return ip;
|
||||
}
|
||||
}
|
||||
|
||||
ip = request.getHeader("X-Real-IP");
|
||||
if (NonUtils.isNotEmpty(ip) && !"unknown".equalsIgnoreCase(ip)) {
|
||||
return ip;
|
||||
}
|
||||
|
||||
ip = request.getHeader("Proxy-Client-IP");
|
||||
if (NonUtils.isNotEmpty(ip) && !"unknown".equalsIgnoreCase(ip)) {
|
||||
return ip;
|
||||
}
|
||||
|
||||
ip = request.getHeader("WL-Proxy-Client-IP");
|
||||
if (NonUtils.isNotEmpty(ip) && !"unknown".equalsIgnoreCase(ip)) {
|
||||
return ip;
|
||||
}
|
||||
|
||||
ip = request.getHeader("HTTP_CLIENT_IP");
|
||||
if (NonUtils.isNotEmpty(ip) && !"unknown".equalsIgnoreCase(ip)) {
|
||||
return ip;
|
||||
}
|
||||
|
||||
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
|
||||
if (NonUtils.isNotEmpty(ip) && !"unknown".equalsIgnoreCase(ip)) {
|
||||
return ip;
|
||||
}
|
||||
|
||||
return request.getRemoteAddr();
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取当前请求的User-Agent
|
||||
* @return String User-Agent值,未找到返回null
|
||||
* @author yslg
|
||||
* @since 2025-10-07
|
||||
*/
|
||||
public static String getUserAgent() {
|
||||
return getHeader("User-Agent");
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取当前请求的完整URL
|
||||
* @return String 完整URL,无当前请求时返回null
|
||||
* @author yslg
|
||||
* @since 2025-10-07
|
||||
*/
|
||||
public static String getRequestUrl() {
|
||||
HttpServletRequest request = getRequest();
|
||||
if (request == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
StringBuffer url = request.getRequestURL();
|
||||
String queryString = request.getQueryString();
|
||||
if (NonUtils.isNotEmpty(queryString)) {
|
||||
url.append('?').append(queryString);
|
||||
}
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 从请求中提取token
|
||||
* @param request HTTP请求对象
|
||||
* @return String token
|
||||
* @author yslg
|
||||
* @since 2025-10-07
|
||||
*/
|
||||
private static String extractTokenFromRequest(HttpServletRequest request) {
|
||||
// 优先从Authorization头获取
|
||||
String authHeader = request.getHeader(HttpHeaders.AUTHORIZATION);
|
||||
if (NonUtils.isNotEmpty(authHeader) && authHeader.startsWith(TOKEN_PREFIX)) {
|
||||
return authHeader.substring(TOKEN_PREFIX.length());
|
||||
}
|
||||
|
||||
// 从请求参数中获取token
|
||||
String token = request.getParameter("token");
|
||||
if (NonUtils.isNotEmpty(token)) {
|
||||
return token;
|
||||
}
|
||||
|
||||
// 从请求头中获取token
|
||||
token = request.getHeader("token");
|
||||
if (NonUtils.isNotEmpty(token)) {
|
||||
return token;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,404 +1,337 @@
|
||||
package org.xyzh.common.utils;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.servlet.http.Cookie;
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.BufferedReader;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.util.Set;
|
||||
import java.util.HashSet;
|
||||
|
||||
/**
|
||||
* @description ServletUtils.java文件描述:Servlet相关常用工具方法
|
||||
* @filename ServletUtils.java
|
||||
* @author yslg
|
||||
* @description ServletUtil.java文件描述 Servlet工具类
|
||||
* @filename ServletUtil.java
|
||||
* @author yslg
|
||||
* @copyright xyzh
|
||||
* @since 2023-12-19
|
||||
* @since 2025-10-07
|
||||
*/
|
||||
public class ServletUtils {
|
||||
|
||||
/**
|
||||
* @description 获取请求的真实IP地址
|
||||
* @param request HTTP请求对象
|
||||
* @return 客户端真实IP地址
|
||||
* @author yslg
|
||||
* @since 2023-12-19
|
||||
*/
|
||||
public static String getClientIp(HttpServletRequest request) {
|
||||
String ip = request.getHeader("X-Forwarded-For");
|
||||
if (ip != null && ip.isEmpty() && !"unknown".equalsIgnoreCase(ip)) {
|
||||
// 多级代理时取第一个
|
||||
int idx = ip.indexOf(',');
|
||||
if (idx > -1) {
|
||||
ip = ip.substring(0, idx);
|
||||
}
|
||||
return ip.trim();
|
||||
}
|
||||
ip = request.getHeader("Proxy-Client-IP");
|
||||
if (ip != null && ip.isEmpty() && !"unknown".equalsIgnoreCase(ip)) {
|
||||
return ip;
|
||||
}
|
||||
ip = request.getHeader("WL-Proxy-Client-IP");
|
||||
if (ip != null && ip.isEmpty() && !"unknown".equalsIgnoreCase(ip)) {
|
||||
return ip;
|
||||
}
|
||||
ip = request.getRemoteAddr();
|
||||
return ip;
|
||||
}
|
||||
private static final String TOKEN_PREFIX = "Bearer ";
|
||||
|
||||
/**
|
||||
* @description 向响应写出JSON字符串
|
||||
* @param response HTTP响应对象
|
||||
* @param json 要写出的JSON字符串
|
||||
* @return void
|
||||
* @author yslg
|
||||
* @since 2023-12-19
|
||||
*/
|
||||
public static void writeJson(HttpServletResponse response, String json) throws IOException {
|
||||
response.setContentType("application/json;charset=UTF-8");
|
||||
PrintWriter writer = response.getWriter();
|
||||
writer.write(json);
|
||||
writer.flush();
|
||||
writer.close();
|
||||
}
|
||||
/**
|
||||
* @description 获取当前请求对象
|
||||
* @return HttpServletRequest 当前请求对象,无当前请求时返回null
|
||||
* @author yslg
|
||||
* @since 2025-10-07
|
||||
*/
|
||||
public static HttpServletRequest getRequest() {
|
||||
ServletRequestAttributes requestAttributes =
|
||||
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
return requestAttributes != null ? requestAttributes.getRequest() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取请求参数(支持默认值)
|
||||
* @param request HTTP请求对象
|
||||
* @param name 参数名
|
||||
* @param defaultValue 默认值
|
||||
* @return 参数值或默认值
|
||||
* @author yslg
|
||||
* @since 2023-12-19
|
||||
*/
|
||||
public static String getParameter(HttpServletRequest request, String name, String defaultValue) {
|
||||
String value = request.getParameter(name);
|
||||
return value != null ? value : defaultValue;
|
||||
}
|
||||
/**
|
||||
* @description 获取当前响应对象
|
||||
* @return HttpServletResponse 当前响应对象,无当前请求时返回null
|
||||
* @author yslg
|
||||
* @since 2025-10-07
|
||||
*/
|
||||
public static HttpServletResponse getResponse() {
|
||||
ServletRequestAttributes requestAttributes =
|
||||
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
return requestAttributes != null ? requestAttributes.getResponse() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 判断请求是否为Ajax
|
||||
* @param request HTTP请求对象
|
||||
* @return 是否为Ajax请求
|
||||
* @author yslg
|
||||
* @since 2023-12-19
|
||||
*/
|
||||
public static boolean isAjaxRequest(HttpServletRequest request) {
|
||||
String header = request.getHeader("X-Requested-With");
|
||||
return "XMLHttpRequest".equalsIgnoreCase(header);
|
||||
}
|
||||
/**
|
||||
* @description 从当前请求中获取token(无参方法)
|
||||
* @return String token值,未找到返回null
|
||||
* @author yslg
|
||||
* @since 2025-10-07
|
||||
*/
|
||||
public static String getToken() {
|
||||
HttpServletRequest request = getRequest();
|
||||
return request != null ? extractTokenFromRequest(request) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取请求体内容
|
||||
* @param request HTTP请求对象
|
||||
* @return 请求体内容
|
||||
* @author yslg
|
||||
* @since 2023-12-19
|
||||
*/
|
||||
public static String getRequestBody(HttpServletRequest request) throws IOException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
BufferedReader reader = request.getReader();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
sb.append(line);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
/**
|
||||
* @description 从当前请求中获取指定请求头
|
||||
* @param headerName 请求头名称
|
||||
* @return String 请求头值,未找到返回null
|
||||
* @author yslg
|
||||
* @since 2025-10-07
|
||||
*/
|
||||
public static String getHeader(String headerName) {
|
||||
HttpServletRequest request = getRequest();
|
||||
return request != null ? request.getHeader(headerName) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 重定向到指定URL
|
||||
* @param response HTTP响应对象
|
||||
* @param url 目标URL
|
||||
* @return void
|
||||
* @author yslg
|
||||
* @since 2023-12-19
|
||||
*/
|
||||
public static void redirect(HttpServletResponse response, String url) throws IOException {
|
||||
response.sendRedirect(url);
|
||||
}
|
||||
/**
|
||||
* @description 从当前请求中获取指定请求参数
|
||||
* @param paramName 参数名称
|
||||
* @return String 参数值,未找到返回null
|
||||
* @author yslg
|
||||
* @since 2025-10-07
|
||||
*/
|
||||
public static String getParameter(String paramName) {
|
||||
HttpServletRequest request = getRequest();
|
||||
return request != null ? request.getParameter(paramName) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取完整请求URL
|
||||
* @param request HTTP请求对象
|
||||
* @return 完整URL
|
||||
* @author yslg
|
||||
* @since 2023-12-19
|
||||
*/
|
||||
public static String getFullUrl(HttpServletRequest request) {
|
||||
StringBuilder url = new StringBuilder();
|
||||
url.append(request.getScheme()).append("://");
|
||||
url.append(request.getServerName());
|
||||
int port = request.getServerPort();
|
||||
if (("http".equals(request.getScheme()) && port != 80) ||
|
||||
("https".equals(request.getScheme()) && port != 443)) {
|
||||
url.append(":").append(port);
|
||||
}
|
||||
url.append(request.getRequestURI());
|
||||
String queryString = request.getQueryString();
|
||||
if (queryString != null) {
|
||||
url.append("?").append(queryString);
|
||||
}
|
||||
return url.toString();
|
||||
}
|
||||
/**
|
||||
* @description 获取当前请求的客户端IP地址
|
||||
* @return String IP地址,未找到返回null
|
||||
* @author yslg
|
||||
* @since 2025-10-07
|
||||
*/
|
||||
public static String getClientIpAddress() {
|
||||
HttpServletRequest request = getRequest();
|
||||
if (request == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 判断请求是否为GET方法
|
||||
* @param request HTTP请求对象
|
||||
* @return 是否为GET请求
|
||||
* @author yslg
|
||||
* @since 2023-12-19
|
||||
*/
|
||||
public static boolean isGet(HttpServletRequest request) {
|
||||
return "GET".equalsIgnoreCase(request.getMethod());
|
||||
}
|
||||
String ip = request.getHeader("X-Forwarded-For");
|
||||
if (NonUtils.isNotEmpty(ip) && !"unknown".equalsIgnoreCase(ip)) {
|
||||
// 多次反向代理后会有多个IP值,第一个为真实IP
|
||||
int index = ip.indexOf(',');
|
||||
if (index != -1) {
|
||||
return ip.substring(0, index);
|
||||
} else {
|
||||
return ip;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 判断请求是否为POST方法
|
||||
* @param request HTTP请求对象
|
||||
* @return 是否为POST请求
|
||||
* @author yslg
|
||||
* @since 2023-12-19
|
||||
*/
|
||||
public static boolean isPost(HttpServletRequest request) {
|
||||
return "POST".equalsIgnoreCase(request.getMethod());
|
||||
}
|
||||
ip = request.getHeader("X-Real-IP");
|
||||
if (NonUtils.isNotEmpty(ip) && !"unknown".equalsIgnoreCase(ip)) {
|
||||
return ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 判断请求是否为PUT方法
|
||||
* @param request HTTP请求对象
|
||||
* @return 是否为PUT请求
|
||||
* @author yslg
|
||||
* @since 2023-12-19
|
||||
*/
|
||||
public static boolean isPut(HttpServletRequest request) {
|
||||
return "PUT".equalsIgnoreCase(request.getMethod());
|
||||
}
|
||||
ip = request.getHeader("Proxy-Client-IP");
|
||||
if (NonUtils.isNotEmpty(ip) && !"unknown".equalsIgnoreCase(ip)) {
|
||||
return ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 判断请求是否为DELETE方法
|
||||
* @param request HTTP请求对象
|
||||
* @return 是否为DELETE请求
|
||||
* @author yslg
|
||||
* @since 2023-12-19
|
||||
*/
|
||||
public static boolean isDelete(HttpServletRequest request) {
|
||||
return "DELETE".equalsIgnoreCase(request.getMethod());
|
||||
}
|
||||
ip = request.getHeader("WL-Proxy-Client-IP");
|
||||
if (NonUtils.isNotEmpty(ip) && !"unknown".equalsIgnoreCase(ip)) {
|
||||
return ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取所有请求参数
|
||||
* @param request HTTP请求对象
|
||||
* @return 参数Map
|
||||
* @author yslg
|
||||
* @since 2023-12-19
|
||||
*/
|
||||
public static Map<String, String> getParameterMap(HttpServletRequest request) {
|
||||
Map<String, String> paramMap = new HashMap<>();
|
||||
Enumeration<String> parameterNames = request.getParameterNames();
|
||||
while (parameterNames.hasMoreElements()) {
|
||||
String paramName = parameterNames.nextElement();
|
||||
String paramValue = request.getParameter(paramName);
|
||||
paramMap.put(paramName, paramValue);
|
||||
}
|
||||
return paramMap;
|
||||
}
|
||||
ip = request.getHeader("HTTP_CLIENT_IP");
|
||||
if (NonUtils.isNotEmpty(ip) && !"unknown".equalsIgnoreCase(ip)) {
|
||||
return ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取请求头信息
|
||||
* @param request HTTP请求对象
|
||||
* @return 头信息Map
|
||||
* @author yslg
|
||||
* @since 2023-12-19
|
||||
*/
|
||||
public static Map<String, String> getHeaderMap(HttpServletRequest request) {
|
||||
Map<String, String> headerMap = new HashMap<>();
|
||||
Enumeration<String> headerNames = request.getHeaderNames();
|
||||
while (headerNames.hasMoreElements()) {
|
||||
String headerName = headerNames.nextElement();
|
||||
String headerValue = request.getHeader(headerName);
|
||||
headerMap.put(headerName, headerValue);
|
||||
}
|
||||
return headerMap;
|
||||
}
|
||||
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
|
||||
if (NonUtils.isNotEmpty(ip) && !"unknown".equalsIgnoreCase(ip)) {
|
||||
return ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取Cookie值
|
||||
* @param request HTTP请求对象
|
||||
* @param name Cookie名
|
||||
* @return Cookie值
|
||||
* @author yslg
|
||||
* @since 2023-12-19
|
||||
*/
|
||||
public static String getCookieValue(HttpServletRequest request, String name) {
|
||||
Cookie[] cookies = request.getCookies();
|
||||
if (cookies != null) {
|
||||
for (Cookie cookie : cookies) {
|
||||
if (name.equals(cookie.getName())) {
|
||||
return cookie.getValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return request.getRemoteAddr();
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 设置Cookie
|
||||
* @param response HTTP响应对象
|
||||
* @param name Cookie名
|
||||
* @param value Cookie值
|
||||
* @param maxAge 过期时间(秒)
|
||||
* @return void
|
||||
* @author yslg
|
||||
* @since 2023-12-19
|
||||
*/
|
||||
public static void setCookie(HttpServletResponse response, String name, String value, int maxAge) {
|
||||
Cookie cookie = new Cookie(name, value);
|
||||
cookie.setMaxAge(maxAge);
|
||||
cookie.setPath("/");
|
||||
cookie.setHttpOnly(true);
|
||||
response.addCookie(cookie);
|
||||
}
|
||||
/**
|
||||
* @description 获取当前请求的User-Agent
|
||||
* @return String User-Agent值,未找到返回null
|
||||
* @author yslg
|
||||
* @since 2025-10-07
|
||||
*/
|
||||
public static String getUserAgent() {
|
||||
return getHeader("User-Agent");
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 删除Cookie
|
||||
* @param response HTTP响应对象
|
||||
* @param name Cookie名
|
||||
* @return void
|
||||
* @author yslg
|
||||
* @since 2023-12-19
|
||||
*/
|
||||
public static void removeCookie(HttpServletResponse response, String name) {
|
||||
Cookie cookie = new Cookie(name, null);
|
||||
cookie.setMaxAge(0);
|
||||
cookie.setPath("/");
|
||||
response.addCookie(cookie);
|
||||
}
|
||||
/**
|
||||
* @description 获取客户端IP(别名方法)
|
||||
* @return String IP地址
|
||||
* @author yslg
|
||||
* @since 2025-10-30
|
||||
*/
|
||||
public static String getClientIp() {
|
||||
return getClientIpAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 判断是否为HTTPS请求
|
||||
* @param request HTTP请求对象
|
||||
* @return 是否为HTTPS请求
|
||||
* @author yslg
|
||||
* @since 2023-12-19
|
||||
*/
|
||||
public static boolean isHttps(HttpServletRequest request) {
|
||||
return "https".equals(request.getScheme()) || request.isSecure() ||
|
||||
"443".equals(request.getHeader("X-Forwarded-Port")) ||
|
||||
"https".equals(request.getHeader("X-Forwarded-Proto"));
|
||||
}
|
||||
/**
|
||||
* @description 获取IP来源地址(简化版)
|
||||
* @return String IP来源
|
||||
* @author yslg
|
||||
* @since 2025-10-30
|
||||
*/
|
||||
public static String getIpSource() {
|
||||
String ip = getClientIp();
|
||||
if (ip == null || ip.isEmpty()) {
|
||||
return "未知";
|
||||
}
|
||||
|
||||
// 判断是否为内网IP
|
||||
if (isInternalIp(ip)) {
|
||||
return "内网IP";
|
||||
}
|
||||
|
||||
// 对于公网IP,返回简单标识
|
||||
// TODO: 可以集成第三方IP地址库进行详细查询
|
||||
return "外网IP";
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取上下文路径
|
||||
* @param request HTTP请求对象
|
||||
* @return 上下文路径
|
||||
* @author yslg
|
||||
* @since 2023-12-19
|
||||
*/
|
||||
public static String getContextPath(HttpServletRequest request) {
|
||||
return request.getContextPath();
|
||||
}
|
||||
/**
|
||||
* @description 获取浏览器信息
|
||||
* @return String 浏览器类型和版本
|
||||
* @author yslg
|
||||
* @since 2025-10-30
|
||||
*/
|
||||
public static String getBrowser() {
|
||||
String userAgent = getUserAgent();
|
||||
if (userAgent == null || userAgent.isEmpty()) {
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 检测请求是否包含指定参数
|
||||
* @param request HTTP请求对象
|
||||
* @param paramName 参数名
|
||||
* @return 是否包含参数
|
||||
* @author yslg
|
||||
* @since 2023-12-19
|
||||
*/
|
||||
public static boolean hasParameter(HttpServletRequest request, String paramName) {
|
||||
return request.getParameter(paramName) != null;
|
||||
}
|
||||
userAgent = userAgent.toLowerCase();
|
||||
|
||||
/**
|
||||
* @description 获取Session对象
|
||||
* @param request HTTP请求对象
|
||||
* @return HttpSession对象
|
||||
* @author yslg
|
||||
* @since 2023-12-19
|
||||
*/
|
||||
public static HttpSession getSession(HttpServletRequest request) {
|
||||
return request.getSession();
|
||||
}
|
||||
// 检测常见浏览器
|
||||
if (userAgent.contains("edg")) {
|
||||
return "Microsoft Edge";
|
||||
} else if (userAgent.contains("chrome")) {
|
||||
return "Chrome";
|
||||
} else if (userAgent.contains("firefox")) {
|
||||
return "Firefox";
|
||||
} else if (userAgent.contains("safari") && !userAgent.contains("chrome")) {
|
||||
return "Safari";
|
||||
} else if (userAgent.contains("opera") || userAgent.contains("opr")) {
|
||||
return "Opera";
|
||||
} else if (userAgent.contains("trident") || userAgent.contains("msie")) {
|
||||
return "Internet Explorer";
|
||||
} else if (userAgent.contains("postman")) {
|
||||
return "Postman";
|
||||
} else if (userAgent.contains("apifox")) {
|
||||
return "Apifox";
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取Session属性
|
||||
* @param request HTTP请求对象
|
||||
* @param attributeName 属性名
|
||||
* @return 属性值
|
||||
* @author yslg
|
||||
* @since 2023-12-19
|
||||
*/
|
||||
public static Object getSessionAttribute(HttpServletRequest request, String attributeName) {
|
||||
HttpSession session = request.getSession(false);
|
||||
return session != null ? session.getAttribute(attributeName) : null;
|
||||
}
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 设置Session属性
|
||||
* @param request HTTP请求对象
|
||||
* @param attributeName 属性名
|
||||
* @param attributeValue 属性值
|
||||
* @return void
|
||||
* @author yslg
|
||||
* @since 2023-12-19
|
||||
*/
|
||||
public static void setSessionAttribute(HttpServletRequest request, String attributeName, Object attributeValue) {
|
||||
request.getSession().setAttribute(attributeName, attributeValue);
|
||||
}
|
||||
/**
|
||||
* @description 获取操作系统信息
|
||||
* @return String 操作系统类型
|
||||
* @author yslg
|
||||
* @since 2025-10-30
|
||||
*/
|
||||
public static String getOs() {
|
||||
String userAgent = getUserAgent();
|
||||
if (userAgent == null || userAgent.isEmpty()) {
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 移除Session属性
|
||||
* @param request HTTP请求对象
|
||||
* @param attributeName 属性名
|
||||
* @return void
|
||||
* @author yslg
|
||||
* @since 2023-12-19
|
||||
*/
|
||||
public static void removeSessionAttribute(HttpServletRequest request, String attributeName) {
|
||||
HttpSession session = request.getSession(false);
|
||||
if (session != null) {
|
||||
session.removeAttribute(attributeName);
|
||||
}
|
||||
}
|
||||
userAgent = userAgent.toLowerCase();
|
||||
|
||||
/**
|
||||
* @description 防止XSS攻击的字符串过滤
|
||||
* @param input 输入字符串
|
||||
* @return 过滤后的字符串
|
||||
* @author yslg
|
||||
* @since 2023-12-19
|
||||
*/
|
||||
public static String escapeXss(String input) {
|
||||
if (input == null) {
|
||||
return null;
|
||||
}
|
||||
return input
|
||||
.replace("<", "<")
|
||||
.replace(">", ">")
|
||||
.replace("'", "'")
|
||||
.replace("\"", """)
|
||||
.replace("&", "&");
|
||||
}
|
||||
// 检测操作系统
|
||||
if (userAgent.contains("windows nt 10.0")) {
|
||||
return "Windows 10/11";
|
||||
} else if (userAgent.contains("windows nt 6.3")) {
|
||||
return "Windows 8.1";
|
||||
} else if (userAgent.contains("windows nt 6.2")) {
|
||||
return "Windows 8";
|
||||
} else if (userAgent.contains("windows nt 6.1")) {
|
||||
return "Windows 7";
|
||||
} else if (userAgent.contains("windows")) {
|
||||
return "Windows";
|
||||
} else if (userAgent.contains("mac os x")) {
|
||||
return "macOS";
|
||||
} else if (userAgent.contains("linux")) {
|
||||
if (userAgent.contains("android")) {
|
||||
return "Android";
|
||||
}
|
||||
return "Linux";
|
||||
} else if (userAgent.contains("iphone") || userAgent.contains("ipad")) {
|
||||
return "iOS";
|
||||
} else if (userAgent.contains("android")) {
|
||||
return "Android";
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取所有请求参数名
|
||||
* @param request HTTP请求对象
|
||||
* @return 参数名集合
|
||||
* @author yslg
|
||||
* @since 2023-12-19
|
||||
*/
|
||||
public static Set<String> getParameterNames(HttpServletRequest request) {
|
||||
Set<String> paramNames = new HashSet<>();
|
||||
Enumeration<String> names = request.getParameterNames();
|
||||
while (names.hasMoreElements()) {
|
||||
paramNames.add(names.nextElement());
|
||||
}
|
||||
return paramNames;
|
||||
}
|
||||
}
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取当前请求的完整URL
|
||||
* @return String 完整URL,无当前请求时返回null
|
||||
* @author yslg
|
||||
* @since 2025-10-07
|
||||
*/
|
||||
public static String getRequestUrl() {
|
||||
HttpServletRequest request = getRequest();
|
||||
if (request == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
StringBuffer url = request.getRequestURL();
|
||||
String queryString = request.getQueryString();
|
||||
if (NonUtils.isNotEmpty(queryString)) {
|
||||
url.append('?').append(queryString);
|
||||
}
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 判断是否为内网IP
|
||||
* @param ip IP地址
|
||||
* @return boolean true-内网IP,false-外网IP
|
||||
* @author yslg
|
||||
* @since 2025-10-30
|
||||
*/
|
||||
private static boolean isInternalIp(String ip) {
|
||||
if (ip == null || ip.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 本地回环地址
|
||||
if (ip.startsWith("127.") || ip.equals("localhost") || ip.equals("0:0:0:0:0:0:0:1") || ip.equals("::1")) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 内网IP段
|
||||
if (ip.startsWith("10.") ||
|
||||
ip.startsWith("192.168.") ||
|
||||
ip.startsWith("172.")) {
|
||||
// 172.16.0.0 - 172.31.255.255
|
||||
if (ip.startsWith("172.")) {
|
||||
String[] parts = ip.split("\\.");
|
||||
if (parts.length >= 2) {
|
||||
try {
|
||||
int secondOctet = Integer.parseInt(parts[1]);
|
||||
return secondOctet >= 16 && secondOctet <= 31;
|
||||
} catch (NumberFormatException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 从请求中提取token
|
||||
* @param request HTTP请求对象
|
||||
* @return String token
|
||||
* @author yslg
|
||||
* @since 2025-10-07
|
||||
*/
|
||||
private static String extractTokenFromRequest(HttpServletRequest request) {
|
||||
// 优先从Authorization头获取
|
||||
String authHeader = request.getHeader(HttpHeaders.AUTHORIZATION);
|
||||
if (NonUtils.isNotEmpty(authHeader) && authHeader.startsWith(TOKEN_PREFIX)) {
|
||||
return authHeader.substring(TOKEN_PREFIX.length());
|
||||
}
|
||||
|
||||
// 从请求参数中获取token
|
||||
String token = request.getParameter("token");
|
||||
if (NonUtils.isNotEmpty(token)) {
|
||||
return token;
|
||||
}
|
||||
|
||||
// 从请求头中获取token
|
||||
token = request.getHeader("token");
|
||||
if (NonUtils.isNotEmpty(token)) {
|
||||
return token;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package org.xyzh.common.utils.spring;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @description Spring上下文工具类 - 用于在非Spring管理的类中获取Bean
|
||||
* @filename SpringContextUtil.java
|
||||
* @author yslg
|
||||
* @copyright xyzh
|
||||
* @since 2025-10-30
|
||||
*/
|
||||
@Component
|
||||
public class SpringContextUtil implements ApplicationContextAware {
|
||||
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext context) throws BeansException {
|
||||
applicationContext = context;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取Spring容器中的Bean
|
||||
* @param beanClass Bean的Class类型
|
||||
* @return T Bean实例
|
||||
*/
|
||||
public static <T> T getBean(Class<T> beanClass) {
|
||||
if (applicationContext == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return applicationContext.getBean(beanClass);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取Spring容器中的Bean
|
||||
* @param beanName Bean的名称
|
||||
* @return Object Bean实例
|
||||
*/
|
||||
public static Object getBean(String beanName) {
|
||||
if (applicationContext == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return applicationContext.getBean(beanName);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取ApplicationContext
|
||||
*/
|
||||
public static ApplicationContext getApplicationContext() {
|
||||
return applicationContext;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user