filter过滤器,用于过滤到servlet的request,它可以改变一个request和修改一个response。
filter不是servlet,不能产生response。
filter能够在一个request到达servlet之前预处理request,也可以在离开servlet时处理response。
WEB工程中有些页面本身是对外开放的,但是我们实际上又会要求登录才可见。
虽然WEB-INFO目录可以禁止页面直接访问资源(WEB-INFO目录只能通过servlet去访问)。
如果我们将资源页面文件放在WEB-INFO下也可以达到无法访问,但是这就对我们前端编码造成很大的困扰。
WEB-INF是受保护目录,WEB-INF里面的文件只可以由servlet去访问,不能通过url地址栏去请求访问。
所以,我们应该通过别的方式来动态判断处理资源文件是否应该被访问
#web.xml配置
一个登录状态检查校验器,检查所有的向jsp文件发起的请求,并排除登录/登出/首页的访问拦截。
<filter>
<filter-name>checkLoginStatusFilter</filter-name>
<filter-class>com.mebugs.filter.CheckLoginStatusFilter</filter-class>
<init-param>
<param-name>exceptPath</param-name>
<param-value>login.jsp;logout.jsp;index.jsp</param-value>
</init-param>
<filter-mapping>
<filter-name>checkLoginStatusFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
</filter>
依赖于服务端保存了登录后新生成的sessionID,且服务端需要实现超时注销和登出注销session的能力。
/*
* 文件名:CheckLoginStatusFilter.java
* 描述:检查是否处于登录状态
* check the request sessionID login status
* @author mebugs
* @version 1.0
*/
public class CheckLoginStatusFilter implements Filter{
//定义除外的访问PATH
private String exceptPath;
//初始化参数,获取web.xml中配置的忽略校验地址
public void init(FilterConfig config) throws ServletException {
this.exceptPath = config.getInitParameter("exceptPath");
}
public void destroy(){
//销毁方法
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = null;
if (request instanceof HttpServletRequest){
httpRequest = (HttpServletRequest)request;
}
HttpServletResponse httpResponse = null;
if (response instanceof HttpServletResponse){
httpResponse = (HttpServletResponse)response;
}
if (null == httpRequest || null == httpResponse) {
return;
}
String requestURI = httpRequest.getRequestURI();
String contextPath = httpRequest.getContextPath();
String sessionID = httpRequest.getSession().getId();
String userName = null;
//Constants.SEMICOLON分号,针对配置多种排除的地址的时候
String[] exceptPathList = this.exceptPath.split(Constants.SEMICOLON);
for (int i = 0; i < exceptPathList.length; i++){
if (requestURI.endsWith(exceptPathList[i]){
//如果请求是忽略的地址
chain.doFilter(request, response);
return;
}
}
if(null != sessionID){
userName = (String) httpRequest.getSession().getAttribute("username");
}
if (null != userName){
//检查浏览器sessionID与服务器中缓存的登录后sessionID是否一致
if(sessionID.equals(UserSessionData.sessionMap.get(userName)){
//登录状态 sessionID一致
chain.doFilter(request, response);
return;
}
}
//其他场景通通非法
httpResponse.setStatus(911);
httpResponse.setHeader("sessionstatus", "timeout");
httpResponse.addHeader("loginPath", contextPath + "/login.jsp");
httpResponse.setCharacterEncoding("UTF-8");
httpResponse.setContentType("text/html");
PrintWriter out = httpResponse.getWriter();
out.println("<script>");
out.println("top.location.href = '" + contextPath + "/login.jsp'");
out.println("</script>");
return;
}
}
通过自定义过滤器可以更加人性化的对客户端发起的请求进行过滤。
对于资源请求限制为已登录用户才能访问,这样可以避免绝大多数非法攻击。
当前还没有观点发布,欢迎您留下足迹!
在web.xml中经常会看到listener,filter,servlet的相关标签配置,它们分别是监听器、过滤器、容器,都是在项目启动的时候就可以进行初始化的加载动作
JAVA中通过继承Thread类、实现Runnable接口以及实现Callable接口配合Future接口实现创建多线程,三种方式各有优缺点,而第三种则具备更多的增强能力
SpringBoot 可以通过 spring.profiles.active 属性指定生效不同配置文件来满足多环境要求,多环境更为复杂的场景,就需要理解配置文件生效优先级,考虑直接引入外部配置项和配置文件
scope属性主要用于控制依赖范围,主要分为编译、打包、运行、测试、依赖传递等各个常见,scope不同于optional提供了更多可选择的配置参数用于应对不同的依赖场景。
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;动态获取的信息以及动态调用对象的方法的功能。
在绝大多数的JAVA经典应用框架中广泛使用到了单例设计模式,单例设计模式最普遍的分类方式分类分为懒汉模式与饿汉模式两种,其主要区别在于初始化创建对象的时机不同。