spring filter 配置
web xml
<filter>
<filter-name>DelegatingFilterProxy</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>autoLoginfilter</param-value>
</init-param>
</filter>
配置自动扫描注解
<context:component-scan base-package="cn.com.do1.important.filter" />
filter
package cn.com.do1.important.filter;
import cn.com.do1.component.common.util.ValidateUtil;
import cn.com.do1.dqdp.core.DqdpAppContext;
import cn.com.do1.important.service.IAuthService;
import cn.com.do1.wechat.aes.AesException;
import cn.com.do1.wechat.aes.WXBizMsgCrypt;
import cn.com.do1.wechat.basic.WeChatReqsUTLCfg;
import cn.com.do1.wechat.common.ConfKit;
import cn.com.do1.wechat.common.RequestUtil;
import cn.com.do1.wechat.common.Tools;
import cn.com.do1.wechat.core.WeChatCore;
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.annotation.Resource;
import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.CookieManager;
import java.util.HashMap;
import java.util.Map;
/**
* 请求拦截
*
* @author ao.ouyang
*/
@Component("autoLoginfilter")
public class AutoLoginFilter extends OncePerRequestFilter {
private final Logger log = LoggerFactory.getLogger(AutoLoginFilter.class);
@Resource
private IAuthService authService;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
try {
HttpSession session = request.getSession(true);
String username = null;
String password = null; // 此sessionid是上次用户登录时保存于用户端的识别码,用于用户后续访问的自动登录。不是本次访问的session id。
Cookie[] cookies;
// 如果session中没有user对象,则创建一个。
Map user = (Map) session.getAttribute("_user");
if (user == null) {
user = new HashMap(); // 此时user中的username属性为"",表示用户未登录。
}
// 如果user对象的username为"",表示用户未登录。则执行自动登录过程。
// 否则不自动登录。
if (ValidateUtil.isNullOrBlank(user.get("USER_NAME"))) {
cookies = request.getCookies();
for (Cookie cookie : cookies){
if(cookie.getName().equalsIgnoreCase("SESSION_LOGIN_USERNAME")){
username = cookie.getValue();
}
if(cookie.getName().equalsIgnoreCase("SESSION_LOGIN_PASSWORD")){
password = cookie.getValue();
}
}
if(!ValidateUtil.isNullOrBlank(username) && !ValidateUtil.isNullOrBlank(password)){
if(authService.validUser(username,password).equalsIgnoreCase("successful")){
user = authService.viewPersonByUserName(username);
authService.updateUserLog(user);
ServletActionContext.getRequest().getSession().setAttribute("_user", user);
}
}
}
}catch (Exception e){
log.error(e.getMessage(),e);
}
}
}