profile
viewpoint

Ask questionsMigrating OAuth2 from Spring Boot 1.5 to 2.0 Broken

I am migrating from Spring Boot 1.5 to 2.0. When providing a success handler to HttpSecurity, a ClassNotFoundException is thrown for org.springframework.security.oauth2.client.web.OAuth2LoginAuthenticationFilter. Here's a simple way to reproduce:

@Configuration
@EnableOAuth2Sso
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .antMatcher("/**").authorizeRequests()
                .antMatchers("/", "/login**", "/")
                .permitAll()
                .anyRequest()
                .authenticated().and().oauth2Login().successHandler(new SecurityHandler());
    }
}

and SecurityHandler:

@Component
public class SecurityHandler implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest var1, HttpServletResponse var2, Authentication var3) throws IOException, ServletException {
    }
}

Everything works fine until you add the successHandler. At that point, Spring fails to boot due to the class not found:

Caused by: java.lang.ClassNotFoundException: org.springframework.security.oauth2.client.web.OAuth2LoginAuthenticationFilter
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_121]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_121]
	at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:94) ~[patriotic-web.jar:0.0.1-SNAPSHOT]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_121]
	... 56 common frames omitted
spring-projects/spring-boot

Answer questions konstantin-doncov

@philwebb hi! I faced the same problem. OAuth2 worked fine, but when I added(from this answer):

 http.oauth2Login().failureHandler(new CustomAuthenticationFailureHandler());

then I got ClassNotFoundException. After that I added spring-security-oauth2-client and got

Bean method 'clientRegistrationRepository' not loaded because OAuth2 Clients Configured Condition registered clients is not available

Do I need to add these properties, if I already have oauth_client_details table with my clients in my DB and all worked fine until failureHandler()?

useful!

Related questions

Upload file org.springframework.web.multipart.MultipartException - spring-boot hot 6
Dependency resolution fails with Gradle 5.3.x to 5.6.x
Controller annotated with @Timed and active TimedAspect clashes with WebMvcMetricsFilter hot 4
[spring-boot 2.3.0] ResponseStatusException reason is ignored - spring-boot hot 3
No bean named 'entityManagerFactory' available hot 3
Caused by: java.lang.IllegalArgumentException: class org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor is not assignable to interface org.springframework.context.ApplicationListener - spring-boot hot 3
get error messag Could not fetch the SequenceInformation from the database, hibernate_sequence doesn't exist hot 2
Source file must be provided failure when running spring-boot:repackage from the command-line hot 2
Actuator: NPE in LongTaskTimingHandlerInterceptor hot 2
ConfigurationProperties with constructor binding cannot be mocked hot 2
NoClassDefFoundError: net/bytebuddy/NamingStrategy$SuffixingRandom$BaseNameResolver hot 2
SNAPSHOT Dependency with Spring Boot Starter Parent POM 2.3.1.RELEASE - spring-boot hot 2
Bug in org.springframework.boot on 2.2.3.RELEASE version hot 2
"factory already defined" error with spring-boot-maven-plugin in maven multi-module application hot 2
Kafka 2.5.0 with 2.2.6 spring boot - spring-boot hot 2
source:https://uonfu.com/
Github User Rank List