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

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条回答 默认 最新

相关推荐 更多相似问题