0

I have this controller to create users:

@PostMapping
public ResponseEntity<?> insert(@Valid @RequestBody UserDTO userDTO,
        UriComponentsBuilder uriInfo) {

    User user = UserDTO.getEntity(userDTO);
    final Long id = service.insert(user);

    final URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(id).toUri();

    return ResponseEntity.created(location).build();

}

And this service expect a JSON like this:

{
    "profile": "ROLE_ADMIN",
    "name": "Administrador",
    "phone": "71234567890",
    "username": "admin",
    "email": "admin@mail.com",
    "password": "123456"
}

I have a service that picks up this user and changes the password to an encrypted one:

public Long insert(User user) {
        user.setPassword(PasswordUtils.generateBCrypt(user.getPassword()));
        repository.save(user);
        return user.getId();
    }

And the PasswordUtils.class have the code below:

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

public class PasswordUtils {

    public static String generateBCrypt(String password) {

        if (password == null) {
            return password;
        }

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        return encoder.encode(password);

    }

}

In my pom.xml I commented the spring-boot-starter-security dependency, because when I do not comment the same it will ask me for username and password to make the request. When I call the REST service the error: java.lang.ClassNotFoundException error occurs: org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder.

How to use BCrypt without my service ask for the credentials?

EDIT

My 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.heart</groupId>
    <artifactId>echocardiogram</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>echocardiogram</name>
    <description>Echocardiogram Management System</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.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-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</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>
Luciano Borges
  • 817
  • 3
  • 12
  • 31

0 Answers0