在操作系统原理中,最基础的一个理论点就是信号量和PV操作。那么什么是信号量和PV操作呢?简单来说就是操作系统处理并发进程资源竞争的一种方法。
详情请查看
信号量我们可以简单理解为一个整型变量。P和V操作我们可以理解为两个函数。我们用伪代码实现如下:
//信号量s
int s;
function P(s){
s=s-1;
if(s<0){
//阻塞当前进程的执行
}
}
function V(s){
s=s+1;
if(s<=0){
//唤醒在s上阻塞的进程
}
}
以上就是信号量和PV操作声明。你可能以为这是在开玩笑,这就是信号量和PV操作?真的就是这样。下面我们来深入了解信号量和PV操作。
我们知道进程是由程序和数据组成的。简单说进程就是操作系统执行中的程序代码。一段程序代码可以同时由多个进程在多用户的操作系统中执行,这就是并发执行程序,或者可以理解为并发进程。
我们来看一个例子。我们有如下代码的一个程序A:
function A(){
getSomeSystemResource();
}
A();
这个程序主要实现了一个函数A
这个函数中调用了一些系统资源。然而这些系统资源仅允许同时只有1个进程来访问。我们同时启动10个A程序,就会产生$A_0$
、$A_1$
、$A_2$
、$A_3$
、$A_4$
、$A_5$
、$A_6$
、$A_7$
、$A_8$
、$A_9$$
这样10个进程。这10个进程同执行到A函数时,由于资源是独占的所以程序执行会出错。
我们在程序A
之外声明一个全局变量S
:
int S=1;
然后修改我们的程序:
P(S);
A();
V(S);
这样,我们就可以同时运行10个A程序,并且他们可以合理的调用系统资源。我们把程序从函数中拿出来,再来看一下PV操作和信号量S是如何工作的。
//P操作
S=S-1;
if(S<0){
//阻塞当前进程的执行
}
/**临界区**/
A();
/**临界区**/
//V操作
S=S+1;
if(S<=0){
//唤醒一个阻塞在S上的进程
}
我们可
更好的阅读体验,请访问gitbook
Zookeeper是Hadoop分布式调度服务,用来构建分布式应用系统。构建一个分布式应用是一个很复杂的事情,主要的原因是我们需要合理有效的处理分布式集群中的部分失败的问题。例如,集群中的节点在相互通信时,A节点向B节点发送消息。A节点如果想知道消息是否发送成功,只能由B节点告诉A节点。那么如果B节点关机或者由于其他的原因脱离集群网络,问题就出现了。A节点不断的向B发送消息,并且无法获得B的响应。B也没有办法通知A节点已经离线或者关机。集群中其他的节点完全不知道B发生了什么情况,还在不断的向B发送消息。这时,你的整个集群就发生了部分失败的故障。
Zookeeper不能让部分失败的问题彻底消失,但是它提供了一些工具能够让你的分布式应用安全合理的处理部分失败的问题。
我们在这里将对一个集成了Spring MVC+Hibernate+Apache Shiro的项目进行了一个简单说明。这个项目将展示如何在Spring MVC 中使用Apache Shiro来构建我们的安全框架。
阅读文章前,您需要做以下准备:
首先,我们在mysql数据库中创建schema,命名为shirodemo。我们在创建两个用户shiroDemo@localhost
和shiroDemo@%
,这里我们将用户的密码简单设置成123456。
然后,我们将项目从git服务器上clone到本地后,我们可以在项目根目录下的resources中发现db.sql
文件。这个文件是项目的数据库结构文件,你可以将db.sql导入到数据库shirodemo中。
我们这里的权限结构设计比较简单,我们以表格的形式说明主要数据库结构:
Name | Type | Length | Describ |
---|---|---|---|
id | int | 11 | 用户表的主键 |
password | varchar | 255 | 密码 |
username | varchar | 255 | 用户名,全局唯一,shiro将使用用户名来锁定安全数据中的用户数据。 |
N |
---|
From:Securing Web Applications with Apache Shiro
这个教程将一步一步的讲解如何在一个web应用中使用Apache Shiro来构建安全框架。阅读之前,我们假设您已经掌握或者阅读了如下两篇文章:
这篇文章的阅读时间大概在45分钟到1小时之间。在你阅读完这篇文章后,你也许会对如何在一个web应用中使用Apache Shiro有一个很好的了解。
目录
If you’re new to Apache Shiro, this short tutorial will show you how to set up an initial and very simple application secured by Apache Shiro. We’ll discuss Shiro’s core concepts along the way to help familiarize you with Shiro’s design and API.
If you don’t want to actually edit files as you follow this tutorial, you can obtain a nearly identical sample application and reference it as you go. Choose a location:
In this simple example, we’ll create a very simple command-line application that will run and quickly exit, just so you can get a feel for Shiro’s API.
这是一个command-line application的例子,跟10分钟教程里的类似。什么是command-line application?可以简单理解为就是一个纯JAVA的程序。
Any Application |
---|
Apac |
【简介】:Apache Shiro十分钟教程,入门级教程,主要讲述shiro的API,使读者对API有一个大概的认识。从中我们也不难看出shiro是能够脱离容器和服务器运行的。
Welcome to Apache Shiro’s 10 Minute Tutoral!
By going through this quick and simple tutorial you should fully understand how a developer uses Shiro in their application. And you should be able to do it in under 10 minutes.
看完这个10分钟教程,你应该就会使用shiro了。让我们拭目以待吧。
What is Apache Shiro?
Apache Shiro is a powerful and easy to use Java security framework that offers developers an intuitive yet comprehensive solution to authentication, authorization, cryptography, and session management.
老生常谈安全框架四大基石。
In practical terms, it achieves to manage all facets of your application’s security, while keeping out of the way as much as possible. It is built on sound interface-driven design and OO principles, enabling custom behavior wherever you can imagine it. But with sensible defaults for everything, it is as “hands off”
Please just take 2 minutes to read and understand this - it is really important. Really. The terms and concepts here are referred to everywhere in the documentation and it will greatly simplify your understanding of Shiro and security in general.
编者建议花点时间来阅读本章内容,本章内容介绍了一些专业术语的概念,这些专业术语充斥着整个shiro文档中。了解这些专业术语的概念,有助于理解shiro和安全方面的知识。
Security can be really confusing because of the terminology used. We’ll make life easier by clarifying some core concepts and you’ll see how nicely the Shiro API reflects them:
安全框架很难理解,那是因为其中包含了很多专业术语。我们将阐述一些核心概念,帮助你轻松的了解shiro,并且揭示shiro api如何清晰的体现这些核心概念。
Authentication
Authentication is the process of verifying a Subject’s identity - essentially proving that someone really is who they say they are. When an authentication attempt is successful the application can trust that the subject is guaranteed to be who the application expects.
身份验证,是一个验证subject身份
Apache Shiro is a powerful and flexible open-source security framework that cleanly handles authentication, authorization, enterprise session management and cryptography.
Apache Shiro 是一个功能强大和灵活的开源安全框架,清晰的集成了身份认证、授权、企业级的会话管理和加密。
Apache Shiro’s first and foremost goal is to be easy to use and understand. Security can be very complex at times, even painful, but it doesn’t have to be. A framework should mask complexities where possible and expose a clean and intuitive API that simplifies the developer’s effort to make their application(s) secure.
Apache Shiro的首要目标是简单易用。应用的安全的设计有时是很复杂,甚至是痛苦的,Shiro并不是这样的。Shiro是一个尽可能隐藏其复杂性,并提供清晰和直管的API来简化开发工作的一个安全框架。
Here are some things that you can do with Apache Shiro:
以下是shiro可以实现的几方面功能
Authenticate a user to verify their identity
验证用户身份
Perform access control for a user, such as:
– Determine if a user is assigned a certain security role or not
翻译至 Hadoop The Definitive Guid 4th Edition