55import org .springframework .beans .factory .annotation .Autowired ;
66import org .springframework .context .annotation .Bean ;
77import org .springframework .context .annotation .Configuration ;
8+ import org .springframework .security .authentication .AuthenticationManager ;
89import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
910import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
10- import org .springframework .security .config .annotation .web .configuration .WebSecurityConfiguration ;
11+ import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
12+ import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
1113import org .springframework .security .crypto .bcrypt .BCryptPasswordEncoder ;
1214import org .springframework .security .crypto .password .PasswordEncoder ;
1315
1416@ Configuration
15- public class WebSecurityConfig extends WebSecurityConfiguration {
17+ @ EnableWebSecurity
18+ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
1619
1720 @ Autowired
1821 RepositoryUserDetailsService userDetailsService ;
@@ -30,27 +33,30 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
3033
3134 @ Override
3235 protected void configure (HttpSecurity http ) throws Exception {
33-
34- // Public pages
35- http .authorizeRequests ().antMatchers ("/" ).permitAll ();
36- http .authorizeRequests ().antMatchers ("/login" ).permitAll ();
37- http .authorizeRequests ().antMatchers ("/loginerror" ).permitAll ();
38- http .authorizeRequests ().antMatchers ("/logout" ).permitAll ();
39-
40- // Private pages
41- http .authorizeRequests ().antMatchers ("/newbook" ).hasAnyRole ("USER" );
42- http .authorizeRequests ().antMatchers ("/editbook/*" ).hasAnyRole ("USER" );
43- http .authorizeRequests ().antMatchers ("/removebook/*" ).hasAnyRole ("ADMIN" );
44-
45- // Login form
46- http .formLogin ().loginPage ("/login" );
47- http .formLogin ().usernameParameter ("username" );
48- http .formLogin ().passwordParameter ("password" );
49- http .formLogin ().defaultSuccessUrl ("/" );
50- http .formLogin ().failureUrl ("/loginerror" );
51-
52- // Logout
53- http .logout ().logoutUrl ("/logout" );
54- http .logout ().logoutSuccessUrl ("/" );
36+
37+ // Public pages
38+ http .authorizeRequests ()
39+ .antMatchers ("/" ).permitAll ()
40+ .antMatchers ("/login" ).permitAll ()
41+ .antMatchers ("/loginerror" ).permitAll ()
42+ .antMatchers ("/logout" ).permitAll ()
43+ // Private pages
44+ .antMatchers ("/newbook" ).hasAnyRole ("USER" )
45+ .antMatchers ("/editbook/*" ).hasAnyRole ("USER" )
46+ .antMatchers ("/removebook/*" ).hasAnyRole ("ADMIN" )
47+ .anyRequest ().authenticated ()
48+ .and ()
49+ // Login form
50+ .formLogin ()
51+ .loginPage ("/login" )
52+ .usernameParameter ("username" )
53+ .passwordParameter ("password" )
54+ .defaultSuccessUrl ("/" )
55+ .failureUrl ("/loginerror" )
56+ .and ()
57+ // Logout
58+ .logout ()
59+ .logoutUrl ("/logout" )
60+ .logoutSuccessUrl ("/" );
5561 }
56- }
62+ }
0 commit comments