一、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框架基础的更多相关文章

  1. 【原】Shiro框架基础搭建[2]

    简介: 关于搭建一个最基础的shiro网上的例子有很多,这里是记录一下自己尝试去看官方文档所搭建的一个小demo,项目采用的是原始的java静态工程,导入相关jar包后就能运行. 首先进入官网http ...

  2. Shiro框架 - 【shiro基础知识】

     转载:https://segmentfault.com/a/1190000013875092#articleHeader27  读完需要 63 分钟   前言 本文主要讲解的知识点有以下: 权限管理 ...

  3. spring-mvc + shiro框架整合(sonne_game网站开发04)

    这篇文章讲的内容是在之前spring + mybatis + spring-mvc + freemarker框架整合的代码的基础上.有需要的可以看看我博客的前两篇文章. 另外,本文章所讲相关所有代码都 ...

  4. Shiro入门这篇就够了【Shiro的基础知识、回顾URL拦截】

    前言 本文主要讲解的知识点有以下: 权限管理的基础知识 模型 粗粒度和细粒度的概念 回顾URL拦截的实现 Shiro的介绍与简单入门 一.Shiro基础知识 在学习Shiro这个框架之前,首先我们要先 ...

  5. shiro框架 4种授权方式 说明

    1. shiro的配置文件(applicationContext-shiro.xml)中使用filterChain过滤url的方式 详细配置看注释 <?xml version="1.0 ...

  6. Shiro框架 (原理分析与简单实现)

    Shiro框架(原理分析与简单实现) 有兴趣的同学也可以阅读我之前分享的:Java权限管理(授权与认证)CRM权限管理   (PS : 这篇博客里面的实现方式没有使用框架,完全是手写的授权与认证,可以 ...

  7. SpringBoot2.0 整合 Shiro 框架,实现用户权限管理

    本文源码:GitHub·点这里 || GitEE·点这里 一.Shiro简介 1.基础概念 Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码和会话管理.作为一款安全 ...

  8. shiro框架学习-5-自定义Realm

    1. 自定义Realm基础 步骤: 创建一个类 ,继承AuthorizingRealm->AuthenticatingRealm->CachingRealm->Realm 重写授权方 ...

  9. (十二)整合 Shiro 框架,实现用户权限管理

    整合 Shiro 框架,实现用户权限管理 1.Shiro简介 1.1 基础概念 1.2 核心角色 1.3 核心理念 2.SpringBoot整合Shiro 2.1 核心依赖 2.2 Shiro核心配置 ...

随机推荐

  1. python 从2个文件中提取不相同的内容并输出到第三个文件中

    #-*- coding: UTF-8 -*- import re import sys import os   str1=[] str2=[] str_dump=[] fa=open("A. ...

  2. 19c PDB数据泵迁入

    1.问题描述 用数据泵进行pdb的迁入迁出,模拟测试将其他库的数据导入到19cpdb中 2.环境介绍 source:12.2.0.1.0 target:19.0.0.0.0 3.源端制造数据 创建表空 ...

  3. Python+Selenium学习笔记11 - python官网的tutorial - 定义函数

    1 def f(a, L=[]): 2 L.append(a) 3 return L 4 5 print f(5) 6 print f(2) 输出 1 def f(a, L=None): 2 if L ...

  4. 混合前端seq2seq模型部署

    混合前端seq2seq模型部署 本文介绍,如何将seq2seq模型转换为PyTorch可用的前端混合Torch脚本.要转换的模型来自于聊天机器人教程Chatbot tutorial. 1.混合前端 在 ...

  5. 语义分割:基于openCV和深度学习(二)

    语义分割:基于openCV和深度学习(二) Semantic segmentation in images with OpenCV 开始吧-打开segment.py归档并插入以下代码: Semanti ...

  6. Netty 框架学习 —— ByteBuf

    概述 网络数据的基本单位总是字节,Java NIO 提供了 ByteBuffer 作为它的字节容器,但这个类的使用过于复杂.Netty 的 ByteBuf 具有卓越的功能性和灵活性,可以作为 Byte ...

  7. C#基础之checked与 unchecked的使用

    C#基础之checked与 unchecked的使用 以上都是C#中的两个关键字的使用.据官网给出的相关介绍是:C# 语句既可以在已检查的上下文中执行,也可以在未检查的上下文中执行. 在已检查的上下文 ...

  8. 并发王者课-铂金1:探本溯源-为何说Lock接口是Java中锁的基础

    欢迎来到<并发王者课>,本文是该系列文章中的第14篇. 在黄金系列中,我们介绍了并发中一些问题,比如死锁.活锁.线程饥饿等问题.在并发编程中,这些问题无疑都是需要解决的.所以,在铂金系列文 ...

  9. python做。大神空闲时间能帮忙弄一串代码嘛?猜拳游戏,分很多种手的背面和正面,最后剩下的再石头剪刀布

    .每天课程结束后,老师会选择一列的同学清扫教室,人数不定(建议根据当时情况输入),在收拾完教室后,最后一步是需要从这一列的同学中选择1-2人去倒垃圾桶,垃圾桶数量根据当时情况决定,目前采取的方式是, ...

  10. yum安装时提示“尚未安装任何 GPG 公钥,请下载您希望安装的软件签名公钥并安装”

    在Linux操作系统中,安装软件依赖包时,出现了尚未安装任何 GPG 公钥,要求使用rpm --import public.gpg.key导入  问题: [root@server7 yum.repos ...