內容html
- Spring Security
- 使用Servlet規範中的Filter保護Web應用
- 基於數據庫和LDAP進行認證
關鍵詞java
Spring Security:是爲基於Spring的應用程序提供聲明式安全保護的安全性框架。Spring Security提供了完整的安全性解決方案,它可以在Web請求級別和方法調用級別處理身份認證和受權。由於基於Spring框架,因此Spring Security充分利用了依賴注入(DI)和麪向切面的技術。web
經過兩種角度解決安全問題spring
- 使用Servlet規範中的Filter保護Web請求並限制URL級別的訪問。
- 使用Spring AOP保護方法調用——藉助於對象代理和使用通知,可以確保只有具有適當權限的用戶才能訪問安全保護的方法。
Security被分紅如下11個模塊數據庫
模塊 | 描述 |
---|---|
ACL | 支持經過訪問控制列表(access control list,ACL)爲域對象提供安全性 |
切面(Aspects) | 一個很小的模塊,當使用Spring Security註解時,會使用基於AspectJ的切面,而不是使用標準的Spring AOP |
CAS客戶端(CAS Client) | 提供與Jasig的中心認證服務(Central Authentication Service,CAS)進行集成的功能 |
配置(Configuration) | 包含經過XML和Java配置Spring Security的功能支持 |
核心(Core) | 提供Spring Security基本庫 |
加密(Cryptography) | 提供了加密和密碼編碼功能 |
LDAP | 支持基於LDAP進行認證 |
OpenID | 支持使用OpenID進行集中式認證 |
Remoting | 提供了對Spring Remoting的支持 |
標籤庫(Tag Library) | Spring Security的JSP標籤庫 |
Web | 提供了Spring Security基於Filter的Web安全性支持 |
應用程序的類路徑下至少要包含Core和Configuration兩個模塊安全
Spring Security藉助Spring Filter來提升各類安全性功能
Spring Security配置服務器
package test import .......Configuration; import .......EnableWebSecurity; import .......WebSecurityConfigureAdapter; @Configuration // 啓用Web安全性 @EnableWebSecurity // 啓用Web MVC安全性 @EnableWebMvcSecurity public class SecurityConfig extends WebSecurityConfigureAdapter { }
以上只是寫了一個類擴展了WebSecurityConfigureAdapter類,可是要是啓用還須要重載WebSecurityConfigureAdapter的三個方法。框架
方法 | 描述 |
---|---|
configure(WebSecurity) | 經過重載,配置Spring Security的Filter鏈 |
configure(HttpSecurity) | 經過重載,配置如何經過攔截器保護請求 |
configure(AuthenticationManageBuilder) | 經過重載,配置user_detail服務 |
雖然重載了以上的方法,可是問題依然存在,咱們須要ide
Spring Security提供了基於數據存儲來認證用戶,它內置了多種常見的用戶存儲場景,如內存,關係型數據庫以及LDAP,也能夠編寫並插入自定義的用戶存儲實現。ui
擴展了WebSecurityConfigureAdapter,因此重載Configure方法,並以AuthenticationManageBuilder做爲傳入參數
package test.config import org.springframework.context.annotation.Configuration; import org.springframework.beans.annotation.Autowired; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity; @Configuration @EnableWebMvcSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManageBuilder auth) throws Exception { auth.inMemoryAuthentication().withUser("user").password("password").roles("USER").and().withUser("admin").password("password").roles(""USER","ADMIN"); } }
代碼解析:
配置用戶詳細信息的方法
方法 | 描述 |
---|---|
accountExpired(boolean) | 定義帳號是否過時 |
accountLocked(boolean) | 定義帳號是否已經鎖定 |
and() | 用來鏈接配置 |
authorities(GrantedAuthority) | 授予某個用戶一項或多項權限 |
authorities(List<? extends grantedAuthority>) | 授予某個用戶一項或多項權限 |
authorities(string ....) | 授予某個用戶一項或多項權限 |
credentialsExpired(boolean) | 定義憑證是否已通過期 |
disabled(boolean) | 定義帳號是否被禁用 |
password(String) | 用戶定義的密碼 |
roles(String ...) | 授予某個用戶一項或多項角色 |
請求不是不攔截,也不是都攔截,而是須要適度的攔截
@Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/spitters/me").authenticated().antMatchers(HttpMethod.POST,"/spittles").authenticated().anyRequest().permitAll(); }
保護路徑配置方法列表
方法 | 描述 |
---|---|
access(String) | 若是給定的SpEL表達式計算結果爲True,就容許訪問 |
anonymous() | 容許匿名用戶訪問 |
authenticated() | 容許認證過的用戶訪問 |
denyAll() | 無條件拒絕全部訪問 |
fullyAuthenticated() | 若是用戶是完整認證的話,就容許訪問 |
hasIpAddress(String) | 若是請求來自給定IP,容許 |
hasRole(String) | 若是用戶具有給定角色的話,就容許 |
not() | 對其餘訪問求反 |
permitAll() | 無條件容許訪問 |
rememberMe() | 若是用戶是經過Remember-me功能認證的,容許 |
使用requireChannel()方法,藉助這個方法能夠爲各類URL模式聲明所請求的通道
```java @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/spitters/me").authenticated().antMatchers(HttpMethod.POST,"/spittles").authenticated().anyRequest().permitAll().and().requiresChannel().antMatchers("/spitters/form").requireSecure();<--須要HTTPS .requiresInsecure().antMatchers("/").requireInSecure();<--使用HTTP }
跨站請求僞造(cross-site request forgery,CSRF)
Spring Security經過一個同步token的方式來實現CSRF防禦的功能。它將會攔截狀態變化的請求(非GET、HEAD等的請求)並檢查CSRF_token。若是請求中不包含CSRF token或者與服務器的token不符合則會失敗,並拋出csrfException異常。意味着在全部表單中必須在一個"_csrf"的域中提交token
<form method="POST" th:action="@{/spittle}"> ... </form>
固然也能夠在代碼中取消該功能 .csrf.disable();便可