欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

SSH与SSM学习之登录校验拦截器

程序员文章站 2022-05-01 18:37:25
SSH与SSM学习之登录校验拦截器 一、图示 除了调用登录、注册、上传文件(因为这个单独的,和其他没关),这三个方法外,其他的方法调用的时候 都需要登录验证。主要的操作方式就...

SSH与SSM学习之登录校验拦截器

一、图示

除了调用登录、注册、上传文件(因为这个单独的,和其他没关),这三个方法外,其他的方法调用的时候

都需要登录验证。主要的操作方式就是通过拦截器来实现的。

SSH与SSM学习之登录校验拦截器


二、PrivilegeInterceptor

/**
 * @author:qiwenming
 * @date:2017/12/3 0003
 * @description:
 * 登录的验证
 */
public class PrivilegeInterceptor extends MethodFilterInterceptor {

    //不校验登陆和注册方法
    @Override
    protected String doIntercept(ActionInvocation invocation) throws Exception {
        //1 获取session
        //2 获取登录标识
        //3 判断标识是否存在
        Map session = ActionContext.getContext().getSession();
        User user = (User)session.get("user");
        if(user!=null){
            //存在-->放行
            return invocation.invoke();
        }else {
            //不存在---> 重定向到登陆页面
            return "toLogin";
        }
    }
}

三、PrivilegeInterceptor配置

主要在 struts.xml 中添加如下配置

  ...................
       
            
            
            
            
                login,regist,uploadFile
                
                
            
            
        
        
        
  ...................

四、struts.xml 配置说明

struts.xml 中的 package中配置的顺序是有要求的,如下:

result-types?

interceptors?

default-interceptor-ref?,

default-action-ref?

default-class-ref?

global-results?

global-allowed-methods?

global-exception-mappings?,

action*

如果不按这个顺序配置,会出错的。

struts.xml的配置如下




    
    

    
    

    login,regist,uploadFile
                
                
            
            
        
        
        

        
        
            /login.jsp
        
        
        regex:.*
        
            
        

        
        
            /index.html
            /login.jsp
            /regist.jsp
        

        
            /jsp/customer/add.jsp
            /jsp/customer/list.jsp
            /CustomerAction_list
            
        

        

        
            /jsp/upload.jsp
        

        
            /jsp/linkman/add.jsp
            /jsp/linkman/list.jsp
            /LinkManAction_list
            
        
    

五、源码下载

https://github.com/wimingxxx/ssh_crm