Spring security 为什么spring security中的BasicAuthenticationFilter只匹配用户名而不匹配密码

Spring security 为什么spring security中的BasicAuthenticationFilter只匹配用户名而不匹配密码,spring-security,Spring Security,BasicAuthenticationFilter,同时仅基于用户名检查现有身份验证匹配项。因此,如果传入的请求发送正确的用户名和错误的密码,用户仍然被认为已经通过了身份验证。这是你想要的行为吗?我觉得不对,有什么看法吗 //////////////////// private boolean authenticationIsRequired(String username) { // Only reauthenticate if username doesn't match S

BasicAuthenticationFilter,同时仅基于用户名检查现有身份验证匹配项。因此,如果传入的请求发送正确的用户名和错误的密码,用户仍然被认为已经通过了身份验证。这是你想要的行为吗?我觉得不对,有什么看法吗

////////////////////

private boolean authenticationIsRequired(String username) {
        // Only reauthenticate if username doesn't match SecurityContextHolder and user
        // isn't authenticated
        // (see SEC-53)
        Authentication existingAuth = SecurityContextHolder.getContext()
                .getAuthentication();

        if (existingAuth == null || !existingAuth.isAuthenticated()) {
            return true;
        }

        // Limit username comparison to providers which use usernames (ie
        // UsernamePasswordAuthenticationToken)
        // (see SEC-348)

        if (existingAuth instanceof UsernamePasswordAuthenticationToken
                && !existingAuth.getName().equals(username)) {
            return true;
        }

正如它在SecurityContextHolder.getContext().getAuthentication()中所示;这意味着客户端已经有了JSESSION cookie,因此它已经用用户名和密码进行了身份验证

再次检查密码将使此方法变得毫无价值,因为它会导致重新执行所有身份验证过程(从数据库检索密码,比较它…),我猜编写此方法是为了避免它。此外,密码不存储在SecurityContextHolder上下文中


CRSF攻击可能会出现潜在的安全漏洞,但spring security提供了针对该漏洞的保护。请参见

谢谢您提供的信息,我检查了相同的设置。我使用Postman测试REST API,如果我向同一用户传递了错误的密码,我将使用401。因为我之前在测试请求中提供了正确的密码,所以pwd错误的新请求也通过了。@dev.K几个月前我写了一篇关于RESTAPI的saml身份验证和jwt身份验证的文章。Saml在这里与您无关,但您可以查看jwt部分:。除了spring安全官方文档是必须阅读的。@dev.K如dur所述,您可以将安全设置为禁用有状态会话和csrf,例如:http.sessionManagement().sessionCreationPolicy(sessionCreationPolicy.STATELESS)和().csrf().disable()。感谢slemoine和dur的解释,通过postman测试,上述配置获得了预期的行为。