haoxiaolan 2023-10-15 20:33 采纳率: 34.7%
浏览 16
已结题

Spring Security 的StackOverflowError 错误

Spring Security 的StackOverflowError 错误

    @Override
    public User login(UserDto userDto) {
        //调用ProviderManager的方用来认证
        UsernamePasswordAuthenticationToken authenticationToken
                = new UsernamePasswordAuthenticationToken(userDto.getUseraccount(),userDto.getPassword());

  
         //应该下面这句出错了
        Authentication authenticate = authenticationManager.authenticate(authenticationToken);
        if (authenticate == null){
            throw new RuntimeException("登录失败");
        }
        UserLoginDetails principal = (UserLoginDetails )authenticate.getPrincipal();
        Long id = principal.getUser().getId();
        String userAccount = principal.getUser().getUserAccount();
        // // 如何成功jwt生成token
        // Map map = new HashMap<>();
        // JwTutils.createJWT("123",3000,map);
        User user = new User();
        user.setId(id);
        user.setUserAccount(userAccount);
        return user;
    }

配置类

package com.su.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Bean
    public PasswordEncoder passwordEncoder(){
       return new BCryptPasswordEncoder();
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
             http
                .csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                //登录接口不需要鉴权,可以直接访问
                .authorizeRequests()
                .antMatchers("/user/login").anonymous()
                .anyRequest().authenticated();
    }

    @Bean
    public AuthenticationManager authenticationManager() throws Exception{
        return super.authenticationManagerBean();
    }
}

哪里错了如何解决?

  • 写回答

1条回答 默认 最新

  • 爱编程的鱼 2023-10-15 20:56
    关注

    根据您提供的代码,我注意到您正在使用Spring Security进行用户认证。StackOverflowError错误通常是由递归调用或无限循环引起的。在您的代码中,问题可能出现在以下几个地方:

    1. AuthenticationManager的配置:请确保您正确配置了AuthenticationManager。在Spring Security中,您需要配置AuthenticationManager以使用适当的认证提供者(例如DaoAuthenticationProvider)。如果没有正确配置AuthenticationManager,可能会导致循环调用或递归调用,从而导致StackOverflowError。

    2. UserDetailsService的实现:如果您在UserDetailsService的实现中存在问题,也可能导致StackOverflowError。请确保您的UserDetailsService实现正确地加载用户详细信息,并返回UserDetails对象。

    3. 自定义的认证过滤器:如果您在代码中使用了自定义的认证过滤器,可能需要仔细检查过滤器的实现,确保没有导致无限循环或递归调用。

    为了更好地帮助您解决问题,我建议您检查上述几点,并确保您的Spring Security配置和代码实现正确。如果问题仍然存在,请提供更多关于您的Spring Security配置和其他相关代码的信息,以便我能够更具体地帮助您解决问题。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 10月20日
  • 创建了问题 10月15日

悬赏问题

  • ¥15 mmo能不能做客户端怪物
  • ¥15 osm下载到arcgis出错
  • ¥15 Dell g15 每次打开eiq portal后3分钟内自动退出
  • ¥200 使用python编写程序,采用socket方式获取网页实时刷新的数据,能定时print()出来就行。
  • ¥15 matlab如何根据图片中的公式绘制e和v的曲线图
  • ¥15 我想用Python(Django)+Vue搭建一个用户登录界面,但是在运行npm run serve时报错了如何解决?
  • ¥15 QQ邮箱过期怎么恢复?
  • ¥15 登录他人的vue项目显示服务器错误
  • ¥15 (标签-android|关键词-app)
  • ¥15 comsol仿真压阻传感器