java shiro实例_Apache Shiro入门实例
Shiro是一个强大灵活的开源安全框架,提供身份验证、授权、会话管理、密码体系。
1.先创建一个Maven项目
2.配置pom
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
cn.edu.stu
shiro-test
0.0.1-SNAPSHOT
org.apache.shiro
shiro-core
1.3.0
org.slf4j
slf4j-log4j12
1.6.4
3.在src/main/java下创建log4j.properties文件,配置logger
log4j.rootLogger=info, ServerDailyRollingFile, stdout
log4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ServerDailyRollingFile.DatePattern='.'yyyy-MM-dd
log4j.appender.ServerDailyRollingFile.File=C://logs/notify-subscription.log
log4j.appender.ServerDailyRollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.ServerDailyRollingFile.layout.ConversionPattern=%d - %m%n
log4j.appender.ServerDailyRollingFile.Append=true
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p [%c] %m%n
4.在根目录下创建auth.ini文件
[users]
lonestarr = vespa
5.示例代码
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
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;
public class ShiroTest {
private static Logger logger = LoggerFactory.getLogger(ShiroTest.class);
public static void main(String[] args) {
Factoryfactory =
new IniSecurityManagerFactory("auth.ini");
SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
//obtain the currently executing user
Subject user = SecurityUtils.getSubject();
//logger.info("User is authenticated: " + user.isAuthenticated());
/*The Session is a Shiro-specific instance that provides most of
* what you're used to with regular HttpSessions but with some
* extra goodies and one big difference: it does not require
* an HTTP environment!
*/
Session session = user.getSession();
session.setAttribute("key", "value");
if(!user.isAuthenticated()) {
UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
token.setRememberMe(true);
try {
user.login(token);
//if no exception, that's it, we're done!
} catch (UnknownAccountException uae) {
//username wasn't in the system, show them an error message?
} catch (IncorrectCredentialsException ice ) {
//password didn't match, try again?
} catch (LockedAccountException lae) {
//account for that username is locked - can't login. Show them a message?
}
//... more types exceptions to check if you want ...
catch (AuthenticationException ae) {
//unexpected condition - error?
}
}
//get user name
logger.info( "User [" + user.getPrincipal() + "] logged in successfully." );
//if user have specific role or not
if(user.hasRole("schwartz")) {
logger.info("May the Schwartz be with you!");
}
else {
logger.info( "Hello, mere mortal.");
}
//we can perform an extremely powerful instance-level permission
//check - the ability to see if the user has the ability to access
//a specific instance of a type
if (user.isPermitted("winnebago:drive:eagle5" ) ) {
logger.info("You are permitted to 'drive' the 'winnebago' with license plate (id) 'eagle5'." +
"Here are the keys - have fun!");
} else {
logger.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
}
// when the user is done using the application, they can log out
user.logout();
}
}
6.运行结果
2016-08-04 15:27:48 INFO [org.apache.shiro.session.mgt.AbstractValidatingSessionManager] Enabling session validation scheduler...
2016-08-04 15:27:48 INFO [cn.edu.stu.shiro.ShiroTest] User [lonestarr] logged in successfully.
2016-08-04 15:27:48 INFO [cn.edu.stu.shiro.ShiroTest] Hello, mere mortal.
2016-08-04 15:27:48 INFO [cn.edu.stu.shiro.ShiroTest] Sorry, you aren't allowed to drive the 'eagle5' winnebago!
java shiro实例_Apache Shiro入门实例相关推荐
- java webservice 示例_Java WebService入门实例
本实例所用工具: 1.eclipse-luna 2.jdk1.6.0_45 一.创建服务端 1.新建 Java Project,命名为TheService 2.在TestService下创建包,包名为 ...
- crawler爬虫java实例,Crawler4j快速入门实例
下面我们来写一个Demo,让大家快速入门crawler4j:代码中加了详细的备注,大家可以好好看看. crawler4j中用了slf4j来记录项目运行日志信息.我们使用slf4j具体实现类log4j: ...
- JAVA的OPENGL,JOGL入门实例----不断变色的点阵 (源代码)
原文:http://blog.csdn.net/sidihuo/article/details/44035015 第一个类(代码解读) [java] view plaincopy package te ...
- 从实例入手Shiro并实现HellloWorld
场景 shiro Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码和会话管理.使用Shiro的易于理解的API,您可以快速.轻松地获得任何应用程序,从最小的移动应用 ...
- 权限控制框架Shiro简单介绍及配置实例
Shiro是什么 Apache Shiro是一个非常易用的Java安全框架它能提供验证.授权.加密和Session控制.Shiro非常轻量级而且API也非常易于理解可以使用Shiro完成从APP到企业 ...
- Java Socket入门实例
基于测试驱动的Socket入门实例(代码的具体功能可以看我的程序中的注释,不理解的可以短信我) 先看Server的代码: package socketStudy; import java.io.Buf ...
- java rmi 入门实例
java rmi 入门实例 (2009-06-16 16:07:55) 转载▼ 标签: java rmi 杂谈 分类: java-基础 java rmi即java远程接口调用,实现了2台虚拟机之间的 ...
- Java 图形化界面编程(Swing)入门实例
本文目录 入门实例 先整一个图形化界面出来 常用布局 流式布局 网格布局 边界布局 边界布局 四个入门实例(附截图) 一个小demo(单词计数) 一个较为完整的demo Java (Swing)文件版 ...
- java oxm_Spring oxm入门实例
o/xmapper是什么? spring3.0的一个新特性是o/xmapper.o/x映射器这个概念并不新鲜,o代表object,x代表xml.它的目的是在java对象(几乎总是一个plainoldj ...
最新文章
- 3分钟销量破千 这款笔记本告诉你大家喜欢的轻薄本什么样!
- 语言怎么绘画人物肖像_坦培拉绘画技法——油画简史
- 服务器性能指标有哪些
- centos下hadoop的安装
- 前端学习(1107):函数进阶
- 能量谱与功率谱(转自百度文库与维基百科)
- Django-restframework 之频率源码分析
- 8599元起!三星Galaxy Z Fold3/Flip3 5G折叠手机国行即将发货
- C++array容器用法解析,它与普通数组究竟有何不同?
- ASN1对象与OID之间转换的函数
- Python 第三方模块安装出现的问题和解决方案.
- 解决虚拟机卡 鼠标拖动很慢 有效解决办法
- Oracle中INSTR函数,及在DB2、Sybase中与Instr函数功能相同的函数
- Tomcat配置两个应用服务
- 在线文件管理html,KODExplorer可道云-开源Web在线文件管理系统
- 【黄冈市中级人民法院在湖北行星传动设备有限公司的强制清算案件中的违法问题给投资者的启示】
- vue使用LayIM组件接入第三方通讯平台:融云
- 【无标题】8421码,5421码,2421码,余3码之间的区别。
- 【好奇心驱动力】ESP32-CAM人体感应拍照并推送到微信
- 联想ghost重装系统_一键ghost官网,图文详解一键ghost怎么重装系统
热门文章
- Marvelous Designer衣袖设计教程
- Go:分布式学习利器(1) -- 开发环境搭建 + 运行第一个go程序
- 递归/回溯:Combination Sum II数组之和
- vue项目构建实战基础知识:SPA理解/RESTful接口介绍/static目录配置/axios封装/打包时map文件去除...
- 深入JDK源码,这里总有你不知道的知识点!
- Java Class SecurityManager
- UVa 679 - Dropping Balls
- Intent携带额外的数据的方法
- flash 入门课知识小结
- 一些技术图书编写、推荐、出版人员需要自重