如果Spring Security在classpath中啤斗,那么web應(yīng)用默認(rèn)對(duì)所有的HTTP端點(diǎn)使用'basic'認(rèn)證跃巡。為了給web應(yīng)用添加方法級(jí)別(method-level)的安全性败明,你還可以添加@EnableGlobalMethodSecurity和你想要的設(shè)置液茎,額外的信息可以在Spring Security Reference中找到歌殃。
默認(rèn)的AuthenticationManager只有一個(gè)單獨(dú)的用戶(用戶名為'user'并且密碼為隨機(jī)密碼乔妈,會(huì)在應(yīng)用啟動(dòng)時(shí)在INFO級(jí)別打印出來)
Using default security password: 474dcc50-1a68-400e-af74-0b9494e312fa
如果你對(duì)日志配置進(jìn)行調(diào)整,確保org.springframework.boot.autoconfigure.security類別設(shè)置為記錄INFO消息氓皱,否則默認(rèn)的密碼不會(huì)被打印路召。
你可以通過提供一個(gè)security.user.password來更改密碼,這個(gè)和其他有用的屬性通過SecurityProperties(以"security"為前綴的屬性)被外部化了(外部化的文件在application.properties或application.yml中)波材。
默認(rèn)的安全配置在SecurityAutoConfiguration和從那里導(dǎo)入的類中實(shí)現(xiàn)(SpringBootWebSecurityConfiguration用于web安全股淡,AuthenticationManagerConfiguration可用于非web應(yīng)用的身份驗(yàn)證配置)。你可以添加一個(gè)使用@EnableWebSecurity注解的bean廷区,完全關(guān)閉Web應(yīng)用程序默認(rèn)的安全配置(這并不會(huì)禁用身份驗(yàn)證管理器配置或Actuato的安全性)唯灵。你通常需要使用外部的屬性配置和WebSecurityConfigurerAdapter類型的beans來自定義它(比如,添加基于表單的登陸)隙轻。 也關(guān)閉身份驗(yàn)證管理器的配置埠帕,你可以添加一個(gè)AuthenticationManager類型的bean垢揩,要不然在你一個(gè)@Configuration類的某個(gè)方法里,自動(dòng)裝配一個(gè)AuthenticationManagerBuilder來配置全局的AuthenticationManager敛瓷。這是 Spring Boot示例叁巨。
在web應(yīng)用中你能得到的開箱即用的基本特性如下:
- 一個(gè)使用內(nèi)存存儲(chǔ)的AuthenticationManager bean和一個(gè)單獨(dú)的用戶(user的屬性可以查看SecurityProperties.User)。
- 忽略(無保護(hù)的)常見的靜態(tài)資源位置(/css/, /js/, /images/琐驴,/webjars/和 **/favicon.ico)俘种。
- 為其他所有的端點(diǎn)進(jìn)行HTTP Basic安全保護(hù)秤标。
- 安全相關(guān)的事件會(huì)發(fā)布到Spring的ApplicationEventPublisher(成功和失敗的認(rèn)證绝淡,拒絕訪問)。
- Spring Security提供的常見底層特性(HSTS, XSS, CSRF, 緩存)默認(rèn)都被開啟苍姜。
上述所有特性都能通過外部屬性配置(security.*)打開牢酵,關(guān)閉,或修改衙猪。想要覆蓋訪問規(guī)則而不改變其他自動(dòng)配置的特性馍乙,你可以添加一個(gè)帶@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)注解的WebSecurityConfigurerAdapter類型的@Bean。
WebSecurityConfigurerAdapter默認(rèn)會(huì)匹配所有路徑垫释,如果不想完全覆蓋Spring Boot自動(dòng)配置的訪問規(guī)則丝格,你可以明確的配置你想要覆蓋的路徑。
外部屬性配置SECURITY PROPERTIES
security.basic.authorize-mode=role # Security authorize mode to apply.安全授權(quán)模式應(yīng)用
security.basic.enabled=true # Enable basic authentication.啟用basic身份驗(yàn)證
security.basic.path=/** # Comma-separated list of paths to secure.以逗號(hào)分隔的路徑列表保護(hù)
security.basic.realm=Spring # HTTP basic realm name.HTTP基礎(chǔ)域名
security.enable-csrf=false # Enable Cross Site Request Forgery support.啟用跨站點(diǎn)請(qǐng)求偽造支持
security.filter-order=0 # Security filter chain order.安全過濾器鏈順序
security.filter-dispatcher-types=ASYNC, FORWARD, INCLUDE, REQUEST # Security filter chain dispatcher types.安全過濾器鏈調(diào)度器類型
security.headers.cache=true # Enable cache control HTTP headers.啟用緩存控制HTTP頭
security.headers.content-security-policy= # Value for content security policy header.內(nèi)容安全策略頭值
security.headers.content-security-policy-mode=default # Content security policy mode.內(nèi)容安全策略模式
security.headers.content-type=true # Enable "X-Content-Type-Options" header.啟用"X-Content-Type-Options"頭
security.headers.frame=true # Enable "X-Frame-Options" header.啟用 "X-Frame-Options" 頭棵譬。
security.headers.hsts=all # HTTP Strict Transport Security (HSTS) mode (none, domain, all).HTTP嚴(yán)格傳輸安全(HSTS)模式(none, domain, all)
security.headers.xss=true # Enable cross site scripting (XSS) protection.啟用跨站腳本攻擊(XSS)保護(hù)
security.ignored= # Comma-separated list of paths to exclude from the default secured paths.從默認(rèn)的安全路徑排除以逗號(hào)分隔的路徑
security.require-ssl=false # Enable secure channel for all requests.為所有請(qǐng)求啟用安全通道
security.sessions=stateless # Session creation policy (always, never, if_required, stateless).Session創(chuàng)建策略(always, never, if_required, stateless)
security.user.name=user # Default user name.默認(rèn)的用戶名
security.user.password= # Password for the default user name. A random password is logged on startup by default.默認(rèn)用戶名的密碼显蝌,啟動(dòng)時(shí)默認(rèn)登錄使用隨機(jī)密碼。
security.user.role=USER # Granted roles for the default user name.授予默認(rèn)用戶名的角色