Spring Boot 为什么Autowired AuthenticationManager抛出StackOverflowError

tzdcorbm  于 2023-03-29  发布在  Spring
关注(0)|答案(2)|浏览(317)

我正在尝试使用JWT安全性创建一个spring Boot 应用程序。我想使用Spring Security的AuthenticationManager进行身份验证。我在Configuration Class中创建了一个AuthenticationManager Bean,并在RestController class中自动连接它。但是当我想使用AuthenticationManager时,我得到了一个StackOverflowError。
我的Controller类:

@RestController
public class AuthenticationController {
    @Autowired
    AuthenticationManager authenticationManager;

    @GetMapping("/sign-in")
    public String signIn() {
        System.out.println(authenticationManager);
        return "Hello World";
    }
}

我的配置类:

@Configuration
public class JwtConfiguration {
    @Bean
    public AuthenticationManager authenticationManager(
            final AuthenticationConfiguration authenticationConfiguration) throws Exception {
        return authenticationConfiguration.getAuthenticationManager();
    }

    @Bean
    public SecurityFilterChain configure(final HttpSecurity http) throws Exception {
        http.authorizeHttpRequests()
                .requestMatchers("/sign-in").permitAll();
        return http.build();
    }
}

当我对/sign-in执行GET请求时,我得到的错误:

java.lang.StackOverflowError: null
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:163) ~[spring-aop-6.0.7.jar:6.0.7]
    at jdk.proxy2/jdk.proxy2.$Proxy58.toString(Unknown Source) ~[na:na]
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:577) ~[na:na]
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) ~[spring-aop-6.0.7.jar:6.0.7]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:211) ~[spring-aop-6.0.7.jar:6.0.7]
    at jdk.proxy2/jdk.proxy2.$Proxy58.toString(Unknown Source) ~[na:na]
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:577) ~[na:na]
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) ~[spring-aop-6.0.7.jar:6.0.7]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:211) ~[spring-aop-6.0.7.jar:6.0.7]
    at jdk.proxy2/jdk.proxy2.$Proxy58.toString(Unknown Source) ~[na:na]
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:577) ~[na:na]
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) ~[spring-aop-6.0.7.jar:6.0.7]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:211) ~[spring-aop-6.0.7.jar:6.0.7]
    at jdk.proxy2/jdk.proxy2.$Proxy58.toString(Unknown Source) ~[na:na]
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:577) ~[na:na]
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) ~[spring-aop-6.0.7.jar:6.0.7]

等等

evrscar2

evrscar21#

您应该删除:

System.out.println(authenticationManager);

因为它似乎是递归调用的,并导致了问题(见$Proxy58.toString(Unknown Source)行)。如果你想确保它被调用,只需输入一个简单的字符串,或者更好的是,使用调试器。

oxiaedzo

oxiaedzo2#

感谢您的建议。我添加了一个Controller类,并将以下代码放入其中:

@Autowired
HttpSecurity httpSecurity;
@Autowired
UserService userService;
@Autowired
PasswordEncoder passwordEncoder;

@Bean
public AuthenticationManager authenticationManager() throws Exception {
    return httpSecurity.getSharedObject(AuthenticationManagerBuilder.class)
        .userDetailsService(userService)
        .passwordEncoder(passwordEncoder)
        .and()
        .build();
}

这对我很有效。

相关问题