[shiro学习笔记]第一节 使用eclipse/myeclipse搭建一个shiro程序
本文地址:http://blog.csdn.net/sushengmiyan/article/details/39519509
shiro官网:http://shiro.apache.org/
本文作者:sushengmiyan
------------------------------------------------------------------------------------------------------------------------------------
最近想做个简单的extjs5的登录跳转,之前做了一个,但是extjs又没有跳转链接,每次刷新都需要重新登录,实现的不好,现在想实现登录用户的认证,于是看到了shiro这个框架,看起来还是比较好用的,于是开始研究。
刚开始搭建,发现官网给的hello world程序需要maven支持对pom文件研究又不深入,只能抛弃这种方式了。自己安装官方参考手册,下载了quickstart文件源码,但是这是maven文件,不会用,只能手动的新建了一个文件这样的,运行的时候报了好多错误,跑了好久,终于把hello world给运行出来了,现在分享一下可以运行的程序的创建的整个过程:
1.使用eclipse/myeclipse新建shirodemo工程
2.新建lib文件夹,将shiro-all-1.1.0.jar、slf4j-api-1.7.7.jar、slf4j-log4j12-1.7.7.jar、log4j-1.2.16.jar这些jar包添加进来。
3.build path---config build path将这些jar包添加到libraries中
4.新建自己的程序包,例如我新建com.susheng包,将quickstart中Quickstart.java中代码复制一份来。
5.添加shiro.ini和log4j.properties文件到src目录下。
6.运行程序:
程序正常运行。
之前遇到错误列表:
1.没有添加shiro.ini文件
错误信息:Exception in thread "main" org.apache.shiro.config.ConfigurationException: java.io.IOException: Resource [classpath:shiro.ini] could not be found.
原因是没有添加shiro.ini文件,解决方法,将shiro.ini放在src目录下即可解决。
2.没有正确添加self4j-log4j12.jar包
错误信息:SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
self4j的包没有添加完成,这个问题真是弄得我好久都郁闷着。看实例代码,我引入slf4j-api-1.7.7.jar包之后,都可以正常些代码,没有错误提示了,但是居然这个jar包还依赖其他jar包才可以,这相当让我不舒服啊。引入其他self4j的jar包(slf4j-log4j12-1.7.7.jar)即可解决。
3.没有引入log4j的jar包
错误信息: java.lang.NoClassDefFoundError: org/apache/log4j/Level
4.没有log4j的配置文件
错误信息:log4j:WARN No appenders could be found for logger (org.apache.shiro.io.ResourceUtils).
添加log4j.properties到src目录下即可解决问题。
最终的程序目录结构如下:
代码都是shiro例子的,也粘贴一下吧。
package com.susheng; 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 currentUser = SecurityUtils.getSubject(); // Do some stuff with a Session (no need for a web or EJB container!!!)
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:
if (!currentUser.isAuthenticated()) {
UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
token.setRememberMe(true);
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?
}
} //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(); System.exit(0);
}
}
[shiro学习笔记]第一节 使用eclipse/myeclipse搭建一个shiro程序的更多相关文章
- [maven学习笔记]第一节,认识maven,搭建maven开发环境,写第一个HelloWorld
本文地址:http://blog.csdn.net/sushengmiyan/article/details/40142771 maven官网:http://maven.apache.org/ 学习视 ...
- [shiro学习笔记]第二节 shiro与web融合实现一个简单的授权认证
本文地址:http://blog.csdn.net/sushengmiyan/article/details/39933993 shiro官网:http://shiro.apache.org/ shi ...
- JDBC 学习笔记(十)—— 使用 JDBC 搭建一个简易的 ORM 框架
1. 数据映射 当我们获取到 ResultSet 之后,显然这个不是我们想要的数据结构. 数据库中的每一个表,在 Java 代码中,一定会有一个类与之对应,例如: package com.gerrar ...
- OpenCV学习笔记(01)我的第一个OpenCV程序(环境配置)
昨天刚刚考完编译原理,私心想着可以做一些与考试无关的东西了.一直想做和图像处理相关的东西,趁这段时间有空学习一下OpenCV,搭建环境真是一件麻烦的事情,搞了近三个小时终于OK了.先来张图: 大致描述 ...
- [struts2学习笔记] 第一节 关于struts2的简单认知
本文地址:http://blog.csdn.net/sushengmiyan/article/details/40298287 官方文档:http://struts.apache.org/releas ...
- [EXtJS5学习笔记]第一节 Sencha Cmd 学习笔记 简介 Sencha Cmd是什么
本文地址: http://blog.csdn.net/sushengmiyan/article/details/38295575 本文作者:sushengmiyan ----------------- ...
- python学习笔记第一节
一.HelloWorld #!/usr/bin/env python #-*- coding:utf-8 -*- print("HelloWorld!") 二.用户交互 #!/us ...
- 【学习笔记】【C语言】第一个C程序
1.新建C语言程序 *打开Xcode *新建文件 *选择C语言程序 *输入程序名称 *选择存放路径 *创建完成后 2.也可以用终端来创建 通过指令来创建.c文件 3.编写代码 4. ...
- WCF学习笔记(一)---我的第一个WCF程序
一.创建WCF程序 1.创建一个控制台程序(WCFBlog) 2.添加wcf项目 3.将默认的IService1和Service1改成自己的名字 4.在ICalculateServic ...
随机推荐
- 关于非旋转treap的学习
非旋转treap的操作基于split和merge操作,其余操作和普通平衡树一样,复杂度保证方式与旋转treap差不多,都是基于一个随机的参数,这样构出的树树高为\(logn\) split 作用:将原 ...
- 【NOIP 2017】宝藏
Description 参与考古挖掘的小明得到了一份藏宝图,藏宝图上标出了 n 个深埋在地下的宝藏屋, 也给出了这 n 个宝藏屋之间可供开发的 m 条道路和它们的长度. 小明决心亲自前往挖掘所有宝藏屋 ...
- linux x86内核中的分页机制
Linux采用了通用的四级分页机制,所谓通用就是指Linux使用这种分页机制管理所有架构的分页模型,即便某些架构并不支持四级分页.对于常见的x86架构,如果系统是32位,二级分页模型就可满足系统需求: ...
- 有源汇上下界可行流(POJ2396)
题意:给出一个n*m的矩阵的每行和及每列和,还有一些格子的限制,求一组合法方案. 源点向行,汇点向列,连一条上下界均为和的边. 对于某格的限制,从它所在行向所在列连其上下界的边. 求有源汇上下界可行流 ...
- Django Class Views
一.Base views View class django.views.generic.base.View 主要的基于类的基本视图.所有其他基于类的视图都从这个基类继承而来.它不是一个通用的视图,因 ...
- django rest-framework 1.序列化 二
在上一节说了Serializers的使用类似Django的From,在Django中有From也有ModelFrom,Serializers也是有个ModelSerializers,下面在讲讲rest ...
- C++ C# python 中输入输出函数对比
C++ cin>>"nihao";cout<<"nihao"<<endl; C# System.Console.ReadLi ...
- 第一节mysql 安装
1 安装之前的检查 先要检查Linux系统中是否已经安装了MySQL,输入命令尝试打开MySQL服务: 输入密码后,如果出现以下提示,则说明系统中已经安装有 MySQL: 如果提示是这样的,则说明系统 ...
- C语言程序第二次作业
(一)改错题 1.输出带框文字:在屏幕上输出以下3行信息. ************* Welcome ************* 源程序 include int mian() { printf(&q ...
- postgresql 安装使用
www.postgresql.org去下载你需要的版本,我下载的9.6.8 安装过程中会让你输入一次密码,其余的默认就ok 默认超级用户名postgres 打开 pgadmin4,我们将语言改为中文会 ...