323.4. 处理身份验证和授权错误

如果 SpringSecurityAuthorizationPolicy 中的身份验证或授权失败,则将抛出 CamelAuthorizationException。这可使用 Camel 的标准异常处理方法处理方法,如 Exception Clause。CamelAuthorizationException 将具有对策略 ID 的引用,该策略会引用该例外情况,因此您可以根据策略处理错误以及异常类型:

<onException>
  <exception>org.springframework.security.authentication.AccessDeniedException</exception>
  <choice>
    <when>
      <simple>${exception.policyId} == 'user'</simple>
      <transform>
        <constant>You do not have ROLE_USER access!</constant>
      </transform>
    </when>
    <when>
      <simple>${exception.policyId} == 'admin'</simple>
      <transform>
        <constant>You do not have ROLE_ADMIN access!</constant>
      </transform>
    </when>
  </choice>
</onException>