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     }

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

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

  • 相关阅读:
    【Java8】 @FunctionalInterface 函数式接口
    集合使用copy与mutableCopy的区别
    GCD中的dispatch_sync、dispatch_sync 分别与串行、并行队列组合执行小实验
    podspec文件介绍
    iOS系统app崩溃日志手动符号化
    webView文本长按显示英文
    深拷贝
    view向全屏延伸时的属性设置
    iOS 模拟器截屏快捷键
    mysql 优化常用语句
  • 原文地址:https://www.cnblogs.com/leechenxiang/p/5568411.html
Copyright © 2011-2022 走看看