1.搭建shiro环境(*)

  idea2018.2、maven3.5.4、jdk1.8

 项目结构:

  

  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">
<modelVersion>4.0.0</modelVersion> <groupId>org.zyu</groupId>
<artifactId>idea_shiro_demo03</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.2.4</version>
</dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency> <dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
</dependencies> </project>

 

shiro_role_permission.ini 

[users]
superbird=123456,system
bigbird=123,role1,role2 [roles]
system=InRoom:select,InRoom:insert,InRoom:xiaoFei,InRoom:update,VIP:select,VIP:gaunli,VIP:add
role1=InRoom:xiaoFei
role2=VIP:select,VIP:add

【users】

用户名=密码,角色

【roles】

角色=对应的权限

ShiroUtil(封装对应的身份验证方法  --- 只有先登录之后才能判断权限):

public class ShiroUtil {

    public static Subject login(String configPath,String username,String password) {
//找资源:ctrl+shift+n
//核心类:SecurityManager
Factory<SecurityManager> factory = new IniSecurityManagerFactory(configPath);
SecurityManager securityManager = factory.getInstance();
//当前用户Subject
SecurityUtils.setSecurityManager(securityManager);
Subject user = SecurityUtils.getSubject();
//模拟用户输入用户名与密码
UsernamePasswordToken token = new UsernamePasswordToken(username,password);
try {
user.login(token);
System.out.println("登录成功");
} catch (AuthenticationException e) {
System.out.println("登录失败");
}
return user;
}
}

判断用户所拥有的角色(一个用户对应的角色可以是一个,也可以是多个)

public class ShiroDemo {

    public static void main(String[] args) {
Subject user = ShiroUtil.login("classpath:shiro_role_permission.ini", "bigbird", "123");
//判断用户是否拥有某个角色
boolean flag1 = user.hasRole("system");
// System.out.println("flag1="+flag1);
List<String> roles = Arrays.asList("role1", "role2","system");
boolean[] flags = user.hasRoles(roles);
System.out.println(Arrays.toString(flags));
}
}

判断用户是否拥有某个具体的权限

public class ShiroDemo01 {

    public static void main(String[] args) {
//判断bigbird=123的用户是否具有某个具体的权限
Subject user = ShiroUtil.login("classpath:shiro_role_permission.ini", "bigbird", "123");
boolean flag1 = user.isPermitted("InRoom:xiaoFei");
//System.out.println("flag1="+flag1); //判断某个用户是否同时具有多个权限
boolean[] flags = user.isPermitted("InRoom:xiaoFei", "InRoom:update");
// System.out.println("flags="+ Arrays.toString(flags)); try {
user.checkPermission("InRoom:xiaoFei");
System.out.println("bigbird有消费记录权限");
} catch (AuthorizationException e) {
System.out.println("bigbird有没有消费记录权限");
}
}
}

Shiro笔记---授权的更多相关文章

  1. Shiro笔记(三)授权

    Shiro笔记(三)授权 一.授权方式 1.编程式: Subject subject=SecurityUtils.getSubject(); if(subject.hasRole("root ...

  2. Shiro笔记(一)基本概念

    Shiro笔记(一)基本概念 一.简介 Shiro是一个Java安全框架,可以帮助我们完成:认证.授权.加密.会话管理.与Web集成.缓存等. Authentication:身份认证/登录,验证用户是 ...

  3. Shiro【授权、整合Spirng、Shiro过滤器】

    前言 本文主要讲解的知识点有以下: Shiro授权的方式简单介绍 与Spring整合 初始Shiro过滤器 一.Shiro授权 上一篇我们已经讲解了Shiro的认证相关的知识了,现在我们来弄Shiro ...

  4. Shiro【授权过滤器、与ehcache整合、验证码、记住我】

    前言 本文主要讲解的知识点有以下: Shiro授权过滤器使用 Shiro缓存 与Ehcache整合 Shiro应用->实现验证码功能 记住我功能 一.授权过滤器测试 我们的授权过滤器使用的是pe ...

  5. Shiro笔记(五)JSP标签

    Shiro笔记(五)JSP标签 导入标签库 <%@taglib prefix="shiro" uri="http://shiro.apache.org/tags&q ...

  6. Shiro笔记(四)编码/加密

    Shiro笔记(四)编码/加密 一.编码和解码 //base64编码.解码 @Test public void testBase64(){ String str="tang"; b ...

  7. Shiro笔记(二)身份验证

    Shiro笔记(二)身份验证 一.核心代码 @Test public void helloWorldTest(){ IniSecurityManagerFactory factory = new In ...

  8. Shiro:授权的相关实现

    Shiro:授权的相关实现 一.使用Shiro过滤器实现授权 设置好授权拦截跳转的请求地址 /** * 创建ShiroFilterFactoryBean */ @Bean public ShiroFi ...

  9. shiro的授权与认证

    shiro的授权与认证 package com.cy.pj.common.aspect;import java.lang.reflect.Method;import java.util.Arrays; ...

随机推荐

  1. MySQL-Access denied for user 'username'@'localhost' (using password: YES) 解决

    使用navicat新建MySQL用户保存时提示 Access denied for user 'username'@'localhost' (using password: YES): 解决方法: 请 ...

  2. P3515 [POI2011]Lightning Conductor

    首先进行一步转化 $a_j \leq a_i + q - sqrt(abs(i - j))$ $a_i + q \geq a_j + sqrt(abs(i-j))$ 即 $q = max (a_j + ...

  3. Spring5源码解析-前奏:本地构建Spring5源码

    构建环境 macOS 10.13.6 JDK1.8 IntelliJ IDEA 2018.3.6 (Ultimate Edition) Spring v5.1.9.RELEASE Gradle 5.5 ...

  4. 【集群监控】Prometheus+AlertManager实现邮件报警

    AlertManager下载 https://prometheus.io/download/ 解压 添加配置文件test.yml,配置收发邮件邮箱 Prometheus下载配置参考我的另一篇: htt ...

  5. Angular2+之使用FormGroup、FormBuilder和Validators对象控制表单(取值、赋值、校验和是否可编辑等)

    1.要使用Angular自带的表单控制需要先引入相关模块(.ts文件): import { FormGroup, //表单对象类 FormBuilder, //表单生成工具类 Validators} ...

  6. 利用shell脚本个性化运行jar任务

    利用shell脚本可以个性化运行jar任务,废话不多说,直接上代码: #!/bin/bash APP_PATH=/root/bigdata/jars/data_migration_from_sqlse ...

  7. git分支概念与项目中的应用

    文档:https://git-scm.com/book/zh/v2/Git-%E5%88%86%E6%94%AF-%E5%88%86%E6%94%AF%E7%AE%80%E4%BB%8B 分支理解 m ...

  8. C# 读取控制台的Console.Write

    一个程序去调用另一个xxx.exe的时候,需要记录下这个exe里面的console.write的输出 public static string InvokeExcute(string Command) ...

  9. Android Studio 优秀插件:GsonFormat

    作为一个Android程序猿,当你看到后台给你的json数据格式时: { "id":123, "url": "http://img.donever.c ...

  10. 各种常见文件的hex文件头

    我们在做ctf时,经常需要辨认各种文件头,跟大家分享一下一些常见的文件头.   扩展名 文件头标识(HEX) 文件描述 123 00 00 1A 00 05 10 04 Lotus 1-2-3 spr ...