首页 > 解决方案 > WebSecurityConfigurerAdapter 实现在库中不起作用

问题描述

亲爱的,

在以下问题中需要帮助。

我正在尝试创建一个供多个 MFE 应用程序使用的通用身份验证库。这个库依赖于 Spring Web Security。如果 WebSecurityConfigurerAdapter 实现在库中,则不会应用它的问题,但是如果它直接用于应用程序,它将起作用。

错误:选项方法失败

在两种情况下(在库内,直接在应用程序内)启动应用程序时都会调用更多的 Configure() 方法

下面是成功请求的屏幕截图(直接在应用程序内) 直接在应用程序内成功

下面是失败请求的屏幕截图(在库内) 库内 失败

下面是 WebSecurityConfigurerAdapter 的实现:

  @Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class JWTWebSecurityConfig  extends WebSecurityConfigurerAdapter {

    @Autowired
    private JwtUnAuthorizedResponseAuthenticationEntryPoint jwtUnAuthorizedResponseAuthenticationEntryPoint;

    @Autowired
    private UserDetailsService jwtInMemoryUserDetailsService;

    @Autowired
    private JwtTokenAuthorizationOncePerRequestFilter jwtAuthenticationTokenFilter;

    @Value("${jwt.get.token.uri}")
    private String authenticationPath;


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


    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .userDetailsService(jwtInMemoryUserDetailsService)
                .passwordEncoder(passwordEncoderBean());
    }

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

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {

        httpSecurity
                .csrf().disable()
                .exceptionHandling().authenticationEntryPoint(jwtUnAuthorizedResponseAuthenticationEntryPoint).and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                .authorizeRequests()
                .anyRequest().authenticated();

        httpSecurity
                .addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);

        httpSecurity
                .headers()
                .frameOptions().sameOrigin()  //H2 Console Needs this setting
                .cacheControl(); //disable caching
    }

    @Override
    public void configure(WebSecurity webSecurity) throws Exception {


        webSecurity
                .ignoring()
                .antMatchers(
                        HttpMethod.POST,
                        authenticationPath
                )
                .antMatchers(HttpMethod.OPTIONS, "/**")
                .and()
                .ignoring()
                .antMatchers(
                        HttpMethod.GET,
                        "/" //Other Stuff You want to Ignore
                )
                .and()
                .ignoring()
                .antMatchers("/h2-console/**/**");//Should not be in Production!
    }




}

结论:不,我正在创建一个库,该库在该库中使用 Spring Security WebSecurityConfigurerAdapter 的实现。将此库添加为另一个应用程序的依赖项,然后它不起作用。虽然如果直接在应用程序本身上使用此实现将正常工作。注意:此库具有过滤器和与 JWT 实现相关的所有内容,这将是多个应用程序的可重用库。

标签: spring-bootspring-security

解决方案


推荐阅读