java拦截器实现强制登录(结合struts2)

来源:互联网 发布:php和java对比 编辑:程序博客网 时间:2024/06/03 22:48

struts2实现强制登陆时,使用比较方便;

java 代码

public class MyInterceptor implements Interceptor{

//销毁

public void destroy(){

}

//初始化

public void init(){

}

public String inteceptor(ActionInvocation ai){

ValueStack vs=ai.getValueStack();//获得值栈

UserInfo user=(UserInfo)vs.findValue("#session.user");//得到登陆保存的session信息

if(user==null){

return "loginError";//返回登陆界面

}else{

ai.invoke();

return null;//放行

}

}

}

struts2.xml文件配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

<!--声明拦截器-->

<interceptors>

<interceptor name="随意取名(myInterceptor)" class="拦截器的全类名"/>

</interceptors>

<action name="test" class="action的全类名">

<!--使用拦截器-->

<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="myInterceptor"></interceptor-ref>
<result name="loginError">/jsp/login.jsp</result>

<!--需要拦截的内容-->

<result name="ok">业务成功后的jsp</result>

<result name="error">业务失败后的jsp</result>

</action>

</struts>


以上便实现了基于struts2的强制登陆。

1 1
原创粉丝点击