【问题标题】:How to encrypte password using MD5 spring security如何使用 MD5 spring security 加密密码
【发布时间】:2023-03-30 11:32:01
【问题描述】:

这是我的代码:

PasswordEncoder encoder = new Md5PasswordEncoder();
String hashedPass = encoder.encodePassword("passwordTest", null);

这是我的 pom.xml 文件:

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-core</artifactId>
</dependency>

<dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
</dependency>

但是我收到了这个错误:

Md5PasswordEncoder cannot be resolved to a type

和:

The import org.springframework.security.authentication.encoding cannot be resolved

【问题讨论】:

  • 你不能用哈希加密一些东西。您可以散列密码,但请使用 SHA-3,而不是 MD5
  • @JCWasmx86 谢谢,但我想使用 Spring Security 的 MD5
  • 你好像只做了import org.springframework.security.authentication.encoding,试试import org.springframework.security.authentication.encoding.Md5PasswordEncoder
  • 使用org.springframework.security.crypto.password.DelegatingPasswordEncoder,可用于不同类型的算法。它默认使用 bcrypt,但支持 LDAP、MD4、MD5、SHA-1、SHA-256、SHA256 等。
  • @JCWasmx86 我做了,但我收到了这个错误:无法解析导入 org.springframework.security.authentication.encoding

标签: java spring spring-security md5


【解决方案1】:

不推荐使用旧的身份验证 PasswordEncoder 接口

package org.springframework.security.authentication.encoding;

@Deprecated
public interface PasswordEncoder {}

你应该使用:

package org.springframework.security.crypto.password;

//Implementation : BCryptPasswordEncoder
public interface PasswordEncoder {}

此用例使用BCryptPasswordEncoder 对密码“xxxxxx”进行哈希处理。

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

public static void main(String[] args) {

    int i = 0;
    while (i < 10) {
        String password = "xxxxxx";
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String hashedPassword = passwordEncoder.encode(password);

        System.out.println(hashedPassword);
        i++;
    }

  }
}

【讨论】:

    猜你喜欢
    • 2010-12-21
    • 2012-03-28
    • 1970-01-01
    • 2018-12-10
    • 2011-12-24
    • 2012-09-21
    • 2015-06-12
    • 2015-01-13
    • 1970-01-01
    相关资源
    最近更新 更多