本文地址:http://blog.csdn.net/sushengmiyan/article/details/39519509

shiro官网:http://shiro.apache.org/

shiro中文手册:http://wenku.baidu.com/link?url=ZnnwOHFP20LTyX5ILKpd_P94hICe9Ga154KLj_3cCDXpJWhw5Evxt7sfr0B5QSZYXOKqG_FtHeD-RwQvI5ozyTBrMAalhH8nfxNzyoOW21K

本文作者: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程序的更多相关文章

  1. [maven学习笔记]第一节,认识maven,搭建maven开发环境,写第一个HelloWorld

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/40142771 maven官网:http://maven.apache.org/ 学习视 ...

  2. [shiro学习笔记]第二节 shiro与web融合实现一个简单的授权认证

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/39933993 shiro官网:http://shiro.apache.org/ shi ...

  3. JDBC 学习笔记(十)—— 使用 JDBC 搭建一个简易的 ORM 框架

    1. 数据映射 当我们获取到 ResultSet 之后,显然这个不是我们想要的数据结构. 数据库中的每一个表,在 Java 代码中,一定会有一个类与之对应,例如: package com.gerrar ...

  4. OpenCV学习笔记(01)我的第一个OpenCV程序(环境配置)

    昨天刚刚考完编译原理,私心想着可以做一些与考试无关的东西了.一直想做和图像处理相关的东西,趁这段时间有空学习一下OpenCV,搭建环境真是一件麻烦的事情,搞了近三个小时终于OK了.先来张图: 大致描述 ...

  5. [struts2学习笔记] 第一节 关于struts2的简单认知

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/40298287 官方文档:http://struts.apache.org/releas ...

  6. [EXtJS5学习笔记]第一节 Sencha Cmd 学习笔记 简介 Sencha Cmd是什么

    本文地址: http://blog.csdn.net/sushengmiyan/article/details/38295575 本文作者:sushengmiyan ----------------- ...

  7. python学习笔记第一节

    一.HelloWorld #!/usr/bin/env python #-*- coding:utf-8 -*- print("HelloWorld!") 二.用户交互 #!/us ...

  8. 【学习笔记】【C语言】第一个C程序

    1.新建C语言程序 *打开Xcode  *新建文件  *选择C语言程序  *输入程序名称  *选择存放路径  *创建完成后  2.也可以用终端来创建  通过指令来创建.c文件   3.编写代码  4. ...

  9. WCF学习笔记(一)---我的第一个WCF程序

    一.创建WCF程序   1.创建一个控制台程序(WCFBlog)   2.添加wcf项目   3.将默认的IService1和Service1改成自己的名字   4.在ICalculateServic ...

随机推荐

  1. hdu 5340 (manacher)

    Sample Input 2 abc abaadada   Sample Output Yes No 判断是否能成为3个非空回文子串 manacher算法求出个点回文长度,在找出第一个和最后一个保存下 ...

  2. 2015多校联赛 ——HDU5288(数学)

    OO’s Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  3. ●HDU 6021 MG loves string

    题链: http://acm.hdu.edu.cn/showproblem.php?pid=6021 题解: 题意:对于一个长度为 N的由小写英文字母构成的随机字符串,当它进行一次变换,所有字符 i ...

  4. poj 3904(莫比乌斯反演)

    POJ 3904 题意: 从n个数中选择4个数使他们的GCD = 1,求总共有多少种方法 Sample Input 4 2 3 4 5 4 2 4 6 8 7 2 3 4 5 7 6 8 Sample ...

  5. [APIO2008]

    A.免费道路roads 题意:给定n个点m条边的图,边有黑白颜色,求是否有一个生成树满足恰好有K条黑边,并输出方案. 题解:先加白边,求出必须加的黑边,然后加黑边到K条,剩下的用白边填充. 顺便说说, ...

  6. python中的缩进问题

    python中没有{}来表示代码块,而是用缩进来表示,刚开始写python代码,没有注意缩进,结果各种报错(( ╯□╰ )). 在python中的原则就是同一层次的代码一定要有相同的缩进!!! 从上图 ...

  7. 数据挖掘_requests模块的get方法

    关于requests模块 之前在跟大家讲通过字典列表批量获取数据的时候用过这个模块 安装过程就不再讲解了 requests模块是python的http库,可以完成绝大部分与http应用相关的工作,所以 ...

  8. SVN提交时显示:Path is not a working copy directory

    说明你地址没有checkout啊 先checkout,才能add和commi. 要是在一个已有的项目出现这个错误,就是包含这个地址的文件夹没添加进去,去上一层再试一次. 总之,养成在项目根目录提交的习 ...

  9. centos 6安装 H3C iNode 上网客户端

    我的安装目录是/usr/iNode 直接把客户端安装包拷到这个文件夹下然后解压: #rar x iNode2.-R0162.rar 然后进入文件夹,里边有一个install.sh文件,这是一个安装文件 ...

  10. JS中的DOM— —节点以及操作

    DOM操作在JS中可以说是非常常见了吧,很多网页的小功能的实现,比如一些元素的增删操作等都可以用JS来实现.那么在DOM中我们需要知道些什么才能完成一些功能的实现呢?今天这篇文章就先简单的带大家入一下 ...