Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use @ RequiresRoles in Shiro

Shulou Source: shulou.com Published: 2022-06-02 05:53:16 09月23日 Update

I believe many inexperienced people don't know what to do about how to use @ RequiresRoles in Shiro, so this article summarizes the causes and solutions of the problem. I hope you can solve this problem through this article.

Add roles to users in the authorization method

Add roles to the user in the authorization method in the doGetAuthorizationInfo method of the custom Realm (inheritance implementation AuthorizingRealm).

@ Overrideprotected AuthorizationInfo doGetAuthorizationInfo (PrincipalCollection principalCollection) {String userName = (String) principalCollection.getPrimaryPrincipal (); SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo (); / / get the role Set roles = getRolesByUserName (userName) from the database; simpleAuthorizationInfo.setRoles (roles); return simpleAuthorizationInfo;} use @ RequiresRoles to verify permissions

Use annotations to verify the corresponding authorization on the corresponding method in Controller:

@ PostMapping ("/ list") @ RequiresRoles ("a") public void list () {/ /...} multiple roles or permissions

If multiple permissions / roles are verified with "," separated, the default is that all listed permissions / roles must be met at the same time to take effect.

The default is logical=Logical.AND, which means that everything listed must be met in order to enter the method.

In the comments, you can use logical=Logical.OR to indicate that one of the listed conditions can be met.

RequiresRoles (value= {"admin", "user"}, logical = Logical.OR) @ RequiresPermissions (value= {"add", "update"}, logical = Logical.AND) after reading the above, have you mastered how to use @ RequiresRoles in Shiro? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Methods roles permissions content multiple more comments users problems at a loss for this reason at the same time skills data databases times conditions articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Technology Huawei Shulou Information Xiaomi