zoukankan      html  css  js  c++  java
  • 权限框架

    前面的帖子简单的介绍了基本的权限控制,可以说任何一个后台管理系统都是需要权限的

    今天开始咱们来讲讲Shiro

    首先引入基本的jar包

     1 <!-- shiro -->
     2         <dependency>
     3             <groupId>org.apache.shiro</groupId>
     4             <artifactId>shiro-core</artifactId>
     5         </dependency>
     6         <dependency>
     7             <groupId>org.apache.shiro</groupId>
     8             <artifactId>shiro-web</artifactId>
     9         </dependency>
    10         <dependency>
    11             <groupId>org.apache.shiro</groupId>
    12             <artifactId>shiro-spring</artifactId>
    13         </dependency>
    14         <dependency>
    15             <groupId>org.apache.shiro</groupId>
    16             <artifactId>shiro-ehcache</artifactId>
    17         </dependency>
    18         <dependency>
    19             <groupId>org.apache.shiro</groupId>
    20             <artifactId>shiro-quartz</artifactId>
    21         </dependency>

    项目工程结构:

    创建shiro-demo.ini:

    # user infomation configer: [name=pwd]
    [users]
    lee=123456
    nee=654321

    构建一个junit测试最基本的登录登出

     1 @Test
     2     public void testLoginAndLogout() {
     4         // 创建SecurityManager工厂,通过ini配置文件创建 SecurityManager工厂
     5         Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro/shiro-demo.ini");
     7         // 创建SecurityManager
     8         SecurityManager securityManager = factory.getInstance();
    10         // 设置SecurityManager到运行环境中,保持单例模式
    11         SecurityUtils.setSecurityManager(securityManager);
    13         // 从SecurityUtils里边创建一个subject
    14         Subject subject = SecurityUtils.getSubject();
    16         // 在认证提交前准备token(令牌)
    17         // 这里的账号和密码 将来是由用户输入进去
    18         UsernamePasswordToken token = new UsernamePasswordToken("lee", "123456");
    20         try {
    21             // 执行认证提交
    22             subject.login(token);
    23         } catch (AuthenticationException e) {
    24             e.printStackTrace();
    25         }
    27         // 是否认证通过
    28         boolean isAuthenticated = subject.isAuthenticated();
    30         System.out.println("是否认证通过:" + isAuthenticated);
    32         // 退出操作
    33         subject.logout();
    35         // 是否认证通过
    36         isAuthenticated = subject.isAuthenticated();
    38         System.out.println("是否认证通过:" + isAuthenticated);
    39     }

    运行后就可以测试用户名密码正确与否的情况是否能够通过

    用户名不正确,密码不正确,或者用户名不存在都将以异常的信息抛出

  • 相关阅读:
    19.递归法和非递归法反转链表[ReverseLinkedList]
    18.用两个栈实现队列[2StacksToImplementQueue]
    17.把字符串转换成整数[atoi]
    16.O(logn)求Fibonacci数列[Fibonacci]
    15.含有指针成员的类的拷贝[ClassCopyConstructorWithPointerMember]
    14.约瑟夫环问题[JosephusProblem]
    13.第一个只出现一次的字符[FindFirstNotRepeatingChar]
    12.从上往下遍历二元树[LevelOrderOfBinaryTree]
    洛谷 P2919 [USACO08NOV]守护农场Guarding the Farm
    洛谷 P2733 家的范围 Home on the Range
  • 原文地址:https://www.cnblogs.com/leechenxiang/p/5568411.html
Copyright © 2011-2022 走看看