11package com .luv2code .springsecurity .demo .config ;
22
3+ import org .springframework .context .annotation .Bean ;
34import org .springframework .context .annotation .Configuration ;
4- import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
55import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
66import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
7- import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
87import org .springframework .security .core .userdetails .User ;
9- import org .springframework .security .core .userdetails .User .UserBuilder ;
8+ import org .springframework .security .core .userdetails .UserDetails ;
9+ import org .springframework .security .provisioning .InMemoryUserDetailsManager ;
10+ import org .springframework .security .web .SecurityFilterChain ;
1011
1112@ Configuration
1213@ EnableWebSecurity
13- public class DemoSecurityConfig extends WebSecurityConfigurerAdapter {
14+ public class DemoSecurityConfig {
1415
15- @ Override
16- protected void configure (AuthenticationManagerBuilder auth ) throws Exception {
17-
18- // add our users for in memory authentication
19-
20- UserBuilder users = User .withDefaultPasswordEncoder ();
21-
22- auth .inMemoryAuthentication ()
23- .withUser (users .username ("john" ).password ("test123" ).roles ("EMPLOYEE" ))
24- .withUser (users .username ("mary" ).password ("test123" ).roles ("EMPLOYEE" , "MANAGER" ))
25- .withUser (users .username ("susan" ).password ("test123" ).roles ("EMPLOYEE" , "ADMIN" ));
26- }
27-
28- @ Override
29- protected void configure (HttpSecurity http ) throws Exception {
30-
31- http .authorizeRequests ()
32- .antMatchers ("/" ).hasRole ("EMPLOYEE" )
33- .antMatchers ("/leaders/**" ).hasRole ("MANAGER" )
34- .antMatchers ("/systems/**" ).hasRole ("ADMIN" )
35- .and ()
36- .formLogin ()
37- .loginPage ("/showMyLoginPage" )
38- .loginProcessingUrl ("/authenticateTheUser" )
39- .permitAll ()
40- .and ()
41- .logout ().permitAll ();
42-
43- }
44-
45- }
16+ @ Bean
17+ public InMemoryUserDetailsManager userDetailsService () {
18+
19+ UserDetails john = User .builder ()
20+ .username ("john" )
21+ .password ("{noop}test123" )
22+ .roles ("EMPLOYEE" )
23+ .build ();
4624
25+ UserDetails mary = User .builder ()
26+ .username ("mary" )
27+ .password ("{noop}test123" )
28+ .roles ("EMPLOYEE" , "MANAGER" )
29+ .build ();
4730
31+ UserDetails susan = User .builder ()
32+ .username ("susan" )
33+ .password ("{noop}test123" )
34+ .roles ("EMPLOYEE" , "ADMIN" )
35+ .build ();
36+
37+ return new InMemoryUserDetailsManager (john , mary , susan );
38+
39+ }
4840
41+ @ Bean
42+ public SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
43+
44+ return http
45+ .authorizeRequests (configurer ->
46+ configurer
47+ .antMatchers ("/" ).hasRole ("EMPLOYEE" )
48+ .antMatchers ("/leaders/**" ).hasRole ("MANAGER" )
49+ .antMatchers ("/systems/**" ).hasRole ("ADMIN" ))
4950
51+ .formLogin (configurer ->
52+ configurer
53+ .loginPage ("/showMyLoginPage" )
54+ .loginProcessingUrl ("/authenticateTheUser" )
55+ .permitAll ())
5056
57+ .logout (configurer ->
58+ configurer
59+ .permitAll ())
5160
61+ .build ();
62+
63+ }
64+
65+ }
0 commit comments