0

将 Spring (4.2.4) 与 MVC (4.2.4) 和 Security (4.0.3) 一起使用。我已经实现了一个 AccessDecisionManager 并且在我的决定方法中我抛出了一个异常:

public void decide(
    Authentication authentication, 
    Object object,
    Collection<ConfigAttribute> configAttributes
)   throws AccessDeniedException, InsufficientAuthenticationException {
        FilterInvocation fi = (FilterInvocation) object;
        String requestUrl = fi.getRequestUrl();
    ...
    throw new SessionCompanyNotRoleTableCompanyException(1, 2);
    ...
    throw new AccessDeniedException("Access denied!");
}

我既不能抓住“ SessionCompanyNotRoleTableCompanyException”也不能AccessDeniedException。我试过使用全局异常处理程序:

@Component
@ControllerAdvice
public class GlobalExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

@ExceptionHandler(SessionCompanyNotRoleTableCompanyException.class)
public ModelAndView sessionCompanyNotRoleTableCompany() {
    log.debug("SessionCompanyNotRoleTableCompanyException captured in GlobalExceptionHandler");
    String reason = "Reason: SessionCompanyNotRoleTableCompanyException";
    ModelAndView mav = new ModelAndView();
    mav.addObject("reason", reason);
    mav.setViewName("error.html");
    return mav;             
}

@ExceptionHandler(Exception.class)
public ModelAndView exception(ModelMap model) {
    log.debug("Exception captured in GlobalExceptionHandler");
    String reason = "General Exception";
    ModelAndView mav = new ModelAndView();
    mav.addObject("reason", reason);
    mav.setViewName("error.html");
    return mav;             
}
}

我什至创建了 ExceptionResolver 类,例如:

@Component
public class SessionCompanyNotRoleTableCompanyExceptionResolver implements HandlerExceptionResolver, Ordered {

    private static final Logger log = LoggerFactory.getLogger(SessionCompanyNotRoleTableCompanyExceptionResolver.class);

    private int order;

    @Override
    public ModelAndView resolveException(
        HttpServletRequest request, 
        HttpServletResponse response, 
        Object handler,
        Exception ex
    ) {
        if (ex instanceof SessionCompanyNotRoleTableCompanyException) {
            log.debug("SessionCompanyNotRoleTableCompanyException captured in SessionCompanyNotRoleTableCompanyExceptionResolver");
            String reason = "Reason: SessionCompanyNotRoleTableCompanyException";
            ModelAndView mav = new ModelAndView();
            mav.addObject("reason", reason);
            mav.setViewName("error.html");
            return mav;             
        }
        return null;
    }

    @Override
    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }

}

...并让它们在我的 web-config-class 中初始化,例如:

@Bean
public SessionCompanyNotRoleTableCompanyExceptionResolver createSessionCompanyNotRoleTableCompanyExceptionResolver() {
    SessionCompanyNotRoleTableCompanyExceptionResolver resolver = new SessionCompanyNotRoleTableCompanyExceptionResolver();
    resolver.setOrder(1);
    return resolver;
}

这些工作,即只有从控制器抛出异常时才会捕获异常。但不是decide来自AccessDecisionManager.

我应该如何以及如何实现可以在控制器外部(之前)捕获这些的东西?

编辑(添加SessionCompanyNotRoleTableCompanyException以向您显示其定义):

public class SessionCompanyNotRoleTableCompanyException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    public SessionCompanyNotRoleTableCompanyException(Long contextCompanyId, Long tableId) {
        super("Context companyId: " + contextCompanyId + ", tableId: " + tableId);
    }

}
4

0 回答 0