1、shiro Hello

shiro教程1(HelloWorld) shiro简介 Apache Shiro是一个强大且易用的Java安全框架,执行身份验证、授权、密码和会话管理。使用Shiro的易于理解的API,您可以快速、轻松地获得任何应用程序,从最小的移动应用程序到最大的网络和企业应用程序。 框架图说明 从外部查看shiro框架 >应用代码直接交互的对象是Subject,也就是说Shiro的对外API核心就是Subject api 说明 .. 阅读详情

一、程序架构

pom.xml:

<dependency>

  <groupId>org.apache.shiro</groupId>

  <artifactId>shiro-core</artifactId>

  <version>1.3.2</version>

</dependency>

<dependency>

  <groupId>org.apache.shiro</groupId>

  <artifactId>shiro-ehcache</artifactId>

  <version>1.3.2</version>

</dependency>

二、简单说明Shiro.ini配置文件

  2.1 内容

 [users]

root = secret, admin

guest = guest, guest

presidentskroob = 12345, president

darkhelmet = ludicrousspeed, darklord, schwartz

lonestarr = vespa, goodguy, schwartz

 

[roles]

admin = *

schwartz = lightsaber:*

goodguy = winnebago:drive:eagle5

 

2.2 ini 文件说明

[users]:用户名=密码,角色1,角色2

[roles]:角色=权限1,权限2

权限:

(1)用简单的字符串来表示一个权限。如:user

(2)多层次管理:如:user:query,user:edit,user:query,edit。第一部分为操作的领域,第二部分为执行的操作。可以使用通配符:user:*,*:query

(3)实例级权限:域:操作:实例

如:user:edit:manager 只能对 user 中的 manager 进行 edit。

通配符:user:edit:*、user:*:*、user:*:manager

等价:user:edit==user:edit:*、user == user:*:* 只能从字符串结尾处省略。

 

三、Quickstart.java

 packagecn.com.bochy.shiro;

import org.apache.shiro.SecurityUtils;

import org.apache.shiro.authc.*;

import org.apache.shiro.config.IniSecurityManagerFactory;

import org.apache.shiro.mgt.SecurityManager;

import org.apache.shiro.session.Session;

import org.apache.shiro.subject.Subject;

import org.apache.shiro.util.Factory;

public class Quickstart {

    public static void main(String[]args) {

        Factory<SecurityManager> factory= new IniSecurityManagerFactory("classpath:shiro.ini");

        SecurityManager securityManager =factory.getInstance();

        SecurityUtils.setSecurityManager(securityManager);

        Subject currentUser = SecurityUtils.getSubject();

        //用会话做一些事情

        Session session = currentUser.getSession();

        session.setAttribute("czf","123456");

        String value = (String)session.getAttribute("czf");

        //Retrieved:取回

        if (value.equals("123456")) {

           System.out.println("-->Retrieved the correct value! [" + value +"]");

        }

        // let's login the current user so we can check against roles andpermissions:

      //  System.out.println(currentUser.);

         if(!currentUser.isAuthenticated()) {//authentication:认证,身份

            UsernamePasswordToken token = newUsernamePasswordToken("lonestarr","vespa");

            token.setRememberMe(true);

            try {

                currentUser.login(token);

                System.out.println("-->User [" + currentUser.getPrincipal() +"] loggedin successfully.");

            } catch(UnknownAccountException uae) {

                //Principal:主要的,首长,负责人

                System.out.println("-->There is no user with username of " + token.getPrincipal());

            } catch (IncorrectCredentialsExceptionice) {

                System.out.println("-->Password for account " + token.getPrincipal() +" was incorrect!");

            } catch (LockedAccountExceptionlae) {

                System.out.println("The account for username " + token.getPrincipal() +" is locked.  " +

                        "Please contact your administrator to unlockit.");

            }

            // ... catch more exceptions here (maybe custom ones specific to yourapplication?

            catch (AuthenticationExceptionae) {

                //unexpected condition?  error?

            }

        }

        //test a role:

        if (currentUser.hasRole("schwartz")) {

            System.out.println("-->May the Schwartz be with you!");

        } else {

            System.out.println("Hello, mere mortal.");

        }

        //test a typed permission (not instance-level)

        //lightsaber: 激光剑;weild:行使

        if (currentUser.isPermitted("lightsaber:weild")) {

            System.out.println("-->You may use a lightsaber ring.  Use it wisely.");

        } else {

            //schwartz:施瓦兹

            System.out.println("Sorry, lightsaber rings are for schwartz mastersonly.");

        }

        //a (very powerful) Instance Level permission:

        //winnebago:温尼贝戈人;房车 eagle:鹰;plate:盘子

        if (currentUser.isPermitted("winnebago:drive:eagle5")) {

            System.out.println("-->You are permitted to 'drive' the winnebagowith license plate (id) 'eagle5'.  " +

                    "Here are the keys - have fun!");

        } else {

            System.out.println("Sorry, you aren't allowed to drive the 'eagle5'winnebago!");

        }

        //all done - log out!

        currentUser.logout();

        System.exit(0);

    }

}

四、执行结果

 -->Retrieved the correct value! [123456]

-->User [lonestarr] logged in successfully.

-->May the Schwartz be with you!

-->You may use a lightsaber ring.  Use it wisely.

-->You are permitted to 'drive' the winnebago withlicense plate (id) 'eagle5'.  Here arethe keys - have fun!

 五、总结

shiro
1、shiro是一个开源免费的Java安全框架,既可以使用在java的程序上,也可以使用在java web,还可以使用在移动端。
2、两大内容
   1、身份验证
   2、授权
3、使用shiro的依赖
 <dependency>
  <groupId>org.apache.shiro</groupId>
  <artifactId>shiro-core</artifactId>
  <version>1.3.2</version>
</dependency>
<dependency>
  <groupId>org.apache.shiro</groupId>
  <artifactId>shiro-ehcache</artifactId>
  <version>1.3.2</version>
</dependency>
4、使用shiro的一般步骤
 1、读取配置文件,获取安全管理者的工厂
       Factory<SecurityManager>   factory= new IniSecurityManagerFactory("classpath:shiro.ini");
     2、获取安全管理者实例  
       SecurityManager securityManager=factory.getInstance();    
          3、设置SecurityUtils的SecurityManager
       SecurityUtils.setSecurityManager(securityManager);
           4、获取当前Subject对象(user) 
     Subject currrentUser=  SecurityUtils.getSubject();
     5、身份验证
      UsernamePasswordToken token=new UsernamePasswordToken(用户名,密码);
         currrentUser.login(token);
     6、授权验证
     7、注销用户
  5、shiro如何判断某个用户拥有哪个角色?
       currrentUser.hasRole(角色名)
       currrentUser.hasAllRoles(多个角色)
  6、如何判断某个用户是否可以访问某个资源
      currentUser.isPermitted(资源名)

「Debug R」在从名字空间‘XVector’中出口方法时发现了不是S4通用的函数‘parallelSlotNames’... 每当R语言版本更新,Bioconductor的版本随之更替的时候,总会有一些神奇的报错会出现,比如说下面这种。 Error: package or namespace load failed for ‘GenomicRanges’: 在从名字空间‘XVector’中出口方法时发现了不是S4通用的函数‘parallelSlotNames’ 如果读中文读不懂,我们可以把它翻译成英文。 packa... 阅读详情

相关推荐

B站充电视频下载器(需配合会员Cookie使用,仅供学习交流,Python)

这个程序是一个用于下载B站充电视频的工具,依赖于用户提供的会员Cookies。

qq_40753021的博客 2万+

securityUtils

package com.mtf.vega.util; import java.lang.reflect.Field; import java.security.MessageDigest; import java.util.List; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; ...

花花的技术博客 5948

Tesla V100 Windows 部署实战:从驱动安装到模式切换的避坑指南

本文是一份详细的Tesla V100在Windows系统下的部署实战指南。文章从硬件组装、系统选择讲起,重点阐述了驱动安装的正确方法,并深入剖析了将计算卡从TCC模式切换至WDDM模式的诱惑与潜在陷阱。最终,作者推荐保持默认TCC模式,并提供了使用nvidia-smi命令行等稳定可靠的显卡监控方案,旨在帮助用户避开常见坑点,高效利用这张专业计算卡进行AI计算等任务。

java5的博客 1717

shiro初步学习

Shiro简介 Shiro架构原理 INI文件介绍 Shiro环境搭建及认证过程 第一个Shiro演示 授权 加密及凭证匹配器 自定义Realm 凭证匹配器 一、 Shiro 简介 1 概述 权限体系在现代软件应用中有着非常重要的地位。一个应用如果没有权限体系都会显着这个系统“特别不安全”,无论是传统的MIS系统还是互联网项目出于对业务数据和应用自身的安全,都会设置自己的安全策略。 目前市场上专门的Java权限框架有Apache Shiro 和 Spring Security。相较于Spring Se.

wyy的博客 964

SystemC实战:用C++搞芯片建模的5个典型场景(含代码示例)

本文通过五个典型实战场景,详细介绍了如何使用SystemC这一基于C++的硬件建模库进行芯片设计与系统级建模。内容涵盖从基础并发模块构建、事务级总线建模、跨时钟域验证,到与ModelSim协同仿真及多核系统性能探索,并提供了完整的代码示例,帮助软件工程师利用熟悉的C++工具链高效参与硬件架构设计与验证。

weixin_29191761的博客 316

CFD-POST中建立变量的方法

本文以测量文丘里管中全压为例阐述CFD_POST中建立变量的方式 点击进入CFD-POST界面的expressions栏中 2.与expression栏中空白处右键,new-输入新建的表达式名称 3. 在下方的空白栏中右键-varible选择需要的变量,并根据公式进行修改 4.点击进入varibles栏,空白处右击-new-输入变量名totalpressure,在下拉框中找到之前定义的totalpressure 完成后可以在varibles栏中看到新建的...

sidxi的博客 1万+

java系统安全工具类(SecurityUtils)

类的主要职责是提供与安全相关的常用功能,比如密码加密、校验、生成token、数据签名、权限验证等操作。该工具类能够帮助开发者快速实现安全功能,保证代码的复用性和一致性。// 私有构造方法,避免实例化// 核心安全功能:密码加密、解密、验证、Token生成等为了设计一个实用的加密与解密数据签名与验证Token生成与解析权限验证输入校验安全配置与常量类提供了多种常见的安全功能,包括加密解密、签名验证、权限控制、输入校验等。

Flying_Fish_roe的博客 2180

渗透测试之哥斯拉实战

可以直接在这里使用meterpreter命令进行对服务端的控制。选择目标 - 添加,配置连接信息,之后测试连接成功!php的shell功能如图。先要进行数据库的配置信息。可以直接在这里打包全站。

大河之犬的博客 2492

SecurityUtils 工具类笔记

SecurityUtils 工具类笔记 SecurityUtils 类 package com.cloud.wl.hp.config.utils; import com.cloud.wl.hp.base.common.mapper.JsonMapperUtil; import com.cloud.wl.hp.base.dto.RestResultDTO; import javax.servlet.http.HttpServletResponse; import java.io.IOException;

王林的博客 2571

30.Labview如何读懂别人程序、修改程序?

之前写过一次教大家的文章,这次教大家如何学习别人的程序,和如何搭建一套适合自己的程序框架。这里事先说明一下:要想搭好程序框架,一定要先把自己的系统,逻辑是什么!这非常重要,是决定你程序的关键因素。废话不多扯,直接说重点。

关注我,更多技术问题欢迎私聊~ 2448

优快云独家 | 全网首发 | 《目标检测蓝皮书》目录

10篇文章从目标检测小白变大神

Coding... 3万+

Shiro学习笔记<2>SecurityUtils,SecurityManager,Subject

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_34113237的博客 697

实干派!网易易盾增强版滑块验证码,全方位讲解识别思路(含源码)

该验证码是异形滑块验证码中的一种,缺口旋转+偏移的处理需要你有很好的图像处理知识,能很好的处理透明通道,同时,该验证码需要你能很好的处理缺口和背景之间匹配的关系,该验证码的处理难点就是两个缺口之间如何找到正确的,两个检测框都有很强的相似度,且加上背景图的干扰,在图像处理和有优化上难度大大增加。

码王吴彦祖的博客 2016

Shiro学习笔记1 —— Hello World

1、创建一个Maven工程加载Shiro的jar包 1 <!-- junit --> 2 <dependency> 3 <groupId>junit</groupId> 4 <artifactId>junit</arti...

131

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

Shiro学习笔记<1>入门--Hello Shiro

Apache Shiro是Apache的一个安全框架.对比Spring Security,可能没有Spring Security功能多,但是在实际并不需要那么重的东西.shiro简小精悍.大多项目绰绰有余.(JBOSS好像也有个什么安全框架…名字忘了,去JBOSS官网找了半天也没找到,找到个jboss sso好像是单点登录方面使用的安全框架)  Shiro主要功能有认证,授权,加密,会话管理,

wblearn的博客 1050

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

Shiro 学习笔记(1)—— Hello World

Shiro 学习笔记(1)—— Hello WorldShiro 学习笔记1 Hello World 什么是 Shiro 两个重要的概念认证授权 另外两个重要的概念角色权限 测试代码的思路什么是 Shiro?百度百科上对 Shiro 的概括是非常准确的: Apache Shiro是一个强大且易用的 Java 安全框架,执行身份验证、授权、密码学和会话管理。 在这里,我就直接拿来使用了。Shiro

李威威的博客 3354

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

shiro学习之路(1)---初识shiro(Hello Word)

1.1  简介 Apache Shiro是Java的一个安全框架。目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring Security,可能没有Spring Security做的功能强大,但是在实际工作时可能并不需要那么复杂的东西,所以使用小而简单的Shiro就足够了。对于它俩到底哪个好,这个不必纠结,能更简单的解决项目问题就好了。 本教程只介绍基本的Shiro使

莫失莫忘的博客 389

shiro的hello world

shiro 安全框架小型而且来说很好用,想比较spring的安全security来讲要简单很多,当然 看开涛老师的博客也能学到很多,当然看海涛老师的博客,个人觉得还是比较乱,当然当你熟悉了shiro在查看开涛老师的shiro进行手册似的查看我觉得非常nice,这里我是参照官方文档搭建的第一个demo,万事开头难,第一hello world运行起来 本项目以Maven进行构建 pom.xm

mexican_jacky的专栏 538

shiro环境搭建——shiro hello world!

 今天记录一下啊shiro的搭建和使用,显示一个hello shiro  1、shiro的作用     shiro是做什么的,我在这里就不说了,网上有答案,如果你还不了解shiro,建议你上网找一下shiro的介绍。 2、环境的介绍     本次实验采用简单的ssm框架, spring 4.3.7     jdk 1.8     tomcat9     shiro 1.3.2     ma...

qq_39069111的博客 1092

二.Shiro的Hello World【Shiro系列教程】

每一个伟大的项目都是从Hellow World开始的, 让我们来看看Shiro的Hello World吧。 在maven中添加相关依赖 <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.4.1</versi.

qq_28248897的博客 270

hello shiro

官网:shiro 先看看从官网偷来的图 shiro的三个主要组件:Subject、SecurityManager、Realm Subject:我们可以将它看作是当前用户或第三方服务, SecurityManager:管理所有的Subject,是Shiro的核心组件 Realm:获取用户认证的数据 接下来通过代码来认识下这几个组件 添加maven依赖 &amp;amp;amp;lt;dependency...

不务正业的野猴子 282

Shiro0-hello

文章目录shiro简介框架说明内部的框架常见的单词Shiro中的shiro.ini说明mainusersrolesurls案例添加依赖添加shiro.ini文件认证操作 shiro简介 官网 Apache Shiro是一个强大且易用的Java安全框架,执行身份验证、授权、密码和会话管理。使用Shiro的易于理解的API,您可以快速、轻松地获得任何应用程序,从最小的移动应用程序到最大的网络和企业应...

十月 340

【Shiro 自学笔记一】Shiro 的基本流程与 Hello World

文章目录什么是 ShiroShiro 核心组件SubjectSecurityManagerRealmsHello World配置pom.xmlShiro 配置文件Main 方法 Apache Shiro是一个强大且易用的Java安全框架,执行身份验证、授权、密码和会话管理。使用Shiro的易于理解的API,您可以快速、轻松地获得任何应用程序,从最小的移动应用程序到最大的网络和企业应用程序。 —— 摘自百度百科 什么是 Shiro Shiro 是 Apache 公司开发的一款安全框架。它支持 Jav

Koorye今天自学了吗 354

Shiro简介与Hello World实现

一、Shiro简介 Apache Shiro是一个强大易用的Java安全框架,提供了认证、授权、加密和会话管理功能,可为任何应用提供安全保障 - 从命令行应用、移动应用到大型网络及企业应用。 Shiro为解决下列问题提供了保护应用的API: 1、认证 - 用户身份识别,常被称为用户“登录”; 2、授权 - 访问控制; 3、密码加密 - 保护或隐藏数据防止被偷窥; 4、会话管理 - 每用

StevenXu的专栏 501

Shiro-01-hello

文章目录Shiro-01-hellopom.xmlshiro.iniShiroTest2.java Shiro-01-hello pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocatio...

qq_32640013的博客 139

Shiro 入门

Apache Shiro是一个功能强大且易于使用的Java安全框架,提供身份认证、授权、加密和会话管理等功能。本文详细介绍了Shiro的核心架构(包括Subject、SecurityManager、Realm等组件)、认证授权流程、MD5与盐值加密、SpringBoot整合、缓存机制(EhCache和Redis)以及权限控制实现。通过实战示例演示了用户注册登录、基于角色和资源的权限控制、Freemarker整合等场景,帮助开发者快速掌握Shiro在企业级应用中的安全防护技术。文章还涵盖了验证码集成、自定义R

weixin_51689532的博客 814
上一篇: Maven+ Redis+Spring开发实例
下一篇: 2、Shiro基础
fullStackChenZF
博客等级 码龄10年 44粉丝 70原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值