Java-27655 | moved jenkins module to more recent implementation of web security

This commit is contained in:
gaepi 2023-11-26 13:12:19 +01:00
parent 65676eb275
commit 033fff1e5e
1 changed files with 24 additions and 32 deletions

View File

@ -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<UserDetails> 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();
}
}