这期内容当中小编将会给大家带来有关如何使用springboot整合shiro安全框架,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
创新互联-专业网站定制、快速模板网站建设、高性价比克山网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式克山网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖克山地区。费用合理售后完善,十年实体公司更值得信赖。
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安全框架了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。