RememberMe Spring Security:多次调用成功处理程序

时间:2015-07-29 16:21:40

标签: java spring tomcat authentication

我正在通过Tomcat7运行Spring应用程序。我在Spring中使用以下bean在security.xml中实现了一个记住我的服务:

<remember-me key="SOMEKEY" user-service-ref="defaultUserService"
             authentication-success-handler-ref="rememberMeAuthenticationSuccessHandler" />

我的成功控制器如下所示:

@Service
public class RememberMeAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    @Autowired
    private UserService userService;

    private static final Logger logger = LoggerFactory.getLogger(RememberMeAuthenticationSuccessHandler.class);

    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        User authenticatedUser = (User)authentication.getPrincipal();

        if (authenticatedUser != null) {
            logger.info("Successfully auto-logged in user: " + authenticatedUser.getUsername());
            authenticatedUser.setLastLogin(new Date());
            userService.save(authenticatedUser);
        } else {
            logger.error("Auto-logged in user is empty!");
        }
    }
}

问题是回调多次运行,因此我将有四次访问数据库。

日志看起来像这样:

2015.07.29 18:13:28 [http-bio-8080-exec-9] INFO  Successfully auto-logged in user: t50@t.it
2015.07.29 18:13:28 [http-bio-8080-exec-1] INFO  Successfully auto-logged in user: t50@t.it
2015.07.29 18:13:28 [http-bio-8080-exec-7] INFO  Successfully auto-logged in user: t50@t.it
2015.07.29 18:13:28 [http-bio-8080-exec-6] INFO  Successfully auto-logged in user: t50@t.it

我认为每个帖子都有一个注册回调。我认为这与它是一种服务类型这一事实有关,但我需要它来自动装配userRepository

有没有办法优雅地解决这个问题?

非常感谢

1 个答案:

答案 0 :(得分:0)

这是不正常的,您是否可以记录有关请求的更多信息并再次检查您的客户端代码?

我很确定这是多个客户电话的结果