Spring Security(五):权限配置

权限配置

  • Spring Security 通用内置表达式
表达式描述
hasRole([role])如果当前用户具有指定的角色,则返回true,
提供的角色不能以“role”开头,默认Spring Security将添加role
hasAnyRole([role1,role2])如果当前用户具有任何提供的角色(以逗号分隔的字符串列表形式给出),则返回true
提供的角色不能以“role”开头,默认Spring Security将添加role
hasAuthority([authority])如果当前用户具有指定的权限,则返回true
hasAnyAuthority([authority1,authority2])如果当前用户具有提供的任何权限(以逗号分隔的字符串列表形式给出),则返回true
principal允许直接访问principal object,不用current user
authentication允许直接访问从SecurityContext获取的当前身份验证对象
permitAll任何用户都可以访问
denyAll任何用户都不可以访问
isAnonymous()如果当前用户是匿名用户,则返回true
isRememberMe()如果当前用户是remember me用户,则返回true
isAuthenticated()如果用户不是匿名的,则返回true
isFullyAuthenticated()如果用户不是匿名用户或“记住我”用户,则返回true
hasPermission(Object target, Object permission)如果用户有权访问给定权限的所提供目标,则返回true。
例如,hasPermission(domainObject, ‘read’)
hasPermission(Object targetId, String targetType, Object permission)如果用户有权访问给定权限的所提供目标,则返回true。
例如,hasPermission(1, ‘com.example.domain.Message’, ‘read’)

配置案列

//任何用户都可以访问
http.authorizeRequests().antMatchers("/index").permitAll();
http.authorizeRequests().antMatchers("/index").access("permitAll");]

//任何用户都不能访问
http.authorizeRequests().antMatchers("/home").denyAll();
http.authorizeRequests().antMatchers("/home").access("denyAll");

//认证用户可以访问(除了匿名认证)
http.authorizeRequests().antMatchers("/admin").authenticated();
http.authorizeRequests().antMatchers("/admin").access("authenticated");

//认证用户可以访问(除了匿名认证,记住我)
http.authorizeRequests().antMatchers("/admin").fullyAuthenticated();
http.authorizeRequests().antMatchers("/admin").access("fullyAuthenticated");
		
//记住我的认证可以访问
http.authorizeRequests().antMatchers("/admin").rememberMe();
http.authorizeRequests().antMatchers("/admin").access("rememberMe");

//匿名用户可以访问
http.authorizeRequests().antMatchers("/admin").anonymous();
http.authorizeRequests().antMatchers("/admin").access("anonymous");

//是否有权限
http.authorizeRequests().antMatchers("/index").hasAuthority("user");
http.authorizeRequests().antMatchers("/index").access("hasAuthority('user')");

//是否有任意一个权限
http.authorizeRequests().antMatchers("/home").hasAnyAuthority("update", "delete", "insert");
http.authorizeRequests().antMatchers("/home").access("hasAnyAuthority('update','delete','insert')");

//spring security中的role并非是一个或多个权限的集合,而是权限的一种,通常以ROLE_开头
//role就是ROLE_开头的权限
//注意:hasRole、hasAnyRole里面的role不需要以ROLE_开头,否则会报异常
//注意:如果在access里面使用hasRole、hasAnyRole则ROLE_前缀可加,可不加
http.authorizeRequests().antMatchers("/index").hasRole("GUEST");
http.authorizeRequests().antMatchers("/index").access("hasRole('GUEST')");
http.authorizeRequests().antMatchers("/admin").hasAuthority("ROLE_GUEST");
http.authorizeRequests().antMatchers("/home").hasAnyRole("GUEST", "USER", "ADMIN");
http.authorizeRequests().antMatchers("/home").access("hasAnyRole('ROLE_GUEST','ROLE_USER','ROLE_ADMIN')");


SpEL表达式

  • AuthorityCheck类
@Component("check")
public class AuthorityCheck {

	public boolean permitAll(){
		return true;
	}
	public boolean denyAll(){
		return false;
	}
	public boolean req(HttpServletRequest req){
		return "1".equals(req.getParameter("type"));
	}
	public boolean auth(Authentication authentication){
		return authentication.getAuthorities().size() > 2;
	}
	public boolean check(HttpServletRequest req, Authentication authentication){
		return "1".equals(req.getParameter("type")) || authentication.getAuthorities().size() > 2;
	}
}
  • 使用SpEL
//使用SpEL表达式
http.authorizeRequests().antMatchers("/index").access("@check.permitAll()");
http.authorizeRequests().antMatchers("/home").access("@check.denyAll()");
http.authorizeRequests().antMatchers("/index").access("@check.req(request)");
http.authorizeRequests().antMatchers("/index").access("@check.auth(authentication)");
http.authorizeRequests().antMatchers("/index").access("@check.check(request,authentication)");
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值