Spring Security- How to change default username and password

In the previous Spring Security tutorial, we have learned the integration of Spring Security in the web application and protect the application via validating the username and password. But the problem is Spring Security gives us some default username and password.

Note: By default, username for Spring Security based application is “user” and password will be printed in your console log followed by “Using generated security password: <your-password>” text.

1. Change using Java file

To change the default username and password of Spring Security, create a class that extends WebSecurityConfigurerAdapter class and override its userDetailsServiceBean() method.

Before jump to the custom user configuration, I recommend walk through our Getting Started with Spring Security tutorial.

package org.websparrow.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

public class SecurityConfig extends WebSecurityConfigurerAdapter {

	public UserDetailsService userDetailsServiceBean() throws Exception {

		// add users in List
		List<UserDetails> users = new ArrayList<UserDetails>();

		return new InMemoryUserDetailsManager(users);

@Configuration annotation indicates that a class declares one or more methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.

@EnableWebSecurity annotation enables the Spring Web Security functionality for the application.

UserDetailsService and UserDetails both are core interface which loads user-specific data provided by Spring Security itself.

User is a model class that retrieved user information by UserDetailsService interface. It also provided by Spring Security itself.

You can add multiple users with the same or different roles also:




You can also change the default username and password by adding the following code in your spring security configuration file.

public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

Note: {noop} is not the part of password, it is just expression used for NoOpPasswordEncoder which store the password in plain text which is not a good practice. If you failed to add any password encoder technique, you may probably face the below exception:

java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"

2. Change using application.properties

You can also change the Spring Security default username and password using application.properties file.

# Your desired user name

# password

# A comma separated list of roles


  1. Securing a Web Application
  2. Class WebSecurityConfigurerAdapter
  3. Password Storage Updated

Similar Posts

About the Author

I like sharing my experiments and ideas with everyone by writing articles on the latest technological trends.