This is how I handle access control in Struts2. It is really easy and quite possible to use:
First create an interface called SecurityCheckAware.
public interface SecurityCheckAware {
void checkRight();
}
Then create an interceptor called SecurityCheckInterceptor.
public class SecurityCheckInterceptor extends AbstractInterceptor {
@Override
public String intercept(final ActionInvocation invocation) throws Exception {
if (invocation.getAction() instanceof SecurityCheckAware) {
SecurityCheckAware action = (SecurityCheckAware) invocation.getAction();
action.checkRight();
}
return invocation.invoke();
}
}
.
, , SecurityCheckAware. :
@Override
public void checkRight() {
User loginUser = (User) session.get("user");
if (loginUser != null && loginUser.getRole().getId() != Constant.AUTHORITY_ADMIN) {
throw new AccessViolation("You do not have permission to access this page.");
}
}
, RuntimeException ( ). AccessViolation.
, AccessViolation struts.xml, :
<global-results>
<result name="accessDenied">/WEB-INF/jsp/accessDenied.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="com.example.AccessViolation" result="accessDenied"/>
</global-exception-mappings>
. SecurityCheckAware SecurityCheckInterceptor Preparable PrepareInterceptor, .
/ ( ), , .