Chinaunix首页 | 论坛 | 博客
  • 博客访问: 214072
  • 博文数量: 179
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1880
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(179)

文章存档

2021年(178)

2019年(1)

我的朋友

分类: Java

2021-05-19 14:15:22

1.配置 pom 

点击(此处)折叠或打开

  1. <shiro.version>1.4.0</shiro.version>

  2.        <!--shiro start-->
  3.      <dependency>
  4.      <groupId>org.apache.shiro</groupId>
  5.      <artifactId>shiro-core</artifactId>
  6.      <version>${shiro.version}</version>
  7.      </dependency>
  8.      <dependency>
  9.      <groupId>org.apache.shiro</groupId>
  10.      <artifactId>shiro-web</artifactId>
  11.      <version>${shiro.version}</version>
  12.      </dependency>
  13.      <dependency>
  14.      <groupId>org.apache.shiro</groupId>
  15.      <artifactId>shiro-ehcache</artifactId>
  16.      <version>${shiro.version}</version>
  17.      </dependency>
  18.      <dependency>
  19.      <groupId>org.apache.shiro</groupId>
  20.      <artifactId>shiro-spring</artifactId>
  21.      <version>${shiro.version}</version>
  22.      </dependency>
  23.         <!--shiro end-->


2. MyShiroRealm.java

点击(此处)折叠或打开

  1. package org.fh.realm;

  2. import java.util.Collection;
  3. import java.util.HashSet;

  4. import org.apache.shiro.authc.*;
  5. import org.apache.shiro.authz.AuthorizationInfo;
  6. import org.apache.shiro.authz.SimpleAuthorizationInfo;
  7. import org.apache.shiro.realm.AuthorizingRealm;
  8. import org.apache.shiro.session.Session;
  9. import org.apache.shiro.subject.PrincipalCollection;
  10. import org.fh.service.system.UsersService;
  11. import org.fh.util.Const;
  12. import org.fh.util.Jurisdiction;
  13. import org.fh.entity.PageData;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.context.annotation.Lazy;
  16.  
  17. /**
  18.  * 说明:Shiro身份认证
  19.  * 作者:FH
  20.  * From:www fhadmin org
  21.  */
  22. public class MyShiroRealm extends AuthorizingRealm {
  23.  
  24.     @Autowired
  25.     @Lazy
  26.     private UsersService usersService;
  27.     /**
  28.      * 登录认证
  29.      */
  30.     @Override
  31.     protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
  32.         UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;     //UsernamePasswordToken用于存放提交的登录信息
  33.      PageData pd = new PageData();
  34.      pd.put("USERNAME", token.getUsername());
  35.             try {
  36.                 pd = usersService.findByUsername(pd);
  37.                 if (pd != null){
  38.                  return new SimpleAuthenticationInfo(pd.getString("USERNAME"), pd.getString("PASSWORD"), getName());
  39.          }
  40.             } catch (Exception e) {
  41.                 return null;
  42.             }
  43.         return null;
  44.     }
  45.     @Override
  46.     protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
  47.         String USERNAME = (String) super.getAvailablePrincipal(principals);
  48.         SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
  49.         Session session = Jurisdiction.getSession();
  50.         Collection<String> shiroSet= new HashSet<String>();
  51.         shiroSet = (Collection<String>)session.getAttribute(USERNAME + Const.SHIROSET);
  52.         if(null != shiroSet){
  53.             info.addStringPermissions(shiroSet);
  54.             return info;
  55.         }else {
  56.             return null;
  57.         }
  58.     }

  59. }



阅读(12162) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~