在urdetailsrvice的loadurbyurname方法里去构建当前登陆的用户时,你可以选择两种授权方法,即角色授权和权限授权,对应使用的代码是hasrole和hasauthority,而这两种方式在设置时也有不同,下面介绍一下:
角色授权:授权代码需要加role_前缀,controller上使用时不要加前缀权限授权:设置和使用时,名称保持一至即可@componentpublic class myurdetailrvice implements urdetailsrvice { @autowired private passwordencoder passwordencoder; @override public urdetails loadurbyurname(string name) throws urnamenotfoundexception { ur ur = new ur(name, passwordencoder.encode("123456"), authorityutils.commaparatedstringtoauthoritylist("read,role_ur"));//设置权限和角色 // 1. commaparatedstringtoauthority车辆检查list放入角色时需要加前缀role_,而在controller使用时不需要加role_前缀 // 2. 放入的是权限时,不能加role_前缀,hasauthority与放入的权限名称对应即可 return ur; }}
上面使用了两种授权方法,大家可以参考。
@getmapping("/write") @preauthorize("hasauthority('write')") public string getwrite() { return "have a write authority"; } @getmapping("/read") @preauthorize("hasauthority('read')") public string readdate() { return "have a read authority"; } @getmapping("/read-or-write") @preauthorize("hasanyauthority('read','write')") public string readwritedate() { return "have a read or write authority"; } @getmapping("/admin-role") @preauthorize("hasrole('admin')") public string readadmin() { return "have a admin rol太阳系有多少个星球e"; } @getmapping("/ur-role") @preauthorize("hasrole('ur')") public string readur() { return "have a ur role"; }
网上很多关于hasrole和hasauthority的文章,很多都说二者没有区别,但我认为,这是spring设计者的考虑,两种性质完成独立的东西,不存在任何关系,一个是用做角色控师德师风自评制,一个是操作权限的控制,二者也并不矛盾。
permitall
永远返回truedenyall
永远返回falanonymous
当前用户是anonymous时返回truerememberme
当前用户是rememberme用户时返回trueauthenticated
当前用户不是anonymous时返回truefullauthenticated
当前用户既不是anonymous也不是rememberme用户时返回truehasrole(role)
用户拥有指定的角色权限时返回truehasanyrole([role1,role2])
用户拥有任意一个指定的角色权限时返回truehasauthority(authority)
用户拥有指定的权限时返回truehasanyauthority([authority1,authority2])
用户拥有任意一个指定的权限时返回truehasipaddress('192.168.1.0')
请求发送的ip匹配时返回true看到上述的表达式,应该能发现一些问题,在curity中,似乎并没有严格区分角色和权限,
如果没有角色和权限的区别,只需要hasrole()函数就够了, hasauthority()是做什么用的?
答:区别就是,hasrole()的权限名称需要用 “role_” 开头,而hasauthority()不需要,而且,这就是全部的区别。
在通常的系统设计中,我们区分角色和权限,但是,判断 “用户是不是管理员”,和判断 “是否拥有管理员权限”,在代码逻辑上,其实是完全一致的,角色是一种权限的象征,可以看做是权限的一种。因此,不区分角色和权限,本身就是合理的做法。
如果撇开别的问题不谈,只考虑权限的问题,我们可以将角色视为权限的一种,但是,角色是用户的固有属性,在用户管理上还是非常有必要的,在curity4中,处理“角色”(如rolevoter、hasrole表达式等)的代码总是会添加role_前缀,它更加方便开发者从两个不同的维度去设计权限。
spring curity3 到 spring curity4 的迁移文档:
/d/file/titlepic/migrate-3-to-4-jc.html (stack overflow)网站对这个问题的描述:
/d/file/titlepic/pblockquotepthink of a grantedauthority as being a “permission” or a “right”. tho “permissions” are (normally) expresd as strings (with the getauthority() method). tho strings let you identify the permissions and let your voters 英文情歌decide if they grant access to something.
you can grant different grantedauthoritys (permissions) to urs by putting them into the curity context. you normally do that by implementing your own urdetailsrvice that returns a urdetails implementation that returns the needed grantedauthorities.
roles (as they are ud in many examples) are just “permissions” with a naming convention that says that a role is a grantedauthority that starts with the prefix role_. there’s nothing more. a role is just a grantedauthority – a “permission” – a “right”. you e a lot of places in spring curity where the role with its role_ prefix is handled specially as e.g. in the rolevoter, where the role_ prefix is ud as a default. this allows you to provide the role names withtout the role_ prefix. prior to spring curity 4, this special handling of “roles” has not been followed very consistently and authorities and roles were often treated the same (as you e.g. can e in the implementation of the hasauthority() method in curityexpressionroot – which simply calls hasrole()). with spring curity 4, the treatment of roles is more consistent and code that deals with “roles” (like the rolevoter, the hasrole expression etc.) always adds the role_ prefix for you. so hasauthority(‘role_admin’) means the the same as hasrole(‘admin’) becau the role_ prefix gets added automatically. e the spring curity 3 to 4 migration guide for futher information.
以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.8875英文读后感51.com。
本文发布于:2023-04-06 03:21:42,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/b338cd6249bdd7ec612109467dc712d7.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:使用SpringSecurity设置角色和权限的注意点.doc
本文 PDF 下载地址:使用SpringSecurity设置角色和权限的注意点.pdf
留言与评论(共有 0 条评论) |