Struts2 自定义拦截器 二则

本文介绍了一种异常拦截器和登录检查拦截器的设计与实现方法。异常拦截器用于捕获并记录应用运行过程中的异常信息,而登录检查拦截器则确保只有已登录用户才能访问特定资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

异常拦截器:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->package org.roadway.lm.util;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * 
@author Huyvanpull
 * 
 
*/
@SuppressWarnings(
"serial")
public class ExceptionInterceptor extends AbstractInterceptor
{
    
private Logger logger = Logger.getLogger(ExceptionInterceptor.class);
    
    
private String interceptorName;
    
    @Override
    
public String intercept(ActionInvocation invocation) throws Exception
    {
        
this.logger.debug("进入" + this.getInterceptorName());
        String result 
= null;
        
try
        {
            result 
= invocation.invoke();
        }
        
catch (Exception exception)
        {
            
this.logger.error(this.getExceptionInfo(exception));
            
throw exception;
        }
        
return result;
    }
    
    
private String getExceptionInfo(Exception exception)
    {
        StringBuffer bExceptionInfo 
= new StringBuffer();
        bExceptionInfo.append(exception.toString());
        bExceptionInfo.append(
"\n\t");
        
        StackTraceElement[] stackTraceElements 
= exception.getStackTrace();
        
for (int i = 0; i < stackTraceElements.length; i++)
        {
            bExceptionInfo.append(
"[" + this.getInterceptorName() + ""
                    
+ stackTraceElements[i].toString() + "\n\t");
        }
        
return bExceptionInfo.toString();
    }
    
    
public String getInterceptorName()
    {
        
return interceptorName;
    }
    
    
public void setInterceptorName(String interceptorName)
    {
        
this.interceptorName = interceptorName;
    }
    
}

登陆检查拦截器:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->package org.roadway.lm.util;

import java.util.Map;

import org.apache.log4j.Logger;
import org.roadway.lm.po.UserInfo;
import org.roadway.lm.user.action.UserInfoAction;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * 
@author tom_hui
 * 
 
*/
@SuppressWarnings(
"serial")
public class LoginCheckerInterceptor extends AbstractInterceptor
{
    
private String userSessionKey = "userInfo";
    
    
private String isCheckLogin   = "true";
    
    
private Logger logger         = Logger
                                          .getLogger(LoginCheckerInterceptor.
class);
    
    @SuppressWarnings(
"unchecked")
    
public String intercept(ActionInvocation actionInvocation) throws Exception
    {
        Object action 
= actionInvocation.getAction();
        
        
/** 如果设置拦截器不检查登陆 */
        
if ("false".equalsIgnoreCase(isCheckLogin))
        {
            actionInvocation.invoke();
        }
        
/** 如果是登陆Action,放其通行 */
        
if (action instanceof UserInfoAction)
        {
            
this.logger.info("登陆Action:" + UserInfoAction.class.getName());
            
return actionInvocation.invoke();
        }
        
        
/** 从session中得到UserInfo的信息 */
        Map session 
= actionInvocation.getInvocationContext().getSession();
        UserInfo userInfo 
= (UserInfo) session.get(userSessionKey);
        
        
/** 如果Session中存在UserInfo对象 */
        
if (userInfo != null)
        {
            
this.logger.info("用户" + userInfo.getUserName() + "("
                    
+ userInfo.getUserId() + ")登陆了.");
            
return actionInvocation.invoke();
            
        }
        
/** 如果没有登陆 */
        
else
        {
            
return Action.LOGIN;
        }
    }
    
    
public String getUserSessionKey()
    {
        
return userSessionKey;
    }
    
    
public void setUserSessionKey(String userSessionKey)
    {
        
this.userSessionKey = userSessionKey;
    }
    
    
public String getIsCheckLogin()
    {
        
return isCheckLogin;
    }
    
    
public void setIsCheckLogin(String isCheckLogin)
    {
        
this.isCheckLogin = isCheckLogin;
    }
    
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值