Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use @ RequiresRoles in Shiro

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report