1+
12package com .luv2code .springsecurity .demo .config ;
23
4+ import org .springframework .context .annotation .Bean ;
35import org .springframework .context .annotation .Configuration ;
4- import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
56import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
67import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
7- import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
88import org .springframework .security .core .userdetails .User ;
9- import org .springframework .security .core .userdetails .User .UserBuilder ;
9+ import org .springframework .security .core .userdetails .UserDetails ;
10+ import org .springframework .security .provisioning .InMemoryUserDetailsManager ;
11+ import org .springframework .security .web .SecurityFilterChain ;
1012
1113@ Configuration
1214@ EnableWebSecurity
13- public class DemoSecurityConfig extends WebSecurityConfigurerAdapter {
14-
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- }
15+ public class DemoSecurityConfig {
2716
28- @ Override
29- protected void configure (HttpSecurity http ) throws Exception {
17+ @ Bean
18+ public InMemoryUserDetailsManager userDetailsService () {
19+
20+ UserDetails john = User .builder ()
21+ .username ("john" )
22+ .password ("{noop}test123" )
23+ .roles ("EMPLOYEE" )
24+ .build ();
3025
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- .and ()
43- .exceptionHandling ().accessDeniedPage ("/access-denied" );
44-
45- }
46-
47- }
26+ UserDetails mary = User .builder ()
27+ .username ("mary" )
28+ .password ("{noop}test123" )
29+ .roles ("EMPLOYEE" , "MANAGER" )
30+ .build ();
4831
32+ UserDetails susan = User .builder ()
33+ .username ("susan" )
34+ .password ("{noop}test123" )
35+ .roles ("EMPLOYEE" , "MANAGER" , "ADMIN" )
36+ .build ();
37+
38+ return new InMemoryUserDetailsManager (john , mary , susan );
39+
40+ }
4941
42+ @ Bean
43+ public SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
44+
45+ return http
46+ .authorizeRequests (configurer ->
47+ configurer
48+ .antMatchers ("/" ).hasRole ("EMPLOYEE" )
49+ .antMatchers ("/leaders/**" ).hasRole ("MANAGER" )
50+ .antMatchers ("/systems/**" ).hasRole ("ADMIN" ))
5051
52+ .formLogin (configurer ->
53+ configurer
54+ .loginPage ("/showMyLoginPage" )
55+ .loginProcessingUrl ("/authenticateTheUser" )
56+ .permitAll ())
5157
58+ .logout (configurer ->
59+ configurer
60+ .permitAll ())
5261
62+ .exceptionHandling (configurer ->
63+ configurer
64+ .accessDeniedPage ("/access-denied" ))
5365
66+ .build ();
67+
68+ }
69+
70+ }
0 commit comments