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-
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 ("MANAGER" ))
25- .withUser (users .username ("susan" ).password ("test123" ).roles ("ADMIN" ));
26- }
27-
28- @ Override
29- protected void configure (HttpSecurity http ) throws Exception {
30-
31- http .authorizeRequests ()
32- .anyRequest ().authenticated ()
33- .and ()
34- .formLogin ()
35- .loginPage ("/showMyLoginPage" )
36- .loginProcessingUrl ("/authenticateTheUser" )
37- .permitAll ();
38-
39- }
40-
41- }
42-
43-
44-
45-
46-
47-
14+ public class DemoSecurityConfig {
15+
16+ @ Bean
17+ public InMemoryUserDetailsManager userDetailsService () {
18+
19+ UserDetails john = User .builder ()
20+ .username ("john" )
21+ .password ("{noop}test123" )
22+ .roles ("EMPLOYEE" )
23+ .build ();
24+
25+ UserDetails mary = User .builder ()
26+ .username ("mary" )
27+ .password ("{noop}test123" )
28+ .roles ("MANAGER" )
29+ .build ();
30+
31+ UserDetails susan = User .builder ()
32+ .username ("susan" )
33+ .password ("{noop}test123" )
34+ .roles ("ADMIN" )
35+ .build ();
36+
37+ return new InMemoryUserDetailsManager (john , mary , susan );
38+
39+ }
40+
41+ @ Bean
42+ public SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
43+
44+ return http
45+ .authorizeRequests (authz ->
46+ authz
47+ .anyRequest ()
48+ .authenticated ())
49+ .formLogin (authz ->
50+ authz
51+ .loginPage ("/showMyLoginPage" )
52+ .loginProcessingUrl ("/authenticateTheUser" )
53+ .permitAll ())
54+ .build ();
55+
56+ }
57+
58+ }
0 commit comments