- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试过滤在我的应用程序中进行用户身份验证期间引发的 AuthenticationException。我知道这些不能用 @ControllerAdvice 和 @ExceptionHandler 进行过滤。因此,尝试找出任何处理程序都可以解决我的问题。
已经尝试了不同的方法,例如AuthenticationFailureHandler,但它们不符合我的要求,因为我正在使用ResourceServerConfigurerAdapter。
请提出建议。
最佳答案
Spring 安全异常由 ExceptionTranslationFilter 处理。您可以创建一个处理 AuthenticationException
的自定义过滤器,并将其添加到 ExceptionTranslationFilter
之后。默认 Spring 安全 Filter Ordering .
public class AuthenticationExceptionFilter extends GenericFilterBean {
@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
try {
chain.doFilter(request, response);
} catch (final Exception exception) {
if (exception instanceof AuthenticationException) {
this.logger.debug("Authentication exception occurred; redirecting to authentication entry point", exception);
}
if(exception instanceof AccessDeniedException) {
....
}
// Check ExceptionTranslationFilter#handleSpringSecurityException(...)
}
您可以通过重写 WebSecurityConfigurerAdapter 的配置方法以编程方式注册过滤器。 .
@Configuration
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.addFilterAfter(new AuthenticationExceptionFilter(), ExceptionTranslationFilter.class);
}
<小时/>
对于所有 @RequestMapping 的集中异常处理:查看ResponseEntityExceptionHandler
A convenient base class for @ControllerAdvice classes that wish to provide centralized exception handling across all @RequestMapping methods through @ExceptionHandler methods.
This base class provides an @ExceptionHandler method for handling internal Spring MVC exceptions.
下面是一个示例代码片段,可帮助您入门:
@ControllerAdvice
public class ExceptionHandler extends ResponseEntityExceptionHandler {
....
@ExceptionHandler({Exception.class})
public ResponseEntity<Object> handleCustomException(final CustomException exception, final WebRequest request) {
return handleExceptionInternal(exception,
ErrorOutputDto.create(exception.getErrorIdentifier(), exception.getMessage()),
new HttpHeaders(),
HttpStatus.UNAUTHORIZED,
request);
}
....
关于java - 过滤或处理 OAuth2 中的 AuthenticationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56061801/
我正在尝试连接到 SFTP 服务器。我有一个私钥和一个密码。我已经尝试阅读SO上的相关问题,但一直无法成功连接。 这是我试过的: pysftp.Connection(host=, username=,
我正在尝试为 ActiveDirectory 创建上下文(客户端和服务器都是 Windows),使用我的 Windows 凭据和 NTLM。 这是我的代码: public void func() {
是否有更简洁的方法来检查 javax.naming.AuthenticationException(或 spring)的主要失败原因? LDAP Wiki (Binding Errors) 中描述了不
INFO [client-001-job-3] o.j.s.s.impl.DataLoaderService - Using registration URL of http://localhost
我们将应用程序从 Spring & Hibernate 3.x 升级到 4.x。问题是 AuthenticationException.getAuthentication() 现在已被弃用。任何人都可
我正在尝试创建一个 AuthenticationException 的异常子类,如下所示: public class RegistrationNotCompleteException extends
这是极少数情况之一,几乎不可能重现,但我见过这种情况发生了 20 次中有 4 次。 这是我的 open_session 方法: def open_session: self.session =
我在使用 Paramiko(版本 1.7.6-2)ssh 客户端连接到设备时遇到问题: $ python Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56
我正在使用 MailKit 尝试通过 SMTP 通过交换服务器发送电子邮件,但当我尝试连接时出现以下错误: An exception of type 'System.Security.Authenti
我正在尝试过滤在我的应用程序中进行用户身份验证期间引发的 AuthenticationException。我知道这些不能用 @ControllerAdvice 和 @ExceptionHandler
我在一个项目中使用 Spring MVC 和 Spring Security,并用它实现一个登录表单。我遇到了一种奇怪的行为,这是我没有预料到的,我想知道是否有办法避免它。 当登录表单上出现身份验证错
当我尝试通过客户端连接 Hazelcast 服务器时,出现以下异常。 com.hazelcast.client.AuthenticationException: Invalid credentials
我正在尝试使用 Java 类 AuthenticationService 进行露天身份验证。这是代码: public void Authenticate() throws Authentication
我正在尝试使用 PushSharp 向我的应用程序发送推送通知。我有两个 Apple 帐户...一个是普通帐户,另一个是企业帐户。我在正常帐户上有一个开发人员证书可以使用,但我的开发和分发证书都无法在
我正在尝试使用 JAVA GSSAPI 执行 NTLM 绑定(bind)。 我收到此错误: javax.naming.AuthenticationException: GSSAPI [Root exc
我一直在使用 Kubernetes .net 客户端库版本来从容器内访问 Kubernetes api。今天我从客户端库版本 1.6.11 更新到 4.0.21 但这破坏了客户端身份验证的某些部分。
import paramiko host='x.x.x.x' port=22 username='root' password='password' cmd='dmidecode > a' ssh=p
我正在尝试定义一个默认入口点来处理AuthenticationException。 这是我的配置: @Override protected void configure(HttpSecurity ht
我在使用纯 Java 通过 ldap 连接到 Active Directory 时遇到问题。 如果 displayName 以 ,(逗号,例如“, name”)开头,我会得到一个 javax.nami
您好,我需要在 Spring 安全登录表单中添加一个新的异常,除了我想要自己的错误消息(直到现在它显示“错误的登录名/密码”)之外,一切正常。 我已经覆盖了用户名密码身份验证过滤器的默认尝试身份验证方
我是一名优秀的程序员,十分优秀!