当前位置: 首页 > 编程日记 > 正文

shiro(2)-架构与配置

认证就是用户确认身份的过程,确认登录的用户身份能够操作的内容。

使用shiro认证分为以下几个步骤:

1,得到主体的认证和凭据。

// let's login the current user so we can check against roles and permissions:if (!currentUser.isAuthenticated()) {UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");token.setRememberMe(true);

2,提交认证和凭据给身份验证系统。

Subject currentUser = SecurityUtils.getSubject();
currentUser.login(token);

3,判断是否允许访问,重试认证或者阻止访问。

try {currentUser.login(token);} 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 more exceptions here (maybe custom ones specific to your application?catch (AuthenticationException ae) {//unexpected condition?  error?}

其中Remember Me的功能包括两个方法,一个是

isRemembered

boolean isRemembered()
非匿名登录的用户可以记住上次使用的主题的信息。

isAuthenticated

boolean isAuthenticated()
在此期间需要使用有效的凭据登录系统,否则值为false.
 
授权操作
授权的例子就是是否可以访问某个页面,可以操作某个按钮,是否可以编缉对应的数据等。
如何在shiro中使用授权
1,使用编程方式
判断是否有管理员角色
if (currentUser.hasRole("admin")) {
判断用户是否有打印的权限
Permission printPermission = new PrinterPermission(“laserjet3000n”,“print”);
If (currentUser.isPermitted(printPermission)) {//do one thing (show the print button?)‏
} else {//don’t show the button?
}

也可以使用字符串的方式验证

String perm = “printer:print:laserjet4400n”;if(currentUser.isPermitted(perm)){//show the print button?
} else {//don’t show the button?
}

2,使用注释方式
 判断用户是否有 创建账户权限
//Will throw an AuthorizationException if none
//of the caller’s roles imply the Account 
//'create' permission\u000B
@RequiresPermissions(“account:create”)‏
public void openAccount( Account acct ) { //create the account
}
判断用户角色,如果符合角色,可以使用对应方法
//Throws an AuthorizationException if the caller
//doesn’t have the ‘teller’ role:@RequiresRoles( “teller” )
public void openAccount( Account acct ) { //do something in here that only a teller//should do
}
3,使用jsp taglib
 判断用户是否有管理权限
<%@ taglib prefix=“shiro” uri=http://shiro.apache.org/tags %>
<html>
<body><shiro:hasPermission name=“users:manage”><a href=“manageUsers.jsp”>Click here to manage users</a></shiro:hasPermission><shiro:lacksPermission name=“users:manage”>No user management for you!</shiro:lacksPermission>
</body>
</html>
 
从高的级别来看shiro:
看一下官方的图

应用程序调用subject(主题),主题可以是一个用户也可以是与系统交互的另一个系统,主题绑定shiro的权限管理,SecurityManager(安全管理),它控制与有与主题相关的安全操作。Realm(桥梁)它是安全与数据之间的桥,它封装了比如DAO的配置信息,可以指定连接的数据源,也可使用其它的认证方式,如LDAP等。

然后看一下详细的架构图:

Subject (org.apache.shiro.subject.Subject)

主题:与系统交互的第三方如(用户,cron服务,第三方应用)等。

SecurityManager (org.apache.shiro.mgt.SecurityManager)

shiro系统的核心,协调主题使用的操作,验证,配置等。

Authenticator (org.apache.shiro.authc.Authenticator)

身份验证组件,对企图登录系统的用户进行身份的验证。其中包含一个Authentication Strategy

(org.apache.shiro.authc.pam.AuthenticationStrategy)组件。配置验证成功与失败的条件。

Authorizer (org.apache.shiro.authz.Authorizer)

授权组件,指用户访问特定应用程序的机制。

SessionManager (org.apache.shiro.session.mgt.SessionManager)

管理会话如何创建生命周期。其中包括的sessiondao是管理会议数据的持久操作:SessionDAO (org.apache.shiro.session.mgt.eis.SessionDAO),代表执行sessionManager的CRUD操作。

CacheManager (org.apache.shiro.cache.CacheManager)

缓存管理模块。

Cryptography (org.apache.shiro.crypto.*)

加密模块。

Realms (org.apache.shiro.realm.Realm)

多种方式处理的桥梁。

多种配置方式:

与spring,jboss,guice等进行配置。

1,编程方式配置

例如:

Realm realm = //instantiate or acquire a Realm instance.  We'll discuss Realms later.SecurityManager securityManager = new DefaultSecurityManager(realm);
//Make the SecurityManager instance available to the entire application via static memory:
SecurityUtils.setSecurityManager(securityManager);

2,sessionManager对象图

如果你想使用sessionManager配置自定义的sessionDao信息,进行自定义会话管理

...DefaultSecurityManager securityManager = new DefaultSecurityManager(realm);
SessionDAO sessionDAO = new CustomSessionDAO();((DefaultSessionManager)securityManager.getSessionManager()).setSessionDAO(sessionDAO);
...

3,INI配置

1) 创建一个INI从SecurityManager

可以从多种方式读取INI配置文件的信息,如文件系统,类路径等

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.util.Factory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.config.IniSecurityManagerFactory;
...Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);

2) 通过Ini实例读取

类似于Properties的方式

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.util.Factory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.config.Ini;
import org.apache.shiro.config.IniSecurityManagerFactory;
...Ini ini = new Ini();
//populate the Ini instance as necessary
...
Factory<SecurityManager> factory = new IniSecurityManagerFactory(ini);
SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);

加载之后就可以操作INI的配置了。

4,INI配置

每一个节点都是单独的,不可以重复,注释可以使用#或者;

配置示例

# =======================
# Shiro INI configuration
# =======================
[main]
# Objects and their properties are defined here, 
# Such as the securityManager, Realms and anything
# else needed to build the SecurityManager[users]
# The 'users' section is for simple deployments
# when you only need a small number of statically-defined 
# set of User accounts.[roles]
# The 'roles' section is for simple deployments
# when you only need a small number of statically-defined
# roles.[urls]
# The 'urls' section is used for url-based security
# in web applications.  We'll discuss this section in the
# Web documentation

1) [main]

配置sessionManager的实例和它的依赖。

配置示例

[main]
sha256Matcher = org.apache.shiro.authc.credential.Sha256CredentialsMatchermyRealm = com.company.security.shiro.DatabaseRealm
myRealm.connectionTimeout = 30000
myRealm.username = jsmith
myRealm.password = secret
myRealm.credentialsMatcher = $sha256Matcher
securityManager.sessionManager.globalSessionTimeout = 1800000

定义一个对象

[main]
myRealm = com.company.shiro.realm.MyRealm
...

简单的属性设置

...
myRealm.connectionTimeout = 30000
myRealm.username = jsmith
...

配置信息将转入到对应的set方法中

...
myRealm.setConnectionTimeout(30000);
myRealm.setUsername("jsmith");
...

参考值

你可以使用$符号引用先前定义的一个对象的实例

...
sha256Matcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher
...
myRealm.credentialsMatcher = $sha256Matcher
...

嵌套属性

...
securityManager.sessionManager.globalSessionTimeout = 1800000
...

将被注入到下面的程序中

securityManager.getSessionManager().setGlobalSessionTimeout(1800000);

引用其它的属性

sessionListener1 = com.company.my.SessionListenerImplementation
...
sessionListener2 = com.company.my.other.SessionListenerImplementation
...
securityManager.sessionManager.sessionListeners = $sessionListener1, $sessionListener2

以键值的配置方式

object1 = com.company.some.Class
object2 = com.company.another.Class
...
anObject = some.class.with.a.Map.propertyanObject.mapProperty = key1:$object1, key2:$object2

2) [users]

在用户比较少的情况下这种配置信息是有效的

[users]
admin = secret
lonestarr = vespa, goodguy, schwartz
darkhelmet = ludicrousspeed, badguy, schwartz

3) [roles]

如果角色信息比较少的情况下可以使用这项配置

[roles]
# 'admin' role has all permissions, indicated by the wildcard '*'
admin = *
# The 'schwartz' role can do anything (*) with any lightsaber:
schwartz = lightsaber:*
# The 'goodguy' role is allowed to 'drive' (action) the winnebago (type) with
# license plate 'eagle5' (instance specific id)
goodguy = winnebago:drive:eagle5

4) [urls]

配置url等可访问的资源信息。


相关文章:

unity中的UI状态机,用于各界面之间的切换和跳转

首先感谢姜雪松先生&#xff0c;大家可以去他的博客查看注释以及代码等&#xff0c;http://jxwgame.blog.51cto.com/943299/1613585 言归正传&#xff1a; 1.在开发项目的过程中&#xff0c;总是会遇到这样的问题&#xff0c;从一个界面跳转到另外一个界面&#xff0c;每次操作…

【转载】Session服务器配置指南与使用经验

作者&#xff1a;张子秋出处&#xff1a;http://www.cnblogs.com/zhangziqiu/ 原文链接&#xff1a;http://www.cnblogs.com/zhangziqiu/archive/2009/03/26/sessionserver.htm一、摘要所有Web程序都会使用Session保存数据. 使用独立的Session服务器可以解决负载均衡场景中的Se…

如何使用jdbc连接数据库

如何使用jdbc连接数据库 数据库是一个有组织的数据集合。数据库管理系统以一种与数据库格式一致的方式&#xff0c;提供了存储和组织数据的机制。数据库管理系统允许在不考虑内部数据表示的情况下访问和存储数据。 java程序使用JDBC API与数据库通信&#xff0c;并用它操纵数…

Mysql配置查询

查看mysql数据库的线程数&#xff1a; show global status like Thread%; 如果我们在MySQL服务器配置文件中设置了thread_cache_size&#xff0c;当客户端断开之后&#xff0c;服务器处理此客户的线程将会缓存起来以响应下一个客户而不是销毁(前提是缓存数未达上限)。Threads_c…

自我暗示的三大规律

伟人之所以伟大&#xff0c;是因为别人放弃时&#xff0c;他却在坚持。理解暗示的三大规律后&#xff0c;你就会清楚地知道&#xff0c;为什么在别人放弃的时候&#xff0c;你仍然要坚持。 自我暗示第一规律&#xff1a;重复 经常重复一种思想会产生信念&#xff0c;进而变得坚…

python爬虫入门urllib库的使用

urllib库的使用&#xff0c;非常简单。 import urllib2response urllib2.urlopen("http://www.baidu.com") print response.read() 只要几句代码就可以把一个网站的源代码下载下来。 官方文档&#xff1a;https://docs.python.org/2/library/urllib2.html urllib2.u…

maven如何在eclipse上加载

maven如何在eclipse上安装和使用 maven是Apache旗下的定级开源工具,在项目管理方面有强大的能力。Maven 除了以程序构建能力为特色之外&#xff0c;还提供高级项目管理工具。由于 Maven 的缺省构建规则有较高的可重用性&#xff0c;所以常常用两三行 Maven 构建脚本就可以构建…

初步判断内存泄漏方法

有时候&#xff0c;内存泄漏不明显&#xff0c;或者怀疑系统有内存泄漏&#xff0c;我们可以通过下面介绍的方法初步确认系统是否存在内存泄漏。首先在Java命令行中增加-verbose:gc参数&#xff0c;然后重新启动java进程。当系统运行过程中,JVM进行垃圾回收的时候&#xff0c;会…

com组件和一般dll的区别

这阵子在想一个需要利用com组件的小程序怎么做&#xff0c;突然想起上次去面试的时候考官问过autocad开发时为什么要利用com&#xff0c;而不采用一般的dll呢&#xff1f; 到google上查了一下&#xff0c;许多人也问了一样的问题&#xff1a;&#xff09; 用com来写程序…

WinCE6.0 修改开机Logo方法集锦(二)

中秋假期已过&#xff0c;回来继续该博文主题。今天讲解第二种方法&#xff0c;将Logo图片的数据写入到Nand Flash中&#xff0c;在启动初始化LCD的时候&#xff0c;从固定的地址将数据读出并填充到显示缓存中。<?xml:namespace prefix o ns "urn:schemas-microsoft…

[动态dp]线段树维护转移矩阵

背景&#xff1a;czy上课讲了新知识&#xff0c;从未见到过&#xff0c;总结一下。 所谓动态dp&#xff0c;是在动态规划的基础上&#xff0c;需要维护一些修改操作的算法。 这类题目分为如下三个步骤&#xff1a;&#xff08;都是对于常系数齐次递推问题&#xff09; 1先不考虑…

原始ajax方式调用asp.net后台方法

aspx页面&#xff1a; <% Page Language"C#" AutoEventWireup"true" CodeFile"Data.aspx.cs" Inherits"Data" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xht…

洛谷P4480 【[BJWC2018]餐巾计划问题】

这道题和网络流 \(24\) 题中的餐巾计划的确不一样, \([\) \(BJWC\) \(2018\) \(]\) 餐巾计划问题的数据范围更大。 一个餐厅在相继的 \(n\) 天里&#xff0c;每天需用的餐巾数不尽相同。假设第 \(i\) 天 \((\) \(i\) \(\) \(1\) \(,\) \(2\) \(,\) \(...\) \(,\) \(n\) \()\)需…

灵活使用java反射简化servlet

在我们初学jsp的时候&#xff0c;我们通常将java代码放到jsp页面

第四篇 Gallery控件

直奔主题~&#xff01; 结构如图&#xff1a; main.xml代码: <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas.android.com/apk/res/android"android:orientation"vertical" android:la…

macaca之app-inspector

简单介绍 之前已经将macaca的环境搭建好了&#xff0c;现在就需要进行元素的定位&#xff0c;这里使用app-inspector&#xff0c;然后进行自动化脚本的编写。 实际操作 一、安装app-inspector npm i app-inspector -g 安装成功确保如下命令中有手机或模拟器的连接&#xff0c;可…

visual-reasoning 笔记

目录 整理最近学习 visual-reasoning的笔记 1. 关注 ACL、EMNLP、NAACLI等会议文章 未开始 2. Cyc项目 2.1 cyc知识库介绍&#xff1a; ​ 该知识库包含了320w条人类断言&#xff0c;30w概念&#xff0c;15000谓词。 ​ Cyc知识库中表示的知识一般形如“每棵树都是植物”、“植…

使用beanutil简化request值的接收

在刚开始学习java web的时候&#xff0c;我们想要接收从其他页面传过来的值常使用以下的语句 request.setCharacterEncoding("UTF-8");String Kind1 request.getParameter("foodKind");String Code1 request.getParameter("foodCode");String…

命令行编译运行CSharp文件

命令行编译运行CSharp文件 找到csc.exe所在的路径。如我本机上为“C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727”在环境变量里增加变量CSC_HOME&#xff0c;值为以上路径。在PATH变量的值中加入%CSC_HOME%在cmd中进入要编译的cs文件所在的文件夹输入命令csc 文件名&#xf…

大话IT职场之工作和生活的平衡

每一个职场人都有自己的规划&#xff0c;特别是IT人员&#xff0c;基本都在想我要几个月掌握这门技术或语言&#xff0c;我要多久能带团队&#xff0c;我多长时间可以做到管理岗位&#xff0c;技术经理、技术总监等&#xff0c;每个人基本都充斥着这样的想法。但是否同时也在考…

安装和使用git遇到的问题总结

一&#xff0c;centos7下安装(因为centos7下用yum安装git的版本太低了&#xff0c;所以只能下载源代码&#xff0c;然后用源代码安装&#xff09; 下载编译工具 yum -y groupinstall "Development Tools" 下载依赖包 yum -y install zlib-devel perl-ExtUtils-MakeM…

Linux系统文本命令快速登录与退出

Linux是一个多用户的操作系统&#xff0c;用户要使用该系统&#xff0c;首先必须登录系统&#xff0c;使用完系统后&#xff0c;必须退出系统。用户登录系统时&#xff0c;为了使系统能够识别自己&#xff0c;必须输入用户名和密码&#xff0c;经系统验证无误后方能进入系统。在…

调试 后台 ajax post 对应的php的方法

在对应的javascript中 $.post("<?php echo ROOTURL ?>/Service/SetPlayerStartCord.php", "IP192.168.0.32&startCord_X400&startCord_Y30", function(data){!!!alert("Data Loaded: " data); }转载于:https://www.cnblogs.com…

log4j在eclipse上使用简介

Log4j是Apache的一个开源项目&#xff0c;通过使用Log4j&#xff0c;我们可以控制日志信息输送的目的地是控制台、文件、GUI组件&#xff0c;甚至是套接口服务器、NT的事件记录器、UNIX Syslog守护进程等&#xff1b;我们也可以控制每一条日志的输出格式&#xff1b;通过定义每…

关于编程的浅学习与深学习

导读&#xff1a;Tanky Woo的程序人生在博客中发表了《关于编程的浅学习与深学习》&#xff0c;文章是关于编程学习的一个提议、归纳、总结。以下是文章全部内容&#xff1a;关于编程的学习&#xff0c;大家肯定都知道&#xff0c;也是大家都说来说去的&#xff0c;就几句话&am…

shiro实战系列(一)之入门实战

一、什么是shiro? Apache Shiro 是一个强大而灵活的开源安全框架&#xff0c;它干净利落地处理身份认证&#xff0c;授权&#xff0c;企业会话管理和加密。 Apache Shiro 的首要目标是易于使用和理解。安全有时候是很复杂的&#xff0c;甚至是痛苦的&#xff0c;但它没有必要…

数据源和连接池

JDBC数据源&#xff1a; Data Source JDBC中提供了javax.sql.DataSource接口&#xff0c;负责建立与数据库的连接 DataSource对象可以由Web服务器提供&#xff0c;前提是需要在服务器配置DataSource&#xff08;包括连接池&#xff09; 连接池&#xff1a;Connection Pool…

FastReport.net 使用 Winform WebForm打印

delphi用的fastreport比较多 所以。net中也研究一下用法,这个打印控件还是很简单的 只要手动设计一下写少许代码就可以打印了 甚至可以写成通用代码 以后就可以不用写代码 安装demo会同时安一个设计器 打开设计器 通过设计器设计模板 新建数据源 新建数据集 查询单表全部内容&…

Ubuntu 12.04安装Sun JDK 6

Ubuntu 12.04安装Sun JDK 6 下载 sun jdk 6 bin. 设置权限 chmod x jdk-6u25-linux-i586.bin 解压文件 ./jdk-6u25-linux-i586.bin 移动位置到 sudo mv jdk1.6.0_25 /usr/lib/jvm/ 设置系统环境 sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.6.…

如果你的云服务商倒闭该怎么办?

如果你的云服务商倒闭或暂时中断服务&#xff0c;以下4个步骤能够帮助你的企业把损失减少到最低。 2009年2月&#xff0c;云服务商Coghead在一封写给客户的电子邮件中宣布该公司"由于受到经济挑战的影响"&#xff0c;将立即终止基于云的开发平台服务。随后&#xff0…