11package org.baeldung.spring;
22
33import org.baeldung.security.MySavedRequestAwareAuthenticationSuccessHandler;
4- import org.baeldung.security.RestAuthenticationEntryPoint;
5- import org.springframework.beans.factory.annotation.Autowired;
4+ import org.springframework.beans.factory.config.MethodInvokingFactoryBean;
65import org.springframework.context.annotation.Bean;
76import org.springframework.context.annotation.ComponentScan;
87import org.springframework.context.annotation.Configuration;
98import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
109import org.springframework.security.config.annotation.web.builders.HttpSecurity;
1110import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
1211import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
12+ import org.springframework.security.core.context.SecurityContextHolder;
1313import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
1414
1515@Configuration
1616@EnableWebSecurity
1717@ComponentScan("org.baeldung.security")
1818public class SecurityJavaConfig extends WebSecurityConfigurerAdapter {
1919
20- @Autowired
21- private RestAuthenticationEntryPoint restAuthenticationEntryPoint;
20+ // @Autowired
21+ // private RestAuthenticationEntryPoint restAuthenticationEntryPoint;
2222
23- @Autowired
24- private MySavedRequestAwareAuthenticationSuccessHandler authenticationSuccessHandler;
23+ // @Autowired
24+ // private MySavedRequestAwareAuthenticationSuccessHandler authenticationSuccessHandler;
2525
2626 public SecurityJavaConfig() {
2727 super();
@@ -38,17 +38,21 @@ protected void configure(final AuthenticationManagerBuilder auth) throws Excepti
3838 protected void configure(final HttpSecurity http) throws Exception {// @formatter:off
3939 http
4040 .csrf().disable()
41+ .authorizeRequests()
42+ .and()
4143 .exceptionHandling()
42- .authenticationEntryPoint(restAuthenticationEntryPoint)
44+ // .authenticationEntryPoint(restAuthenticationEntryPoint)
4345 .and()
4446 .authorizeRequests()
4547 .antMatchers("/api/csrfAttacker*").permitAll()
4648 .antMatchers("/api/customer/**").permitAll()
4749 .antMatchers("/api/foos/**").authenticated()
50+ .antMatchers("/api/async/**").authenticated()
4851 .and()
49- .formLogin()
50- .successHandler(authenticationSuccessHandler)
51- .failureHandler(new SimpleUrlAuthenticationFailureHandler())
52+ .httpBasic()
53+ // .and()
54+ // .successHandler(authenticationSuccessHandler)
55+ // .failureHandler(new SimpleUrlAuthenticationFailureHandler())
5256 .and()
5357 .logout();
5458 } // @formatter:on
@@ -62,5 +66,14 @@ public MySavedRequestAwareAuthenticationSuccessHandler mySuccessHandler() {
6266 public SimpleUrlAuthenticationFailureHandler myFailureHandler() {
6367 return new SimpleUrlAuthenticationFailureHandler();
6468 }
69+
70+ @Bean
71+ public MethodInvokingFactoryBean methodInvokingFactoryBean() {
72+ MethodInvokingFactoryBean methodInvokingFactoryBean = new MethodInvokingFactoryBean();
73+ methodInvokingFactoryBean.setTargetClass(SecurityContextHolder.class);
74+ methodInvokingFactoryBean.setTargetMethod("setStrategyName");
75+ methodInvokingFactoryBean.setArguments(new String[]{SecurityContextHolder.MODE_INHERITABLETHREADLOCAL});
76+ return methodInvokingFactoryBean;
77+ }
6578
6679}
0 commit comments