过滤器实现手机跳转指定前缀url

简介: 过滤器实现手机跳转指定前缀url

信念!有信念的人经得起任何风暴——奥维德

代码需要用到Opt

实现效果如下:

web访问/index页面正常跳转

手机访问/index页面,跳转到/h5/index页面(这里不只是index页面,其余页面同理)

完整代码:https://gitee.com/VampireAchao/simple-scaffold.git

package com.ruben.simplescaffold.filter;
import com.ruben.simplescaffold.utils.Opt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
/**
 * 用户浏览器标识过滤器
 *
 * @author <achao1441470436@gmail.com>
 * @since 2021/8/4 17:09
 */
@Slf4j
@Component
@WebFilter(filterName = "LogFilter", urlPatterns = "/**")
public class UserAgentFilter implements Filter {
    /**
     * 移动端浏览器标识
     */
    private final static List<String> MOBILE_AGENT_LIST = Arrays.asList("Android", "iPhone", "iPod", "iPad", "Windows Phone", "MQQBrowser");
    /**
     * The <code>doFilter</code> method of the Filter is called by the container
     * each time a request/response pair is passed through the chain due to a
     * client request for a resource at the end of the chain. The FilterChain
     * passed in to this method allows the Filter to pass on the request and
     * response to the next entity in the chain.
     * <p>
     * A typical implementation of this method would follow the following
     * pattern:- <br>
     * 1. Examine the request<br>
     * 2. Optionally wrap the request object with a custom implementation to
     * filter content or headers for input filtering <br>
     * 3. Optionally wrap the response object with a custom implementation to
     * filter content or headers for output filtering <br>
     * 4. a) <strong>Either</strong> invoke the next entity in the chain using
     * the FilterChain object (<code>chain.doFilter()</code>), <br>
     * 4. b) <strong>or</strong> not pass on the request/response pair to the
     * next entity in the filter chain to block the request processing<br>
     * 5. Directly set headers on the response after invocation of the next
     * entity in the filter chain.
     *
     * @param request  The request to process
     * @param response The response associated with the request
     * @param chain    Provides access to the next filter in the chain for this
     *                 filter to pass the request and response to for further
     *                 processing
     * @throws IOException      if an I/O error occurs during this filter's
     *                          processing of the request
     * @throws ServletException if the processing fails for any other reason
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String uri = httpRequest.getRequestURI();
        if (uri.contains("favicon")) {
            // 过滤图标和错误页面
            response.getWriter().write("");
            return;
        }
        // 判断请求是否来自手机
        String userAgent = httpRequest.getHeader("User-Agent");
        if (Opt.ofNullable(userAgent).filter(agent -> agent.contains("Windows NT")).filter(agent -> agent.contains("compatible; MSIE 9.0;")).or(() -> Opt.ofNullable(userAgent).filter(agent -> !agent.contains("Macintosh"))).filter(agent -> MOBILE_AGENT_LIST.parallelStream().anyMatch(agent::contains)).isPresent()) {
            request.getRequestDispatcher("/h5" + uri).forward(request, response);
        } else {
            chain.doFilter(request, response);
        }
    }
}
相关文章
|
2月前
|
前端开发 JavaScript
前端知识(十)———JavaScript 使用URL跳转传递数组对象数据类型的方法
前端知识(十)———JavaScript 使用URL跳转传递数组对象数据类型的方法
66 0
|
12天前
Discuz 手机版访问自动跳转到手机域名
Discuz 手机版访问自动跳转到手机域名
21 1
|
11天前
|
JavaScript
技术心得:根据不同访问设备跳转到PC页面或手机页面
技术心得:根据不同访问设备跳转到PC页面或手机页面
|
1月前
|
应用服务中间件 nginx Windows
nginx实现网站url带参跳转 POST请求GET请求跳转
nginx实现网站url带参跳转 POST请求GET请求跳转
27 1
vos3000外呼系统非标准的11位手机号码开启国内业务和黑白名单时需设置忽略前缀
登录VOS3000管理界面: 使用管理员账号登录VOS3000管理界面。 进入业务配置界面: 在管理界面中找到业务配置或类似的选项,进入到业务配置的设置界面。 设置国内业务规则: 找到国内业务规则设置选项,一般会有关于号码长度、号码前缀等的设置。在此处设置忽略前缀,使系统能够识别非标准的11位手机号码。 设置黑白名单: 进入到黑白名单的设置界面,在添加黑白名单规则时,同样需要设置忽略前缀,以确保能够正确匹配非标准的手机号码。 保存设置: 在完成配置后,记得保存设置,确保所做的修改生效。 测试验证: 设置完成后,进行一些测试验证,确保系统能够正确识别和处理非标准的手机号码,以及正确应用
|
2月前
|
JavaScript 前端开发 UED
JS自动跳转手机移动网页
JS自动跳转手机移动网页
466 0
|
2月前
|
Java C# Android开发
Xamarin.Android | 界面跳转到手机自带的自启动管理界面,引导用户将APP加入自启动
为了帮助用户在使用 APP 时提高其稳定性和使用体验,有时候我们需要让安卓手机的界面跳转到手机自带的自启动管理界面,以此来引导用户将 APP 加入自启动,确保应用程序在后台运行时不被系统杀死,从而保证应用程序服务的稳定性和可靠性。同时,这也可以提高用户的使用体验,使用户能够更好地享受应用程序的功能和服务。
160 0
Xamarin.Android | 界面跳转到手机自带的自启动管理界面,引导用户将APP加入自启动
|
8月前
|
JavaScript
jQuery带参跳转新页面,新页面获取url多个参数的办法
jQuery带参跳转新页面,新页面获取url多个参数的办法
33 0
|
8月前
|
JavaScript
jQuery带参数跳转,新页面获取url的参数id
jQuery带参数跳转,新页面获取url的参数id
39 0
|
8月前
thymeleaf获取url地址跳转时所带参数
thymeleaf获取url地址跳转时所带参数
183 0