Home > AI > Backend > SpringBoot > spring-boot-starter-security >

Username and password

Default setting

The default UserDetailsService has a single user.

Username: user

Password: print on console

if now, open this

org.springframework.boot.autoconfigure.security = INFO

How to change default username + password

application.properties

spring.security.user.name = user
spring.security.user.password = user

Simple Storage with In-Memory Authentication

WebSecurityConfig.java with AuthenticationManagerBuilder

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {



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

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .passwordEncoder(passwordEncoder())
                .withUser("user")
                .password(passwordEncoder().encode("user"))
                .roles("USER");
    }
}

WebSecurityConfig.java with UserDetailsService

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {




    @Bean
    public UserDetailsService users() {
        UserDetails user = User.builder()
                .username("user")
                .password("{bcrypt}$2a$12$5mWRFBiAZ6/ThIttM7FtHOkLRoTB/zLgnGK3sNPdb68T0YeKdfi32")
                .roles("USER")
                .build();
        UserDetails admin = User.builder()
                .username("admin")
                .password("{bcrypt}$2a$12$qxgSB6ViUaP3C04jbcam/.8tkPAqS9lH1QLMlLNiXn7.NzLH5fwaq")
                .roles("USER", "ADMIN")
                .build();
        return new InMemoryUserDetailsManager(user, admin);
    }


}

MongoDB database

Leave a Reply