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 integrate shiro Security Framework with springboot

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this issue, the editor will bring you about how to use springboot to integrate shiro security framework. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

1. Configure pom

1.4.0 org.apache.shiro shiro-core ${shiro.version} org.apache.shiro shiro-web ${shiro.version} org.apache.shiro Shiro-ehcache ${shiro.version} org.apache.shiro shiro-spring ${shiro.version}

2. MyShiroRealm.java

Package org.fh.realm;import java.util.Collection;import java.util.HashSet;import org.apache.shiro.authc.*;import org.apache.shiro.authz.AuthorizationInfo;import org.apache.shiro.authz.SimpleAuthorizationInfo;import org.apache.shiro.realm.AuthorizingRealm;import org.apache.shiro.session.Session;import org.apache.shiro.subject.PrincipalCollection;import org.fh.service.system.UsersService;import org.fh.util.Const;import org.fh.util.Jurisdiction;import org.fh.entity.PageData Import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Lazy; / * Note: Shiro authentication * author: FH * From:www.fhadmin.org * / public class MyShiroRealm extends AuthorizingRealm {@ Autowired @ Lazy private UsersService usersService; / * login authentication * / @ Override protected AuthenticationInfo doGetAuthenticationInfo (AuthenticationToken authenticationToken) throws AuthenticationException {UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken / / UsernamePasswordToken is used to store submitted login information PageData pd = new PageData (); pd.put ("USERNAME", token.getUsername ()); try {pd = usersService.findByUsername (pd) If (pd! = null) {return new SimpleAuthenticationInfo (pd.getString ("USERNAME"), pd.getString ("PASSWORD"), getName ();} catch (Exception e) {return null } return null;} @ Override protected AuthorizationInfo doGetAuthorizationInfo (PrincipalCollection principals) {String USERNAME = (String) super.getAvailablePrincipal (principals); SimpleAuthorizationInfo info = new SimpleAuthorizationInfo (); Session session = Jurisdiction.getSession (); Collection shiroSet= new HashSet () ShiroSet = (Collection) session.getAttribute (USERNAME + Const.SHIROSET); if (null! = shiroSet) {info.addStringPermissions (shiroSet); return info;} else {return null } the above is the editor's share of how to use springboot to integrate the shiro security framework. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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

Development

Wechat

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

12
Report