session失效後重新登入
阿新 • • 發佈:2019-01-02
有的時候使用者登陸網站後,長時間未操作或者其他原因導致,session會話失效,需要重新登陸。如果沒有考慮這一點,對使用者請求只拿ID作為驗證的有求必應的話,情況就不太妙了。所以今天來談談使用者登陸這個行為,為了安全考慮,從使用者登陸的這一刻起,只要涉及個人資訊的請求都需要驗證會話的有效性。在Java中可以設計一個過濾器來過濾請求,只需要兩步輕鬆解決,首寫一個過濾器filter然後把它配置在filter的配置檔案中,讓伺服器知道過濾器的存在。
1)編寫filter 考慮請求資源地方式為http 請求和ajax請求
package com.sgcc.evoms.filter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.sgcc.uap.utils.StringUtils;
/**
* <P>Description:(過濾器,限制未登入的使用者訪問頁面資源)</P>
* @version 1.0
* @author:
* @CreateDate:2014-7-22
* */
public class AuthFilter implements Filter {
public void destroy () {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)resp;
HttpSession session = request.getSession(false);
String requestType = request.getHeader("X-Requested-With");
if(!request.isRequestedSessionIdValid()){
//session過期,轉向session過期提示頁,最終跳轉至登入頁面
if(StringUtils.isNotBlank(requestType) && requestType.equalsIgnoreCase("XMLHttpRequest")){
//ajax請求
response.setHeader("sessionstatus", "timeout");
response.sendError(518, "session timeout."); //狀態碼,自定
return ;
}else{
String login = "/portal/csService/2_login.html";
PrintWriter out = response.getWriter();
out.write("<html><script type='text/javascript'>window.open('"+login+"','_top');</script></html>");
return;
}
}else{
//驗證是否已經登入
if (session.getAttribute("mobile")==null){
//尚未登入,跳轉至登入頁面
//session過期,轉向session過期提示頁,最終跳轉至登入頁面
if(StringUtils.isNotBlank(requestType) && requestType.equalsIgnoreCase("XMLHttpRequest")){
//ajax請求
response.setHeader("sessionstatus", "timeout");
response.sendError(518, "session timeout."); //狀態碼,自定
return ;
}else{
String login = "/portal/csService/2_login.html";
PrintWriter out = response.getWriter();
out.write("<html><script type='text/javascript'>window.open('"+login+"','_top');</script></html>");
return;
}
}
}
//驗證Session是否過期
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
}
}
2)配置bean中新增 filter
<module:servletfilter id="AuthFilterId">
<module:filter-name>AuthFilter</module:filter-name>
<module:filter-class>com.sgcc.evoms.filter.AuthFilter</module:filter-class>
<module:url-pattern>/*/2_loginSucc_5.html</module:url-pattern>
<module:url-pattern>/*/pay_alipayapi.jsp</module:url-pattern>
<module:url-pattern>/*/save</module:url-pattern>
<module:url-pattern>/*/myPasswordMagAction</module:url-pattern>
<module:url-pattern>/*/queryChargeRecord</module:url-pattern>
<module:url-pattern>/*/codecharge</module:url-pattern>
<module:url-pattern>/*/query</module:url-pattern>
<module:url-pattern>/*/querydata</module:url-pattern>
<module:url-pattern>/*/personal.jsp</module:url-pattern>
</module:servletfilter>