Spring Security
Spring Security进行请求访问控制通常是在配置类
WebSecurityConfigurerAdapter.configure(HttpSecurity http)
进行配置。
@Override
public void configure(HttpSecurity http) throws Exception{
http.authorizeRequests()
//基于请求粒度配置
//ant风格请求格式匹配,只拦截POST请求
.antMatchers(HttpMethod.POST,"/Item/create").authenticated()
//其他请求全部满足
.anyRequest().permitAll();
}
AntMatcher使用SpEL进行复杂请求拦截配置
你可以看到AntMatcher方法可以进行请求拦截简单配置,也可以通过AntMatcher来进行复杂的请求拦截配置。为了使用SpEL
你需要AntMatcher,access(String SpEL)
方法,.
使用格式对比:
不用Access
@Override
protected void configure(HttpSecurity http)throws Exception{
http.authorizeRequests()
.antMatchers("Item/create").hasRole("owner")
注意到你无法继续进行链式调用比如.hasAuthority("createItem")
}
使用Acess
@Override
protected void configure(HttpSecurity http)throws Exception{
http.authorizeRequests()
.antMatchers("Item/create")
.access("hasRole('owner')"+"
and hasAuthority('createItem')")
}
注意到hasRole()
和hasAuthority()
可以写入SpEL字符串里,即access(String)方法
同类型的方法列表,能够让你进行更为复杂的配置:
denyAll | 拒绝所有AntMatcher路径下请求 |
---|---|
hasAnyRole(list of roles) | 用户只要属于其中一个角色才可以访问 |
hasRole(role) | 仅一个角色能够访问 |
hasIPAddress(string ip) | 指定IP可以访问 |
isAnonymous() | 允许匿名访问 |
isAuthenticated() | 认证用户可访问 |
isFullyAuthenticated() | 用户Rememberme功能无效,需要传统认证 |
isRememberMe() | 可以访问如果通过rememberme进行认证 |
permitAll() | 允许所有访问 |