Jave自定义过滤器Filter

在web开发中,我们经常用到过滤器Filter,用来对所有web资源:例如Jsp, Servlet, 静态图片文件或静态 html 文件等进行拦截,Jave自定义过滤器Filter,从而实现一些特殊的功能,

下面展示一些 内联代码片

// A code block
**系统中因为要做活动,做一个问卷调查,需求:针对系统管理员创建的一个活动,针对发布后,
用户登录系统后,弹框弹问卷活动,必须回答调查,不然不能操作系统的任何页面(刷新、直接拼接url、缓存页面都不
可以用)  jave 的过滤器很好的实现这个功能,实现的代码如下,**
// An highlighted block
public class TaskPublishFilter implements Filter
{
    private String taskUrl;
    private List<String> excludeList = new ArrayList();

    @Override
    public void init(FilterConfig filterConfig) throws ServletException
    {
        String taskUrl = filterConfig.getInitParameter("taskUrl");
        this.taskUrl = taskUrl == null ? "task/publish/items" : taskUrl;

        String excludeActions = filterConfig.getInitParameter("excludeUrls");
        if (excludeActions != null) {
            String[] tokenizer = StringUtils.tokenizeToStringArray(excludeActions, ",; \t\n");
            this.excludeList.addAll(Arrays.asList(tokenizer));
        }
    }

    protected boolean isNeedValid(HttpServletRequest httpRequest) {
        String requestUrl = WebUtils.getRequestUrl(httpRequest);
        if (requestUrl != null) {
            String[] staticResources = new String[]{".jpg", ".png", ".jpeg", ".gif", ".js", ".css", ".html", ".htm", ".swf", ".doc", ".docx", ".xls", ".xlsx", ".jar", ".class"};
            String[] var7 = staticResources;
            int var6 = staticResources.length;

            String tmp;
            for(int var5 = 0; var5 < var6; ++var5) {
                tmp = var7[var5];
                if (StringUtils.endsWithIgnoreCase(requestUrl, tmp)) {
                    return false;
                }
            }

            if (!requestUrl.startsWith(this.taskUrl) && !requestUrl.startsWith("theme/default")) {
                if (this.excludeList.isEmpty()) {
                    return true;
                } else {
                    Iterator var8 = this.excludeList.iterator();

                    do {
                        if (!var8.hasNext()) {
                            return true;
                        }

                        tmp = (String)var8.next();
                    } while(!requestUrl.endsWith(tmp) && !MatcherUtils.matchStrings(tmp, requestUrl));

                    return false;
                }
            } else {
                return false;
            }
        } else {
            return true;
        }
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
    {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        HttpSession session = httpRequest.getSession(true);

        Object object = HttpSessionHolder.getAttribute(AuthenticationSessionKey.AUTHENTICATED_USER);
        if (!isNeedValid(httpRequest) || object == null || ValidationUtils.isSuperUser() || ValidationUtils.isManager() || !ValidationUtils.isSupplier())
        {
            chain.doFilter(httpRequest, httpResponse);
        }
        else
        {
            PortalUser user = (PortalUser) object;
            Object taskPublish = session.getAttribute(SessionKeyConstants.TASK_PUBLISH_TIPS);
            if (taskPublish != null)
            {
                chain.doFilter(request, response);
            }
            else
            {
                TaskPublishService taskPublishService = SpringContextHolder.getBean("taskPublishService", TaskPublishService.class);

                TaskPublish taskPublishTop1 = taskPublishService.getTaskPublishTop1();
                if (taskPublishTop1 == null || !taskPublishTop1.getEnabled())
                {
                    chain.doFilter(request, response);
                }
                else
                {
                    TaskPublishItemService taskPublishItemService = SpringContextHolder.getBean("taskPublishItemService", TaskPublishItemService.class);
                    List<TaskPublishItem> taskPublishItems = taskPublishItemService.queryTaskPublishItemList(taskPublishTop1.getId(), user.getUserId());

                    if (CollectionUtils.isNotEmpty(taskPublishItems))
                    {
                        HttpSessionHolder.setAttribute(SessionKeyConstants.TASK_PUBLISH_TIPS, taskPublishTop1);
                        chain.doFilter(request, response);
                    }
                    else
                    {
                        String contextPath = httpRequest.getContextPath();
                        httpResponse.sendRedirect(contextPath + "/" + this.taskUrl);
                    }
                }
            }
        }
    }

    @Override
    public void destroy()
    {

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值