33import javax .sql .DataSource ;
44
55import org .springframework .beans .factory .annotation .Autowired ;
6+ import org .springframework .context .annotation .Bean ;
67import org .springframework .context .annotation .Configuration ;
7- import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
88import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
99import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
10- import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
10+ import org .springframework .security .provisioning .JdbcUserDetailsManager ;
11+ import org .springframework .security .provisioning .UserDetailsManager ;
12+ import org .springframework .security .web .SecurityFilterChain ;
1113
1214@ Configuration
1315@ EnableWebSecurity
14- public class DemoSecurityConfig extends WebSecurityConfigurerAdapter {
16+ public class DemoSecurityConfig {
1517
1618// add a reference to our security data source
17-
18- @ Autowired
19+
1920private DataSource securityDataSource ;
2021
21-
22- @ Override
23- protected void configure (AuthenticationManagerBuilder auth ) throws Exception {
24-
25- // use jdbc authentication ... oh yeah!!!
26-
27- auth .jdbcAuthentication ().dataSource (securityDataSource );
28-
29- }
30-
31- @ Override
32- protected void configure (HttpSecurity http ) throws Exception {
33-
34- http .authorizeRequests ()
35- .antMatchers ("/" ).hasRole ("EMPLOYEE" )
36- .antMatchers ("/leaders/**" ).hasRole ("MANAGER" )
37- .antMatchers ("/systems/**" ).hasRole ("ADMIN" )
38- .and ()
39- .formLogin ()
40- .loginPage ("/showMyLoginPage" )
41- .loginProcessingUrl ("/authenticateTheUser" )
42- .permitAll ()
43- .and ()
44- .logout ().permitAll ()
45- .and ()
46- .exceptionHandling ().accessDeniedPage ("/access-denied" );
47-
22+ @ Autowired
23+ public DemoSecurityConfig (DataSource theSecurityDataSource ) {
24+ securityDataSource = theSecurityDataSource ;
4825}
26+
27+ @ Bean
28+ public UserDetailsManager userDetailsService () {
29+ return new JdbcUserDetailsManager (securityDataSource );
30+ }
31+
32+ @ Bean
33+ public SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
34+
35+ return http
36+ .authorizeRequests (configurer ->
37+ configurer
38+ .antMatchers ("/" ).hasRole ("EMPLOYEE" )
39+ .antMatchers ("/leaders/**" ).hasRole ("MANAGER" )
40+ .antMatchers ("/systems/**" ).hasRole ("ADMIN" ))
41+
42+ .formLogin (configurer ->
43+ configurer
44+ .loginPage ("/showMyLoginPage" )
45+ .loginProcessingUrl ("/authenticateTheUser" )
46+ .permitAll ())
47+
48+ .logout (configurer ->
49+ configurer
50+ .permitAll ())
51+
52+ .exceptionHandling (configurer ->
53+ configurer
54+ .accessDeniedPage ("/access-denied" ))
55+
56+ .build ();
57+
58+ }
4959
50- }
51-
52-
53-
54-
55-
56-
60+ }
0 commit comments