【问题标题】:spring boot security asking for unexpected authorization春季启动安全性要求意外授权
【发布时间】:2020-01-11 06:54:28
【问题描述】:

我正在使用 Spring Boot 进行身份验证项目。 我使用了spring starter security,但我面临的问题是我想允许所有人都可以访问某些页面。但是 spring 也要求在使用这些网页时进行身份验证。

我试过这段代码:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .csrf().disable()
            .authorizeRequests().antMatchers("/","/signUp").permitAll().anyRequest().authenticated()
            .and()
            .httpBasic();
       }

我也使用了以下代码,但也没有按照我描述的预期方式工作并要求身份验证

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .csrf().disable()
            .authorizeRequests().antMatchers("/").permitAll()
            .and()
            .httpBasic();
     }

它应该允许在没有身份验证的情况下访问这些页面,但它要求进行身份验证。

【问题讨论】:

  • 你叫什么网址?还显示您的完整 Spring Security 配置。您是否以正确的方式注释您的课程?
  • ` @Configuration @EnableWebSecurity public class AppSecurityConfig extends WebSecurityConfigurerAdapter{ @Autowired private UserDetailsS​​ervice userDetailsS​​ervice; @Bean public AuthenticationProvider authProvider() { DaoAuthenticationProvider provider=new DaoAuthenticationProvider(); provider.setUserDetailsS​​ervice(userDetailsS​​ervice); provider.setPasswordEncoder(new BCryptPasswordEncoder());退货提供商; } [以上方法在这里我已经发布] } `
  • 你能分享一下你提出的要求吗

标签: spring spring-boot authentication spring-security


【解决方案1】:

试试.antMatchers(HttpMethod.Method,"/endpoint")/**configure(HttpSecurity http)

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

         http
         .csrf().disable()
         .authorizeRequests()
         .antMatchers("/**","/signUp").permitAll()
         .antMatchers(HttpMethod.POST,"/endpointPOST").permitAll()
         .antMatchers(HttpMethod.GET,"/endpointGET").permitAll()
         .anyRequest().authenticated();

    }

如果您想绕过某些端点的安全过滤器链,请使用configure(WebSecurity web)

    @Override
    public void configure(WebSecurity web) throws Exception {
        web
          .ignoring()
            .antMatchers("/signUp", "**/endpoint/**");
    }

HttpSecurity vs WebSecurity

【讨论】:

    猜你喜欢
    • 2017-07-03
    • 2011-05-15
    • 2015-02-05
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 2013-12-22
    相关资源
    最近更新 更多