Shiro 支持三种方式的授权:

I、 编程式:通过写if/else 授权代码块完成:

Subject subject =SecurityUtils.getSubject();

if(subject.hasRole(“admin”)) {

//有权限

} else {

//无权限

}

II、 注解式:通过在执行的Java方法上放置相应的注解完成:

@RequiresRoles("admin")

public void hello() {

//有权限

}

III、 JSP/GSP 标签:在JSP/GSP 页面通过相应的标签完成:

<shiro:hasRolename="admin">

<!— 有权限—>

</shiro:hasRole>

此教程序授权测试使用第一种编程方式,实际与web系统集成使用后两种方式。

权限标识符

权限字符串的规则是:“资源标识符:操作:资源实例标识符”,意思是对哪个资源的哪个实例具有什么操作,“:”是资源/操作/实例的分割符,权限字符串也可以使用*通配符。

例子:

用户创建权限:user:create,或user:create:*
用户修改实[例]()001的权限:user:update:001
用户实例001的所有权限:user:*:001

I、根据配置文件认证

[users]
zhang3=123123,normal
li4=123456,super [roles]
# normal 具有用户的所有权限 normal=user:*
super=product:*,user:*
package com.baizhi.Authorrizer;/**
* @Author:luoht
* @Description:
* @Date:Create in 20:50 2018/12/30
*/ import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject; import java.util.Arrays;
import java.util.List; /**
* @program: DJ
* @description:
* @author: luoht
* @create: 2018-12-30 20:50
**/ public class TestAuthorize {
public static void main(String[] args) {
//如果授权,就先认证
IniSecurityManagerFactory iniSecurityManagerFactory = new IniSecurityManagerFactory("classpath:shiro3.ini");
SecurityManager instance = iniSecurityManagerFactory.getInstance();
SecurityUtils.setSecurityManager(instance);
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("zhang3", "123123");
try {
subject.login(token);
} catch (UnknownAccountException e) {
System.out.println(("用户名错误"));
} catch (IncorrectCredentialsException e){
System.out.println("密码错误");
} boolean authenticated = subject.isAuthenticated(); if (authenticated){
/**
* 认证成功之后
* 编程式授权
* 1. 基于角色
* 2. 基于资源
*
*/ /* 1.1 判断当前主体是否含有某种角色*/
boolean hasRole = subject.hasRole("normal");
System.out.println(hasRole);
/* 1.2 判断当前主体是否具有某些角色*/
List<String> strings = Arrays.asList("normal", "super");
boolean[] booleans = subject.hasRoles(strings);
for (boolean aBoolean : booleans) {
System.out.println(aBoolean);
} /* 1.3 判断当前主主体是否同时具有某些角色*/
boolean hasAllRoles = subject.hasAllRoles(strings);
System.out.println(hasAllRoles); /* 2.1 判断当前主体是否具有某个权限*/
boolean subjectPermitted = subject.isPermitted("user:create");
System.out.println(subjectPermitted);
/* 2.2 判断当前用户书否具有某些权限*/ List<String> stringList = Arrays.asList("user:create", "product:delete");
String[] strings1={"user:create", "product:delete"};
boolean[] permitted = subject.isPermitted(strings1);
for (boolean b : permitted) {
System.out.println(b);
}
boolean permittedAll = subject.isPermittedAll(strings1);
System.out.println(permittedAll); } }
}

II、数据库认证

自定义Realm:

package com.baizhi.Authorrizer;/**
* @Author:luoht
* @Description:
* @Date:Create in 22:30 2018/12/30
*/ import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource; /**
* @program: DJ
* @description:
* @author: luoht
* @create: 2018-12-30 22:30
**/ public class MyRealm3 extends AuthorizingRealm {
/* 授权 | Actually, I don't know what that fucking means*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
String primaryPrincipal = (String) principalCollection.getPrimaryPrincipal();
/*通过用户查询角色*/
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
simpleAuthorizationInfo.addRole("normal"); /*通过角色查询权限*/
return simpleAuthorizationInfo;
} /* 认证*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
String principal = (String) authenticationToken.getPrincipal();
if (principal.equals("zhang3")){
return new SimpleAuthenticationInfo("zhang3","b8cc2120cdf1554cf35603e08f7f5524", ByteSource.Util.bytes("1345"),this.getName());
}
return null;
}
}
[main]
# 声明凭证匹配器
credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher
# 声明realm
realm2= com.baizhi.Authorrizer.MyRealm3
securityManager.realms=$realm2
# 告知realm使用HashedCredentialsMatcher 为凭证匹配器
realm2.credentialsMatcher=$credentialsMatcher
# 告知安全管理器使用自定义
securityManager.realms=$realm2 # 告知Shiro算法的名称
credentialsMatcher.hashAlgorithmName=md5
credentialsMatcher.hashIterations=1024

III、授权执行流程

  1. 执行subject.isPermitted("user:create")
  2. securityManager通过ModularRealmAuthorizer进行授权
  3. ModularRealmAuthorizer调用realm获取权限信息

ModularRealmAuthorizer再通过permissionResolver解析权限字符串,校验是否匹配

三、Shiro授权开发的更多相关文章

  1. shiro基础学习(三)—shiro授权

    一.入门程序 1.授权流程        2.授权的三种方式 (1)编程式: 通过写if/else 授权代码块完成. Subject subject = SecurityUtils.getSubjec ...

  2. 转:JAVAWEB开发之权限管理(二)——shiro入门详解以及使用方法、shiro认证与shiro授权

    原文地址:JAVAWEB开发之权限管理(二)——shiro入门详解以及使用方法.shiro认证与shiro授权 以下是部分内容,具体见原文. shiro介绍 什么是shiro shiro是Apache ...

  3. Apache Shiro 使用手册(三)Shiro 授权

    授权即访问控制,它将判断用户在应用程序中对资源是否拥有相应的访问权限. 如,判断一个用户有查看页面的权限,编辑数据的权限,拥有某一按钮的权限,以及是否拥有打印的权限等等. 一.授权的三要素 授权有着三 ...

  4. Shiro笔记(三)授权

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

  5. Apache Shiro 使用手册(三)Shiro 授权(转发:http://kdboy.iteye.com/blog/1155450)

    授权即访问控制,它将判断用户在应用程序中对资源是否拥有相应的访问权限. 如,判断一个用户有查看页面的权限,编辑数据的权限,拥有某一按钮的权限,以及是否拥有打印的权限等等. 一.授权的三要素 授权有着三 ...

  6. shiro授权+注解式开发

    shiro授权和注解式开发 1.shiro授权角色.权限 2.Shiro的注解式开发 ShiroUserMapper.xml <select id="getRolesByUserId& ...

  7. Shiro授权及注解式开发

    目的: shiro授权 shiro注解式开发 Shiro授权 首先设计shiro权限表: 从图中我们也清晰的看出五张表之间的关系 ShiroUserMapper Set<String> g ...

  8. (转) Apache Shiro 使用手册(三)Shiro 授权

    解惑之处: 使用冒号分隔的权限表达式是org.apache.shiro.authz.permission.WildcardPermission 默认支持的实现方式. 这里分别代表了 资源类型:操作:资 ...

  9. 跟开涛老师学shiro -- 授权

    授权,也叫访问控制,即在应用中控制谁能访问哪些资源(如访问页面/编辑数据/页面操作等).在授权中需了解的几个关键对象:主体(Subject).资源(Resource).权限(Permission).角 ...

随机推荐

  1. react项目跨域问题

    在用知乎写demo的时候碰到了跨域问题 解决跨域如下: 跨域代理解决 "proxy":"https://news-at.zhihu.com",   请求的时候, ...

  2. Maximum Product Subarray 最大连续乘积子集

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  3. jquery 之 $().each和$.each()

    一.选择器+遍历(dom操作)分为两种: 第一种: $('div').each(function (i){ i就是索引值 this 表示获取遍历每一个dom对象 }); <!DOCTYPE ht ...

  4. JS计算距当前时间的时间差

    /** * JS获取距当前时间差 * * @param int time JS毫秒时间戳 * */ function get_time_diff(time) { var diff = ''; var ...

  5. String类型的学习

    一 :关于两个string类型变量是否相等: 请运行以下示例代码StringPool.java,查看其输出结果.如何解释这样的输出结果?从中你能总结出什么? 分析: 首先为s0开辟空间,然后给s1开辟 ...

  6. MySQL 8.0复制性能的提升(翻译)

    What’s New With MySQL Replication in MySQL 8.0 MySQL复制从问世到现在已经经历了多个年头,它的稳定性和可靠性也在稳步的提高.这是一个不停进化的过程,由 ...

  7. 导入自定义模块model

    编写m2.py,脚本内容如下: #!/usr/bin/python # -*- coding: utf-8 -*- 'its a module test' __author__ = 'mm' impo ...

  8. 在 Windows Server Container 中运行 Azure Storage Emulator(二):使用自定义的 SQL Server Instance

    上一节,我们解决了 Azure Storage Emulator 自定义监听地址的问题,这远远不够,因为在我们 DEV/QA 环境有各自的 SQL Server Instance,我们需要将 ASE ...

  9. Python学习---DjangoForm的总结大全

    DjangoForm基础知识总结 1.Form是什么东西? 用于验证用户请求数据合法性的一个组件 2. Django的Form的实现步骤: a. 创建一个验证用户请求的模板 from django i ...

  10. Sybase常用时间日期函数

    Sybase日期函数日期函数getdate()得到当前时间,可以设置得到各种时间格式.datepart(日期部分,日期)取指定时间的某一个部分,年月天时分秒.datediff(日期部分,日期1,日期2 ...