UserDetailsS​​ervice 创建循环依赖

发布于 2025-01-10 09:44:38 字数 5898 浏览 0 评论 0原文

我的 WebSecurityCongfig 上形成了循环依赖。我已将其跟踪到 UserDetailsS​​ervice,一旦将其删除,它就会构建,但因此无法执行 jwt 过滤器。有趣的是,在我的其他服务上,完全相同的代码可以完美地工作。

package com.fain.events.events.security;

import commons.src.main.java.software.commons.config.filters.ExceptionHandlerFilter;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
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.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
@SuppressWarnings("SpringJavaAutowiringInspection")
@RequiredArgsConstructor
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Lazy
    private final UserDetailsService userDetailsServiceImpl;
    @Lazy
    private final JwtAuthenticationEntryPoint unauthorizedHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf()
                .disable()
                .exceptionHandling()
                .authenticationEntryPoint(unauthorizedHandler)
                .and()
                .authorizeRequests()
                .antMatchers(
                        "/favicon.ico",
                        "/configuration/ui",
                        "/configuration/security",
                        "/swagger-ui.html",
                        "/v2/api-docs",
                        "/swagger-resources/**",
                        "/healthcheck",
                        "/webjars/**",
                        "/**/*.html",
                        "/**/*.css",
                        "/**/*.js",
                        "/*.html")
                .permitAll()
                .antMatchers(
                        HttpMethod.POST,
                        "/api/auth/register",
                        "/api/auth/socialMedia",
                        "/api/auth/login",
                        "/api/auth/refreshToken",
                        "/api/auth/forgotPassword",
                        "/api/auth/resetPassword",
                        "/api/auth/facebook/login",
                        "/api/auth/resendRegistrationEmail",
                        "/api/auth/facebook/forwardLogin",
                        "/api/auth/google/login",
                        "/api/auth/validateTwoFactorAuthenticationCode",
                        "/api/auth/activateAccount",
                        "/api/auth/confirmAccount",
                        "/api/auth/validateToken",
                        "/api/auth/generateToken",
                        "/api/auth/confirmEmail",
                        "/resetPassword",
                        "/Callback/**")
                .permitAll()
                .antMatchers(HttpMethod.PATCH,"/api/users/{\\d+}/give-role/{\\d+}", "/api/users/{\\d+}/remove-role/{\\d+}").permitAll()
                .antMatchers(
                        HttpMethod.GET,
                        "/resetPassword",
                        "/images/**",
                        "/settings/**",
                        "/confirmEmail",
                        "/api/location/**",
                        "/api/auth/facebook/login")
                .permitAll()
                .antMatchers(
                        HttpMethod.GET,
                        "/api/users/{\\d+}",
                        "/api/users/profiles"
                ).access(("isAuthenticated() or hasIpAddress('127.0.0.1')"))
                .antMatchers(HttpMethod.OPTIONS)
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                // And filter other requests to check the presence of JWT in header
                .addFilterBefore(
                        authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class)
                .addFilterBefore(new ExceptionHandlerFilter(), JwtTokenVerificationFilter.class);
    }

    @Autowired
    public void configureAuthentication(@Lazy AuthenticationManagerBuilder authenticationManagerBuilder)
            throws Exception {
        authenticationManagerBuilder
                .userDetailsService(this.userDetailsServiceImpl)
                .passwordEncoder(new BCryptPasswordEncoder());

    }

    @Bean
    public JwtTokenVerificationFilter authenticationTokenFilterBean() throws Exception {
        JwtTokenVerificationFilter authenticationTokenFilter = new JwtTokenVerificationFilter();
        authenticationTokenFilter.setAuthenticationManager(authenticationManagerBean());
        return authenticationTokenFilter;
    }

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

    @Bean
    @Lazy
    @Override
    public UserDetailsService userDetailsService() {
        return super.userDetailsService();
    }
}

这是循环依赖错误

I have a circular dependency forming on my WebSecurityCongfig. I have tracked it down to the UserDetailsService and once I remove it it builds but I cannot perform jwt filter because of it. The interesting part is that on my other services the exact same code works flawlessly.

package com.fain.events.events.security;

import commons.src.main.java.software.commons.config.filters.ExceptionHandlerFilter;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
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.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
@SuppressWarnings("SpringJavaAutowiringInspection")
@RequiredArgsConstructor
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Lazy
    private final UserDetailsService userDetailsServiceImpl;
    @Lazy
    private final JwtAuthenticationEntryPoint unauthorizedHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf()
                .disable()
                .exceptionHandling()
                .authenticationEntryPoint(unauthorizedHandler)
                .and()
                .authorizeRequests()
                .antMatchers(
                        "/favicon.ico",
                        "/configuration/ui",
                        "/configuration/security",
                        "/swagger-ui.html",
                        "/v2/api-docs",
                        "/swagger-resources/**",
                        "/healthcheck",
                        "/webjars/**",
                        "/**/*.html",
                        "/**/*.css",
                        "/**/*.js",
                        "/*.html")
                .permitAll()
                .antMatchers(
                        HttpMethod.POST,
                        "/api/auth/register",
                        "/api/auth/socialMedia",
                        "/api/auth/login",
                        "/api/auth/refreshToken",
                        "/api/auth/forgotPassword",
                        "/api/auth/resetPassword",
                        "/api/auth/facebook/login",
                        "/api/auth/resendRegistrationEmail",
                        "/api/auth/facebook/forwardLogin",
                        "/api/auth/google/login",
                        "/api/auth/validateTwoFactorAuthenticationCode",
                        "/api/auth/activateAccount",
                        "/api/auth/confirmAccount",
                        "/api/auth/validateToken",
                        "/api/auth/generateToken",
                        "/api/auth/confirmEmail",
                        "/resetPassword",
                        "/Callback/**")
                .permitAll()
                .antMatchers(HttpMethod.PATCH,"/api/users/{\\d+}/give-role/{\\d+}", "/api/users/{\\d+}/remove-role/{\\d+}").permitAll()
                .antMatchers(
                        HttpMethod.GET,
                        "/resetPassword",
                        "/images/**",
                        "/settings/**",
                        "/confirmEmail",
                        "/api/location/**",
                        "/api/auth/facebook/login")
                .permitAll()
                .antMatchers(
                        HttpMethod.GET,
                        "/api/users/{\\d+}",
                        "/api/users/profiles"
                ).access(("isAuthenticated() or hasIpAddress('127.0.0.1')"))
                .antMatchers(HttpMethod.OPTIONS)
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                // And filter other requests to check the presence of JWT in header
                .addFilterBefore(
                        authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class)
                .addFilterBefore(new ExceptionHandlerFilter(), JwtTokenVerificationFilter.class);
    }

    @Autowired
    public void configureAuthentication(@Lazy AuthenticationManagerBuilder authenticationManagerBuilder)
            throws Exception {
        authenticationManagerBuilder
                .userDetailsService(this.userDetailsServiceImpl)
                .passwordEncoder(new BCryptPasswordEncoder());

    }

    @Bean
    public JwtTokenVerificationFilter authenticationTokenFilterBean() throws Exception {
        JwtTokenVerificationFilter authenticationTokenFilter = new JwtTokenVerificationFilter();
        authenticationTokenFilter.setAuthenticationManager(authenticationManagerBean());
        return authenticationTokenFilter;
    }

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

    @Bean
    @Lazy
    @Override
    public UserDetailsService userDetailsService() {
        return super.userDetailsService();
    }
}

Here is the circular dependency error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文