shiro简单入门介绍
shiro是apache的一个java安全框架
可以完成认证,授权,加密,会话管理,基于web继承,缓存等
功能简介:
从外部来看:
shiro架构
Subject:主体,代表了当前“用户”,这个用户不一定是一个具体的人,与当前应用交互的任何东西都是Subject,如网络爬虫,机器人等;即一个抽象概念;所有Subject都绑定到SecurityManager,与Subject的所有交互都会委托给SecurityManager;可以把Subject认为是一个门面;SecurityManager才是实际的执行者;
从内部看:
Subject:主体,可以看到主体可以是任何可以与应用交互的“用户”;
下载地址:
http://shiro.apache.org/download.html
HellowWord:
package cn.com.MrChengs.helloword;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/ 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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; /**
* Simple Quickstart application showing how to use Shiro's API.
*
* @since 0.9 RC2
*/
public class Quickstart { private static final transient Logger log = LoggerFactory.getLogger(Quickstart.class); public static void main(String[] args) { // The easiest way to create a Shiro SecurityManager with configured
// realms, users, roles and permissions is to use the simple INI config.
// We'll do that by using a factory that can ingest a .ini file and
// return a SecurityManager instance: // Use the shiro.ini file at the root of the classpath
// (file: and url: prefixes load from files and urls respectively):
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager securityManager = factory.getInstance(); // for this simple example quickstart, make the SecurityManager
// accessible as a JVM singleton. Most applications wouldn't do this
// and instead rely on their container configuration or web.xml for
// webapps. That is outside the scope of this simple quickstart, so
// we'll just do the bare minimum so you can continue to get a feel
// for things.
SecurityUtils.setSecurityManager(securityManager); // Now that a simple Shiro environment is set up, let's see what you can do: // get the currently executing user:
//获取当前的subject
//调用SecurityUtils.getSubject();
Subject currentUser = SecurityUtils.getSubject(); // Do some stuff with a Session (no need for a web or EJB container!!!)
//测试使用session
//首先获取session:currentUser.getSession();
//
Session session = currentUser.getSession();
session.setAttribute("someKey", "aValue");
String value = (String) session.getAttribute("someKey");
if (value.equals("aValue")) {
log.info("--->Retrieved the correct value! [" + value + "]");
} // let's login the current user so we can check against roles and permissions:
//测试当前用户是否已经被认证,即是否已经登陆
//调用subject的isAuthenticated() if (!currentUser.isAuthenticated()) {
//把用户名和密码封装为UsernamePasswordToken对象
UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
token.setRememberMe(true);
try {
//执行登陆
currentUser.login(token);
}
//若没有指定的消息,则shiro会抛出UnknownAccountException异常
catch (UnknownAccountException uae) {
log.info("There is no user with username of " + token.getPrincipal());
}
//若账户存在,但是密码不匹配则抛出IncorrectCredentialsException
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?
}
} //say who they are:
//print their identifying principal (in this case, a username):
log.info("User [" + currentUser.getPrincipal() + "] logged in successfully."); //test a role:
//测试是否有某一个角色
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)
//测试是否具有某一个行为
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();
}
运行即可体验shiro的简单功能....
shiro简单入门介绍的更多相关文章
- 权限框架 - shiro 简单入门实例
前面的帖子简单的介绍了基本的权限控制,可以说任何一个后台管理系统都是需要权限的 今天开始咱们来讲讲Shiro 首先引入基本的jar包 <!-- shiro --> <dependen ...
- EVE-NG简单入门介绍
此篇文章简单的介绍下模拟器EVE-NG的使用,具体包括Dynamips设备导入与运行,IOL设备的导入与运行,QEMU设备的导入与运行,客户端软件的安装,物理网络与虚拟网络的结合等. 一.导入镜像 D ...
- mybatis简单入门介绍
mybatis入门 简介 什么是mybatis? MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及 ...
- SpringDataRedis简单入门介绍
1:问题引入 在实际开发中,开发的每一个项目,每天都有大量的人访问,对数据库造成很大的访问压力,甚至是瘫痪.那如何解决呢?我们通常的做法有两种:一种是数据缓存.一种是网页静态化.我们今天讨论第一种解决 ...
- qing-automation简单入门介绍
1.相关文档:http://www.51testing.com/html/50/category-catid-250.html 2.进行Qing automation相关操作之前,必须安装好jdk跟a ...
- Apache shiro集群实现 (一) shiro入门介绍
近期在ITOO项目中研究使用Apache shiro集群中要解决的两个问题,一个是Session的共享问题,一个是授权信息的cache共享问题,官网上给的例子是Ehcache的实现,在配置说明上不算很 ...
- shiro的简单入门使用
这里只是测试登录认证,没有web模块,没有连接数据库,用户密码放在shiro.ini配置中,密码没有加密处理,简单入门. 基于maven 先看目录结构 测试结果 pom.xml <?xml ve ...
- [原创]MYSQL的简单入门
MYSQL简单入门: 查询库名称:show databases; information_schema mysql test 2:创建库 create database 库名 DEFAULT CHAR ...
- 初识Hadoop入门介绍
初识hadoop入门介绍 Hadoop一直是我想学习的技术,正巧最近项目组要做电子商城,我就开始研究Hadoop,虽然最后鉴定Hadoop不适用我们的项目,但是我会继续研究下去,技多不压身. < ...
随机推荐
- Linux文件夹和文件创建删除命令
Linux删除文件夹命令 linux删除目录很简单,很多人还是习惯用rmdir,不过一旦目录非空,就陷入深深的苦恼之中,现在使用rm -rf命令即可.直接rm就可以了,不过要加两个参数-rf 即:rm ...
- Silverlight & Blend动画设计系列六:动画技巧(Animation Techniques)之对象与路径转化、波感特效
当我们在进行Silverlight & Blend进行动画设计的过程中,可能需要设计出很多效果不一的图形图像出来作为动画的基本组成元素.然而在设计过程中可能会出现许多的问题,比如当前绘制了一个 ...
- Jquery+Aajax 批量上传
注:转载请标明文章原始出处及作者信息 网上有现成的Uploadify.WebUpload等插件,自己写一个简单的(非插件). 1.页面 批量上传页面 <form action="&qu ...
- 二:Vim常用命令
一般模式下的命令: -- 插入命令 i 光标前插入 I 当前行开始 o 下一行 O 上一行插入新行 a 光标后插入 A 当前行末尾 -- 定位命令 :set nu 显示行号 :set nonu 取消行 ...
- php array 数组及数组索引
array (PHP 4, PHP 5, PHP 7) array — 新建一个数组 说明 array array ([ mixed $... ] ) 创建一个数组.关于数组是什么的信息请阅读数组一节 ...
- Nginx 504错误总结
Nginx 504错误(Gateway time-out 网关超时)的含义是所请求的网关没有请求到,简单来说就是没有请求到可以执行的PHP-CGI. 一般看来, 这种情况可能是由于nginx默认的f ...
- Spring Data JPA 缓存结合Ehcache介绍
一级缓存: 会话session.事务级别的,事务退出,缓存就失效了. 实体管理器在事务执行期间持有一份数据的拷贝,而非直接操作数据源. 二级缓存: 进程范围级或集群范围的缓存,这个级别的缓存可配置和修 ...
- Cocos2d-js 开发记录:骨骼动画载入
不得不说cocos2d-js的文档实在是少,骨骼动画的载入和C++版本的好像还有些不同不能直接依样画葫芦. 一个由cocos studio编辑生成的骨骼动画一般会包含如下几个文件: .ExportJs ...
- flask 服务器详解
#!/usr/local/bin/python # coding=utf-8 from flask import Flask app = Flask(__name__) @app.route('/') ...
- JavaScript 事件委托
JavaScript事件委托,或者叫事件代理,是利用事件冒泡,只指定一个事件处理程序,就可以管理某一类型的所有事件. 借花献佛的例子(取快递): 有三个同事预计会在周一收到快递.为签收快递,有两种办法 ...