今天用到了过滤器小结一下:
SpringMVC过滤器在web.xml中通常有两种配置一种是字符集过滤器(CharacterEncodingFilter),一种是自定义的过滤器(实现Filter接口)
过滤器类继承关系和实现如下:
web.xml字符集过滤器配置如下
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
CharacterEncodingFilter源码如下:
package org.springframework.web.filter;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.filter.OncePerRequestFilter;
public class CharacterEncodingFilter extends OncePerRequestFilter {
private String encoding;
private boolean forceEncoding = false;
public CharacterEncodingFilter() {
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}
public void setForceEncoding(boolean forceEncoding) {
this.forceEncoding = forceEncoding;
}
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
if(this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) {
request.setCharacterEncoding(this.encoding);
if(this.forceEncoding) {
response.setCharacterEncoding(this.encoding);
}
}
filterChain.doFilter(request, response);
}
}
encoding属性:指定的编码格式(在该配置文件中该属性被SpringIoC注入的值为UTF-8)
forceEncoding属性:表示在response字符集是否也采用encoding属性值(源码中默认为false,所以想要项目编码格式一致,应该在web.xml中配置其初始值为true)
当属性注入完成后开始执行doFilterInternal()函数:在this.encoding中指定了值的前提下如果指定forceEncoding=true或者发出的请求中没有指定request字符集的编码格式(request.getCharacterEncoding() == null))那么将过滤到的request的字符集设置为encoding指定的值,并根据this.forceEncoding的值判断是否需要设置response的字符集的值,如果this.encoding为空那么直接放行.
web.xml自定义过滤器配置如下
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>com.filter.LoginFilter</filter-class>
<init-param>
<param-name>unchecked</param-name>
<param-value>/WEB-INF/Public/login.jsp,/R33/menu/login1.do,/R33/menu/login.do,/R33/Js/jquery.sorted.js,/R33/Js/jquery.sorted.js,/R33/menu/childs1.do</param-value>
</init-param>
</filter>
注:在为param-value字段指定值时最好可一行写,换行后貌似会出现空格问题
LoginFilter代码如下
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Arrays;
public class LoginFilter implements Filter{
private String[] unchecked;
//private List<User> userarr=new ArrayList<User>();
public void init(FilterConfig config) throws ServletException {
String url=config.getInitParameter("unchecked");
unchecked=url.split(",");
System.out.println(url+"==============");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("登陆路径===="+java.util.Arrays.toString(unchecked));
//如何获取当前的路径
HttpServletRequest request1=(HttpServletRequest)request;
HttpServletResponse response1=(HttpServletResponse)response;
HttpSession session=request1.getSession();
User user=(User) session.getAttribute("u");
String currenturl=request1.getRequestURI();
System.out.println("当前的路径====="+currenturl);
//路径放行*/
if(Arrays.asList(unchecked).contains(currenturl)||user!=null){
chain.doFilter(request,response);
}else{
response1.sendRedirect(request1.getContextPath()+"/menu/login1.do");
}
}
public void destroy() {
}
}
设置项目开始运行时的跳转页(登陆页面)
<welcome-file-list>
<welcome-file>/R33/menu/login1.do</welcome-file>
</welcome-file-list>
login1.do代码
@RequestMapping("/login1")
public String login1(){
return "Public/login";
}
因为/R33/menu/login1.do在unchecked集合中所以直接放行,登陆页面跳转出来。
输入用户名和密码点击登录,登录按钮的action为/R33/menu/login.do,该地址也在unchecked集合中直接放行,login.do代码如下
@RequestMapping("/login")
public String login(User user,HttpServletRequest request,HttpServletResponse responseUser,Model model){
System.out.println(user.getName()+"----"+user.getPassword());
System.out.println("=======");
User u=uservice.querybyUser(user);//查询数据库中是否有此用户
request.getSession().setAttribute("u",u);
System.out.println("=======");
if(u==null){
return "redirect:/menu/login1.do";
}else{
//获取用户一级菜单列表
List<Menu> list=aservice.querybyRoleid(u.getRole_id());
model.addAttribute("mlist",list);
model.addAttribute("u",u);
return "../index";
}
}
LoginFilter的doFilter()函数功能是如果请求的路径在 unchecked集合中,或者用户已登录成功(user!=null)那么所有请求均放行,其他请求一律跳转到登录页面。