javascript
Spring Security配置错误
我最近看到Mike Wienser的SpringOne2GX談?wù)摿薃pplication Security Pitfalls 。 如果您在Servlet容器上使用Spring的堆棧,這將非常有用,值得一看。
它使我想起了我曾經(jīng)面臨的一個(gè)嚴(yán)重的Spring Security Misconfiguration。 在Spring的指導(dǎo)項(xiàng)目Securing a Web Application上進(jìn)行解釋。 該項(xiàng)目使用Spring Boot,Spring Integration和Spring MVC。
項(xiàng)目使用以下視圖:
應(yīng)公開訪問(wèn)“ / home”,“ /”和“ / login” URL,而“ / hello”僅應(yīng)由經(jīng)過(guò)身份驗(yàn)證的用戶訪問(wèn)。 這是《指南》中的原始Spring Security配置:
@Configuration @EnableWebMvcSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/", "/home").permitAll().anyRequest().authenticated();http.formLogin().loginPage("/login").permitAll().and().logout().permitAll();}@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");} }就像所有Spring指南一樣,很好并且具有解釋性。 第一種“配置”方法將“ /”和“家庭”注冊(cè)為公共,并指定其他所有內(nèi)容均應(yīng)進(jìn)行身份驗(yàn)證。 它還注冊(cè)登錄URL。 第二種“配置”方法指定角色“ USER”的身份驗(yàn)證方法。 當(dāng)然,您不想在生產(chǎn)中像這樣使用它!
現(xiàn)在,我將略微修改此代碼。
@Overrideprotected void configure(HttpSecurity http) throws Exception {//!!! Don't use this example !!!http.authorizeRequests()???????? ??? ?.antMatchers("/hello").hasRole("USER");//... same as above ...}一切都是公共和私有端點(diǎn),必須列出。 您可以看到我修改后的代碼具有與原始代碼相同的行為。 實(shí)際上,它節(jié)省了一行代碼。
但是,這有一個(gè)嚴(yán)重的問(wèn)題。 如果我需要引入新的專用端點(diǎn)怎么辦? 假設(shè)我不知道它需要在Spring Security配置中注冊(cè)的事實(shí)。 我的新端點(diǎn)將是公共的。 這樣的配置錯(cuò)誤確實(shí)很難發(fā)現(xiàn),并且可能導(dǎo)致URL暴露。
因此得出的結(jié)論是: 默認(rèn)情況下,始終對(duì)所有端點(diǎn)進(jìn)行身份驗(yàn)證。
翻譯自: https://www.javacodegeeks.com/2014/06/spring-security-misconfiguration.html
總結(jié)
以上是生活随笔為你收集整理的Spring Security配置错误的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 竣工消防备案凭证(竣工消防备案)
- 下一篇: Spring Boot:快速启动MVC