|
1 | 1 | package com.baeldung.comparison.springsecurity.config;
|
2 | 2 |
|
3 | 3 | import org.springframework.context.annotation.Bean;
|
4 |
| -import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; |
5 | 4 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
6 | 5 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
7 |
| -import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 6 | +import org.springframework.security.core.userdetails.User; |
| 7 | +import org.springframework.security.core.userdetails.UserDetails; |
8 | 8 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
9 | 9 | import org.springframework.security.crypto.password.PasswordEncoder;
|
| 10 | +import org.springframework.security.provisioning.InMemoryUserDetailsManager; |
| 11 | +import org.springframework.security.web.SecurityFilterChain; |
10 | 12 |
|
11 | 13 | @EnableWebSecurity
|
12 |
| -public class SecurityConfig extends WebSecurityConfigurerAdapter { |
| 14 | +public class SecurityConfig { |
13 | 15 |
|
14 |
| - @Override |
15 |
| - protected void configure(HttpSecurity http) throws Exception { |
16 |
| - http.csrf().disable().authorizeRequests(authorize -> authorize.antMatchers("/index", "/login") |
17 |
| - .permitAll() |
18 |
| - .antMatchers("/home", "/logout") |
19 |
| - .authenticated() |
20 |
| - .antMatchers("/admin/**") |
21 |
| - .hasRole("ADMIN")) |
| 16 | + @Bean |
| 17 | + public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { |
| 18 | + http.csrf() |
| 19 | + .disable() |
| 20 | + .authorizeRequests(authorize -> authorize.antMatchers("/index", "/login") |
| 21 | + .permitAll() |
| 22 | + .antMatchers("/home", "/logout") |
| 23 | + .authenticated() |
| 24 | + .antMatchers("/admin/**") |
| 25 | + .hasRole("ADMIN")) |
22 | 26 | .formLogin(formLogin -> formLogin.loginPage("/login")
|
23 | 27 | .failureUrl("/login-error"));
|
| 28 | + return http.build(); |
24 | 29 | }
|
25 | 30 |
|
26 |
| - @Override |
27 |
| - protected void configure(AuthenticationManagerBuilder auth) throws Exception { |
28 |
| - auth.inMemoryAuthentication() |
29 |
| - .withUser("Jerry") |
| 31 | + @Bean |
| 32 | + public InMemoryUserDetailsManager userDetailsService() throws Exception { |
| 33 | + UserDetails jerry = User.withUsername("Jerry") |
30 | 34 | .password(passwordEncoder().encode("password"))
|
31 | 35 | .authorities("READ", "WRITE")
|
32 | 36 | .roles("ADMIN")
|
33 |
| - .and() |
34 |
| - .withUser("Tom") |
| 37 | + .build(); |
| 38 | + UserDetails tom = User.withUsername("Tom") |
35 | 39 | .password(passwordEncoder().encode("password"))
|
36 | 40 | .authorities("READ")
|
37 |
| - .roles("USER"); |
| 41 | + .roles("USER") |
| 42 | + .build(); |
| 43 | + return new InMemoryUserDetailsManager(jerry, tom); |
38 | 44 | }
|
39 | 45 |
|
40 | 46 | @Bean
|
|
0 commit comments