Skip to content

spring.security.user.name & password not working in spring-boot 3.0.6 #13121

@voodemsanthosh

Description

@voodemsanthosh

Describe the bug
Migrating spring-boot project to 3.0.6, we use spring security to authenticate urls by user and password

spring:
security:
user:
name: user #${SPRING_SECURITY_USER_NAME}
password: passcode ${SPRING_SECURITY_USER_PASSWORD}

This works with old spring boot version and new spring boot version always reporting following error,

Caused by: java.lang.IllegalArgumentException: username cannot be null at org.springframework.util.Assert.notNull(Assert.java:204) ~[spring-core-6.0.8.jar:6.0.8] at org.springframework.security.core.userdetails.User$UserBuilder.username(User.java:357) ~[spring-security-core-6.0.3.jar:6.0.3] at org.springframework.security.core.userdetails.User.withUsername(User.java:216) ~[spring-security-core-6.0.3.jar:6.0.3]

My security class

package org.selflearn.spring.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
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.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.util.matcher.RegexRequestMatcher;

@configuration
@EnableWebSecurity
public class SecurityConfiguration {

 String name;
String password;

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {

    httpSecurity.authorizeHttpRequests( authorizationManagerRequestMatcherRegistry -> {
        try {
            authorizationManagerRequestMatcherRegistry
                .requestMatchers(new RegexRequestMatcher("/", HttpMethod.GET.name(), false),
                        new RegexRequestMatcher("/actuator/health", HttpMethod.GET.name(), false),
                        new RegexRequestMatcher("/actuator/health/readiness", HttpMethod.GET.name(), false),
                        new RegexRequestMatcher("/actuator/health/liveness", HttpMethod.GET.name(), false),
                        new RegexRequestMatcher("/health*", HttpMethod.GET.name(), false),
                        new RegexRequestMatcher("/api-docs.*", HttpMethod.GET.name(), false),
                        new RegexRequestMatcher("/swagger-resources/.*", HttpMethod.GET.name(), false)
                        ).permitAll()
                    .anyRequest().authenticated()
                    .and()
                    .httpBasic()
                    .and()
                    .csrf(AbstractHttpConfigurer::disable);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }).httpBasic(Customizer.withDefaults());
    return httpSecurity.build();
}

@Bean
public UserDetailsService userDetailsService() {
    UserDetails userDetails = User.withUsername(name)
            .password(passwordEncoder().encode(password))
            .roles("user", "ACTUATOR", "ADMIN")
            .build();
    return new InMemoryUserDetailsManager(userDetails);
}

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

}

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions