java - Spring 安全5 : No Beans of type BCryptPasswordEncoder found

标签 java spring spring-boot spring-security

我正在尝试使用 JWT token 保护 REST API 的安全。为了保存编码的用户密码,我尝试使用 bcrypt 进行编码。但是,当我尝试 Autowiring BCryptPasswordEncoder 时,出现以下错误:

Could not autowire. No beans of BCryptPasswordEncoder type found. Check autowiring problems in bean class.

这就是我的 Controller 的样子:

package com.starter.springsecurity.demo.controller;

import com.starter.springsecurity.demo.domain.User;
import com.starter.springsecurity.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserRepository userRepository;

    private BCryptPasswordEncoder bCryptPasswordEncoder;

    public UserController(BCryptPasswordEncoder bCryptPasswordEncoder){
        this.bCryptPasswordEncoder = bCryptPasswordEncoder;
    }

    @RequestMapping("/register")
    public void register(@RequestBody User user){

    }
}

这是我正在关注的博客: https://dzone.com/articles/implementing-jwt-authentication-on-spring-boot-api

Pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.starter.springsecurity</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Spring Security demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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


</project>

Spring Security 5 中有关 Bcrypt 的内容有变化吗?

enter image description here

最佳答案

您提到的文章对此进行了进一步描述:

There is no default instance of BCryptPasswordEncoder that can be injected in the UserController class

稍后在代码中

@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
    return new BCryptPasswordEncoder();
}

您是否按照这些步骤操作并定义了 BCryptPasswordEncoder 类?

关于java - Spring 安全5 : No Beans of type BCryptPasswordEncoder found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49116485/

相关文章:

java - 如何按某些属性对对象列表进行排序

java - spring 为带注释的方法强制执行事务逻辑

java - 使用 log4j 将 spring 消息从 sysout 获取到文件

java - 我可以否定(!) Spring 配置文件的集合吗?

java - Tomcat - 没有领域的 SSO?

java - 如何处理不可中断阻塞(ExecutorService)

java - 将资源包读取为 UTF-8。 getString() 方法似乎将编码更改为 ISO-8859

java - eureka 中所有注册微服务的 swagger 配置

java - 在 Spring Controller 中的请求之间共享数组列表

java - Spring Boot 。如何将 Optional<> 传递给实体类