Search code examples
javaspringspring-bootspring-security

How to use BycrpytEncoder without springsecurity?


So previously i was using bcrpytEncoder / passwordEncoder in the springboot to register the user with the encrypted password in the JPA like this :

import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

@Component
public class PasswordEncoderUtil {
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
}

then i configure the springsecurity in my application using other dependencies :


    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
//  implementation 'com.okta.spring:okta-spring-boot-starter:3.0.5'
//  implementation 'org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-web-services'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'

Now i want to remove springsecurity from the application and only want to use PasswordEncoder ,how should i do it ?

I don't want this login page : Login Page


Solution

  • You can add single dependency

    implementation group: 'org.springframework.security', name: 'spring-security-crypto', version: '6.2.4'
    

    This will allow you to import BCryptPasswordEncoder class in your application.