首页 > 其他分享 >拦截器Filter(过滤器)

拦截器Filter(过滤器)

时间:2024-11-12 15:20:02浏览次数:3  
标签:拦截器 public Filter init 过滤器 import servlet javax

拦截器也叫过滤器,拦截器就是前端和servlet之间的一个东西,可以用拦截器进行编码统一拦截没登陆就进页面的

实现Filter(Servlet包下的)

那三个方法是init、doFilter、destroy,它们是生命周期

init是初始化,doFilter是内容,destroy是销毁

拦截没登陆的

1.置web.xml方法

这里的配置替代的是java文件中的注解(java文件也要有只是可以不写注解),个人认为还是注解更方便

    <!-- 配置过滤器 -->
    <filter>
        <filter-name>LoginFilter</filter-name>
        <filter-class>com.hz.filter.LoginFilter</filter-class>
    </filter>

    <!-- 配置过滤器映射 -->
    <filter-mapping>
        <filter-name>LoginFilter</filter-name>
        <url-pattern>/servlet/*</url-pattern>  <!-- 只拦截 /secure 路径下的请求 -->
    </filter-mapping>

2.使用注解配置:

package com.hz.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter("/servlet/*")
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // Filter 初始化,可以不做任何事
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest,
                         ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException, ServletException {

        //转换类型,创建session对
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
        HttpSession session = httpServletRequest.getSession();


        if (session.getAttribute("smbmsProvider")==null){
//            如果没有登录,返回登录页
            httpServletResponse.sendRedirect("/login.jsp");

        }else {
//放行

            filterChain.doFilter(servletRequest,servletResponse);
        }

    }
    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}

统一设置编码:

1、使用注解配置:

@WebFilter("/*")

package com.hz.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter("/*")
public class CharacterEncodingFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;

        // 设置请求的字符编码
        httpServletRequest.setCharacterEncoding("UTF-8");

        // 设置响应的字符编码
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("text/html;charset=UTF-8");

        // 继续执行过滤链
        filterChain.doFilter(httpServletRequest, httpServletResponse);
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}

2.使用web.xml配置文件

    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>com.hz.filter.CharacterEncodingFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>  <!-- 拦截所有请求 -->
    </filter-mapping>

标签:拦截器,public,Filter,init,过滤器,import,servlet,javax
From: https://blog.csdn.net/qq_62859013/article/details/143713153

相关文章

  • 插件工厂和AOP拦截器机制
    一.插件工厂基于接口机制的插件工厂是一种设计模式,用于创建和管理插件。插件是独立的模块,可以动态加载和卸载,以扩展应用程序的功能。通过使用接口,插件工厂可以提供一种统一的方式来创建和管理这些插件,而不需要了解插件的具体实现细节。具体实现来说,实现一个插件工厂通过哈希记......
  • ffmpeg Audio Filters acrossover
    Splitaudiostreamintoseveralbands.Thisfiltersplitsaudiostreamintotwoormorefrequencyranges.Summingallstreamsbackwillgiveflatoutput.Thefilteracceptsthefollowingoptions:splitSetsplitfrequencies.Thosemustbepositiveandincr......
  • ffmpeg Audio Filters acrusher
    Reduceaudiobitresolution.Thisfilterisbitcrusherwithenhancedfunctionality.Abitcrusherisusedtoaudiblyreducenumberofbitsanaudiosignalissampledwith.Thisdoesn’tchangethebitdepthatall,itjustproducestheeffect.Materialre......
  • ffmpeg Filters adeclick
    Removeimpulsivenoisefrominputaudio.Samplesdetectedasimpulsivenoisearereplacedbyinterpolatedsamplesusingautoregressivemodelling.window,wSetwindowsize,inmilliseconds.Allowedrangeisfrom10to100.Defaultvalueis55milliseconds.......
  • ffmpeg Audio Filters adeclip
    Removeclippedsamplesfrominputaudio.Samplesdetectedasclippedarereplacedbyinterpolatedsamplesusingautoregressivemodelling.window,wSetwindowsize,inmilliseconds.Allowedrangeisfrom10to100.Defaultvalueis55milliseconds.Thisse......
  • ffmpeg Audio Filters adelay
    Delayoneormoreaudiochannels.Samplesindelayedchannelarefilledwithsilence.Thefilteracceptsthefollowingoption:delaysSetlistofdelaysinmillisecondsforeachchannelseparatedby’|’.Unuseddelayswillbesilentlyignored.Ifnumberof......
  • FFmpeg Filtering Introduction
    FilteringinFFmpegisenabledthroughthelibavfilterlibrary.Inlibavfilter,afiltercanhavemultipleinputsandmultipleoutputs.Toillustratethesortsofthingsthatarepossible,weconsiderthefollowingfiltergraph.[main]input-->split-......
  • ffmpeg Filtergraph description
    Afiltergraphisadirectedgraphofconnectedfilters.Itcancontaincycles,andtherecanbemultiplelinksbetweenapairoffilters.Eachlinkhasoneinputpadononesideconnectingittoonefilterfromwhichittakesitsinput,andoneoutputpad......
  • 布隆过滤器
    了解布隆过滤器日常生活中,包括在设计计算机软件时,我们经常要判断一个元素是否在一个集合中。比如在字处理软件中,需要检查一个英语单词是否拼写正确(也就是要判断它是否在已知的字典中);在FBI,一个嫌疑人的名字是否已经在嫌疑名单上;在网络爬虫里,一个网址是否被访问过等等。最直接......
  • Kafka 之自定义消息拦截器【Kafka 拦截器】
    前言:拦截器这个概念相信大部分朋友都不会陌生,SpringMVC拦截器相信大家都用过,拦截器的核心思想就是运行应用程序在不修改业务逻辑的前提下,动态的实现一组可插拔的事件处理器链,它可以在业务链路中的前后各个点进行对应的拦截,做一些统一的处理,SpingMVC的拦截器大家都了解,......