java实现判断是手机端还是pc端访问, nginx实现判断是手机端还是pc端访问

猿份哥 5月前 ⋅ 219 阅读

1.编写工具类

/**
 * @author 缘分哥
 * @description 判断是否移动端工具
 */
public class ClientUtils {
    public static String[] mobileAgents = { "iphone", "android","ipad", "phone", "mobile", "wap", "netfront", "java", "opera mobi",
        "opera mini", "ucweb", "windows ce", "symbian", "series", "webos", "sony", "blackberry", "dopod",
        "nokia", "samsung", "palmsource", "xda", "pieplus", "meizu", "midp", "cldc", "motorola", "foma",
        "docomo", "up.browser", "up.link", "blazer", "helio", "hosin", "huawei", "novarra", "coolpad", "webos",
        "techfaith", "palmsource", "alcatel", "amoi", "ktouch", "nexian", "ericsson", "philips", "sagem",
        "wellcom", "bunjalloo", "maui", "smartphone", "iemobile", "spice", "bird", "zte-", "longcos",
        "pantech", "gionee", "portalmmm", "jig browser", "hiptop", "benq", "haier", "^lct", "320x320",
        "240x320", "176x220", "w3c ", "acs-", "alav", "alca", "amoi", "audi", "avan", "benq", "bird", "blac",
        "blaz", "brew", "cell", "cldc", "cmd-", "dang", "doco", "eric", "hipt", "inno", "ipaq", "java", "jigs",
        "kddi", "keji", "leno", "lg-c", "lg-d", "lg-g", "lge-", "maui", "maxo", "midp", "mits", "mmef", "mobi",
        "mot-", "moto", "mwbp", "nec-", "newt", "noki", "oper", "palm", "pana", "pant", "phil", "play", "port",
        "prox", "qwap", "sage", "sams", "sany", "sch-", "sec-", "send", "seri", "sgh-", "shar", "sie-", "siem",
        "smal", "smar", "sony", "sph-", "symb", "t-mo", "teli", "tim-", "tosh", "tsm-", "upg1", "upsi", "vk-v",
        "voda", "wap-", "wapa", "wapi", "wapp", "wapr", "webc", "winw", "winw", "xda", "xda-",
        "Googlebot-Mobile" };
    /**
     * 判断是否为手机客户端
     */
    public static boolean isMoblie(HttpServletRequest request) {
        boolean isMoblie = false;

        if (request.getHeader("User-Agent") != null) {
            String agent=request.getHeader("User-Agent");
            for (String mobileAgent : mobileAgents) {
                if (agent.toLowerCase().indexOf(mobileAgent) >= 0&&agent.toLowerCase().indexOf("windows nt")<=0 &&agent.toLowerCase().indexOf("macintosh")<=0) {
                    isMoblie = true;
                    break;
                }
            }
        }
        return isMoblie;
    }
}

2.编写处理器

/**
 * @author 缘分哥
 * @description 客户端区分处理
 */
public class ClientHandler extends HandlerInterceptorAdapter {

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

        super.afterCompletion(request, response, handler, ex);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

        boolean isMoblie = ClientUtils.isMoblie(request);
        if (isMoblie == true) {
            System.out.println("===============手机端========================");
            //编写自己的业务逻辑....
        }

        super.postHandle(request, response, handler, modelAndView);


    }

}

3.注册处理器

/**
 * @author 缘分哥
 * @description 注册处理器
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Bean
    public ClientHandler cilentHandler(){
        return new ClientHandler();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(cilentHandler()).addPathPatterns("/**");
    }
}

参照:https://blog.csdn.net/qq_32657581/article/details/71405838

##======================分割线====================

nginx判断是pc还是移动访问

nginx.conf加入

......
http{
 include /etc/nginx/conf.d/*.conf;
}

在/etc/nginx/conf.d/目录下创建client.conf


   server {
        listen 9080 default_server;
		server_name pc.test.cn;
		
		#是手机访问就301重定向到m.test.cn:8080
		if ($http_user_agent ~* "(mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)") {
           rewrite  ^/(.*)$  http://m.test.cn:8080$uri permanent;
        }


        location / {

			proxy_pass http://localhost:8082/;
			proxy_set_header Host $http_host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header X-Forwarded-Proto $scheme;
			proxy_redirect off;
			add_header Cache-Control no-cache;
			add_header Pragma no-cache;
			add_header Expires 0;

        }
    }

       server {
        listen 9080;
	    server_name mobile.test.cn;
		#不是手机访问就301重定向到pc.test.cn:8080
		if ($http_user_agent !~* "(mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)") {
           rewrite  ^/(.*)$  http://pc.test.cn:8082$uri permanent;
        }

        location / {

			proxy_pass http://localhost:8082/;
			proxy_set_header Host $http_host;
			proxy_set_header X-Real-IP $remote_addr;
    		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    		proxy_set_header X-Forwarded-Proto $scheme;
			proxy_redirect off;
			add_header Cache-Control no-cache;
			add_header Pragma no-cache;
			add_header Expires 0;

        }
    }


全部评论: 0

    我有话说: