11package com .baeldung .springsecurity ;
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 ;
7+ import org .springframework .security .core .userdetails .User ;
8+ import org .springframework .security .core .userdetails .UserDetails ;
9+ import org .springframework .security .provisioning .InMemoryUserDetailsManager ;
10+ import org .springframework .security .web .SecurityFilterChain ;
811
912@ Configuration
1013@ EnableWebSecurity
11- public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
12- @ Override
13- protected void configure (AuthenticationManagerBuilder auth ) throws Exception {
14- auth
15- .inMemoryAuthentication ()
16- .withUser ("user1" )
17- .password ("user1Pass" )
18- .roles ("USER" )
19- .and ()
20- .withUser ("admin" )
21- .password ("adminPass" )
22- .roles ("ADMIN" );
14+ public class SpringSecurityConfig {
15+
16+ @ Bean
17+ public InMemoryUserDetailsManager userDetailsService () {
18+ UserDetails user = User .withUsername ("user1" )
19+ .password ("{noop}user1Pass" )
20+ .roles ("USER" )
21+ .build ();
22+ UserDetails admin = User .withUsername ("admin" )
23+ .password ("{noop}adminPass" )
24+ .roles ("ADMIN" )
25+ .build ();
26+ return new InMemoryUserDetailsManager (user , admin );
2327 }
2428
25- @ Override
26- protected void configure (HttpSecurity http ) throws Exception {
27- http
28- . csrf ()
29- . disable ()
30- . authorizeRequests ( )
31- . antMatchers ( "/auth/login*" )
32- . anonymous ( )
33- . antMatchers ( "/home/admin* " )
34- . hasRole ( "ADMIN" )
35- . anyRequest ()
36- . authenticated ()
37- . and ()
38- . formLogin ( )
39- . loginPage ("/auth/login" )
40- . defaultSuccessUrl ("/home" , true )
41- . failureUrl ( "/auth/login?error=true" )
42- . and ()
43- . logout ()
44- . logoutSuccessUrl ( "/auth/login" );
29+ @ Bean
30+ public SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
31+ http . csrf ()
32+ . disable ()
33+ . authorizeRequests ()
34+ . antMatchers ( "/auth/login*" )
35+ . anonymous ( )
36+ . antMatchers ( "/home/admin*" )
37+ . hasRole ( "ADMIN " )
38+ . anyRequest ( )
39+ . authenticated ()
40+ . and ()
41+ . formLogin ()
42+ . loginPage ( "/auth/login" )
43+ . defaultSuccessUrl ("/home" , true )
44+ . failureUrl ("/auth/login?error= true" )
45+ . and ( )
46+ . logout ()
47+ . logoutSuccessUrl ( "/auth/login" );
48+ return http . build ( );
4549 }
4650}
0 commit comments