创建一个Filter只需两个步骤:1.创建filter处理类;2.web.xml中配置Filter 或 采用注解形式。
如果需要检查权限,可以在Filter类中对用户的Session进行判断,如果没有登录,跳转到登陆页面,否则正常放行即可。
<filter> <filter-name>WebServiceFilter</filter-name> <filter-class>com.likeyichu.webservice.filter.WebServiceFilter</filter-class> </filter> <filter-mapping> <filter-name>WebServiceFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { if(!acl.contains(request.getRemoteHost())){ HttpServletResponse resp = (HttpServletResponse) response; //403返回 哈哈哈 resp.sendError(HttpServletResponse.SC_FORBIDDEN); return; } chain.doFilter(request, response); }