|
2 | 2 |
|
3 | 3 | import org.springframework.context.annotation.Bean;
|
4 | 4 | import org.springframework.context.annotation.Configuration;
|
5 |
| -import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; |
| 5 | +import org.springframework.security.authentication.AuthenticationManager; |
| 6 | +import org.springframework.security.config.Customizer; |
6 | 7 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
7 | 8 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
8 |
| -import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 9 | +import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; |
| 10 | +import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer; |
9 | 11 | import org.springframework.security.config.http.SessionCreationPolicy;
|
| 12 | +import org.springframework.security.core.userdetails.User; |
| 13 | +import org.springframework.security.core.userdetails.UserDetails; |
10 | 14 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
11 | 15 | import org.springframework.security.crypto.password.PasswordEncoder;
|
| 16 | +import org.springframework.security.provisioning.InMemoryUserDetailsManager; |
| 17 | +import org.springframework.security.web.SecurityFilterChain; |
12 | 18 | import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
13 | 19 |
|
14 | 20 | @Configuration
|
15 | 21 | @EnableWebSecurity
|
16 |
| -public class SecurityConfiguration extends WebSecurityConfigurerAdapter { |
| 22 | +public class SecurityConfiguration { |
17 | 23 |
|
18 |
| - @Override |
19 |
| - protected void configure(AuthenticationManagerBuilder auth) throws Exception { |
20 |
| - auth.inMemoryAuthentication() |
21 |
| - .passwordEncoder(passwordEncoder()) |
22 |
| - .withUser("user") |
23 |
| - .password(passwordEncoder().encode("baeldung")) |
24 |
| - .roles("tenant_1"); |
| 24 | + @Bean |
| 25 | + public InMemoryUserDetailsManager userDetailsService() { |
| 26 | + UserDetails user1 = User |
| 27 | + .withUsername("user") |
| 28 | + .password(passwordEncoder().encode("baeldung")) |
| 29 | + .roles("tenant_1") |
| 30 | + .build(); |
25 | 31 |
|
26 |
| - auth.inMemoryAuthentication() |
27 |
| - .passwordEncoder(passwordEncoder()) |
28 |
| - .withUser("admin") |
29 |
| - .password(passwordEncoder().encode("baeldung")) |
30 |
| - .roles("tenant_2"); |
| 32 | + UserDetails user2 = User |
| 33 | + .withUsername("admin") |
| 34 | + .password(passwordEncoder().encode("baeldung")) |
| 35 | + .roles("tenant_2") |
| 36 | + .build(); |
| 37 | + return new InMemoryUserDetailsManager(user1, user2); |
31 | 38 | }
|
32 | 39 |
|
33 | 40 | @Bean
|
34 | 41 | public PasswordEncoder passwordEncoder(){
|
35 | 42 | return new BCryptPasswordEncoder();
|
36 | 43 | }
|
37 | 44 |
|
38 |
| - @Override |
39 |
| - protected void configure(HttpSecurity http) throws Exception { |
| 45 | + @Bean |
| 46 | + public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { |
| 47 | + final AuthenticationManager authenticationManager = http.getSharedObject(AuthenticationManager.class); |
40 | 48 | http
|
41 |
| - .authorizeRequests() |
42 |
| - .antMatchers("/login").permitAll() |
43 |
| - .anyRequest().authenticated() |
44 |
| - .and() |
45 |
| - .sessionManagement() |
46 |
| - .sessionCreationPolicy(SessionCreationPolicy.STATELESS) |
47 |
| - .and() |
48 |
| - .addFilterBefore(new LoginFilter("/login", authenticationManager()), |
49 |
| - UsernamePasswordAuthenticationFilter.class) |
50 |
| - .addFilterBefore(new AuthenticationFilter(), UsernamePasswordAuthenticationFilter.class) |
51 |
| - .csrf().disable() |
52 |
| - .headers().frameOptions().disable() |
53 |
| - .and() |
54 |
| - .httpBasic(); |
| 49 | + .authorizeHttpRequests(authorize -> |
| 50 | + authorize.requestMatchers("/login").permitAll().anyRequest().authenticated()) |
| 51 | + .sessionManagement(securityContext -> securityContext.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) |
| 52 | + .addFilterBefore(new LoginFilter("/login", authenticationManager), UsernamePasswordAuthenticationFilter.class) |
| 53 | + .addFilterBefore(new AuthenticationFilter(), UsernamePasswordAuthenticationFilter.class) |
| 54 | + .csrf(AbstractHttpConfigurer::disable) |
| 55 | + .headers(header -> header.frameOptions(HeadersConfigurer.FrameOptionsConfig::disable)) |
| 56 | + .httpBasic(Customizer.withDefaults()); |
| 57 | + |
| 58 | + return http.build(); |
55 | 59 | }
|
56 | 60 | }
|
0 commit comments