一、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. shell应用之习题一

    1 #!/bin/bash 2 #.写一个脚本/root/bin/argsnum.sh,接受一个文件路径作 为参数:如果参数个数小于1,则提示用户"至少应该给一个 参数",并立即退 ...

  2. addrinfo结构体原型-(转自 cxz2009)

    addrinfo结构体原型 typedef struct addrinfo {    int ai_flags;        //AI_PASSIVE,AI_CANONNAME,AI_NUMERIC ...

  3. docker中ubuntu源更新慢加速 换为国内源 Debian10源

    本来以为是Ubuntu打包的镜像,换了阿里源老是报错100公钥不可用,结果发现是Debian的操作系统,换位Debian的操作系统打包的,换位Debian的源即可 #源如果使用错误也会报错,没有Deb ...

  4. 第11讲 | TCP协议(上):因性恶而复杂,先恶后善反轻松

    第11讲 | TCP协议(上):因性恶而复杂,先恶后善反轻松 TCP 包头格式 我们先来看 TCP 头的格式.从这个图上可以看出,它比 UDP 复杂得多. 首先,源端口号和目标端口号是不可少的,这一点 ...

  5. 使用Python操作InfluxDB时序数据库

    使用Python操作InfluxDB时序数据库 安装python包 influxdb,这里我安装的是5.3.0版本 pip install influxdb==5.3.0   使用 from infl ...

  6. GO语言常用标准库02---os包

    package main import ( "fmt" "os" ) func main() { //获得当前工作路径(当前工程根目录) dir, err := ...

  7. Selenium click不生效 报错selenium.common.exceptions.InvalidArgumentException

    记录在使用selenium过程中踩的坑------ 在使用selenium时,用click点击网站弹出的文件上传框的"上传文件"按钮不生效,报错selenium.common.ex ...

  8. Count(1),Count(*),Count(column)区别

    count是一种最简单的聚合函数,一般也是我们第一个开始学习的聚合函数,那么他们之间究竟由什么区别呢? 有的人说count(1)和count(*)他们之间有区别,而有的人说他们之间没有区别那么他们之间 ...

  9. AI推理单元

    AI推理单元 推理服务供了一套面向 MLU(Machine Learning Unit,机器学习单元)设备的类似服务器的推理接口(C++11标准),以及模型加载与管理,推理任务调度等功能,极大地简化了 ...

  10. MPC算法

    MPC算法 一.    引言 在工程技术方面,MPC全称可指Model Predictive Control模型预测控制(又称RHC, Receding Horizon  ). 模型预测控制算法 一种 ...