我有一個異常處理程序設置與下面的代碼春天@ExceptionHandler不趕AccessDeniedException異常
@ExceptionHandler(Throwable.class)
public @ResponseBody CustomResponse handleException(Throwable throwable){
// handles exception, returns a JSON
}
我使用的Spring Security 3.1。當我嘗試在沒有身份驗證的情況下執行操作時,應用程序拋出一個AccessDeniedException。它從來沒有涉及到這種方法。但與其他例外情況正常工作。它應該如何運作?或者我的代碼有問題嗎?
This看起來像一個解決方案。但如果我能在一個點上處理例外情況會更好。
這裏是我的配置文件
<global-method-security secured-annotations="enabled" pre-post-annotations="enabled" />
<http auto-config="true" use-expressions="true">
//intercept urls
<form-login login-page="/signin" default-target-url="/" always-use-default-target="true"/>
</http>
<authentication-manager>
<authentication-provider user-service-ref="userDetailsService">
<password-encoder ref="encoder" />
</authentication-provider>
</authentication-manager>
<!-- This encoder doesn't require a salt -->
<beans:bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />
UPDATE
在這裏,用戶沒有通過驗證(ROLE_ANONYMOUS)。當我嘗試訪問受保護的頁面時,它會將我重定向到登錄URL。我的問題在於,我做了一個AJAX調用。因此重定向到返回ModelAndView的方法不起作用。這裏有工作嗎?
謝謝你明白了。我想做我所做的其他所有例外,即返回一個JSON響應。有沒有辦法在AccessDeniedHandler中做類似的事情? – shazinltc
您可以編寫一個自定義'AuthenticationEntryPoint'並將其用在上面的代碼片段中。使用'Http403ForbiddenEntryPoint'作爲起點。 –
明白了:)我不必編寫自定義的AuthenticationEntryPoint。一個簡單的403響應就足夠了。謝謝btw。 – shazinltc