当前位置:网站首页>Filter的注解配置
Filter的注解配置
2022-06-12 05:37:00 【Fairy-KunKun】
<filter >
<filter-name >authorityFilter</filter-name>
<filter-class >com.njwbhz.mood.filter.AuthorityFilter</filter-class>
</filter>
<filter-mapping >
<filter-name >authorityFilter</filter-name>
<url-pattern >/*</url-pattern>
</filter-mapping>
<filter >
<filter-name >characterEncodingFilter</filter-name>
<filter-class >com.njwbhz.mood.filter.CharacterEncodingFilter</filter-class>
<init-param >
<param-name >encoding</param-name>
<param-value >UTF-8</param-value>
</init-param>
</filter>
<filter-mapping >
<filter-name >characterEncodingFilter</filter-name>
<url-pattern >*.do</url-pattern>
</filter-mapping>
package com.njwbhz.mood.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet ( value = "*.do", initParams = { @WebInitParam ( name = "encoding", value = "UTF-8" ) } )
public class CharacterEncodingFilter implements Filter {
//采用配置
private String encoding;
@Override
public void init ( FilterConfig filterConfig ) throws ServletException {
//初始化的时候获取编码方式
this.encoding = filterConfig.getInitParameter ( "encoding" );
}
@Override
public void doFilter ( ServletRequest servletRequest , ServletResponse servletResponse , FilterChain filterChain ) throws IOException, ServletException {
//过滤器的业务逻辑——>有时会说拦截器的业务逻辑
HttpServletRequest request = ( HttpServletRequest ) servletRequest;
HttpServletResponse response = ( HttpServletResponse ) servletResponse;
//获取请求方式
String method = request.getMethod ( );
if ( method.equalsIgnoreCase ( "POST" ) ) {
//设置编码
request.setCharacterEncoding ( encoding );
}
//放行
filterChain.doFilter ( request , response );
}
@Override
public void destroy ( ) {
}
}
package com.njwbhz.mood.filter;
import com.njwbhz.mood.entity.User;
import com.njwbhz.mood.util.AuthorityUtil;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
/**
* 登录验证过滤器
*/
@WebFilter ( "/*" )
public class AuthorityFilter implements Filter {
private List < String > urls;
@Override
public void init ( FilterConfig filterConfig ) throws ServletException {
//加载文件
urls = AuthorityUtil.load ( );
}
@Override
public void doFilter ( ServletRequest servletRequest , ServletResponse servletResponse , FilterChain filterChain ) throws IOException, ServletException {
//每个请求都能被AuthorityFilter拦截住
//该请求在验证的范围内?
//先得到当前的URL是什么?
HttpServletRequest request = ( HttpServletRequest ) servletRequest;
HttpServletResponse response = ( HttpServletResponse ) servletResponse;
//如当前的URL:http://127.0.0.1:8080/mood/page/user/login.do?xxxx
String currentUri = request.getRequestURI ( );// /mood/page/user/login.do?xxxx
String contextPath = request.getContextPath ( );
String currentURL = currentUri.substring ( contextPath.length ( ) );// /page/user/login.do?xxxx
if ( currentURL.contains ( "?" ) ) {
currentURL = currentURL.substring ( 0 , currentURL.lastIndexOf ( "?" ) );
}
//如果在,判断session
if ( urls.contains ( currentURL ) ) {
User user = ( User ) request.getSession ( ).getAttribute ( "user" );
if ( null != user ) { //已经登录过了
//放行
filterChain.doFilter ( request , response );
} else {
//登录页
String path = request.getContextPath ( );
String basePath = request.getScheme ( )
+ "://" + request.getServerName ( )
+ ":" + request.getServerPort ( )
+ path + "/";
String loginUrl = basePath + "page/user/login.jsp";
response.setContentType ( "text/html;charset=UTF-8" );
PrintWriter writer = response.getWriter ( );
writer.write ( "<script type=\"text/javascript\">" );
writer.write ( "alert(\"还未登录,请先登录!\");" );
writer.write ( "window.top.location.href='" );
writer.write ( loginUrl );
writer.write ( "';" );
writer.write ( "</script>" );
writer.flush ( );
writer.close ( );
}
} else { //如果不在,直接放行
filterChain.doFilter ( request , response );
}
}
@Override
public void destroy ( ) {
}
}
删除web.xml中的配置

再测试
问题:
采用XML配置的过滤器链,优先配置的先执行,调整位置即可。
采用@WebFilter注解,如何控制过滤器的执行顺序?
按照类文件的文件名自然排序的顺序

边栏推荐
- 59 - I. maximum value of sliding window
- Vivado HLS introductory notes
- 29. print matrix clockwise
- FPGA语法的细节
- Lesson 5: data warehouse construction (III)
- Laravel8 when search
- C language - how to define arrays
- Codis 3. X expansion and contraction
- [gpio] how to modify / display GPIO status through ADB shell
- 43. Number of occurrences of 1 in 1 ~ n integers
猜你喜欢

Detailed analysis of the 2021 central China Cup Title A (color selection of mosaic tiles)

yolov5

38. appearance series

How long is the company's registered capital subscribed

MySQL Linux Installation mysql-5.7.24

WebRTC AEC 流程解析

yolov5

Go 面向接口编程实战

Vivado HLS introductory notes

Performance test - performance test tool analysis
随机推荐
[gin] gin framework for golang web development
20. string representing numeric value
The way to promote software test engineer
How Wireshark decrypts WiFi data packets
第五讲:数据仓库搭建(三)
Multi thread learning v. volatile visibility and cache inconsistency, instruction reordering
Detailed analysis of the 2021 central China Cup Title A (color selection of mosaic tiles)
16. 最接近的三數之和
Towards End-to-End Lane Detection: an Instance SegmentationApproach
Esp32-who face detection
PHP实现图片登录验证码的解决方案
[speech] how to customize ring back tone according to different countries
WebRTC AEC 流程解析
arp timer and arptables
China's alternative sports equipment market trend report, technology dynamic innovation and market forecast
Codec of ASoC framework driven by alsa
Webrtc AEC process analysis
Necessary for Test Engineer -- package capturing tool fiddler
个体工商户是不是法人企业
[JS knowledge] easily understand JS anti shake and throttling