过滤器实现手机跳转指定前缀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);
        }
    }
}
相关文章
|
5月前
|
前端开发 JavaScript
前端知识(十)———JavaScript 使用URL跳转传递数组对象数据类型的方法
前端知识(十)———JavaScript 使用URL跳转传递数组对象数据类型的方法
101 0
|
2月前
|
JavaScript 数据安全/隐私保护
Vue中如何使用过滤器来隐藏手机号、邮箱等字符串的中间部分内容
这篇文章介绍了在Vue中如何使用过滤器来隐藏手机号和邮箱等字符串的中间部分内容,以提高隐私保护。文章展示了实现效果,并提供了实现过程的代码示例,包括HTML部分的绑定、data部分的数据定义和script部分的过滤器定义。文章还解释了过滤器的概念、语法和使用方式,并提供了一个外部链接供读者获取更加详细的过滤器知识。
|
26天前
|
安全 PHP 开发者
Web安全-URL跳转与钓鱼
Web安全-URL跳转与钓鱼
26 8
|
11天前
|
JavaScript 前端开发
在url中取ip或者键值对、手机号脱敏、电话号脱敏、身份证脱敏、银行卡号脱敏、身份证校验
本文提供了JavaScript代码示例,用于从URL中提取IP地址、键值对,以及对手机号、电话号码、身份证号和银行卡号进行脱敏处理,还包含了身份证号的校验方法。
12 0
|
2月前
|
Java Android开发 UED
安卓scheme_url调端:如果手机上多个app都注册了 http或者https 的 intent。 调端的时候,调起哪个app呢?
当多个Android应用注册了相同的URL Scheme(如http或https)时,系统会在尝试打开这类链接时展示一个选择对话框,让用户挑选偏好应用。若用户选择“始终”使用某个应用,则后续相同链接将直接由该应用处理,无需再次选择。本文以App A与App B为例,展示了如何在`AndroidManifest.xml`中配置对http与https的支持,并提供了从其他应用发起调用的示例代码。此外,还讨论了如何在系统设置中管理这些默认应用选择,以及建议开发者为避免冲突应注册更独特的Scheme。
|
4月前
Discuz 手机版访问自动跳转到手机域名
Discuz 手机版访问自动跳转到手机域名
60 1
|
3月前
|
UED
返回按钮——没有上一页的URL时,跳转到首页(document.referrer的妙用)
返回按钮——没有上一页的URL时,跳转到首页(document.referrer的妙用)
28 0
|
4月前
|
应用服务中间件 nginx Windows
nginx实现网站url带参跳转 POST请求GET请求跳转
nginx实现网站url带参跳转 POST请求GET请求跳转
179 1
|
4月前
|
JavaScript
技术心得:根据不同访问设备跳转到PC页面或手机页面
技术心得:根据不同访问设备跳转到PC页面或手机页面
38 0
vos3000外呼系统非标准的11位手机号码开启国内业务和黑白名单时需设置忽略前缀
登录VOS3000管理界面: 使用管理员账号登录VOS3000管理界面。 进入业务配置界面: 在管理界面中找到业务配置或类似的选项,进入到业务配置的设置界面。 设置国内业务规则: 找到国内业务规则设置选项,一般会有关于号码长度、号码前缀等的设置。在此处设置忽略前缀,使系统能够识别非标准的11位手机号码。 设置黑白名单: 进入到黑白名单的设置界面,在添加黑白名单规则时,同样需要设置忽略前缀,以确保能够正确匹配非标准的手机号码。 保存设置: 在完成配置后,记得保存设置,确保所做的修改生效。 测试验证: 设置完成后,进行一些测试验证,确保系统能够正确识别和处理非标准的手机号码,以及正确应用

热门文章

最新文章

下一篇
无影云桌面