您好,登錄后才能下訂單哦!
cookie 設置 httpOnly屬性防止js讀取cookie.
建立filter攔截器類
CookieHttpOnlyFilter
import java.io.IOException; 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.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * * <P>CookieにHTTPOnly屬性を設定インターセプタークラス.</P> * * @author hnnc * @author $Author$ * @version $Id$ */ public class CookieHttpOnlyFilter implements Filter { /** {@inheritDoc} **/ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (!(request instanceof HttpServletRequest)) { chain.doFilter(request, response); return; } HttpServletRequest httpReq = (HttpServletRequest) request; HttpServletResponse httpResp = (HttpServletResponse) response; Cookie[] cookies = httpReq.getCookies(); if (cookies != null) { Cookie cookie = cookies[0]; if (cookie != null) { HttpSession session = httpReq.getSession(); if (session != null) { String sessionId = session.getId(); // httpの設置 httpResp.addHeader("Set-Cookie", "JSESSIONID=" + sessionId + "; Path=/admin; HttpOnly"); // httpsの設置 // httpResp.addHeader("Set-Cookie", "JSESSIONID=" + sessionId // + "; Path=/admin;Secure; HttpOnly"); } } } chain.doFilter(httpReq, httpResp); } /** {@inheritDoc} **/ public void destroy() { } /** {@inheritDoc} **/ public void init(FilterConfig filterConfig) throws ServletException { } }
web.xml中配置攔截器
<filter> <filter-name>CookieHttpOnly</filter-name> <filter-class>jp.co.univ.www.admin.filter.CookieHttpOnlyFilter</filter-class> </filter> <filter-mapping> <filter-name>CookieHttpOnly</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
參考:
http://conkeyn.iteye.com/blog/2025484
http://blog.csdn.net/a19881029/article/details/27536917
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。