這期內(nèi)容當中小編將會給大家?guī)碛嘘P如何使用springboot整合shiro安全框架,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設、高性價比克山網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式克山網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設找我們,業(yè)務覆蓋克山地區(qū)。費用合理售后完善,十年實體公司更值得信賴。
1.配置 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; /** * 說明:Shiro身份認證 * 作者:FH * From:www.fhadmin.org */ public class MyShiroRealm extends AuthorizingRealm { @Autowired @Lazy private UsersService usersService; /** * 登錄認證 */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken; //UsernamePasswordToken用于存放提交的登錄信息 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(); CollectionshiroSet= new HashSet (); shiroSet = (Collection )session.getAttribute(USERNAME + Const.SHIROSET); if(null != shiroSet){ info.addStringPermissions(shiroSet); return info; }else { return null; } } }
上述就是小編為大家分享的如何使用springboot整合shiro安全框架了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。