拦截请求-安全配置

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()允许所有访问
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值