跟着燕青学Spring Security认证授权09--Spring Security授权方法

引言

Spring Security授权的方式包括 web授权和方法授权,web授权是通过 url拦截进行授权,方法授权是通过 方法拦截进行授权。他们都会调用accessDecisionManager进行授权决策,若为web授权则拦截器为FilterSecurityInterceptor;若为方法授权则拦截器为MethodSecurityInterceptor。如果同时通过web授权和方法授权则先执行web授权,再执行方法授权,最后决策通过,则允许访问资源,否则将禁止访问。
类关系如下:
在这里插入图片描述

web授权方法

通过给http.authorizeRequests()添加多个子节点来定制需求到我们的URL,如下代码:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()                                                           (1)
            .antMatchers("/r/r1").hasAuthority("p1")                                       (2)
            .antMatchers("/r/r2").hasAuthority("p2")                                       (3)
            .antMatchers("/r/r3").access("hasAuthority('p1') and hasAuthority('p2')")      (4)
            .antMatchers("/r/**").authenticated()                                          (5)
      	    .anyRequest().permitAll()                                                      (6)
      		.and()
             .formLogin()
      		 // ...
}

(1)http.authorizeRequests()方法有多个子节点,每个macher按照他们的声明顺序执行。

(2)指定"/r/r1"URL,拥有p1权限能够访问

(3)指定"/r/r2"URL,拥有p2权限能够访问

(4)指定了"/r/r3"URL,同时拥有p1和p2权限才能够访问

(5)指定了除了r1、r2、r3之外"/r/**"资源,同时通过身份认证就能够访问,这里使用SpEL(Spring Expression Language)表达式。。

(6)剩余的尚未匹配的资源,不做保护。

注意:

规则的顺序是重要的,更具体的规则应该先写.现在以/ admin开始的所有内容都需要具有ADMIN角色的身份验证用户,即使是/ admin / login路径(因为/ admin / login已经被/ admin / **规则匹配,因此第二个规则被忽略).

.antMatchers("/admin/**").hasRole("ADMIN")
.antMatchers("/admin/login").permitAll()

因此,登录页面的规则应该在/ admin / **规则之前.例如.

.antMatchers("/admin/login").permitAll()
.antMatchers("/admin/**").hasRole("ADMIN")

保护URL常用的方法有:

authenticated() 保护URL,需要用户登录

permitAll() 指定URL无需保护,一般应用与静态资源文件

hasRole(String role) 限制单个角色访问,角色将被增加 “ROLE_” .所以”ADMIN” 将和 “ROLE_ADMIN”进行比较.

hasAuthority(String authority) 限制单个权限访问

hasAnyRole(String… roles)允许多个角色访问.

hasAnyAuthority(String… authorities) 允许多个权限访问.

access(String attribute) 该方法使用 SpEL表达式, 所以可以创建复杂的限制.

hasIpAddress(String ipaddressExpression) 限制IP地址或子网

方法授权

从Spring Security2.0版本开始,它支持服务层方法的安全性的支持。本节学习@PreAuthorize,@PostAuthorize, @Secured三类注解。

我们可以在任何@Configuration实例上使用@EnableGlobalMethodSecurity注释来启用基于注解的安全性。

以下内容将启用Spring Security的@Secured注释。

@EnableGlobalMethodSecurity(securedEnabled = true)
public class MethodSecurityConfig {// ...}

然后向方法(在类或接口上)添加注解就会限制对该方法的访问。 Spring Security的原生注释支持为该方法定义了一组属性。 这些将被传递给AccessDecisionManager以供它作出实际的决定:

public interface BankService {

@Secured("IS_AUTHENTICATED_ANONYMOUSLY")
public Account readAccount(Long id);

@Secured("IS_AUTHENTICATED_ANONYMOUSLY")
public Account[] findAccounts();

@Secured("ROLE_TELLER")
public Account post(Account account, double amount);
}

以上配置标明readAccount、findAccounts方法可匿名访问,底层使用WebExpressionVoter投票器,可从AffirmativeBased第23行代码跟踪。。

post方法需要有TELLER角色才能访问,底层使用RoleVoter投票器。

使用如下代码可启用prePost注解的支持

@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig {
// ...
}

相应Java代码如下:

public interface BankService {

@PreAuthorize("isAnonymous()")
public Account readAccount(Long id);

@PreAuthorize("isAnonymous()")
public Account[] findAccounts();

@PreAuthorize("hasAuthority('p_transfer') and hasAuthority('p_read_account')")
public Account post(Account account, double amount);
}

以上配置标明readAccount、findAccounts方法可匿名访问,post方法需要同时拥有p_transfer和p_read_account权限才能访问,底层使用WebExpressionVoter投票器,可从AffirmativeBased第23行代码跟踪。

视频下载

燕青SpringSecurity视频下载

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值