Josep García
2011-12-01 10:30:22 UTC
For Struts2 actions to correctly show 403 page and return 403 status when
an AccessDeniedException is thrown within actions code.
Must be added to the defaultStack of your struts.xml file.
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.springframework.security.access.AccessDeniedException;
/**
* Correctly report spring-security's AccessDeniedException thrown from
within Struts actions as 403 error
* @author jgarcia
*/
public class AccessDeniedInterceptor implements Interceptor {
public String intercept(ActionInvocation invocation) throws Exception {
try {
return invocation.invoke();
} catch (AccessDeniedException e) {
HttpServletResponse response =
ServletActionContext.getResponse();
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return null;
}
}
/**
* This method currently does nothing.
*/
public void destroy() {
}
/**
* This method currently does nothing.
*/
public void init() {
}
}
Cheers,
Josep
an AccessDeniedException is thrown within actions code.
Must be added to the defaultStack of your struts.xml file.
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.springframework.security.access.AccessDeniedException;
/**
* Correctly report spring-security's AccessDeniedException thrown from
within Struts actions as 403 error
* @author jgarcia
*/
public class AccessDeniedInterceptor implements Interceptor {
public String intercept(ActionInvocation invocation) throws Exception {
try {
return invocation.invoke();
} catch (AccessDeniedException e) {
HttpServletResponse response =
ServletActionContext.getResponse();
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return null;
}
}
/**
* This method currently does nothing.
*/
public void destroy() {
}
/**
* This method currently does nothing.
*/
public void init() {
}
}
Cheers,
Josep