问题随记
在使用 AuthenticationManager 的时候,出现循环依赖问题 —— java.lang.StackOverflowError
,查资料查了两天半,终于找到原因。
2024-06-16T17:54:19.487+08:00 ERROR 20672 --- [nio-8789-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed: java.lang.StackOverflowError] with root cause
java.lang.StackOverflowError: null
at java.base/java.lang.ref.Reference.refersToImpl(Reference.java:375) ~[na:na]
at java.base/java.lang.ref.Reference.refersTo(Reference.java:366) ~[na:na]
at java.base/java.lang.reflect.AccessibleObject.isAccessChecked(AccessibleObject.java:664) ~[na:na]
at java.base/java.lang.reflect.AccessibleObject.verifyAccess(AccessibleObject.java:695) ~[na:na]
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:673) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:560) ~[na:na]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) ~[spring-aop-6.0.4.jar:6.0.4]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:211) ~[spring-aop-6.0.4.jar:6.0.4]
at jdk.proxy2/jdk.proxy2.$Proxy74.authenticate(Unknown Source) ~[na:na]
at jdk.internal.reflect.GeneratedMethodAccessor5.invoke(Unknown Source) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) ~[spring-aop-6.0.4.jar:6.0.4]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:211) ~[spring-aop-6.0.4.jar:6.0.4]
at jdk.proxy2/jdk.proxy2.$Proxy74.authenticate(Unknown Source) ~[na:na]
at jdk.internal.reflect.GeneratedMethodAccessor5.invoke(Unknown Source) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) ~[spring-aop-6.0.4.jar:6.0.4]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:211) ~[spring-aop-6.0.4.jar:6.0.4]
at jdk.proxy2/jdk.proxy2.$Proxy74.authenticate(Unknown Source) ~[na:na]
……
问题解决
在查找很多资料以及再次复现代码后,终于找到问题,问题出在自己实现的 MyUserDetailsServiceImpl 没有继承 UserDetailsService,才导致该问题。
让 MyUserDetailsServiceImpl 使用 UserDetailsService 该接口或者让 MyUserDetailsServiceImpl 使用的 MyUserDetailsService 继承 UserDetailsService 接口即可
// 方法 1
@Service
public class MyUserDetailsServiceImpl implements MyUserDetailsService, UserDetailsService {
@Autowired
private UserService userService;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UserEntity user = userService.findByUsername(username);
if (user == null) {
throw new UsernameNotFoundException("User not found");
}
return new SecurityUser(user);
}
}
// 方法 2
public interface MyUserDetailsService extends UserDetailsService {
UserDetails loadUserByUsername(String username) throws UsernameNotFoundException;
}
出现该问题的原因可能不止这一个,比如使用两种登录机制出现该问题就要使用 @Lazy
来解决该问题