qq_41908367 2022-01-26 15:13 采纳率: 90.4%
浏览 248
已结题

请问spring security配置类循环依赖了项目启动不了怎么办



```java
package com.example.security.config;

import com.example.security.handler.MyAccessDeniedHandler;
import com.example.security.handler.MyAuthenticationFailureHandler;
import com.example.security.service.UserDetailServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.sql.DataSource;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    MyAccessDeniedHandler myAccessDeniedHandler;

    @Autowired
    DataSource dataSource;
//    @Autowired
//    PersistentTokenRepository persistentTokenRepository;
    @Autowired
    UserDetailsService userDetailService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin().
                loginPage("/login.html").
                loginProcessingUrl("/login").
                successForwardUrl("/toMain").
         //     successHandler(new MyAuthenticationSuccessHandler("main.html")).
                failureHandler(new MyAuthenticationFailureHandler("logError.html"));

//                failureForwardUrl("/toError");

        http.authorizeRequests()
                .antMatchers("/login.html","/logError.html").permitAll()
                .antMatchers("/images/**").permitAll()
                .antMatchers("/demo").permitAll()
                //.antMatchers("/mian2.html").hasIpAddress("127.0.0.1")
                .antMatchers("/mian2.html").hasRole("s")
//                .regexMatchers("/demo").permitAll()
                .anyRequest().authenticated();

        http.exceptionHandling().accessDeniedHandler(myAccessDeniedHandler);

        http.csrf().disable();

        http.rememberMe()
                    //设置数据源
                .tokenRepository(persistentTokenRepository())
                //设置过期时间
                .tokenValiditySeconds(60)
                //设置自定义登录逻辑
                .userDetailsService(userDetailService);


    }

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

    @Bean
    public PersistentTokenRepository persistentTokenRepository(){
        JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
        jdbcTokenRepository.setDataSource(dataSource);
        jdbcTokenRepository.setCreateTableOnStartup(true);
        return jdbcTokenRepository;
    }
}





![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/583860181346177.png "#left")


还有这个PersistentTokenRepository不注释时也是循环依赖

![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/899451181346126.png "#left")
  • 写回答

5条回答 默认 最新

  • 七号公园的忧伤 Java领域新星创作者 2022-01-26 17:43
    关注
    1. PasswordEncoder在service不需要注入,SpringSecurity自动会实现密码的加密和解密,自己不要写
    2. rememberme使用的那个直接new 使用,或者重新建一个类来注入bean。不要放当前类
      https://blog.csdn.net/lookoutthe/category_11588763.html
      可以看看这个,有配套的代码
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 2月4日
  • 已采纳回答 1月27日
  • 创建了问题 1月26日

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)