springmvc 全局参数转换器
程序员文章站
2022-07-07 22:21:45
...
GetRequestInfo.java
/** * web-parent * Created by hfj * on 2017/7/7. */ @Target(ElementType.PARAMETER) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface GetRequestInfo { }
WebUtils.java
package com.j2eecms.web.util; import com.google.common.base.Strings; import com.j2eecms.core.model.RequestInfo; import com.j2eecms.web.exception.CaptchaException; import com.j2eecms.web.exception.CaptchaTimeOutException; import eu.bitwalker.useragentutils.UserAgent; import org.springframework.http.HttpHeaders; import org.springframework.mobile.device.Device; import org.springframework.mobile.device.DeviceType; import org.springframework.mobile.device.DeviceUtils; import javax.servlet.http.HttpServletRequest; import java.util.Date; import java.util.regex.Pattern; /** * Created by hfj on 2016/2/6. */ public class WebUtils { public static final String UNKNOWN = "UNKNOWN"; private final static String REGEX = "[@?`~!#$%^&*()+=|{}':;',//[//]<>/?~!#¥%……&*()——+|{}【】‘;:”“’。,、?]"; private final static Pattern STRING_PATTERN = Pattern.compile(REGEX); /** * 验证码默认超时时间 * 10分钟 */ public final static long CAPTCHA_TIMEOUT = 600000; private WebUtils() { } public static String getIpAddr(HttpServletRequest request) { String ip = request.getHeader("x-forwarded-for"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } else { int index = ip.lastIndexOf(","); if (index != -1) { ip = ip.substring(index + 1); } } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } return ip; } /** * 获取RequestInfo * * @param request * @return */ public static RequestInfo getRequestInfo(HttpServletRequest request) { RequestInfo requestInfo = new RequestInfo(); String curIp = getIpAddr(request); Device device = DeviceUtils.getCurrentDevice(request); if (null == device) { requestInfo.setDeviceType(UNKNOWN); } else if (device.isMobile()) { requestInfo.setDeviceType(DeviceType.MOBILE.toString()); } else if (device.isNormal()) { requestInfo.setDeviceType(DeviceType.NORMAL.toString()); } else if (device.isTablet()) { requestInfo.setDeviceType(DeviceType.TABLET.toString()); } else { requestInfo.setDeviceType(UNKNOWN); } if (!Strings.isNullOrEmpty(curIp)) { requestInfo.setRequestIp(curIp.trim()); } String serverName = request.getServerName(); String domainName; if (null!=serverName && serverName.split("\\.").length > 2) { domainName = serverName.replaceAll("^.*?\\.", ""); } else { domainName = serverName; } requestInfo.setDomainName(domainName); requestInfo.setWebsite(request.getScheme() + "://" + request.getServerName()); String userAgent = request.getHeader(HttpHeaders.USER_AGENT); if (!Strings.isNullOrEmpty(userAgent)) { UserAgent useragent = UserAgent.parseUserAgentString(userAgent); String browser = useragent.getBrowser().getName(); String operatingSystem = useragent.getOperatingSystem().getName(); requestInfo.setBrowser(browser); requestInfo.setOperatingSystem(operatingSystem); } return requestInfo; } /** * 验证码校验 * 默认验证码超时时间 5 分钟 * * @param captcha 验证码 * @param exitCodeObject session中保存的验证码 * @param captchaTimeObject 验证码记录的时间 * @throws CaptchaException 验证码为空 * @throws CaptchaTimeOutException 验证码超时 * @throws CaptchaException 验证码错误 */ public static void verifyCaptcha(String captcha, Object exitCodeObject, Object captchaTimeObject) { verifyCaptcha(captcha, exitCodeObject, captchaTimeObject, CAPTCHA_TIMEOUT); } /** * 验证码校验 * 默认验证码超时时间 5 分钟 * * @param captcha 验证码 * @param exitCodeObject session中保存的验证码 * @param captchaTimeObject 验证码记录的时间 * @param timeOut 验证码超时时间 毫秒 * @throws CaptchaException 验证码为空 * @throws CaptchaTimeOutException 验证码超时 * @throws CaptchaException 验证码错误 */ private static void verifyCaptcha(String captcha, Object exitCodeObject, Object captchaTimeObject, long timeOut) { if (null == exitCodeObject || null == captchaTimeObject) { throw new CaptchaException("Captcha is null"); } String exitCode = (String) exitCodeObject; long captchaTime = (Long) captchaTimeObject; if (new Date().getTime() - captchaTime > timeOut) { throw new CaptchaTimeOutException("captcha timeout"); } if (!exitCode.equalsIgnoreCase(captcha)) { throw new CaptchaException("captcha exitCode error"); } } /** * 验证码校验超时 * 默认验证码超时时间 5 分钟 * * @param exitCodeObject session中保存的验证码 * @param captchaTimeObject 验证码记录的时间 * @param timeOut 验证码超时时间 毫秒 * @throws CaptchaException 验证码为空 * @throws CaptchaTimeOutException 验证码超时 * @throws CaptchaException 验证码错误 */ public static void verifyCaptchaTime(Object exitCodeObject, Object captchaTimeObject, long timeOut) { if (null == exitCodeObject || null == captchaTimeObject) { throw new CaptchaException("Captcha is null"); } long captchaTime = (Long) captchaTimeObject; if (new Date().getTime() - captchaTime > timeOut) { throw new CaptchaTimeOutException("captcha timeout"); } } /** * 特殊字符转换为" "后,移除首位空格 * * @param str 原字符串 * @return */ public static String stringFilter(String str) { if (Strings.isNullOrEmpty(str)) { return str; } return STRING_PATTERN.matcher(str).replaceAll(" ").trim(); } }
RequestInfoArgumentResolver .java
package com.j2eecms.web.resolver; import com.j2eecms.web.annotation.GetRequestInfo; import com.j2eecms.web.util.WebUtils; import org.springframework.core.MethodParameter; import org.springframework.web.bind.support.WebDataBinderFactory; import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.context.request.ServletWebRequest; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.ModelAndViewContainer; import javax.servlet.http.HttpServletRequest; /** * web-parent * Created by hfj * on 2017/7/7. */ public class RequestInfoArgumentResolver implements HandlerMethodArgumentResolver { @Override public boolean supportsParameter(MethodParameter parameter) { return parameter.hasParameterAnnotation(GetRequestInfo.class); } @Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { HttpServletRequest request = ((ServletWebRequest) webRequest).getRequest(); return WebUtils.getRequestInfo(request); } }
springmvc配置
<mvc:annotation-driven> <mvc:argument-resolvers> <bean class="com.j2eecms.web.converter.RequestInfoArgumentResolver"/> </mvc:argument-resolvers> </mvc:annotation-driven>
spring boot配置
@Override public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { argumentResolvers.add(new RequestInfoArgumentResolver()); }
上一篇: http头信息详解