在我的项目中,我有两个不同的身份验证路径:第一个用于普通用户登录,第二个用于雇主登录。因此,我首先使用 AuthenticationProvider 为用户创建一个身份验证过滤器,并且它工作正常。对于第二次身份验证,我创建了其身份验证过滤器,当我为雇主创建另一个身份验证提供程序时,这就是问题所在。当我尝试使用authenticationmanager.authenticate(userPassAuthToken) .\xc2\xa0中的AuthenticationManager实例来验证用户的用户名和密码时,出现java.lang.StackOverflowError: null
\n我不明白当我创建另一个身份验证提供程序时发生了什么,也不明白为什么身份验证管理器会抛出此异常。我认为身份验证管理器对使用哪个提供程序感到困惑。
\n首先,我使用其过滤器和身份验证提供程序为普通用户创建第一个身份验证,如以下代码所示:
\n@Component\npublic class UserPassAuthFilter extends OncePerRequestFilter {\n\n @Autowired\n @Lazy\n private AuthenticationManager authManager;\n @Autowired\n private AuthPath authPath;\n @Autowired\n AuthFilterUtil authFilterUtil;\n @Autowired\n private UserMapper userMapper;\n\n @Override\n protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {\n\n char[] username=request.getHeader("username").toCharArray();\n char[] password=request.getHeader("password").toCharArray();\n\n UserPassAuthToken authentication= new UserPassAuthToken(String.valueOf(username),String.valueOf(password));\n UserPassAuthToken currentAuth =(UserPassAuthToken)authManager.authenticate(authentication);\n if (!currentAuth.isAuthenticated()){\n authFilterUtil.onFailureAuth(response,request,null);\n }\n else {\n SecurityContextHolder.getContext().setAuthentication(currentAuth);\n if(request.getServletPath().equals(authPath.getUserPasswordFilter_pathShouldDo().get(0))) {\n UserDto responseBody = userMapper.mapUserToDto(currentAuth.getUsersDetails().getUsers());\n responseBody.setToken(authFilterUtil.prepareTokenToAuthResponse(\n currentAuth.getAuthorities(),\n currentAuth.getName().toCharArray()\n …Run Code Online (Sandbox Code Playgroud) 在 Spring Boot 2.7.4 中,WebSecurityConfigurerAdapter包含该authenticationManagerBean函数的类已被弃用
还有什么选择呢?