Paras
Paras

Reputation: 3481

Spring Security 5 : No Beans of type BCryptPasswordEncoder found

I'm trying to secure a REST API using JWT tokens. For saving encoded user password I'm trying to use bcrypt for encoding. However when I'm trying to autowire BCryptPasswordEncoder, I'm getting following error :

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

This is how my controller looks like :

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){

    }
}

This is the blog which I'm following : 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>

Has something change in Spring Security 5 regarding Bcrypt?

enter image description here

Upvotes: 12

Views: 16526

Answers (6)

Mateusz Mrozewski
Mateusz Mrozewski

Reputation: 2191

The article you mentioned describes it a little bit further:

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

And later in code

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

Did you follow these steps and do you define a BCryptPasswordEncoder class?

Upvotes: 33

Mounir bkr
Mounir bkr

Reputation: 1635

you have just add BCryptPasswordEncoder in your code or in a main class:

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

Upvotes: 0

JhonEx
JhonEx

Reputation: 11

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

This code worked for me by placing it main class of spring (Spring5MvcRestApplication)

Upvotes: 1

User
User

Reputation: 1628

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

This code works for me

Upvotes: 0

Aqib Nabi
Aqib Nabi

Reputation: 192

add it like this in your main application class

    @SpringBootApplication
    public class YourApplicationName{

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

Upvotes: 14

soufiane ELAMMARI
soufiane ELAMMARI

Reputation: 1029

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

add this injection in the same class ,cause spring could not inject automaticaly this dependency by using only autowired

Upvotes: 1

Related Questions