下面内容是在看了涛哥的《跟我一起学shiro》 和 视频《一头扎入进shiro》 后整理出来备忘和方便自己和其它人学习。

个人主页:http://www.itit123.cn/ 很多其它干货等你来拿

第一步:创建maven版web项目:http://blog.csdn.net/qq_19558705/article/details/49887717

创建好后须要: 右击项目 ----> build path ----> config build path ----> add library ----> server runtime ----> 选择合适的就可以 避免出现 “The
superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path” 错误

第二步:导入相关的jar

<!-- 日志管理 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency> <dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency> <!-- shiro -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.2.4</version>
</dependency> <dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>1.2.4</version>
</dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>

第三步:配置web.xml文件(该配置方法是载入shiro.ini方法,实际开发中不是这样,能够看官网文档)

<!-- shiro 监听 -->
<listener>
<listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
</listener> <!-- shiro 拦截 -->
<filter>
<filter-name>ShiroFilter</filter-name>
<filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ShiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

第四步:身份验证

shiro.ini文件:

[main]
#用户登入路径
authc.loginUrl=/login
[users]
ITDragon=123456,admin
[urls]
#该路径为匿名登入
/login=anon
#身份验证后才干登入
/admin=authc

login.jsp:

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登录页面</title>
</head>
<body>
<form action="login" method="post">
userName:<input type="text" name="userName" /><br />
password:<input type="password" name="password" /><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>

LoginServlet:

package com.shiro.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject; @WebServlet("/login")
public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("login doGet ... ");
request.getRequestDispatcher("login.jsp").forward(request, response);
} protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("login doPost ... ");
String userName = request.getParameter("userName");
String password = request.getParameter("password");
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(userName,password);
try {
subject.login(token);
response.sendRedirect("success.jsp");
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("errorInfo", "username或者密码错误");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
} }

通过浏览器訪问发现:未登入的訪问/admin会跳转到登入页面,若登入后在訪问就可以进入成功页面,说明身份验证成功。

第五步:权限认证

shiro文件:

[main]
#用户登入路径
authc.loginUrl=/login
#角色验证
roles.unauthorizedUrl=/unauthorized.jsp
#权限验证
perms.unauthorizedUrl=/unauthorized.jsp
[users]
ITDragon=123456,admin
teacher1=123456,teacher
student1=123456
[roles]
admin=user:*
teacher=student:*
[urls]
#该路径为匿名登入
/login=anon
#身份验证后才干登入
/admin=authc
#该路径验证是否拥有teacher角色
/student=roles[teacher]
#该路径验证该角色是否拥有权限
/teacher=perms["admin:delete"]

unauthorized.jsp:

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>错误页面</title>
</head>
<body>
对不起。您不具备该权限。
</body>
</html>

在浏览器中訪问/student。会先跳到登入页面进行身份验证。然后在推断该用户是否拥护teacher角色权限

在浏览器中訪问/teacher,推断用户是否拥有该权限。

由于没有准备相应的servlet,所以正确情况会显示404,若没有权限则会跳到 unauthorized.jsp 页面。

这样就完毕了web中shiro的HelloWorld,之后会具体记录笔记。

源代码下载路径:http://download.csdn.net/detail/qq_19558705/9449892

Apache shiro 笔记整理之web整合一的更多相关文章

  1. Apache shiro 笔记整理之编程式授权

    下面内容是在看了涛哥的<跟我一起学shiro> 和 视频<一头扎入进shiro> 后整理出来备忘和方便自己和其它人学习. 个人主页:http://www.itit123.cn/ ...

  2. 【Shiro】Apache Shiro架构之集成web

    Shiro系列文章: [Shiro]Apache Shiro架构之身份认证(Authentication) [Shiro]Apache Shiro架构之权限认证(Authorization) [Shi ...

  3. Apache Shiro 快速入门教程,shiro 基础教程

    第一部分 什么是Apache Shiro     1.什么是 apache shiro :   Apache Shiro是一个功能强大且易于使用的Java安全框架,提供了认证,授权,加密,和会话管理 ...

  4. 【Shiro】Apache Shiro架构之自定义realm

    [Shiro]Apache Shiro架构之身份认证(Authentication) [Shiro]Apache Shiro架构之权限认证(Authorization) [Shiro]Apache S ...

  5. 【Shiro】Apache Shiro架构之权限认证(Authorization)

    Shiro系列文章: [Shiro]Apache Shiro架构之身份认证(Authentication) [Shiro]Apache Shiro架构之集成web [Shiro]Apache Shir ...

  6. 【Shiro】Apache Shiro架构之身份认证(Authentication)

    Shiro系列文章: [Shiro]Apache Shiro架构之权限认证(Authorization) [Shiro]Apache Shiro架构之集成web [Shiro]Apache Shiro ...

  7. Shiro学习总结(2)——Apache Shiro快速入门教程

    第一部分 什么是Apache Shiro 1.什么是 apache shiro : Apache Shiro是一个功能强大且易于使用的Java安全框架,提供了认证,授权,加密,和会话管理 如同 spr ...

  8. 在 Web 项目中应用 Apache Shiro

    Apache Shiro 是功能强大并且容易集成的开源权限框架,它能够完成认证.授权.加密.会话管理等功能.认证和授权为权限控制的核心,简单来说,"认证"就是证明你是谁? Web ...

  9. [转]在 Web 项目中应用 Apache Shiro

    目录[-] 用户权限模型 图 1. 用户权限模型 认证与授权 Shiro 认证与授权处理过程 Shiro Realm 清单 1. 实现自己的 JDBC Realm 为何对 Shiro 情有独钟 与 S ...

随机推荐

  1. 常见的DNS攻击——偷(劫持)、骗(缓存投毒)、打(DDos)

    常见的DNS攻击包括: 1) 域名劫持 通过采用黑客手段控制了域名管理密码和域名管理邮箱,然后将该域名的NS纪录指向到黑客可以控制的DNS服务器,然后通过在该DNS服务器上添加相应域名纪录,从而使网民 ...

  2. 《剑指offer》反转链表

    一.题目描述 输入一个链表,反转链表后,输出链表的所有元素. 二.输入描述 输入一个链表 三.输出描述 返回逆转后的链表 四.牛客网提供的框架 /* struct ListNode { int val ...

  3. NSStream 流式思想

    流式思想的本质是将数据或信号看作流.流的管理者NSStream看作管道. 内容包含两方面: 1.流的建立:源.目的地: 2.流的管理:状态事件与数据事件. 本质上是建立联系.处理数据.处理状态.

  4. redis 扩展 windows

    下载地址:http://windows.php.net/downloads/pecl/releases/redis/

  5. [CQOI2013]新Nim游戏(线性基)

    P4301 [CQOI2013]新Nim游戏 题目描述 传统的Nim游戏是这样的:有一些火柴堆,每堆都有若干根火柴(不同堆的火柴数量可以不同).两个游戏者轮流操作,每次可以选一个火柴堆拿走若干根火柴. ...

  6. ZJU 1346 Comparing Your Heroes 状态压缩DP 拓扑排序的计数

    做多校的时候遇见一个求拓扑排序数量的题,就顺便来写了一下. 题意: 你有个朋友是KOF的狂热粉丝,他有一个对其中英雄的强弱比较,让你根据这些比较关系来给这些英雄排名.问一共有多少种排名方式. 思路: ...

  7. WHU 1540 Fibonacci 递推

    武大邀请赛的网络预选赛,就去做了个签到题,居然连这个递推都没推出来,真是惭愧. 而且好久没写矩阵乘法了,来回顾一下. 题意: 求Fibonacci数列的,前n项立方和. 思路: 可以求得一下递推公式: ...

  8. 洛谷 P3902 递增

    P3902 递增 题目描述 现有数列A_1,A_2,\cdots,A_NA1​,A2​,⋯,AN​,修改最少的数字,使得数列严格单调递增. 输入输出格式 输入格式: 第1 行,1 个整数N 第2 行, ...

  9. AP设备漫游阈值设置

    在多个AP部署的场景下,默认情况下,手持移动设备(如PDA.手机)信号弱到断掉时才切换AP,无线信号很弱的情况下网络是非常的不稳定的,因此我们需要配置AP设备的漫游阈值(RSSI阈值),以便连接的无线 ...

  10. EventBus框架原理解析(结合源代码)(上)

    上一篇文章http://blog.csdn.net/crazy__chen/article/details/47425779 和大家一起模仿EventBus的实现机制.和大家一起写出了一个简易的Eve ...