1+ package org .baeldung .spring ;
2+
3+ import org .baeldung .security .MySavedRequestAwareAuthenticationSuccessHandler ;
4+ import org .baeldung .security .RestAuthenticationEntryPoint ;
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 .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
10+ import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
11+ import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
12+ import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
13+ import org .springframework .security .web .authentication .SimpleUrlAuthenticationFailureHandler ;
14+
15+ @ Configuration
16+ @ EnableWebSecurity
17+ @ ComponentScan ("org.baeldung.security" )
18+ public class SecurityWithCsrfConfig extends WebSecurityConfigurerAdapter {
19+
20+ @ Autowired
21+ private RestAuthenticationEntryPoint restAuthenticationEntryPoint ;
22+
23+ @ Autowired
24+ private MySavedRequestAwareAuthenticationSuccessHandler authenticationSuccessHandler ;
25+
26+ public SecurityWithCsrfConfig () {
27+ super ();
28+ }
29+
30+ //
31+
32+ @ Override
33+ protected void configure (final AuthenticationManagerBuilder auth ) throws Exception {
34+ auth .inMemoryAuthentication ().withUser ("temporary" ).password ("temporary" ).roles ("ADMIN" ).and ().withUser ("user" ).password ("userPass" ).roles ("USER" );
35+ }
36+
37+ @ Override
38+ protected void configure (final HttpSecurity http ) throws Exception {// @formatter:off
39+ http
40+ .exceptionHandling ()
41+ .authenticationEntryPoint (restAuthenticationEntryPoint )
42+ .and ()
43+ .authorizeRequests ()
44+ .antMatchers ("/**" ).authenticated ()
45+ .and ()
46+ .formLogin ()
47+ .successHandler (authenticationSuccessHandler )
48+ .failureHandler (new SimpleUrlAuthenticationFailureHandler ())
49+ .and ()
50+ .logout ();
51+ } // @formatter:on
52+
53+ @ Bean
54+ public MySavedRequestAwareAuthenticationSuccessHandler mySuccessHandler () {
55+ return new MySavedRequestAwareAuthenticationSuccessHandler ();
56+ }
57+
58+ @ Bean
59+ public SimpleUrlAuthenticationFailureHandler myFailureHandler () {
60+ return new SimpleUrlAuthenticationFailureHandler ();
61+ }
62+
63+ }
0 commit comments