servlet过滤器(Filter)详解(九)

网友投稿 293 2023-03-28


servlet过滤器(Filter)详解(九)

本文实例为大家分享了servlet过滤器的具体代码,供大家参考,具体内容如下

1.servlet过滤器产生背景

项目中我们会遇到这样一类的需求,对访问服务器的用户ip实施过滤,只有在允许名单中的ip才能访问服务,为了实现需求,每当有客户端请求时,我们都会写校验ip的代码,客户端能够访问到的servlet我们都需要这样做,很明显这样做有一个缺点,就是代码冗余,维护不方便,如果验证规则改变,修改起来也特别麻烦。

为了解决以上问题,Filter 技术应运而生。

2.servlet过滤器是什么?

servlet过滤器是在java servlet 2.3 中定义的。它能够对servlet容器传给servlet组件的ServletRequest 对象和 ServletResponse对象进行检查和修改。

3.案例

所有servlet过滤器都要实现javax.servlet.Filter接口。

NoteFilter

package com.learn;

import javax.servlet.*;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

import java.io.PrintWriter;

/**

* Created by Administrator on 2017/09/29.

*/

public class NoteFilter implements Filter {

private FilterConfig config = null;

private String ipTable = null; //ip名单

@Override

public void init(FilterConfig filterConfig) throws ServletException {

System.out.println("note filter initial");

this.config = filterConfig;

this.ipTable = config.getInitParameter("ipTable");

}

@Override

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

System.out.println("do filter starting");

//校验ip地址

if(!veryfyIP(request,response))

return;

long befor = System.currentTimeMillis();

config.getServletContext().log("before call note Filter");

chain.doFilter(request,response);

config.getServletContext().log("after call note Filter");

long after = System.currentTimeMillis();

String name ="";

if(request instanceof HttpServletRequest){

name = ((HttpServletRequest) request).getRequestURI();

}

config.getServletContext().log("Note Filter : name:"+name +" time :"+(after -befor)+"ms");

}

@Override

public void destroy() {

}

private boolean veryfyIP(ServletRequest request, ServletResponse response){

String ip = request.getRemoteAddr();

System.out.println("请求ip:"+ip);

System.out.println("ipTable 黑名单:"+ipTahttp://ble);

if(ip.indexOf(ipTable) == -1){

System.out.println("校验不通过");

response.setContentType("text/html");

PrintWriter out = null;

try {

out = response.getWriter();

out.print("

out.flush();

out.close();

} catch (IOException e) {

e.printStackTrace();

}

return false;

} else {

return true;

}

}

}

NoteServlet

package com.learn;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

/**

* Created by Administrator on 2017/09/29.

*/

public class NoteServlet extends HttpServlet {

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

System.out.println("已经通过了过滤器");

}

}

web.xml配置

ip

com.learn.NoteFilter

ipTable

127.0.0.1

ip

/note

ip

/note

note

com.learn.NoteServlet

note

/note

结果如下:


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:城市管理服务平台接口规范(城市管理服务平台接口规范最新)
下一篇:浅谈Java Fork/Join并行框架
相关文章

 发表评论

暂时没有评论,来抢沙发吧~