【发布时间】:2015-07-28 11:41:42
【问题描述】:
如果用户尝试使用错误的凭据进行身份验证,我想记录。因此,我已将此事件侦听器类添加到我的项目中:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent;
import org.springframework.stereotype.Component;
@Component
public class AuthenticationFailureListener
implements ApplicationListener<AuthenticationFailureBadCredentialsEvent>{
private final Logger logger = LoggerFactory.getLogger(getClass());
@Override
public void onApplicationEvent(AuthenticationFailureBadCredentialsEvent event) {
System.out.println("test");
logger.info("test2");
}
}
问题是它根本不起作用。我使用 Spring Security 默认登录页面。该页面在使用错误凭据时显示“错误凭据”错误,但我上面的方法没有被调用。 我有一个非常相似的成功事件监听器代码,效果很好:
@Component
public class AuthenticationSuccessListener implements
ApplicationListener<InteractiveAuthenticationSuccessEvent> {
private final Logger logger = LoggerFactory.getLogger(getClass());
@Autowired private UserService users;
@Override
public void onApplicationEvent(InteractiveAuthenticationSuccessEvent event) {
User user = users.get(event.getAuthentication().getName());
boolean isAdmin = user.getRole().equals(User.ROLE_ADMIN);
logger.info((isAdmin ? "Admin" : "User") + " with id " + user.getIdLink()
+ " has successfully logged in!");
}
}
这是我的 Spring Security Java 配置:
@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{
@Autowired
private CustomUserDetailsService userDetailsService;
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.formLogin()
.and()
.httpBasic();
}
}
我不知道这里发生了什么,非常感谢您的帮助!
春季版:4.0.9
Spring Security 版本:3.2.5(也试过 4.0.1)
编辑:
好的,我将 Spring 的日志级别设置为 DEBUG,但没有。我搜索了“Listener”的每一次出现,并且日志指出 AuthenticationFailureListener 以及 AuthenticationSuccessListeners 的实例已创建且没有任何错误。
我什至将日志放入 diff 工具中(在替换所有时间和审查之后),并与注释掉 FailureListener 代码的代码版本进行比较,但没有找到任何东西。想的话可以自行搜索:
https://www.diffchecker.com/cwdn4sp4
在页面底部,您会在左侧找到纯日志文本。
Edit2:部分解决
Serges 解决方案有帮助,这是我对 onAuthenticationFailure 方法的完整实现:
@Override
public void onAuthenticationFailure(
HttpServletRequest request, HttpServletResponse response,
AuthenticationException exception) throws IOException, ServletException {
if (exception instanceof BadCredentialsException) {
String name = request.getParameter("username");
String password = request.getParameter("password");
Authentication auth =
new UsernamePasswordAuthenticationToken(name, password);
eventPublisher.publishEvent(
new AuthenticationFailureBadCredentialsEvent(auth, exception));
}
super.onAuthenticationFailure(request, response, exception);
}
【问题讨论】:
-
您是否设置了适当的组件扫描,以便将您的
AuthenticationFailureListener创建为 bean?启用 Spring 调试日志记录以验证它是否实际创建。这将为您/我们提供更多信息。 -
FailureListener 与 SuccessListener 位于同一个包中,因此 scan 应该同时适用于两者。我现在尝试调试日志记录,谢谢!
标签: java spring spring-security