11package org .baeldung .spring ;
22
33import org .baeldung .security .MySavedRequestAwareAuthenticationSuccessHandler ;
4+ import org .baeldung .security .RestAuthenticationEntryPoint ;
45import org .baeldung .web .error .CustomAccessDeniedHandler ;
56import org .springframework .beans .factory .annotation .Autowired ;
67import org .springframework .context .annotation .Bean ;
1213import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
1314import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
1415import org .springframework .security .core .context .SecurityContextHolder ;
16+ import org .springframework .security .crypto .bcrypt .BCryptPasswordEncoder ;
17+ import org .springframework .security .crypto .password .PasswordEncoder ;
1518import org .springframework .security .web .authentication .SimpleUrlAuthenticationFailureHandler ;
1619
1720@ Configuration
@@ -23,56 +26,55 @@ public class SecurityJavaConfig extends WebSecurityConfigurerAdapter {
2326 @ Autowired
2427 private CustomAccessDeniedHandler accessDeniedHandler ;
2528
26- // @Autowired
27- // private RestAuthenticationEntryPoint restAuthenticationEntryPoint;
29+ @ Autowired
30+ private RestAuthenticationEntryPoint restAuthenticationEntryPoint ;
31+
32+ @ Autowired
33+ private MySavedRequestAwareAuthenticationSuccessHandler mySuccessHandler ;
2834
29- // @Autowired
30- // private MySavedRequestAwareAuthenticationSuccessHandler authenticationSuccessHandler;
35+ private SimpleUrlAuthenticationFailureHandler myFailureHandler = new SimpleUrlAuthenticationFailureHandler ();
3136
3237 public SecurityJavaConfig () {
3338 super ();
3439 SecurityContextHolder .setStrategyName (SecurityContextHolder .MODE_INHERITABLETHREADLOCAL );
3540 }
3641
37- //
38-
3942 @ Override
4043 protected void configure (final AuthenticationManagerBuilder auth ) throws Exception {
41- auth .inMemoryAuthentication ().withUser ("temporary" ).password ("temporary" ).roles ("ADMIN" ).and ().withUser ("user" ).password ("userPass" ).roles ("USER" );
44+ auth .inMemoryAuthentication ()
45+ .withUser ("admin" ).password (encoder ().encode ("adminPass" )).roles ("ADMIN" )
46+ .and ()
47+ .withUser ("user" ).password (encoder ().encode ("userPass" )).roles ("USER" );
4248 }
4349
4450 @ Override
45- protected void configure (final HttpSecurity http ) throws Exception {// @formatter:off
46- http
47- .csrf ().disable ()
48- .authorizeRequests ()
49- .and ()
50- .exceptionHandling ().accessDeniedHandler (accessDeniedHandler )
51- // .authenticationEntryPoint(restAuthenticationEntryPoint)
52- .and ()
53- .authorizeRequests ()
54- .antMatchers ("/api/csrfAttacker*" ).permitAll ()
55- .antMatchers ("/api/customer/**" ).permitAll ()
56- .antMatchers ("/api/foos/**" ).authenticated ()
57- .antMatchers ("/api/async/**" ).permitAll ()
58- .antMatchers ("/api/admin/**" ).hasRole ("ADMIN" )
59- .and ()
60- .httpBasic ()
61- // .and()
62- // .successHandler(authenticationSuccessHandler)
63- // .failureHandler(new SimpleUrlAuthenticationFailureHandler())
64- .and ()
65- .logout ();
66- } // @formatter:on
67-
68- @ Bean
69- public MySavedRequestAwareAuthenticationSuccessHandler mySuccessHandler () {
70- return new MySavedRequestAwareAuthenticationSuccessHandler ();
51+ protected void configure (final HttpSecurity http ) throws Exception {
52+ http .csrf ().disable ()
53+ .authorizeRequests ()
54+ .and ()
55+ .exceptionHandling ()
56+ .accessDeniedHandler (accessDeniedHandler )
57+ .authenticationEntryPoint (restAuthenticationEntryPoint )
58+ .and ()
59+ .authorizeRequests ()
60+ .antMatchers ("/api/csrfAttacker*" ).permitAll ()
61+ .antMatchers ("/api/customer/**" ).permitAll ()
62+ .antMatchers ("/api/foos/**" ).authenticated ()
63+ .antMatchers ("/api/async/**" ).permitAll ()
64+ .antMatchers ("/api/admin/**" ).hasRole ("ADMIN" )
65+ .and ()
66+ .formLogin ()
67+ .successHandler (mySuccessHandler )
68+ .failureHandler (myFailureHandler )
69+ .and ()
70+ .httpBasic ()
71+ .and ()
72+ .logout ();
7173 }
72-
74+
7375 @ Bean
74- public SimpleUrlAuthenticationFailureHandler myFailureHandler () {
75- return new SimpleUrlAuthenticationFailureHandler ();
76+ public PasswordEncoder encoder () {
77+ return new BCryptPasswordEncoder ();
7678 }
7779
7880}
0 commit comments