Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Security: How to change default user and password?

I have Spring Security in my pom.xml, and Spring Security is automatically configured with a default user and generated password:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

How do I change the default user and password?

like image 405
ahmedmess Avatar asked Aug 07 '17 22:08

ahmedmess


People also ask

How do I find my Spring Security username and password?

As of Spring Security version 5.7. 1, the default username is user and the password is randomly generated and displayed in the console (e.g. 8e557245-73e2-4286-969a-ff57fe326336 ).

What is default password for Spring Security?

The default user name is “user” and the password is generated every time the application is restarted. The generated security password is shown in the startup log of the spring boot application console.

How can we in our application override the default user configuration in Spring Security?

In Spring Boot 2, if we want our own security configuration, we can simply add a custom WebSecurityConfigurerAdapter. This will disable the default auto-configuration and enable our custom security configuration.


2 Answers

This can be easly done in your application.properties file:

spring.security.user.name=user # Default user name.
spring.security.user.password= # Password
spring.security.user.role= # A comma separated list of roles

Here is the documentation.

like image 166
Juan Carlos Mendoza Avatar answered Nov 15 '22 06:11

Juan Carlos Mendoza


This is straight from the docs:

Create a configuration class:

@Configuration
@EnableWebSecurity
public class HelloWebSecurityConfiguration
   extends WebSecurityConfigurerAdapter {

  @Autowired
  public void configureGlobal(AuthenticationManagerBuilder auth) {
    auth
      .inMemoryAuthentication()
        .withUser("user").password("password").roles("USER");
  }
}

Newer Docs

This is slightly different, but the effect would be the same:

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public UserDetailsService userDetailsService() throws Exception {
        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
        manager.createUser(User.withUsername("user").password("password").roles("USER").build());
        return manager;
    }
}
like image 24
Brian Avatar answered Nov 15 '22 07:11

Brian