攔截JSP頁面,校正是否已登入詳解及實現代碼_java

來源:互聯網
上載者:User

攔截JSP頁面,校正是否已登入

   最近使用struts2做項目,需要攔截JSP以校正是否已經登,但struts2的過濾器鏈只能過濾action。後來發現在web.xml裡可以直接增加javax.servlet.Filter實作類別的過濾器,可以過濾JSP。請看代碼

Filter實作類別

<span style="font-size:18px;">package com.common;  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.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;  public class LoginFilter extends HttpServlet implements Filter {        private static final long serialVersionUID = 1L;      @Override    public void doFilter(ServletRequest request, ServletResponse response,        FilterChain chain) throws IOException, ServletException {      HttpServletRequest req = (HttpServletRequest)request;      HttpServletResponse res = (HttpServletResponse)response;      String path = req.getContextPath();     String indexPath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+path+ "/pages/login.jsp";     if(req.getRequestURI().endsWith("login.jsp"))     {       chain.doFilter(request, response);        return;     }          Object loginuser = req.getSession().getAttribute("<strong><span style="color:#ff0000;">loginSession</span></strong>");      if(loginuser == null){       res.sendRedirect(indexPath);        return;      }     chain.doFilter(request, response);    }      @Override    public void init(FilterConfig arg0) throws ServletException {      // TODO Auto-generated method stub     }    }  </span> 

登入方法:

<span style="font-size:18px;"> public String login()   {     Login login = new Login();            //.......校正登入資訊     ActionContext ctx = ActionContext.getContext();     Map<String, Object> session = ctx.getSession();     session.put("<strong><span style="color:#ff0000;">loginSession</span></strong>", login);     return "success";   }</span> 

 web.xml配置

<filter>     <filter-name>setlog</filter-name>     <filter-class>com.common.LoginFilter</filter-class>  </filter>  <filter-mapping>     <filter-name>setlog</filter-name>     <url-pattern>/pages/home.jsp</url-pattern>    <url-pattern>/pages/channel/*</url-pattern>    <url-pattern>/pages/content/*</url-pattern>    <url-pattern>/pages/product/*</url-pattern>    <url-pattern>/pages/vodtask/*</url-pattern> </filter-mapping> 

注意,如果頁面上使用了架構,則可能導致session逾時後退出到登入頁面,此時登入頁面顯示在架構的某個子頁面,解決方案,在登入頁面加入如下代碼即可。

<span style="font-size: 18px;">  <script language="javascript">       if (top != window)         top.location.href = window.location.href;     </script></span> 

感謝閱讀,希望能協助到大家,謝謝大家對本站的支援!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.