TA的每日心情 | 开心 2021-12-13 21:45 |
---|
签到天数: 15 天 [LV.4]偶尔看看III
|
1,创建一个继承自com.opensymphony.xwork2.interceptor.MethodFilterInterceptor的拦截器类 :CheckLoginInterceptor.java如下:
import javax.Servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.bgi.commons.util.CommonParamsGetter;
import org.bgi.pg.domain.Customer;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
/**
* 拦截未登录的访问
*
*/
public class CheckLoginInterceptor extends MethodFilterInterceptor{
private static final long serialVersionUID = 5933090591407046439L;
@Override
public String doIntercept(ActionInvocation invocation) throws Exception {
HttpSession session = ServletActionContext.getRequest().getSession();
Customer customer = (Customer) session.getAttribute(CommonParamsGetter.SESSION_USER_INFO);
Boolean loginFlag = true;
if(customer == null){
HttpServletRequest request = ServletActionContext.getRequest();
String path = request.getContextPath();
String basePath = request.getScheme() + “://” + request.getServerName() + “:” + request.getServerPort() + path + “/”;
HttpServletResponse response = ServletActionContext.getResponse();
// response.addHeader(“_timeout”,”true”);
loginFlag = false;
}
session.setAttribute(CommonParamsGetter.SESSION_LOGIN_INFO, loginFlag);
return invocation.invoke();
}
}
2,在 spring配置文件中配置该实体。
<bean id=”checkLoginIntercepter” class=”org.*.*.CheckLoginInterceptor”/>
3,在struts.xml中配置该拦截器
<interceptors>
<interceptor name=”checkLogin” class=”checkLoginIntercepter”>
</interceptor>
<interceptor-stack name=”bgiDefaultStack” >
<interceptor-ref name=”checkLogin” />
<interceptor-ref name=”defaultStack” />
</interceptor-stack>
</interceptors> |
|