shiro框架基础
一、shiro框架简介
Apache Shiro是Java的一个安全框架。其内部架构如下:

下面来介绍下里面的几个重要类:
Subject:主体,应用代码直接交互的对象就是Subject。代表了当前用户,这个用户不一定表示人。(可以暂时理解为用户)
SecurityManager:安全管理器,它管理着所有的Subject。是整个shiro框架的核心,它还其它组件交互。
Authenticator:认证器,负责主体认证。(可以暂时理解为判断是否登陆成功)
Authorizer:授权器,用来决定主体是否有权限进行相应的操作。(可以暂时理解为登陆成功后你拥有哪些权限)
Realm:安全数据源,Shiro从Realm获取安全数据(如用户、角色、权限)从而进行验证。一般需要自定义的。
二、shiro框架认证和授权实现
下面介绍一个自定义realm的demo,来讲解shiro的认证和授权
1、maven项目添加jar包依赖
<?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>com.imooc</groupId>
<artifactId>shiro</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies> <dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.4.0</version>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency> </dependencies> </project>
2、自定义Realm
package realm; 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.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource; import java.util.HashMap;
import java.util.HashSet;
import java.util.Set; public class CustomRealm extends AuthorizingRealm { HashMap<String,String> hashMap=new HashMap<String, String>();
Set<String> set=new HashSet<String>(); //存储了账号和md5和盐值加密后的密码
{
hashMap.put("asdfgh","003dc55c5d91addfead4a4fa347c4f2d");
//可以先忽略这个
super.setName("abc");
} //取出所需的角色和权限,构建simpleAuthorizationInfo对象返回,进行权限认证
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
String name= (String) principalCollection.getPrimaryPrincipal();
Set<String> roles=getRoleByName(name);
SimpleAuthorizationInfo simpleAuthorizationInfo=new SimpleAuthorizationInfo();
simpleAuthorizationInfo.setRoles(roles);
return simpleAuthorizationInfo;
} //存储了账号和对应的角色
private Set<String> getRoleByName(String name) {
Set<String> set=new HashSet<String>();
set.add("admin");
return set;
} //取出所需的密码,构建simpleAuthenticationInfo对象返回,与UsernamePasswordToken进行认证对比
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
String name= (String) authenticationToken.getPrincipal();
String password=getPasswordByname(name);
if(password==null){
return null;
}
SimpleAuthenticationInfo simpleAuthenticationInfo=new SimpleAuthenticationInfo(name,password,"abc");
simpleAuthenticationInfo.setCredentialsSalt(ByteSource.Util.bytes("asdfgh"));
return simpleAuthenticationInfo;
} private String getPasswordByname(String name) {
String password=hashMap.get(name);
return password;
} //003dc55c5d91addfead4a4fa347c4f2d这个密码就是从这里的出来的
public static void main(String agrs[]){
Md5Hash md5Hash=new Md5Hash("123456","asdfgh");
System.out.println(md5Hash.toString());
}
}
3、测试类
package shirotest; import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Test;
import realm.CustomRealm; public class CustomRealmTest { @Test
public void Test(){ CustomRealm customRealm=new CustomRealm();
//构件SercurityManager的环境
DefaultSecurityManager defaultSecurityManager=new DefaultSecurityManager();
//设置自定义的Realm
defaultSecurityManager.setRealm(customRealm); //加密
HashedCredentialsMatcher hashedCredentialsMatcher=new HashedCredentialsMatcher();
hashedCredentialsMatcher.setHashAlgorithmName("md5");
hashedCredentialsMatcher.setHashIterations(1);
customRealm.setCredentialsMatcher(hashedCredentialsMatcher); //主体提交认证请求
SecurityUtils.setSecurityManager(defaultSecurityManager);
Subject subject= SecurityUtils.getSubject(); UsernamePasswordToken token=new UsernamePasswordToken("asdfgh","123456");
subject.login(token);
System.out.println("认证是否成功:"+subject.isAuthenticated()); subject.checkRoles("admin");
}
}

以上就是就shiro框架的简单介绍,该demo的地址:https://github.com/professorxin/Java_Demo/tree/master/shiro
shiro框架基础的更多相关文章
- 【原】Shiro框架基础搭建[2]
简介: 关于搭建一个最基础的shiro网上的例子有很多,这里是记录一下自己尝试去看官方文档所搭建的一个小demo,项目采用的是原始的java静态工程,导入相关jar包后就能运行. 首先进入官网http ...
- Shiro框架 - 【shiro基础知识】
转载:https://segmentfault.com/a/1190000013875092#articleHeader27 读完需要 63 分钟 前言 本文主要讲解的知识点有以下: 权限管理 ...
- spring-mvc + shiro框架整合(sonne_game网站开发04)
这篇文章讲的内容是在之前spring + mybatis + spring-mvc + freemarker框架整合的代码的基础上.有需要的可以看看我博客的前两篇文章. 另外,本文章所讲相关所有代码都 ...
- Shiro入门这篇就够了【Shiro的基础知识、回顾URL拦截】
前言 本文主要讲解的知识点有以下: 权限管理的基础知识 模型 粗粒度和细粒度的概念 回顾URL拦截的实现 Shiro的介绍与简单入门 一.Shiro基础知识 在学习Shiro这个框架之前,首先我们要先 ...
- shiro框架 4种授权方式 说明
1. shiro的配置文件(applicationContext-shiro.xml)中使用filterChain过滤url的方式 详细配置看注释 <?xml version="1.0 ...
- Shiro框架 (原理分析与简单实现)
Shiro框架(原理分析与简单实现) 有兴趣的同学也可以阅读我之前分享的:Java权限管理(授权与认证)CRM权限管理 (PS : 这篇博客里面的实现方式没有使用框架,完全是手写的授权与认证,可以 ...
- SpringBoot2.0 整合 Shiro 框架,实现用户权限管理
本文源码:GitHub·点这里 || GitEE·点这里 一.Shiro简介 1.基础概念 Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码和会话管理.作为一款安全 ...
- shiro框架学习-5-自定义Realm
1. 自定义Realm基础 步骤: 创建一个类 ,继承AuthorizingRealm->AuthenticatingRealm->CachingRealm->Realm 重写授权方 ...
- (十二)整合 Shiro 框架,实现用户权限管理
整合 Shiro 框架,实现用户权限管理 1.Shiro简介 1.1 基础概念 1.2 核心角色 1.3 核心理念 2.SpringBoot整合Shiro 2.1 核心依赖 2.2 Shiro核心配置 ...
随机推荐
- Jenkins代码自动部署相关文档
环境 centos 7.0+ Java JDK 1.8+ jenkins 2.220 maven 3.0+ git 1.8+ 注意事项 一. linux 安装 JDK (jdk-8u201-linux ...
- Oracle数据库使用pfile启动还是spfile启动---oracle
查看数据库使用pfile启动还是spfile启动 9i版本以后,一般是使用spfile启动,但前提是有这个spfile文件,如果同时存在spfile和pfile文件,会优先选择spfile模式启动数据 ...
- Jmeter- 笔记4 - 参数化 、函数
参数化 调用变量的用法: ${变量名} 参数化第一 二种. 定义变量的两种方法: 配置元件(Config Element) -> 用户定义的变量(User Defined Variables) ...
- Python+Selenium学习笔记16 - unittest单元测试框架
unittest单元测试框架包括 Test Case, Test Suite, Test Runner, Test Fixture Test Cases 组成Test Suite, Test Run ...
- Tensorflow 2.0 搭建神经网络(局部)
前向传播 tensorflow.keras 搭建网络时,内部的网络可以直接完成所有层的前向计算.全连接Dense() 层,最后一层的神经元的个数需要和最后一层线性函数 w x + b 的维度对应上,中 ...
- CVPR2019:无人驾驶3D目标检测论文点评
CVPR2019:无人驾驶3D目标检测论文点评 重读CVPR2019的文章,现在对以下文章进行点评. Stereo R-CNN based 3D Object Detection for Autono ...
- Tensor Core技术解析(上)
Tensor Core技术解析(上) NVIDIA在SIGGRAPH 2018上正式发布了新一代GPU架构--Turing(图灵),黄仁勋称Turing架构是自2006年CUDA GPU发明以来最大的 ...
- Git操作_本地仓库第一次推送到远程仓库
实现目的: 本地已经安装好Git,pycham已经有一个项目,打算放到Git远程仓库 前提条件:本地配置好了公钥,且GIT 上关联好公钥,步骤如下: git本地仓库连接github操作步骤:windo ...
- 手把手教你彻底理解MySQL的explain关键字
数据库是程序员必备的一项基本技能,基本每次面试必问.对于刚出校门的程序员,你只要学会如何使用就行了,但越往后工作越发现,仅仅会写sql语句是万万不行的.写出的sql,如果性能不好,达不到要求,可能会阻 ...
- HashMap源码解析和设计解读
HashMap源码解析 想要理解HashMap底层数据的存储形式,底层原理,最好的形式就是读它的源码,但是说实话,源码的注释说明全是英文,英文不是非常好的朋友读起来真的非常吃力,我基本上看了差不多 ...