自定义登录拦截器

package com.cvicse.interceptor;

 

import javax.servlet.http.HttpServletResponse;

 

import org.apache.struts2.ServletActionContext;

 

import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

 

public class AuthrityInterceptor extends AbstractInterceptor {

 

private static final long serialVersionUID = 4297635740201526003L;

 

@Override

public String intercept(ActionInvocation invocation) throws Exception {

System.out.println("hello");

ActionContext ctx = invocation.getInvocationContext();

String user = (String)ctx.getSession().get("user");

System.out.println(user);

if(user != null){

return invocation.invoke();

}

HttpServletResponse response = ServletActionContext.getResponse();

String rootPath = ServletActionContext.getRequest().getContextPath();

response.sendRedirect(rootPath + "/login.jsp");

 

return null;

}

 

}



<?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>
<package name="user" extends="struts-default">
<interceptors>
<interceptor name="authrity" class="com.cvicse.interceptor.AuthrityInterceptor" >
<param name="login">/login.jsp</param>
</interceptor>
<interceptor-stack name="authrityStack">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="authrity" />
</interceptor-stack>
</interceptors>

<!-- <default-interceptor-ref name="authrityStack" />  -->

<global-results>
<result name="success">/success.jsp</result>
</global-results>

<action name="regist" class="com.cvicse.action.RegistAction">
</action>

<action name="login" class="com.cvicse.action.LoginAction">
<interceptor-ref name="authrityStack" />
</action>
</package>
</struts>

你可能感兴趣的:(自定义登录)