Spring Framework Tutorial
Software Setup and Configuration (STS/Eclipse/IntelliJ)
Core Spring
Spring Annotations
Spring Data
Spring JDBC
Spring Security
In Spring Security, the default username is "user," and the default password is generated randomly and printed in the console logs during application startup. To change these defaults, you have several options:
application.properties
or application.yml
You can override the default username and password by specifying them in the application.properties
file located in the src/main/resources
directory of your Spring Boot project.
# application.properties spring.security.user.name=myUsername spring.security.user.password=myPassword
Or, if you're using YAML (application.yml
):
# application.yml spring: security: user: name: myUsername password: myPassword
WebSecurityConfigurerAdapter
Another way to configure the default user and password is by extending WebSecurityConfigurerAdapter
and overriding its configure
method:
import org.springframework.context.annotation.Bean; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.builders.HttpSecurity; 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; @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .anyRequest().authenticated() .and() .httpBasic(); } @Bean @Override public UserDetailsService userDetailsService() { UserDetails user = User.withDefaultPasswordEncoder() .username("myUsername") .password("myPassword") .roles("USER") .build(); return new InMemoryUserDetailsManager(user); } }
Note that using withDefaultPasswordEncoder
is not recommended for production use due to its lack of security. This is just for demonstration purposes.
For more complex scenarios, like fetching user credentials from a database, you can define your own AuthenticationProvider
bean:
import org.springframework.context.annotation.Bean; import org.springframework.security.authentication.AuthenticationProvider; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Bean public AuthenticationProvider authenticationProvider() { return new AuthenticationProvider() { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { String username = authentication.getName(); String password = authentication.getCredentials().toString(); if ("myUsername".equals(username) && "myPassword".equals(password)) { return new UsernamePasswordAuthenticationToken(username, password, new ArrayList<>()); } else { return null; } } @Override public boolean supports(Class<?> authentication) { return authentication.equals(UsernamePasswordAuthenticationToken.class); } }; } }
Here, you have full control over the authentication logic.
You can choose the most suitable method depending on your use case. The application.properties
method is usually appropriate for development and testing, whereas WebSecurityConfigurerAdapter
and AuthenticationProvider
are better suited for production or more complex scenarios.
Changing Default User and Password in Spring Security:
@Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("customUser").password("{noop}customPassword").roles("USER"); } }
Customizing Default User Credentials in Spring Security:
WebSecurityConfigurerAdapter
and overriding the configure
method.@Configuration public class CustomSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("myUser").password("{noop}myPassword").roles("USER"); } }
How to Modify Default Username and Password in Spring Security:
@Configuration public class CustomSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("newUser").password("{noop}newPassword").roles("USER"); } }
Configuring Custom Authentication in Spring Security:
AbstractAuthenticationProcessingFilter
.public class CustomAuthenticationFilter extends AbstractAuthenticationProcessingFilter { // Implementation details }
Customizing AuthenticationProvider in Spring Security:
@Bean public AuthenticationProvider customAuthenticationProvider() { return new CustomAuthenticationProvider(); }
Override Default User and Password in Spring Boot Security:
spring: security: user: name: customUser password: customPassword
Spring Security Custom Authentication with Different User Details:
UserDetailsService
to load user details from a different source.@Service public class CustomUserDetailsService implements UserDetailsService { // Implementation details }
Modifying AuthenticationManager in Spring Security:
AuthenticationManager
to use a custom authentication provider.@Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.authenticationProvider(customAuthenticationProvider()); }
Custom UserDetailsService in Spring Security:
UserDetailsService
to load user details from a custom source.@Service public class CustomUserDetailsService implements UserDetailsService { // Implementation details }
Changing Default Authentication Parameters in Spring Security:
@Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .formLogin() .loginPage("/custom-login") .successHandler(customSuccessHandler()) .failureHandler(customFailureHandler()); } }
Spring Security Custom Authentication Provider Example:
public class CustomAuthenticationProvider implements AuthenticationProvider { // Implementation details }
Configuring In-Memory Authentication with Custom User Details:
@Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(customUserDetailsService()); }
Securing a Spring Boot Application with Custom User Authentication:
@Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/secured-endpoint").hasRole("USER") .and() .formLogin(); }
Spring Security Custom Authentication Filters for User Authentication:
public class CustomAuthenticationFilter extends AbstractAuthenticationProcessingFilter { // Implementation details }