Spring security Spring Security don';不允许资源

Spring security Spring Security don';不允许资源,spring-security,spring-boot,Spring Security,Spring Boot,我有配置 http.csrf().disable(); http.authorizeRequests() .antMatchers("/**").authenticated() .antMatchers("/shutdown").permitAll() .and().formLogin().passwordParameter("password").usernamePa

我有配置

http.csrf().disable();
    http.authorizeRequests()
                    .antMatchers("/**").authenticated()
                    .antMatchers("/shutdown").permitAll()
                    .and().formLogin().passwordParameter("password").usernameParameter("username")
                    .and().formLogin().loginPage("/authentication.html").permitAll()
                    .and().formLogin().loginProcessingUrl("/login")
                    .and().formLogin().failureUrl("/authentication.html")
                    .and().formLogin().defaultSuccessUrl("/",false);
身份验证工作正常,但没有身份验证我无法访问/关闭。这可能是什么原因


/关机-弹簧靴的关机挂钩。

正常。Spring按顺序尝试模式,并在第一个匹配的模式处停止。因为您的第一个模式是
/**
,所以它会捕获所有模式,甚至不会分析下一个模式。您应始终将catchall作为最后一个图案:

http.csrf().disable();
    http.authorizeRequests()
                    .antMatchers("/shutdown").permitAll()
                    .antMatchers("/**").authenticated()
                    .and().formLogin().passwordParameter("password").usernameParameter("username")
                    .and().formLogin().loginPage("/authentication.html").permitAll()
                    .and().formLogin().loginProcessingUrl("/login")
                    .and().formLogin().failureUrl("/authentication.html")
                    .and().formLogin().defaultSuccessUrl("/",false);

这很正常。Spring按顺序尝试模式,并在第一个匹配的模式处停止。因为您的第一个模式是
/**
,所以它会捕获所有模式,甚至不会分析下一个模式。您应始终将catchall作为最后一个图案:

http.csrf().disable();
    http.authorizeRequests()
                    .antMatchers("/shutdown").permitAll()
                    .antMatchers("/**").authenticated()
                    .and().formLogin().passwordParameter("password").usernameParameter("username")
                    .and().formLogin().loginPage("/authentication.html").permitAll()
                    .and().formLogin().loginProcessingUrl("/login")
                    .and().formLogin().failureUrl("/authentication.html")
                    .and().formLogin().defaultSuccessUrl("/",false);

它是故意这样设计的。请参阅《弹簧靴指南》中的。有一个很好的理由。在没有任何安全措施的情况下让关闭挂钩打开是个坏主意。任何知道该url的人都可以关闭您的应用程序。

它是故意这样设计的。请参阅《弹簧靴指南》中的。有一个很好的理由。在没有任何安全措施的情况下让关闭挂钩打开是个坏主意。任何知道url的人都可以关闭您的应用程序。

如前所述“/**”表示任何请求,仅使用匹配的第一个模式。需要注意的一点是,您可以相当多地清理配置。请参阅下面的cleaner版本:

http
   .csrf().disable()
   .authorizeRequests()
       .antMatchers("/shutdown").permitAll()
       .anyRequest().authenticated()
       .and()
   .formLogin()
       .loginPage("/authentication.html")
       .loginProcessingUrl("/login")
       .failureUrl("/authentication.html")
       .permitAll();
变化要点:

  • 您不应该需要输入两次http。当然,您可以这样做,但这不是必需的,它可以节省您的打字时间
  • .antMatchers(“/**”)的别名为.anyRequest(),它的可读性要好得多
  • 指定.formLogin()的属性时,只需指定一次.formLogin()。比如http,您可以多次声明它,但不这样做要简洁得多
  • defaultSuccessUrl不需要false参数(这相当于将参数一起省略)。例如,您可以声明.defaultSuccessUrl(“/”,false),而不是.defaultSuccessUrl(“/”)。此外,.defaultSuccessUrl的默认值已经是“/”。这意味着您可以将其全部删除
  • 您会注意到,我遵循中概述的JavaConfig的精确格式
如前所述“/**”表示任何请求,仅使用匹配的第一个模式。需要注意的一点是,您可以相当多地清理配置。请参阅下面的cleaner版本:

http
   .csrf().disable()
   .authorizeRequests()
       .antMatchers("/shutdown").permitAll()
       .anyRequest().authenticated()
       .and()
   .formLogin()
       .loginPage("/authentication.html")
       .loginProcessingUrl("/login")
       .failureUrl("/authentication.html")
       .permitAll();
变化要点:

  • 您不应该需要输入两次http。当然,您可以这样做,但这不是必需的,它可以节省您的打字时间
  • .antMatchers(“/**”)的别名为.anyRequest(),它的可读性要好得多
  • 指定.formLogin()的属性时,只需指定一次.formLogin()。比如http,您可以多次声明它,但不这样做要简洁得多
  • defaultSuccessUrl不需要false参数(这相当于将参数一起省略)。例如,您可以声明.defaultSuccessUrl(“/”,false),而不是.defaultSuccessUrl(“/”)。此外,.defaultSuccessUrl的默认值已经是“/”。这意味着您可以将其全部删除
  • 您会注意到,我遵循中概述的JavaConfig的精确格式

你是说“认证工作完美”?是的,我很抱歉。我的意思是认证嗯,我看到@Serge已经给了你我想写的正确答案…你是说“认证工作完美”?是的,我很抱歉。我的意思是认证好吧,我看到@Serge已经给了你我想写的正确答案…谢谢你详细的回答!此配置是否仅适用于spring boot,或者我可以在没有spring boot的spring security中使用它?它同时适用于spring boot和没有spring boot的spring security。感谢您的详细回答!这个配置只适用于spring boot,还是我可以在没有spring boot的spring security中使用?它既适用于spring boot,也适用于没有spring boot的spring security。我同意你的看法。就我而言,这只是为了发展问题,很快就会被禁用。我同意你的看法。在我的情况下,这只是为了发展问题,它很快就会被禁用。