【问题标题】:Encoded password does not look like BCrypt - spring security version is 5.3.2编码密码看起来不像 BCrypt - spring security 版本是 5.3.2
【发布时间】:2020-04-13 21:22:33
【问题描述】:

发布 API 后,我在验证用户时遇到问题,我收到此错误, 我的数据库是从基于 PHP 的源代码恢复的,哈希密码以:

开头
$2y$

我不想更新我的数据库,有什么办法可以解决这个错误吗?

更多信息:我的春季版本是 5.2.6.BUILD-SNAPSHOT 和我的 spring 安全版本是 5.3.2.BUILD-SNAPSHOT

Pom 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.Loyalty</groupId>
    <artifactId>OpenLoyalty</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>OpenLoyalty</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring.version>5.2.6.BUILD-SNAPSHOT</spring.version>
        <spring-security.version>5.3.2.BUILD-SNAPSHOT</spring-security.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.200</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>

            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>

        </dependency>

        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

【问题讨论】:

    标签: spring-boot authentication spring-security jwt bcrypt


    【解决方案1】:

    您可以使用 BCryptVersion 枚举指定要与 Spring Security 的 BCryptPasswordEncoder 一起使用的 bcrypt 版本。这是 5.2.0 版中的新增功能。默认情况下,版本设置为$2A。因此,您无法在您的应用中进行身份验证。

    您的安全配置如下所示:

    ...
    import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder.BCryptVersion;
    
    @Configuration
    public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            ...
        }
    
        @Bean
        public UserDetailsService userDetailsService() {
            ...
        }
    
        @Bean
        public PasswordEncoder passwordEncoder() {
            return new BCryptPasswordEncoder(BCryptVersion.$2Y); // specified your bcrypt version here
        }
    
    }
    

    【讨论】:

    • 感谢您的回答,但我无法导入 org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder.BCryptVersion;我在 Question 中添加了我的 pom 文件。
    • 我从你的 pom 中看到你正试图在你的 pom 中使用BUILD-SNAPSHOT w/o Spring Snapshots &lt;repository&gt;。因此,您的 Spring Security 版本实际上是 Spring Boot 2.1.x 中使用的版本,而不是您的快照覆盖,即 5.1.x,请参阅here。更重要的是,Spring Boot 2.1.x 不打算与那些被覆盖的版本一起使用,并且可能会遇到问题。我建议升级到 Spring Boot 2.2 以使用具有 BCryptVersion 枚举的 Spring Security 5.2。
    猜你喜欢
    • 2021-03-20
    • 2020-05-26
    • 2019-03-25
    • 2021-12-12
    • 2018-10-09
    • 2017-04-03
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多