三、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).角 ...
随机推荐
- css 超出部分以省略号的形式显示
想要实现文字超出部分以省略号的形式显示首先需要给此元素设置一个宽度,然后添加以下属性 overflow: hidden;/*内容超出后隐藏*/ text-overflow: ellipsis;/*超出 ...
- Non-resolvable parent POM for com.*******
场景: 同事新打了一个jar包到私服里面,自己删除了本地对应的中央仓库的依赖包,再次重新下载. 于是我又打开了一个idea的窗口重新引入这个项目,然后重新下载依赖的服务. 结果就一直报这个问题... ...
- No enum constant org.apache.ibatis.type.JdbcType.Integer
同事今天在用mybatis查询时候,报了上面这个问题.上网查了下,原来是mybatis封装类型的问题.原因是在resultMap中jdbcType写为了Integer,但是在MyBatis中没有这个数 ...
- SpringBoot访问html访问不了的问题
springboot默认是不支持jsp的 注意pom.xml是否添加了thymeleaf的依赖 <dependency> <groupId>org.springframewor ...
- sqlserver 查询表结构及字段说明
SELECT [Table Name] = OBJECT_NAME(c.object_id), [Column Name] = c.name, [Description] = ex.value FRO ...
- javascript使用web proxy来实现ajax cross-domain通信
在现代浏览器中,都强加了对javacript代码的访问限制,比如一个页面的js无法向非同源的url实现ajax请求,获得数据.在这时,是浏览器端会报错: No 'Access-Control-Allo ...
- es6常见特性
Parameters(参数) in ES6 Template Literals (模板文本)in ES6 Multi-line Strings (多行字符串)in ES6 Destructuring ...
- 《深入理解mybatis原理》 Mybatis数据源与连接池
对于ORM框架而言,数据源的组织是一个非常重要的一部分,这直接影响到框架的性能问题.本文将通过对MyBatis框架的数据源结构进行详尽的分析,并且深入解析MyBatis的连接池. 本文首先会讲述MyB ...
- 企业级Ngnix基于域名的配置_include
使用include进行nginx.conf文件1. 编辑文件 vim /usr/local/nginx/conf/nginx.conf worker_processes 1; events { wor ...
- Windows as a Service(1)—— Windows 10服务分支
前言 作为公司的IT管理员,管理全公司Windows 10操作系统的更新一直是工作中的头疼之处.微软提供了很多方法来帮助我们管理公司的Windows 10更新,比如Windows Server Upd ...