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 SecurityJavaConfig extends WebSecurityConfigurerAdapter {
19+
20+ @ Autowired
21+ private RestAuthenticationEntryPoint restAuthenticationEntryPoint ;
22+
23+ @ Autowired
24+ private MySavedRequestAwareAuthenticationSuccessHandler authenticationSuccessHandler ;
25+
26+ public SecurityJavaConfig () {
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+ .csrf ().disable ()
41+ .exceptionHandling ()
42+ .authenticationEntryPoint (restAuthenticationEntryPoint )
43+ .and ()
44+ .authorizeRequests ()
45+ .antMatchers ("/api/foos" ).authenticated ()
46+ .and ()
47+ .formLogin ()
48+ .loginProcessingUrl ("/j_spring_security_check" )
49+ .usernameParameter ("j_username" )
50+ .passwordParameter ("j_password" )
51+ .successHandler (authenticationSuccessHandler )
52+ .failureHandler (new SimpleUrlAuthenticationFailureHandler ())
53+ .and ()
54+ .logout ();
55+ } // @formatter:on
56+
57+ @ Bean
58+ public MySavedRequestAwareAuthenticationSuccessHandler mySuccessHandler (){
59+ return new MySavedRequestAwareAuthenticationSuccessHandler ();
60+ }
61+
62+ @ Bean
63+ public SimpleUrlAuthenticationFailureHandler myFailureHandler (){
64+ return new SimpleUrlAuthenticationFailureHandler ();
65+ }
66+
67+ }
0 commit comments