java Spring 安全5:未找到BCryptPasswordEncoder类型的Bean

hgb9j2n6  于 2022-12-17  发布在  Java
关注(0)|答案(6)|浏览(313)

我尝试使用JWT令牌来保护REST API。为了保存编码的用户密码,我尝试使用bcrypt进行编码。然而,当我尝试自动连接BCryptPasswordEncoder时,我收到以下错误:

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

这是我的控制器的外观:

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的内容有什么变化吗?

vh0rcniy

vh0rcniy1#

你提到的那篇文章描述得更深入一点:
没有可以注入UserController类的BCryptPasswordEncoder的默认示例
稍后在代码中

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

您是否遵循了这些步骤并定义了BCryptPasswordEncoder类?

dwthyt8l

dwthyt8l2#

像这样将其添加到主应用程序类中

@SpringBootApplication
    public class YourApplicationName{

        public static void main(String[] args) {
            SpringApplication.run(MobileAppWsApplication.class, args);
        }
        @Bean
        public BCryptPasswordEncoder bCryptPasswordEncoder() {
            return new BCryptPasswordEncoder();
        }
}
zour9fqk

zour9fqk3#

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

在同一个类中添加此注入,因为spring无法仅使用autowired自动注入此依赖项

kmbjn2e3

kmbjn2e34#

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

字符串
这段代码通过将其放置在spring的主类(Spring5MvcRestApplication)中来为我工作

ia2d9nvy

ia2d9nvy5#

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

这个代码对我有效

emeijp43

emeijp436#

您刚刚在代码或主类中添加了BCryptPasswordEncoder:

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

相关问题