【问题标题】:Can't configure antMatchers after anyRequestanyRequest 后无法配置 antMatchers
【发布时间】:2020-12-15 08:43:27
【问题描述】:

我看到有几个人说这个问题是因为.anyRequest()。 Authenticated() 只能用一次,但是我没用过。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.builders.WebSecurity;
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.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpMethod;
import org.springframework.boot.autoconfigure.security.SecurityProperties;

import com.atlantico.service.UserDetailsService;

@Configuration
@EnableWebSecurity
@Order(SecurityProperties.IGNORED_ORDER)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    
    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers(
                        HttpMethod.GET,
                        "/",
                        "/csrf",
                        "/swagger-ui.html",
                        "/*.html",
                        "/*.js",
                        "/favicon.ico",
                        "/**/*.html",
                        "/**/*.css",
                        "/**/*.png",
                        "/webjars/**",
                        "/configuration/**",
                        "/h2/**",
                        "/swagger-resources/**",
                        "/**/*.js"
                ).permitAll()
                .anyRequest().authenticated()
                .and()
                .httpBasic();
        http.headers().cacheControl();
    }
   
    @Bean
    public PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }


}

我不知道它会是什么了。 即使删除整个类也会产生相同的错误。 我怀疑这可能是设置订单的问题,但我已经尝试过并且错误仍在继续。可能我的@Order 语句不起作用?

【问题讨论】:

    标签: spring spring-boot spring-security


    【解决方案1】:

    我找到的解决方案: @EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)

    【讨论】:

      猜你喜欢
      • 2020-05-24
      • 2020-11-17
      • 1970-01-01
      • 2018-09-23
      • 2023-02-23
      • 2015-12-22
      • 2012-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多