java通过拦截器判断当前访问的设备是否是移动设备
程序员文章站
2022-06-11 13:26:51
...
拦截器从请求中提取信息判断访问的设备是否是手机。
一.拦截器配置
<!-- 跳转手机 -->
<mvc:interceptor>
<!-- 需拦截的地址 -->
<!-- 一级目录 -->
<mvc:mapping path="/*" />
<mvc:mapping path="/*mobile*/*" />
<!-- 需排除拦截的地址 -->
<mvc:exclude-mapping path="/candidateUser/login"/>
<mvc:exclude-mapping path="/candidateUser/register"/>
<mvc:exclude-mapping path="/candidateUser/loginData"/>
<mvc:exclude-mapping path="/candidateUser/registerData"/>
<mvc:exclude-mapping path="/invite/*"/>
<mvc:exclude-mapping path="/candidateUser/*"/>
<mvc:exclude-mapping path="/inviteUser/*"/>
<mvc:exclude-mapping path="/*Message*/*"/>
<mvc:exclude-mapping path="/*Message*"/>
<mvc:exclude-mapping path="/adminLogin"/>
<mvc:exclude-mapping path="/adminLogining"/>
<mvc:exclude-mapping path="/*mobile*"/>
<mvc:exclude-mapping path="/*mobile*/*"/>
<bean class="com.calaisweb.framework.base.interceptor.MobileInter" />
</mvc:interceptor>
二.拦截器代码
package com.calaisweb.framework.base.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.calaisweb.framework.tool.UrlUtil;
import com.calaisweb.framework.tool.mobile.DeviceUtils;
public class MobileInter implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
Boolean retBoolean=true;
Boolean isMobileDevice;
isMobileDevice=DeviceUtils.check(request,response);
if(isMobileDevice) {
String basePath = request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort()+"/calaisweb/";
response.sendRedirect(basePath+"/mobile/index");
retBoolean=false;
}
UrlUtil.getUrlInfo(request);
return retBoolean;
/*Boolean retBoolean=true;
Boolean isMobileDevice;
isMobileDevice=DeviceUtils.check(request,response);
if(isMobileDevice) {
String basePath = request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort()+"/calaisweb/";
String urlp=request.getRequestURI();
urlp.replaceAll("//","/");
String[] urlPathArr=urlp.split("/");
if(urlPathArr[2].equals("index")) {
response.sendRedirect(basePath+"/mobile/index");
}else if(urlPathArr.length==3) {
response.sendRedirect(basePath+urlPathArr[2]+"mobile");
}else if(urlPathArr.length==4){
response.sendRedirect(basePath+urlPathArr[2]+"Mobile/"+urlPathArr[3]);
}
retBoolean=false;
}
UrlUtil.getUrlInfo(request);
return retBoolean;*/
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
}
}
三.具体判断的工具类
package com.calaisweb.framework.tool.mobile;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* 判断访问设备为PC或者手机--工具类
*/
public class DeviceUtils {
// \b 是单词边界(连着的两个(字母字符 与 非字母字符) 之间的逻辑上的间隔),
// 字符串在编译时会被转码一次,所以是 "\\b"
// \B 是单词内部逻辑间隔(连着的两个字母字符之间的逻辑上的间隔)
static String phoneReg = "\\b(ip(hone|od)|android|opera m(ob|in)i"
+"|windows (phone|ce)|blackberry"
+"|s(ymbian|eries60|amsung)|p(laybook|alm|rofile/midp"
+"|laystation portable)|nokia|fennec|htc[-_]"
+"|mobile|up.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";
static String tableReg = "\\b(ipad|tablet|(Nexus 7)|up.browser"
+"|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";
//移动设备正则匹配:手机端、平板
static Pattern phonePat = Pattern.compile(phoneReg, Pattern.CASE_INSENSITIVE);
static Pattern tablePat = Pattern.compile(tableReg, Pattern.CASE_INSENSITIVE);
/**
* 检测是否是移动设备访问
*/
public static boolean check(String userAgent){
if(null == userAgent){
userAgent = "";
}
// 匹配
Matcher matcherPhone = phonePat.matcher(userAgent);
Matcher matcherTable = tablePat.matcher(userAgent);
if(matcherPhone.find() || matcherTable.find()){
return true;
} else {
return false;
}
}
/**
* 检查访问方式是否为移动端
*
*/
public static boolean check(HttpServletRequest request,HttpServletResponse response) throws IOException{
boolean isFromMobile=false;
HttpSession session= request.getSession();
//检查是否已经记录访问方式(移动端或pc端)
session.removeAttribute("ua");
if(null==session.getAttribute("ua")){
try{
//获取ua,用来判断是否为移动端访问
String userAgent = request.getHeader( "USER-AGENT" ).toLowerCase();
if(null == userAgent){
userAgent = "";
}
isFromMobile=DeviceUtils.check(userAgent);
//判断是否为移动端访问
if(isFromMobile){
System.out.println("移动端访问");
session.setAttribute("ua","mobile");
} else {
System.out.println("pc端访问");
session.setAttribute("ua","pc");
}
}catch(Exception e){}
}else{
isFromMobile=session.getAttribute("ua").equals("mobile");
}
return isFromMobile;
}
}