我对shiro的初步认识
package com.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.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; /**
* @since 0.9 RC2
*/
public class HelloShiro { private static final transient Logger log = LoggerFactory.getLogger(HelloShiro.class); public static void main(String[] args) { //创建一个最简单的方法与配置Shiro SecurityManager
//服务器、用户、角色和权限是使用简单的INI配置。
//我们会做,通过使用一个工厂能摄取。ini文件和
//返回一个SecurityManager实例: //使用shiro。ini文件在类路径的根
//(文件:和url前缀负载分别从文件和url):
//1根据配置文件获取一个shiro的的工厂类
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
//2在工厂类中获取shiro的安全管理器
SecurityManager securityManager = factory.getInstance();
//3把securityManager放入SecurityUtils中
SecurityUtils.setSecurityManager(securityManager);
//4使用SecurityUtils获取当前登录的用户
Subject currentUser = SecurityUtils.getSubject();
//5获取当前用户的会话状态 shiro内置的session
Session session = currentUser.getSession(); //可自行放入一些属性到session里,然后根据自己的业务逻辑进行处理
session.setAttribute("someKey", "aValue");
String value = (String) session.getAttribute("someKey");
if (value.equals("aValue")) {
log.info("Retrieved the correct value! [" + value + "]");
}
//6判断当前用户是否已验证身份
if (!currentUser.isAuthenticated()) {
System.out.println("当前用户没有登录---->");
//7模拟创建一个登录令牌
UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
token.setRememberMe(true);
try {
//8进行登录操作
//登录流程
//currentUser.login(token);--->调用了securityManager.login(this, token)方法
//---securityManager的login方法首先调用AuthenticatingSecurityManager的authenticate(AuthenticationToken token)方法
//---然后AuthenticatingSecurityManager的authenticate方法调用了ModularRealmAuthenticator的doAuthenticate(AuthenticationToken authenticationToken)
//---然后ModularRealmAuthenticator的doAuthenticate方法获取配置文件的的realm,如果配置文件没有定义realm,则默认使用SimpleAccountRealm
//--,若配置文件配置了多个realm,则遍历这些realm,并一一调用realm里的getAuthenticationInfo方法来获取AuthenticationInfo(身份验证信息)验证当前登录用户
//---从以上流程说明,我们可以通过自定多个realm,然后覆盖getAuthenticationInfo方法,从而达到自定义验证身份的方法
currentUser.login(token);
//Principal因为是object类型,所以我们在实际业务中可以自己扩展这个类
System.out.println("用户已登录----> 用户名:"+currentUser.getPrincipal().toString());
} catch (UnknownAccountException uae) {
log.info("There is no user with username of " + token.getPrincipal());
} catch (IncorrectCredentialsException ice) {
log.info("Password for account " + token.getPrincipal() + " was incorrect!");
} catch (LockedAccountException lae) {
log.info("The account for username " + token.getPrincipal() + " is locked. " +
"Please contact your administrator to unlock it.");
}
catch (AuthenticationException ae) {
}
} //9判断当前登录用户的角色---
//从源代码可以看出,currentUser.hasRole("schwartz")---调用了securityManager.hasRole(getPrincipals(), roleIdentifier)的方法
//再然后调用了AuthorizingRealm的hasRole(PrincipalCollection principal, String roleIdentifier)方法,
//而AuthorizingRealm的hasRole方法会先使用getAvailableAuthorizationCache()在缓存里获取AuthorizationInfo,如果缓存里没有AuthorizationInfo
//则默认的子类SimpleAccountRealm里获取的doGetAuthorizationInfo的方法里AuthorizationInfo(授权信息),而子类SimpleAccountRealm会在 factory.getInstance();时就把用户和角色放入一个map中,
//也就是说我们可以通过自定义AuthorizingRealm然后覆盖doGetAuthorizationInfo方法来实现我们的自定义验证授权信息
if (currentUser.hasRole("schwartz")) {
log.info("May the Schwartz be with you!");
} else {
log.info("Hello, mere mortal.");
} //test a typed permission (not instance-level)
//10权限验证
//从源代码可以看出currentUser.isPermitted("lightsaber:weild")---调用了securityManager.isPermitted(getPrincipals(), permission),
//----然后调用了AuthorizingRealm的isPermitted(PrincipalCollection principals, String permission),该方法里获取默认的permissionResolver,就是权限字符的解析器
//---然后调用了AuthorizingRealm。getAuthorizationInfo(),首先在缓存里获取AuthorizationInfo,如果没有,再从子类的SimpleAccountRealm的 doGetAuthorizationInfo去获取AuthorizationInfo
//----也就是说,我们可以通过AuthorizingRealm类,然后实现doGetAuthenticationInfo方法,从而实现自己业务逻辑的权限验证
if (currentUser.isPermitted("lightsaber:weild")) {
log.info("You may use a lightsaber ring. Use it wisely.");
} else {
log.info("Sorry, lightsaber rings are for schwartz masters only.");
} //a (very powerful) Instance Level permission:
if (currentUser.isPermitted("winnebago:drive:eagle5")) {
log.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'. " +
"Here are the keys - have fun!");
} else {
log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
} //all done - log out!
currentUser.logout();
System.exit(0);
}
}
我对shiro的初步认识的更多相关文章
- 2521-Shiro系列-基本使用
在springboot中使用SHiro作为安全框架,非常简单,灵活性也比较高 在springboot中使用Shiro不用像官网教程那样,官网是将配置文件写在INI文件和XML配置中 springboo ...
- shiro权限控制(一):shiro介绍以及整合SSM框架
shiro安全框架是目前为止作为登录注册最常用的框架,因为它十分的强大简单,提供了认证.授权.加密和会话管理等功能 . shiro能做什么? 认证:验证用户的身份 授权:对用户执行访问控制:判断用户是 ...
- 记录心得-shiro框架demo示例
从懵懂到了解,再到熟悉,是一个进步的过程! 先撸代码,跑起来看效果,再做详细的介绍,开始干活! 1,先列出工程目录结构,自己需要创建对应层级的程序和相关配置文件. 2,导入maven依赖的jar包.打 ...
- 【shiro】(5)---基于Shiro的权限管理
基于Shiro的权限管理项目搭建 前面写了四篇有关权限的文章,算是这篇文章的铺垫了.这篇文章采用 开发环境 JDK1.8 Eclipse Mav ...
- Shiro+JWT+Spring Boot Restful简易教程
序言 我也是半路出家的人,如果大家有什么好的意见或批评,请务必issue下. 项目地址:https://github.com/Smith-Cruise/Spring-Boot-Shiro . 如果想要 ...
- 从零搭建java后台管理系统(一)框架初步搭建
框架搭建 一.初步设想,使用springboot,框架打算用到依赖 spring web,devTools,mysql,Aspect,Redis,Lombok,Freemark,Shiro,Rabbi ...
- Springboot + Vue + shiro 实现前后端分离、权限控制
本文总结自实习中对项目对重构.原先项目采用Springboot+freemarker模版,开发过程中觉得前端逻辑写的实在恶心,后端Controller层还必须返回Freemarker模版的ModelA ...
- Shiro的认证原理(Subject#login的背后故事)
登录操作一般都是我们触发的: Subject subject = SecurityUtils.getSubject(); AuthenticationToken authenticationToken ...
- shiro小记
今天主要看了Shiro的认证,授权功能初步了解了一下,其他的功能用的不多,之后再看. 先说一下Shiro的三个核心概念: 1.Subject: 代表当前正在执行操作的用户,但Subject代表的可以是 ...
随机推荐
- 第三百一十八节,Django框架,信号
第三百一十八节,Django框架,信号 Django中提供了“信号调度”,用于在框架执行操作时解耦.通俗来讲,就是一些动作发生的时候,信号允许特定的发送者去提醒一些接受者. 也就是当程序有指定动作时, ...
- free 和delete 把指针怎么啦?
别看 free 和 delete 的名字恶狠狠的(尤其是 delete),它们只是把指针所指的内存给 释放掉,但并没有把指针本身干掉. 发现指针 p 被 free 以后其地址仍然不变(非 NULL), ...
- (转)_declspec(dllexport)
先看代码:以下是在dev-c++里建立自已的dll时的dll.h里面的代码,这里面有一个:_declspec(dllexport) #ifndef _DLL_H_#define _DLL_H_//防重 ...
- 使用Ultra Librarian转换芯片的Altium Designer封装格式
第一步:找到对应芯片的CAD文件,以OPA350为例: http://www.ti.com/product/opa350 RE: 使用Ultra Librarian转换TI芯片的Altium De ...
- python PIL 库处理文件
通过PIL库提供的API接口可以很方便的处理图像,功能十分强大: 最近有一个替换png背景色的需求,替换背景色的同时又不能够覆盖原来的文字,之前利用perl 的CD 模块一直没能够正确处理,最终用PI ...
- xss过滤函数
XSS是一种经常出现在web应用中的计算机安全漏洞,它允许恶意web用户将代码植入到提供给其它用户使用的页面中. 比如这些代码包括HTML代码和客户端脚本. function remove_xss($ ...
- while循环中,break,continue,return的差别
break 结束循环,跳出循环体: continue 结束本次循环.进行下次循环: return 跳出循环体所在的方法,相当于跳出循环体.
- SharePoint 2010 自定义页面出现“项目可能已被其他用户删除或重命名”问题跟踪
异常详细信息: Microsoft.SharePoint.SPException: 位置 http://portal/Pages/ShowArticle.aspx?id=19&mylist=8 ...
- 学习 TList 类的实现[1]
最近整理了一些函数列表, 算是一个宏观的安排; 等以后再碰到一些函数时就可以放置的更有次序一些. 我对函数与类的理解是: 函数是一个功能模块, 类是一个更强大的功能模块; Delphi 已经提供了很多 ...
- jQuery弹出遮罩层效果完整示例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...