java代码:
package com.tci.login;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/**
*
* @author dell
*/
public class LoginFilter implements Filter {
private String encoding;
public FilterConfig filterConfig;
public void init(FilterConfig config) throws ServletException {
encoding = config.getInitParameter("requestEncoding");
if (encoding == null) {
encoding = "UTF-8";
}
filterConfig = config;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain next) throws IOException, ServletException {
request.setCharacterEncoding(encoding);
// response.setContentType("text/html;charset=" + encoding);
HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper((HttpServletResponse) response);
// check login
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
HttpSession session = req.getSession(true);
User user = (User) session.getAttribute("user");
String url = req.getRequestURI();
String loginStrings = filterConfig.getInitParameter("loginStrings");
String includeStrings = filterConfig.getInitParameter("includeStrings");
String redirectPath = filterConfig.getInitParameter("redirectPath");
String disabletestfilter = filterConfig.getInitParameter("disabletestfilter");
redirectPath = req.getContextPath() + redirectPath;
if (disabletestfilter.toUpperCase().equals("Y")) {
next.doFilter(request, response);
return;
}
String[] loginList = loginStrings.split(";");
String[] includeList = includeStrings.split(";");
if (!(url.contains("css")||url.contains("js"))) {
response.setContentType("text/html;charset=utf-8");
}
if (!this.isContains(url, includeList)) {// 只对指定过滤参数后缀进行过滤
next.doFilter(request, response);
return;
}
if (this.isContains(url, loginList)) {// 对登录页面不进行过滤
next.doFilter(request, response);
return;
}
if (user == null) {
wrapper.sendRedirect(redirectPath);
return;
}else {
next.doFilter(request, response);
return;
}
// if(url.endsWith("login.jsp")
|| url.endsWith("login.html")
// || url.endsWith("login.action")
|| url.endsWith("login.ajax")
// || url.equals("/")){
// next.doFilter(request, response);
return;
// }else if(url.endsWith(".css")
// || url.endsWith(".js")
// || url.endsWith(".jpg")
// || url.endsWith(".gif")
// || url.endsWith(".png")
// || user != null){
// next.doFilter(request, response);
// }else {
// request.getRequestDispatcher("/login.jsp").forward(request, response);
return;
// }
}
public static boolean isContains(String container, String[] regx) {
boolean result = false;
for (int i = 0; i < regx.length; i++) {
if (container.indexOf(regx[i]) != -1) {
return true;
}
}
return result;
}
public void destroy() {
this.filterConfig = null;
}
}
web.xml
<filter>
<description>登录过滤器</description>
<filter-name>LoginFilter</filter-name>
<filter-class>com.tci.login.LoginFilter</filter-class>
<init-param>
<param-name>requestEncoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>loginStrings</param-name><!-- 对登录页面不进行过滤 -->
<param-value>/login.jsp;/login.action</param-value>
</init-param>
<init-param>
<param-name>includeStrings</param-name><!-- 只对指定过滤参数后缀进行过滤 -->
<param-value>.jsp;.html;.htm;.action;.ajax</param-value>
</init-param>
<init-param>
<param-name>redirectPath</param-name><!-- 未通过跳转到登录界面 -->
<param-value>/login.jsp</param-value>
</init-param>
<init-param>
<param-name>disabletestfilter</param-name><!-- Y:过滤无效 -->
<param-value>N</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
解决登录之后出现的样式问题的代码是:
if (!(url.contains("css")||url.contains("js"))) {
response.setContentType("text/html;charset=utf-8");
}
其实在doFilter方法里就设置过这行代码,但是仍然是样式不生效,后来根据代码搜了下css和js的内容类型,结果发现如下:
文件扩展名 | Content-Type(Mime-Type) |
.js | application/x-javascrip |
.css | text/css |
直接设置response.setContentType("text/html;charset=utf-8")的话,js和css估计就不生效了。。
设置session过期时间,单位是分钟,位置在web.xml中:
<session-config> <session-timeout>5</session-timeout> </session-config>