【问题标题】:Spring Security 5 : No Beans of type BCryptPasswordEncoder foundSpring Security 5:找不到 BCryptPasswordEncoder 类型的 Bean
【发布时间】:2018-08-13 11:08:01
【问题描述】:

我正在尝试使用 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 有什么变化吗?

【问题讨论】:

    标签: java spring spring-boot spring-security


    【解决方案1】:

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

    在 UserController 类中没有可以注入的 BCryptPasswordEncoder 的默认实例

    稍后在代码中

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

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

    【讨论】:

      【解决方案2】:
          @Bean
      public BCryptPasswordEncoder bCryptPasswordEncoder() {
          return new BCryptPasswordEncoder();
      }
      

      在同一个类中添加这个注入,因为 spring 不能通过只使用 autowired 来自动注入这个依赖

      【讨论】:

        【解决方案3】:

        像这样在你的主应用程序类中添加它

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

        【讨论】:

        • 好消息!在过去的 3 个小时里,我一直只将它添加到我的 SecurityConfig.java 文件中,一遍又一遍。关于这个问题,这种类型的“添加 bean”建议遍布 Stack Overflow,但没有人提到您应该将 bean 添加到您的主应用程序类中。为我工作!
        【解决方案4】:
        @Bean
        public BCryptPasswordEncoder bCryptPasswordEncoder() {
            return new BCryptPasswordEncoder();
        }
        

        此代码适用于我

        【讨论】:

          【解决方案5】:
          @Bean
          public BCryptPasswordEncoder bCryptPasswordEncoder() {
              return new BCryptPasswordEncoder();
          }
          

          这段代码对我有用,因为它是 spring 的主类 (Spring5MvcRestApplication)

          【讨论】:

            猜你喜欢
            • 2021-05-31
            • 2021-09-15
            • 1970-01-01
            • 1970-01-01
            • 2015-01-20
            • 1970-01-01
            • 1970-01-01
            • 2018-06-12
            • 2013-10-08
            相关资源
            最近更新 更多