struts2 登陆的拦截器(1)

今天写了基于struts2的登录。它的拦截器真的很强大。我的目的就是让用户只有在登录的情况下才能访问其他的页面。做到这点很简单就在原来登陆的基础上加上如下的两处就可以了,那有不妥当的地方请各位指教,在下也是个刚刚学下这方面技术。
首先建立LoginInterceptor类如下:
import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.test.action.user.LoginAction;

public class LoginInterceptor extends AbstractInterceptor {

/**
*
*/
private static final long serialVersionUID = 1L;
@SuppressWarnings("unchecked")
public String intercept(ActionInvocation actionInvocation) throws Exception {
System.out.println("begin check login interceptor!");
// 对LoginAction不做该项拦截
Object action = actionInvocation.getAction();
if (action instanceof LoginAction) {
System.out.println("exit check login, because this is login action.");
return actionInvocation.invoke();
}
// 确认Session中是否存在LOGIN
Map session = actionInvocation.getInvocationContext().getSession();
String login = (String) session.get("login_user");
if (login != null && login.length() > 0) {
// 存在的情况下进行后续操作。
System.out.println("already login!");
return actionInvocation.invoke();
} else {
// 否则终止后续操作,返回LOGIN
System.out.println("no login, forward login page!");
return Action.LOGIN;

}

}

}
然后在struts.xml里面配置如下信息
<!-- 拦截器的配置 -->
<interceptors>
<!--定义一个名为authority的拦截器-->
<interceptor name="login" class="com.system.filter.LoginInterceptor"/>
<!--定义一个包含权限检查的拦截器栈-->
<interceptor-stack name="mydefault">
<!--配置内建默认拦截器-->
<interceptor-ref name="defaultStack"/>
<!--配置自定义的拦截器-->
<interceptor-ref name="login"/>
</interceptor-stack>
</interceptors>

<default-interceptor-ref name="mydefault" />
<!--定义全局Result-->
<global-results>
<result name="login">/Login.jsp</result>
</global-results>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值