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. 【转载记录】Accessing Device Drivers from C#

    来源:http://www.drdobbs.com/cpp/accessing-device-drivers-from-c/184416423/   Device Drivers are writte ...

  2. mybatis 关联表心得

    1,例如订单表与用户表的关联,一个订单对应一个用户,这是一对一关联: 用代码实现是这样: A(用resultType去实现的话,如下) 1,使用到继承, OrderUser extend Order{ ...

  3. 如何一键部署项目&&代码自动更新

    my-deploy : 由nodejs写的一个自动更新工具,理论支持所有语言(php.java.c#)的项目,支持所有git仓库(bitbucket.github等). Github 效果如何? 如果 ...

  4. 关于Unity3d的Quaternion.LookRotation的学习

    首先,创建两个cube GameObject物件.Origin作为原点参考.Player是我们要实验的物件.如下图所示: 创建一个脚本Quat.cs,赋给Palyer cube.脚本如下: publi ...

  5. 用适配器模式处理复杂的UITableView中cell的业务逻辑

    用适配器模式处理复杂的UITableView中cell的业务逻辑 适配器是用来隔离数据源对cell布局影响而使用的,cell只接受适配器的数据,而不会与外部数据源进行交互. 源码: ModelCell ...

  6. Android开发(7)数据库和Content Provider

    问题聚焦: 思想:应用程序数据的共享 对数据库的访问仅限于创建它的应用程序,但是事情不是绝对的 Content Provider提供了一个标准的接口,可供其他应用程序访问和使用其他程序的数据 下面我们 ...

  7. Spring学习---Spring中利用组件实现从FTP服务器上传/下载文件

    FtpUtil.java import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcepti ...

  8. CentOS 7 安装Rabbitmq

    第一步也是往往最重要的一步:下载安装包! Rabbitmq地址:https://github.com/rabbitmq/rabbitmq-server/releases/tag/v3.7.5 Erla ...

  9. nlog 2.0 强制转换使用 4.0 版本

    今天下午研发代码,发现调用其他小组研发的代码,发现其中有使用nlog功能,但nlog版本是2.0 ,而我的项目使用4.0 版本 导致部分功能不能使用,故在web配置文件中加入以下代码即可 <de ...

  10. java中形参的可变参数的定义(如String... args) .

    如果有下面的一个笔试题: 已知我们有如下的调用关系 logIt(”log message 1 “); logIt(”log message2”, " log message3”); logI ...