今天写了基于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>
首先建立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>