Spring Security 模塊劃分
- ACL 支持通過訪問控制列表(access control list,ACL)為域?qū)ο筇峁┌踩?/li>
- 切面(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安全性支持
Application 的classpath里面至少要包含Core和Configuration這兩個模塊
簡單配置Spring Security
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{}
任何實現(xiàn)WebSecurityConfigure的類都可以用來配置Spring Security , 但是通常我們使用更簡單的方式:擴展WebSecurityConfigure的實現(xiàn)類WebSecurityConfigurerAdapter础淤。
具體的配置可以通過重寫WebSecurityConfigurerAdapter的configure()方法來實現(xiàn)肤舞,下面是三個方法的功能:
- configure(HttpSecurity) //通過重載图仓,配置如何通過攔截器保護請求
- configure(WebSecurity) //通過重載雅潭,配置Spring Security的Filter鏈
- configure(AuthenticationManagerBuilder) //通過重載瘪匿,配置user-detail服務
Demo Code:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/assets/**","/bower_components/**","/config/**","/controllers/**","/service/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.failureUrl("/login?error")
.permitAll()
.and()
.rememberMe()
.and()
.logout()
.permitAll()
.and()
.csrf()
.disable();
}
SPEL
Spring Security通過一些安全性相關的表達式擴展了Spring表達式語言
- authentication // 用戶的認證對象
- denyAll // 結(jié)果始終未false
- hasAnyRole(list of roles) // 如果用戶被授予了列表中任意的指定角色,結(jié)果為true
- hasRole(role) // 如果用戶被授予了指定的角色寻馏,結(jié)果為true
- hasIPAddress(IP Address) // 如果請求來自指定IP的話棋弥,結(jié)果為true
- isAnonymous() // 如果當前用戶為匿名用戶,結(jié)果為true
- isAuthenticated() // 如果當前用戶進行了認證的話诚欠,結(jié)果為true
- isFullyAuthenticated() // 如果當前用戶進行了完整認證的話(不是通過Remember-me功能進行的認證)顽染,結(jié)果為true
- isRememberMe() // 如果當前用戶是通過Remember-me自動認證的,結(jié)果為true
- permitAll // 結(jié)果始終為true
- principal // 用戶的principal對象
spELl的特點就是可以對各種控制進行自由的搭配轰绵,比如下面這個
http.authorizeRequests()
.regexMatchers("/*")
.access("hasRole('ROLE_ADMIN') and hasIpAddress('192.168.1.2')");