diff --git a/jmeter/src/main/java/com/baeldung/configuration/WebSecurityConfiguration.java b/jmeter/src/main/java/com/baeldung/configuration/WebSecurityConfiguration.java index 8700dc3df4..033281e07f 100644 --- a/jmeter/src/main/java/com/baeldung/configuration/WebSecurityConfiguration.java +++ b/jmeter/src/main/java/com/baeldung/configuration/WebSecurityConfiguration.java @@ -1,52 +1,44 @@ package com.baeldung.configuration; +import java.util.HashSet; +import java.util.Set; + +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; -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.crypto.factory.PasswordEncoderFactories; import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.security.provisioning.InMemoryUserDetailsManager; +import org.springframework.security.web.SecurityFilterChain; @Configuration -public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { +public class WebSecurityConfiguration { - @Override - protected void configure(AuthenticationManagerBuilder auth) throws Exception { - + @Bean + public InMemoryUserDetailsManager userDetailsService() { PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder(); - - auth.inMemoryAuthentication() - .withUser("admin").password(encoder.encode("admin")).roles("USER", "ADMIN") - .and() - .withUser("user1").password(encoder.encode("password1")).roles("USER") - .and() - .withUser("user2").password(encoder.encode("password2")).roles("USER") - .and() - .withUser("user3").password(encoder.encode("password3")).roles("USER") - .and() - .withUser("user4").password(encoder.encode("password4")).roles("USER") - .and() - .withUser("user5").password(encoder.encode("password5")).roles("USER") - .and() - .withUser("user6").password(encoder.encode("password6")).roles("USER") - .and() - .withUser("user7").password(encoder.encode("password7")).roles("USER") - .and() - .withUser("user8").password(encoder.encode("password8")).roles("USER") - .and() - .withUser("user9").password(encoder.encode("password9")).roles("USER") - .and() - .withUser("user10").password(encoder.encode("password10")).roles("USER"); + + Set users = new HashSet<>(); + users.add(User.withUsername("admin").password(encoder.encode("admin")).roles("USER", "ADMIN").build()); + for(int i=1;i<=10;i++){ + users.add(User.withUsername("user"+i).password(encoder.encode("password")+i).roles("USER").build()); + } + + return new InMemoryUserDetailsManager(users); } - @Override - protected void configure(HttpSecurity http) throws Exception { - + @Bean + public SecurityFilterChain securityFilter(HttpSecurity http) throws Exception { + http .authorizeRequests() .antMatchers("/secured/**").authenticated() .anyRequest().permitAll() .and() .httpBasic(); + + return http.build(); } }