[英]Spring security custom exception handler with preauth
我正在嘗試使用Spring Security 3.1.2配置一些自定義異常處理。 我嘗試按照此處和此處找到的示例進行操作,但均無濟於事。 我是Spring Security的新手,我想知道這是否與我使用preauth過濾器有關。 我從AuthenticationUserDetailsService實現的loadUserDetails()方法中拋出了自定義異常。
public class AuthServiceImpl implements AuthenticationUserDetailsService<Authentication> {
@Autowired
private AuthDao authDao;
@Override
public UserDetails loadUserDetails(Authentication auth) throws UsernameNotFoundException {
Request req = (Request) auth.getPrincipal();
//get user details
User u = authDao.loadUser(req.getSessionId());
//check user rights for requested action
if(!u.getRights().contains(req.getAction()){
throw new CustomAuthException("User does not have permission to perform this action");
}
return u;
}
}
當引發異常時,我將獲得包含異常詳細信息的常規Tomcat 500頁面。 無論出於什么原因,我的自定義異常都不會得到處理。 我什至在自定義處理程序中添加了一些println(),甚至沒有被調用。
我開始懷疑這個方法是否以某種方式被排除在Spring的異常處理之外。 如果需要,我可以提供更多代碼示例,但是目前我不確定要共享的內容。
您使用SimpleMappingExceptionResolver 。 它是Spring MVC組件。 因此,當您在執行某些控制器期間遇到某些異常時,DispatcherServlet將調用SimpleMappingExceptionResolver。 問題是您的AuthenticationUserDetailsService實現僅在登錄操作期間使用。 並且該動作直接由Spring Security過濾器處理(不使用Spring MVC) 。 請求未到達DispatcherServlet,在這種情況下將永遠不會調用SimpleMappingExceptionResolver。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.