1+ package org .baeldung .rolesauthorities .config ;
2+
3+ import org .baeldung .rolesauthorities .CustomAuthenticationProvider ;
4+ import org .baeldung .rolesauthorities .persistence .UserRepository ;
5+ import org .springframework .beans .factory .annotation .Autowired ;
6+ import org .springframework .context .annotation .Bean ;
7+ import org .springframework .context .annotation .ComponentScan ;
8+ import org .springframework .context .annotation .Configuration ;
9+ import org .springframework .security .authentication .dao .DaoAuthenticationProvider ;
10+ import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
11+ import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
12+ import org .springframework .security .config .annotation .web .builders .WebSecurity ;
13+ import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
14+ import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
15+ import org .springframework .security .core .userdetails .UserDetailsService ;
16+ import org .springframework .security .crypto .bcrypt .BCryptPasswordEncoder ;
17+ import org .springframework .security .crypto .password .PasswordEncoder ;
18+ import org .springframework .security .web .authentication .logout .LogoutSuccessHandler ;
19+
20+ @ Configuration
21+ @ ComponentScan (basePackages = { "org.baeldung.rolesauthorities" })
22+ @ EnableWebSecurity
23+ public class SecurityConfig extends WebSecurityConfigurerAdapter {
24+
25+ @ Autowired
26+ private UserRepository userRepository ;
27+
28+ @ Autowired
29+ private UserDetailsService userDetailsService ;
30+
31+ @ Autowired
32+ private LogoutSuccessHandler myLogoutSuccessHandler ;
33+
34+ public SecurityConfig () {
35+ super ();
36+ }
37+
38+ @ Override
39+ protected void configure (final AuthenticationManagerBuilder auth ) throws Exception {
40+ auth .authenticationProvider (authProvider ());
41+ }
42+
43+ @ Override
44+ public void configure (final WebSecurity web ) throws Exception {
45+ web .ignoring ()
46+ .antMatchers ("/resources/**" );
47+ }
48+
49+
50+ @ Override
51+ protected void configure (final HttpSecurity http ) throws Exception {
52+
53+ // @formatter:off
54+ http
55+ .csrf ().disable ()
56+ .authorizeRequests ()
57+ .antMatchers ("/login*" , "/logout*" , "/protectedbynothing*" , "/home*" ).permitAll ()
58+ .antMatchers ("/protectedbyrole" ).hasRole ("USER" )
59+ .antMatchers ("/protectedbyauthority" ).hasAuthority ("READ_PRIVILEGE" )
60+ .and ()
61+ .formLogin ()
62+ .loginPage ("/login" )
63+ .failureUrl ("/login?error=true" )
64+ .permitAll ()
65+ .and ()
66+ .logout ()
67+ .logoutSuccessHandler (myLogoutSuccessHandler )
68+ .invalidateHttpSession (false )
69+ .logoutSuccessUrl ("/logout.html?logSucc=true" )
70+ .deleteCookies ("JSESSIONID" )
71+ .permitAll ();
72+ // @formatter:on
73+ }
74+
75+ // beans
76+
77+ @ Bean
78+ public DaoAuthenticationProvider authProvider () {
79+ final CustomAuthenticationProvider authProvider
80+ = new CustomAuthenticationProvider (userRepository , userDetailsService );
81+ authProvider .setPasswordEncoder (encoder ());
82+ return authProvider ;
83+ }
84+
85+ @ Bean
86+ public PasswordEncoder encoder () {
87+ return new BCryptPasswordEncoder (11 );
88+ }
89+
90+ }
0 commit comments