gpt4 book ai didi

Spring Security 身份验证日志记录

转载 作者:行者123 更新时间:2023-12-04 16:22:20 24 4
gpt4 key购买 nike

我正在使用 Spring Security 3.1 对网站的用户进行身份验证。当由于 spring security 无法连接到数据库而导致登录失败时,我在日志中得到以下语句:

2012-07-12 11:42:45,419 [ajp-bio-8009-exec-1] DEBUG      org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter - Authentication request failed: org.springframework.security.authentication.AuthenticationServiceException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Connections could not be acquired from the underlying database!

我的问题是,为什么这是 DEBUG 语句而不是 ERROR?我必须通过大量的调试语句才能找到实际的错误。

编辑

这是我的身份验证管理器:
<bean id="securityDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/securityDS"/>
<property name="resourceRef" value="true"/>
</bean>

<bean id="encoder" class="org.springframework.security.crypto.password.StandardPasswordEncoder" />

<security:authentication-manager>
<security:authentication-provider>
<security:password-encoder ref="encoder" />
<security:jdbc-user-service
data-source-ref="securityDataSource"
authorities-by-username-query="SELECT username, authority FROM login WHERE username = ?"
users-by-username-query="SELECT username, password, enabled FROM login WHERE username = ?"
/>
</security:authentication-provider>
</security:authentication-manager>

最佳答案

我的解决方案:

@Component
public class AuthenticationEventListener implements ApplicationListener<AbstractAuthenticationEvent> {

private static Logger logger = Logger.getLogger(AuthenticationEventListener.class);

@Override
public void onApplicationEvent(AbstractAuthenticationEvent authenticationEvent) {
if (authenticationEvent instanceof InteractiveAuthenticationSuccessEvent) {
// ignores to prevent duplicate logging with AuthenticationSuccessEvent
return;
}
Authentication authentication = authenticationEvent.getAuthentication();
String auditMessage = "Login attempt with username: " + authentication.getName() + "\t\tSuccess: " + authentication.isAuthenticated();
logger.info(auditMessage);
}

}

不需要其他配置。

关于Spring Security 身份验证日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11456774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com