【问题标题】:Digest authentication with Spring Security, BCrypt and Spring Data Rest使用 Spring Security、BCrypt 和 Spring Data Rest 进行摘要式身份验证
【发布时间】:2016-11-09 18:50:12
【问题描述】:

我已经在 spring security 中实现了一个 Digest 身份验证,它工作正常,直到我将它设置为使用 BCrypt 加密。

    @Bean
    public DigestAuthenticationEntryPoint digestEntryPoint() {
        DigestAuthenticationEntryPoint digestAuthenticationEntryPoint = new DigestAuthenticationEntryPoint();
        digestAuthenticationEntryPoint.setKey("myKey");
        digestAuthenticationEntryPoint.setRealmName("Digest Realm");
        return digestAuthenticationEntryPoint;
    }

    @Bean
    public DigestAuthenticationFilter digestAuthenticationFilter(
            DigestAuthenticationEntryPoint digestAuthenticationEntryPoint) {
        DigestAuthenticationFilter digestAuthenticationFilter = new DigestAuthenticationFilter();
        digestAuthenticationFilter.setAuthenticationEntryPoint(digestEntryPoint());
//      digestAuthenticationFilter.setPasswordAlreadyEncoded(true);
        digestAuthenticationFilter.setUserDetailsService(userDetailsServiceBean());
        return digestAuthenticationFilter;
    }

这些是我设置为启用摘要并与它们一起使用的 bean:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .exceptionHandling()
            .authenticationEntryPoint(digestEntryPoint())
        .and()
        .addFilter(digestAuthenticationFilter(digestEntryPoint()))
        //.httpBasic()
        //.and()
        .antMatcher("/**")
        .csrf()
            .disable()
            .authorizeRequests()
            .anyRequest()
            .authenticated()
        .and()
            .formLogin()
            .permitAll()
        .and()
        .logout()
            .deleteCookies("remove")
            .invalidateHttpSession(true)
            .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
            .logoutSuccessUrl("/login")
            .permitAll();
    }

问题是服务器端生成的MD5响应和我自己的响应不匹配。 在 DigestAuthenticationFilter.java 中

if (!serverDigestMd5.equals(digestAuth.getResponse())) {
            if (logger.isDebugEnabled()) {
                logger.debug("Expected response: '" + serverDigestMd5
                        + "' but received: '" + digestAuth.getResponse()
                        + "'; is AuthenticationDao returning clear text passwords?");
            }

            fail(request,
                    response,
                    new BadCredentialsException(messages.getMessage(
                            "DigestAuthenticationFilter.incorrectResponse",
                            "Incorrect response")));
            return;
        }

服务器“serverDigestMd5”使用散列密码创建 md5digest,但在客户端(使用邮递员)我使用无盐密码,这就是生成响应的方式。如果我在客户端使用加盐密码,它可以工作,但这不是很可选。 有没有办法让它在不使用客户端加盐密码的情况下工作?

【问题讨论】:

标签: spring spring-security spring-boot spring-data-rest


【解决方案1】:

BCrypt 不适用于 Digest 身份验证,事实上,Digest 身份验证必须以纯文本形式访问密码(因此您必须将其保存为纯文本或使用可逆算法(如 Base64、MD5 等)对其进行编码,BCrypt不能反转,因为它更安全。Spring Security 文档指出:

如果 DigestAuthenticationFilter.passwordAlreadyEncoded 设置为 true,则可以使用 HEX( MD5(username:realm:password) ) 格式对密码进行编码。但是,其他密码编码不适用于摘要式身份验证。

【讨论】:

    猜你喜欢
    • 2014-07-03
    • 1970-01-01
    • 2019-05-29
    • 2014-03-18
    • 1970-01-01
    • 2018-11-07
    • 2012-11-27
    • 2011-10-17
    相关资源
    最近更新 更多