??????? 用于检查用户是否登录了系统,如果未登录,则重定向到指的登录页面。
??????? 在 java web 项目的 web.xml 文件中添加如下代码,对每个参数都进行了详细的说明。
class="xml plain" style="background: none !important; margin: 0px !important; padding: 0px !important; border-radius: 0px !important; border-image: none !important; width: auto !important; height: auto !important; text-align: left !important; color: #000000 !important; line-height: 1.8em !important; overflow: visible !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; font-size: 12px !important; font-style: normal !important; font-weight: normal !important; vertical-align: baseline !important; float: none !important; white-space: pre-wrap;"><!—检查用户是否登录了系统的过滤器配置? 开始 -->
<filter>
?<filter-name>SessionFilter</filter-name>
?<filter-class>com.hmw.filter.SessionFilter</filter-class>
?<init-param>
?????<description>将当前登录的用户的信息保存在 session 中时使用的key,如果没有配置此参数,则该过滤器不起作用</description>
?????<param-name>sessionKey</param-name>
?????<param-value>userInfo</param-value>
?</init-param>
?<init-param>
?????<description>
?????????如果用户未登录(即在 session 中 key 为 sessionKey 的属性不存在或为空),则将请求重定向到该 url。
?????????该 url 不包含web应用的 ContextPath。
?????????如果不配置此参数,则在用户未登录系统的情况下,直接重定向到web应用的根路径(/)
?????</description>
?????<param-name>redirectUrl</param-name>
?????<param-value>/login.jsp</param-value>
?</init-param>
?<init-param>
?????<description>
?????????不需要进行拦截的 url 的正则表达式,即:如果当前请求的 url 的 servletPath 能匹配该正则表达式,则直接放行(即使未登录系统)。
?????????此参数的值一般为 loginServlet 和 registServlet 等。
?????????另外,参数 redirectUrl 的值不用包含在该正则表达式中,因为 redirectUrl 对应的 url 会被自动放行。
?????????还有一点需要说明的是,该参数的值不包含web应用的 ContextPath。
?????</description>
?????<param-name>excepUrlRegex</param-name>
?????<!-- 不拦截 /servlets/loginServlet 和 /servlets/registServlet -->
?????<param-value>/servlets/(login|regist)Servlet</param-value>
?</init-param>
</filter>
?
<filter-mapping>
?<filter-name>SessionFilter</filter-name>
?<url-pattern>/servlets/*</url-pattern>
</filter-mapping>
<filter-mapping>
?<filter-name>SessionFilter</filter-name>
?<url-pattern>/jsp/*</url-pattern>
</filter-mapping>
<!—检查用户是否登录了系统的过滤器配置? 结束 -->
package?com.hmw.filter;
?
import?java.io.IOException;
import?java.net.URLEncoder;
import?java.util.regex.Pattern;
?
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?org.apache.commons.lang.StringUtils;
?
/**
?* 用于检查用户是否登录了系统的过滤器<br>
?* 创建日期:2012-01-09
?*/
public?class?SessionFilter implements?Filter {
?
????/** 要检查的 session 的名称 */
????private?String sessionKey;
?????
????/** 需要排除(不拦截)的URL的正则表达式 */
????private?Pattern excepUrlPattern;
?????
????/** 检查不通过时,转发的URL */
????private?String forwardUrl;
?
????@Override
????public?void?init(FilterConfig cfg) throws?ServletException {
????????sessionKey = cfg.getInitParameter("sessionKey");
?
????????String excepUrlRegex = cfg.getInitParameter("excepUrlRegex");
????????if?(!StringUtils.isBlank(excepUrlRegex)) {
????????????excepUrlPattern = Pattern.compile(excepUrlRegex);
????????}
?
????????forwardUrl = cfg.getInitParameter("forwardUrl");
????}
?
????@Override
????public?void?doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws?IOException, ServletException {
????????// 如果 sessionKey 为空,则直接放行
????????if?(StringUtils.isBlank(sessionKey)) {
????????????chain.doFilter(req, res);
????????????return;
????????}
?
//???????? * 请求 http://127.0.0.1:8080/webApp/home.jsp?&a=1&b=2 时
//????????? * request.getRequestURL(): http://127.0.0.1:8080/webApp/home.jsp
//???????? * request.getContextPath(): /webApp
//???????? * request.getServletPath():/home.jsp
//???????? * request.getRequestURI(): /webApp/home.jsp
//???????? * request.getQueryString():a=1&b=2
????????HttpServletRequest request = (HttpServletRequest) req;
????????HttpServletResponse response = (HttpServletResponse) res;
????????String servletPath = request.getServletPath();
?
????????// 如果请求的路径与forwardUrl相同,或请求的路径是排除的URL时,则直接放行
????????if?(servletPath.equals(forwardUrl) || excepUrlPattern.matcher(servletPath).matches()) {
????????????chain.doFilter(req, res);
????????????return;
????????}
?
????????Object sessionObj = request.getSession().getAttribute(sessionKey);
????????// 如果Session为空,则跳转到指定页面
????????if?(sessionObj == null) {
????????????String contextPath = request.getContextPath();
????????????String redirect = servletPath + "?"?+ StringUtils.defaultString(request.getQueryString());
????????????/*
?????????????* login.jsp 的 <form> 表单中新增一个隐藏表单域:
?????????????* <input type="hidden" name="redirect" value="${param.redirect }">
?????????????*
?????????????*? LoginServlet.java 的 service 的方法中新增如下代码:
?????????????*? String redirect = request.getParamter("redirect");
?????????????*? if(loginSuccess){
?????????????*????? if(redirect == null || redirect.length() == 0){
?????????????*????????? // 跳转到项目主页(home.jsp)
?????????????*????? }else{
?????????????*????????? // 跳转到登录前访问的页面(java.net.URLDecoder.decode(s, "UTF-8"))
?????????????*????? }
?????????????*? }
?????????????*/
????????????response.sendRedirect(contextPath + StringUtils.defaultIfEmpty(forwardUrl, "/")
????????????????????????????+ "?redirect="?+ URLEncoder.encode(redirect, "UTF-8"));
????????} else?{
????????????chain.doFilter(req, res);
????????}
????}
?
????@Override
????public?void?destroy() {
????}
}
?