下面内容是在看了涛哥的《跟我一起学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. 1.MySQL与MongoDB的操作对比,以及区别

    转自:https://www.cnblogs.com/chris-oil/p/4982490.html MySQL与MongoDB都是开源的常用数据库,但是MySQL是传统的关系型数据库,MongoD ...

  2. POJ 3225 线段树+lazy标记

    lazy写崩了--. 查了好久 /* U-> [l,r]–>1 I-> [1,l-1] [r+1,+无穷] –>0 D-> [l,r]–>0 C-> [1,l ...

  3. JavaScript 获取移动设备的型号

    https://joyqi.com/javascript/how-to-detect-mobile-devices-model-using-javascript.html?utm_source=too ...

  4. SSL和SSH

    简单的来说:SSL是安全传输的一种安全协议,SSH只是在传输的时候为了防止"中间人"篡改数据而提供的安全的"通道" 在使用的时候我们只关心传输数据的安全性,那么 ...

  5. win7 ssd评分降为5.9的诡异问题解决方法

    某一天偶然发现win7的系统评分里面,磁盘一项由之前的7.9降到5.9了,SSD早听说会有降速的问题,但无论如何降,也不至于被降到5.9分这一机械硬盘普遍的分数. 百度搜了,Google搜了,中文搜了 ...

  6. css与jquery、图标字体

    *)还能这样选择 header #search input[type="text"] *)按钮常用颜色:#008cBA(字母大小写没有区别) *)清除浮动后,text-align没 ...

  7. 云服务器 ECS Linux 系统下使用 dig 命令查询域名解析

    云服务器 ECS Linux 系统可以使用通常自带的 dig 命令来查询域名解析情况.本文对此进行简要说明. 查询域名 A 记录 命令格式: dig <域名> 比如,查询域名 www.al ...

  8. 洛谷 P2005 A/B Problem II

    P2005 A/B Problem II 题目背景 为了让大家紧张的心情放松一下,这一题题是一道非常简单的题目. 题目描述 给出正整数N和M,请你计算N div M(N/M的下取整). 输入输出格式 ...

  9. Spring-statemachine Action不能并发执行的问题

    Spring-statemachine版本:当前最新的1.2.3.RELEASE版本 这几天一直被Action是串行执行搞得很郁闷,写了一个demo专门用来测试: public static void ...

  10. 第二十四天 框架之痛-Spring MVC(四)

    6月3日,晴."绿树浓阴夏日长. 楼台倒影入池塘. 水晶帘动微风起, 满架蔷薇一院香". 以用户注冊过程为例.我们可能会选择继承AbstractController来实现表单的显示 ...