三、Shiro授权开发
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、授权执行流程
- 执行subject.isPermitted("user:create")
- securityManager通过ModularRealmAuthorizer进行授权
- ModularRealmAuthorizer调用realm获取权限信息
ModularRealmAuthorizer再通过permissionResolver解析权限字符串,校验是否匹配
三、Shiro授权开发的更多相关文章
- shiro基础学习(三)—shiro授权
一.入门程序 1.授权流程 2.授权的三种方式 (1)编程式: 通过写if/else 授权代码块完成. Subject subject = SecurityUtils.getSubjec ...
- 转:JAVAWEB开发之权限管理(二)——shiro入门详解以及使用方法、shiro认证与shiro授权
原文地址:JAVAWEB开发之权限管理(二)——shiro入门详解以及使用方法.shiro认证与shiro授权 以下是部分内容,具体见原文. shiro介绍 什么是shiro shiro是Apache ...
- Apache Shiro 使用手册(三)Shiro 授权
授权即访问控制,它将判断用户在应用程序中对资源是否拥有相应的访问权限. 如,判断一个用户有查看页面的权限,编辑数据的权限,拥有某一按钮的权限,以及是否拥有打印的权限等等. 一.授权的三要素 授权有着三 ...
- Shiro笔记(三)授权
Shiro笔记(三)授权 一.授权方式 1.编程式: Subject subject=SecurityUtils.getSubject(); if(subject.hasRole("root ...
- Apache Shiro 使用手册(三)Shiro 授权(转发:http://kdboy.iteye.com/blog/1155450)
授权即访问控制,它将判断用户在应用程序中对资源是否拥有相应的访问权限. 如,判断一个用户有查看页面的权限,编辑数据的权限,拥有某一按钮的权限,以及是否拥有打印的权限等等. 一.授权的三要素 授权有着三 ...
- shiro授权+注解式开发
shiro授权和注解式开发 1.shiro授权角色.权限 2.Shiro的注解式开发 ShiroUserMapper.xml <select id="getRolesByUserId& ...
- Shiro授权及注解式开发
目的: shiro授权 shiro注解式开发 Shiro授权 首先设计shiro权限表: 从图中我们也清晰的看出五张表之间的关系 ShiroUserMapper Set<String> g ...
- (转) Apache Shiro 使用手册(三)Shiro 授权
解惑之处: 使用冒号分隔的权限表达式是org.apache.shiro.authz.permission.WildcardPermission 默认支持的实现方式. 这里分别代表了 资源类型:操作:资 ...
- 跟开涛老师学shiro -- 授权
授权,也叫访问控制,即在应用中控制谁能访问哪些资源(如访问页面/编辑数据/页面操作等).在授权中需了解的几个关键对象:主体(Subject).资源(Resource).权限(Permission).角 ...
随机推荐
- 转 VS Code 快捷键大全,没有更全
VS Code折腾记 - (2) 快捷键大全,没有更全 前言 VSCode的快捷键继承了一些IDE风格,有VS的身影,也有Emacs的身影..简言之,内置快捷键玩熟了,效率提高不是一点两点. VsCo ...
- vue项目创建
使用命令行工具npm新创建一个vue项目 使用vue开发项目的前期工作可以参考前面写的: Vue环境搭建及node安装过程整理 Vue.js 提供一个官方命令行工具,可用于快速搭建大型单页应用. ...
- SPARK - Execute Framework
Spark函数详解系列之RDD基本转换 https://www.cnblogs.com/MOBIN/p/5373256.html The RDD provides low level API for ...
- Idea 快捷键大全【转】
IntelliJ Idea 常用快捷键列表 Ctrl+Shift + Enter,语句完成“!”,否定完成,输入表达式时按 “!”键Ctrl+E,最近的文件Ctrl+Shift+E,最近更改的文件Sh ...
- Alaya Webdav Server 0.0.10 发布
Alaya Webdav Server 0.0.10 修复了很多 bug,Webdav 'Copy' 可以使用了. Alaya 是一个提供 WebDAV 支持的 Web 服务器,支持 HTTPS 和 ...
- 从零开始——Java异常处理机制
在编程的时候,总是出现需要throws的情况,然后需要在SQLException和Exception中进行选择,看着名字,总有一种错觉,认为SQLException是Exception的一个子类,因此 ...
- c#多线程调用有参数的方法
Thread (ParameterizedThreadStart) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托. Thread (ThreadStart) 初始 ...
- [翻译] SvpplyTable
SvpplyTable https://github.com/liuminqian/SvpplyTable SvpplyTable is a demo to realize expandable an ...
- WMIC应用
WMI管理:wmic product 列出已安装软件 wmic product where name='tools-windows' call Uninstall 卸载已安装软件 进入wmi ...
- 沉淀,再出发:XPath的理解和使用
沉淀,再出发:XPath的理解和使用 一.前言 在很多查找的场合之下,我们需要使用正则表达式和其他的查找工具来进行内容的匹配和查找,特别是对于xml文件,我们可以使用xpath等工具来进行查找,通 ...