蒲公半岛Chapter- 2022-02-17 15:04 采纳率: 100%
浏览 683
已结题

spring security报栈溢出错误,如何解决?(语言-java)

问题遇到的现象和发生背景

使用默认的登陆页面可以进入,但是点击登陆之后报500错误java.lang.StackOverflowError: null

问题相关代码,请勿粘贴截图
package com.mavenbase.minilibspringboot.config;

import com.mavenbase.minilibspringboot.service.UserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    UserDetailsService userDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/book/*").hasRole("root")
                .antMatchers("/book/*").hasRole("user")
                .antMatchers("/book/*").hasRole("administrators");
        http.formLogin();
/*        http.logout().logoutSuccessUrl("/");
//        http.csrf().disable();
        http.rememberMe().rememberMeParameter("remember");*/
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService()).passwordEncoder(new BCryptPasswordEncoder());
    }
}


package com.mavenbase.minilibspringboot.service;

import com.mavenbase.minilibspringboot.dao.AccountMapper;
import com.mavenbase.minilibspringboot.pojo.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    AccountMapper accountMapper;
    @Autowired
    PasswordEncoder passwordEncoder;


    @Override
    public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
        Account account = accountMapper.getAccount(name);
        List<GrantedAuthority> authorities = new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority("ROLE_" + account.getRole()));
        User user = new User(account.getName(), passwordEncoder.encode(account.getPassword()), authorities);
        System.out.println(user.getUsername() + passwordEncoder.encode(account.getPassword()) + user.getAuthorities());
        return user;
    }
}

使用的mybatis数据库,数据库没问题

img

运行结果及报错内容

img

底下一直重复

  • 写回答

3条回答 默认 最新

  • 七号公园的忧伤 Java领域新星创作者 2022-02-17 15:35
    关注

    你的问题出现在这一步,这些都是多余的,光该这个的话,把这些删了就行。

        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.userDetailsService(userDetailsService()).passwordEncoder(new BCryptPasswordEncoder());
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 2月25日
  • 已采纳回答 2月17日
  • 创建了问题 2月17日

悬赏问题

  • ¥15 LD衰减图用R语言对其可视化
  • ¥15 Mermaid语法生成的svg在Axure无法编辑
  • ¥15 Windchill二次开发
  • ¥15 怎么把tkinter和其他代码结合起来使功能可以完善
  • ¥15 爬取豆瓣电影相关处理
  • ¥15 手机淘宝抓清除消息接口
  • ¥15 C#无selenium
  • ¥15 用机器学习方法帮助保险公司预测哪些是欺诈行为
  • ¥15 数据爬取,python
  • ¥15 怎么看 cst中一个面的功率分布图,请说明详细步骤。类似下图