【问题标题】:How to redirect to a custom page on @PreAuthorize error?如何在 @PreAuthorize 错误时重定向到自定义页面?
【发布时间】:2019-11-10 17:27:48
【问题描述】:

我想在 @PreAuthorize 身份验证错误时重定向到自定义 html 页面?

我使用@PreAuthorize("hasRole("Admin")) 作为控制器。因此,如果用户没有管理员角色,他将被踢出,我会看到身份验证错误。

但我想覆盖此身份验证失败错误并将用户重定向到我开发的标准错误页面。

有解决办法吗?

代码:

@PreAuthorize("hasRole("Admin")){
public List<Object> getBooksList(String id){
    return bookList;
}

【问题讨论】:

    标签: spring-boot spring-security spring-annotations


    【解决方案1】:

    首先要注意的是@PreAuthorize 注释链接到一个AuthenticationManager 可以用GlobalMethodSecurityConfiguration 设置。

    您可以使用带注释的@ControllerAdvice 类捕获身份验证异常,但您必须注册一个新的AuthenticationEntryPoint

    方法如下:Handle spring security authentication exceptions with @ExceptionHandler

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-23
      • 2022-10-12
      • 2015-08-01
      • 1970-01-01
      相关资源
      最近更新 更多