zoukankan      html  css  js  c++  java
  • Struts2拦截器

    实现用户登录过滤

    UserLoginInterceptor.java

     1 package com.tcf.inter;
     2 
     3 import java.util.List;
     4 
     5 import com.opensymphony.xwork2.Action;
     6 import com.opensymphony.xwork2.ActionContext;
     7 import com.opensymphony.xwork2.ActionInvocation;
     8 import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
     9 import com.tcf.entity.User;
    10 
    11 public class UserLoginInterceptor extends AbstractInterceptor {
    12     private String excludeMethods;
    13     
    14     public String getExcludeMethods() {
    15         return excludeMethods;
    16     }
    17 
    18     public void setExcludeMethods(String excludeMethods) {
    19         this.excludeMethods = excludeMethods;
    20     }
    21 
    22     @Override
    23     public String intercept(ActionInvocation arg0) throws Exception {
    24         User user = (User) ActionContext.getContext().getSession().get("userInfo");
    25         String name = ActionContext.getContext().getName();
    26         String[] actions = excludeMethods.split(",");
    27         
    28         if(user == null && !contains(actions,name)){
    29             return Action.INPUT;
    30         }
    31         arg0.invoke();
    32         return Action.SUCCESS;
    33     }
    34     
    35     public boolean contains(String[] list,String str){
    36         for(String s:list){
    37             if(s.equals(str)){
    38                 return true;
    39             }
    40         }
    41         return false;
    42     }
    43 }

    struts.xml

     1 <interceptors>
     2     <interceptor name="userInter" class="com.tcf.inter.UserLoginInterceptor"></interceptor>
     3     <interceptor-stack name="allInter">
     4         <interceptor-ref name="userInter">
     5             <!--不进行拦截的action名称 -->
     6             <param name="excludeMethods">loginUser,registerUser,upload,uploades,download</param>
     7         </interceptor-ref>
     8         <interceptor-ref name="defaultStack" />
     9     </interceptor-stack>
    10 </interceptors>
    11 <default-interceptor-ref name="allInter"></default-interceptor-ref>
  • 相关阅读:
    lombok 下的@Builder注解用法
    (springboot)自定义Starter
    各种 Spring-Boot-Starters系列 介绍
    MQ的使用场景
    SpringBoot中使用rabbitmq,activemq消息队列和rest服务的调用
    RPC原理详解
    Java性能优化的50个细节
    thinkphp3.2.2有预览的多图上传
    关于php中的exec命令
    关于thinkphp3.1无法加载模块解决办法
  • 原文地址:https://www.cnblogs.com/yunwei1237/p/5899599.html
Copyright © 2011-2022 走看看