sturts2 需要加一个拦截器在action中做登录验证校验,主要的思想是登录后在session中存储一个标志,然后在过滤器中验证这个标志,如果有则通过验证。如果没有,则返回到登录页面。
过滤器代码如下,我把标志就设置为username,需要修改的同学自己改名字
package com.tc.blacktea.util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.ServletRedirectResult;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class LoginInterceptor implements Interceptor {
private static final long serialVersionUID = -2255797147687651066L;
private static final Log log = LogFactory.getLog(LoginInterceptor.class);
public String intercept(ActionInvocation invocation) throws Exception {
final ActionContext context = invocation.getInvocationContext();
HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
HttpSession session = request.getSession();
String username=(String)session.getAttribute("username");
if (username == null) {
ServletRedirectResult result = new ServletRedirectResult();
result.setLocation("/toLogin.action");
try {
result.execute(invocation);
} catch (Exception ex) {
log.error("Unable to create debugging console", ex);
}
return Action.NONE;
}
return invocation.invoke();
}
public void destroy() {
}
public void init() {
}
}
在struts的总配置文件struts.xml里增加对拦截器的引用
<package name="blakctea-default" extends="struts-default">
<interceptors>
<interceptor name="loginInterceptor" class="com.tc.blacktea.util.LoginInterceptor">
<param name="loginActionName">/login.action</param>
</interceptor>
</interceptors>
</package>
注意这个package的name,在需要引入这个拦截器的action的配置文件中,首先要extends这个package的name,然后在action中<interceptor-ref >引入拦截器
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="advstoreclass" extends="blacktea-defalut" namespace="/advstoreclass">
<action name="storeclass" class="com.tc.blacktea.adv.action.StoreClassAction">
<!-- 定义转发路径对应的字符串名 -->
<result name="success">/jsp/storeclass/storeclasslist.jsp</result>
<result name="queryOne">/jsp/storeclass/storeclass.jsp</result>
<interceptor-ref name="loginInterceptor"></interceptor-ref>
</action>
</package>
</struts>
此时拦截器已经被引入,注意到我们的拦截器类中
result.setLocation("/toLogin.action");
这句话,所以要有一个配置文件和类专门处理toLogin这个action
新建一个struts-login.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="toLogin" namespace="/" extends="struts-default">
<action name="toLogin" class="com.tc.blacktea.login.action.LoginAction" method="toLogin">
<result>/jsp/index.jsp</result>
</action>
</package>
</struts>
对应的class需要新建一个LoginAction来做跳转
package com.tc.blacktea.login.action;
import com.tc.blacktea.util.BaseAction;
public class LoginAction extends BaseAction {
private static final long serialVersionUID = 5555663601912656942L;
public String toLogin(){
return SUCCESS;
}
}
大功告成。1.需要注意我的登录页面是在jsp/index.jsp下以此对应了sturts-login.xml。同时登录成功后,要在session中放入username这个属性。
Map session=(Map)ActionContext.getContext().get(ActionContext.SESSION);
session.put("username", username);