tags:

views:

390

answers:

0

Acegi provides ExceptionTranslationFilter for handling authentication or access denied exceptions. We can use Struts 2 RolesInterceptor raise these exception and there by delegating authentication and authorization to Acegi.

RolesInterceptor checks using isUserInRole method in the servlet request and calls handleRejection method. SecurityContextHolderAwareRequestFilter provided by Acegi needs to be configured in the filter chain in order to wrap the isUserInRole method. Then override handleRejection method of RolesInterceptor as below:

protected String handleRejection(final ActionInvocation invocation,
        final HttpServletResponse response) throws Exception {
    //throw respective exceptions so that Acegi exception translator will handle.
    if (SecurityContextHolder.getContext().getAuthentication() == null) {
        throw new AuthenticationCredentialsNotFoundException("Credential not found...");
    }
    throw new AccessDeniedException("Access is denied...");
}