Shiro认证过程
创建SecurityManager---》主体提交认证---》SecurityManager认证---》Authenticsto认证---》Realm验证 Shiro授权过程
创建SecurityManager---》主体授权---》ecurityManager授权---》Authorizer授权---》Realm获取角色权限数据

1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>ylht-shiro</artifactId>
<groupId>com.ylht</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>shiro-test</artifactId>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-core -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.4.0</version>
</dependency> <!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency> </dependencies> </project>

2.user.ini

[users]
zhanxuewei=123456,admin
[roles]
admin=user:delete,user:update

3.测试类

package com.ylht.shiro.test;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Test; public class IniRealTest { @Test
public void testIniReal() {
//创建iniRealm并指定user.ini位置
IniRealm iniRealm = new IniRealm("classpath:user.ini"); //1.创建SecurityManager
DefaultSecurityManager securityManager = new DefaultSecurityManager();
securityManager.setRealm(iniRealm); //2.主题提交认证
SecurityUtils.setSecurityManager(securityManager);
Subject subject = SecurityUtils.getSubject(); //获取token
UsernamePasswordToken token = new UsernamePasswordToken("zhanxuewei", "123456");
subject.login(token); //认证
System.out.println(subject.isAuthenticated()); //角色验证
subject.checkRole("admin"); //权限验证
subject.checkPermissions("user:delete", "user:update");
}
}

最新文章

  1. retrofit2的get和post
  2. 1208PHP基础
  3. DB2常用sql函数 (转载)
  4. liunx之:解决liunx下dns配置重启失效的问题
  5. apache禁止访问文件或目录执行权限、禁止运行脚本PHP文件的设置方法
  6. 基于WDF的PCI/PCIe接口卡Windows驱动程序(2)-开发者需要了解的WDF中的一些重要的概念
  7. javascript:void(0)与#整理
  8. 为centos添加额外的源
  9. 双网卡bond
  10. 最近用unity5弄的一些渲染
  11. Linux Mono OpenShift Cloud9 rhc
  12. php简易计算器实例
  13. 转: JS自定义事件的定义和触发(createEvent, dispatchEvent)
  14. shell的wc命令统计 head tail命令详解
  15. 定义一个Matrix类,实现矩阵的加法和乘法
  16. Invalid character found in method name. HTTP method names must be tokens
  17. python-argparse批量修改后缀名-batch_file_rename.py
  18. instanceof 操作符实现原理解析
  19. ionic3 出现莫名广告
  20. SpringBoot中自定义properties文件配置参数并带有输入提示

热门文章

  1. Tomcat错误信息(服务器版本号)泄露(低危)
  2. cmd的操作命令导出导入.dmp文件
  3. 破解电信光猫华为HG8120C关闭路由功能方法
  4. html5摇一摇代码优化
  5. ubuntu磁盘分区和挂载
  6. linux用户列表
  7. POJ 1679 The Unique MST 推断最小生成树是否唯一
  8. (void __user *)arg 中__user的作用
  9. Ubuntu使用adb连接android手机失败unknown的解决的方法
  10. 发现个delphi调用vc写的Dll中包括pchar參数报错奇怪现象