服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|

服务器之家 - 编程语言 - JAVA教程 - 拦截JSP页面,校验是否已登录详解及实现代码

拦截JSP页面,校验是否已登录详解及实现代码

2020-07-02 11:36java教程网 JAVA教程

这篇文章主要介绍了拦截JSP页面,校验是否已登录详解及实现代码的相关资料,需要的朋友可以参考下

拦截JSP页面,校验是否已登录

   最近使用struts2做项目,需要拦截JSP以校验是否已经登,但struts2的过滤器链只能过滤action。后来发现在web.xml里可以直接增加javax.servlet.Filter实现类的过滤器,可以过滤JSP。请看代码

Filter实现类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<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>

登录方法:

?
1
2
3
4
5
6
7
8
9
10
<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配置

?
1
2
3
4
5
6
7
8
9
10
11
12
<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超时后退出到登录页面,此时登录页面显示在框架的某个子页面,解决方法,在登录页面加入如下代码即可。

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

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

延伸 · 阅读

精彩推荐