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的精确格式
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的精确格式