![](/img/trans.png)
[英]Spring Webflux - Proper way to throw checked custom exception (not RuntimeException)
[英]Throw and handle custom exception during authentication Spring Security + WebFlux
我正在嘗試在身份驗證期間在WebFlux中引發自定義異常,並使用ControllerAdvice( @ExceptionHandler
)處理它。 不幸的是,它不會傳播,如果拋出異常,則返回HTTP 500;如果將異常返回為Mono.error(),則返回HTTP 401。
@Override //in authentication service
public Mono<UserDetails> findByUsername(String username) {
//find user by username from database,
//if not enabled, throw a custom exception,
//if doesn't exist, throw UsernameNotFoundException,
//return org.springframework.security.core.userdetails.User otherwise.
}
@ExceptionHandler //in controller advice
public Mono<HttpStatus> handleException(MyCustomExceptionThrownFromFindByUsername ex) {
//implemented
}
有什么方法可以幫助將異常添加到ExceptionHandler?
UserDetailsService
( 反應式和非反應式 )都具有根據username
檢索用戶的任務。 一無所有,一無所有。 將檢查是否啟用了用戶委托給UserDetailsChecker
,后者在UserDetails
實現上調用某些方法,並將相應地做出反應。 不要在這里嘗試做更多的事情,因為那不是UserDetailsService
的任務。
默認的UserDetails
, User
實現有2個構造函數,一個帶有3個構造函數,一個帶有7個參數。 使用第二個來根據您的業務規則正確填充已enabled
,其余的將由Spring Security來完成。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.