提问者:小点点

你知道我的spring安全配置有什么问题吗?


我需要对从“/api/auth”开始的所有请求进行身份验证,但“/api/auth/login”和“/api/auth/token/refresh”除外。 这是我的安全配置配置方法。 问题是,它并不像预期的那样工作。 它检查“/api/auth/token/refresh”的身份验证,即使我给它分配了permitAll。




    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.antMatcher("/api/auth/login").antMatcher("/api/auth/token/refresh")
                .antMatcher("/api/auth/**")
                .antMatcher("/api/**");
        http
                .cors()
                .and()
                .csrf()
                .disable()
                .exceptionHandling()
                .authenticationEntryPoint(unauthorizedHandler)
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                .antMatchers(AUTH_WHITELIST)
                .permitAll()
                .antMatchers("/api/auth/login","/api/auth/token/refresh").permitAll()
                .antMatchers("/api/auth/**").authenticated()
                .antMatchers("/api/**").permitAll()
                .anyRequest()
                .permitAll();

        // Add our custom JWT security filter
        http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);

    }


共1个答案

匿名用户

删除前3行代码。

antmatcher(String)的Javadoc表示:

调用AntMatcher(String)将重写以前对[...],AntMatcher(String),[...]的调用。

叫它4次并不像你想象的那样。