第二章、Web中使用shiro(实现登陆)
虽然Apache Shiro的核心设计目标允许它用于保护任何基于JVM的应用程序(如命令行应用程序,服务器守护程序,Web应用程序等),但本指南将着重讨论最常见的用例:确保运行的Web应用程序一个Servlet容器,比如Tomcat或者Jetty。 先决条件 预计将在本地开发机器上安装以下工具,以便跟随本教程。
- Java SDK 7
- Maven 3
- 您最喜爱的IDE,如IntelliJ IDEA或Eclipse,甚至是一个简单的文本编辑器来查看文件并进行更改。
配置pom.xml
导入jar
<dependencies>
<!-- shiro -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>${shiro.version}</version>
</dependency>
</dependencies>
配置build
<build>
<finalName>shiro-web-test</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<!-- jetty插件 -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<!--<version>9.0.0.v20130308</version> -->
<version>9.2.7.v20150116</version>
<configuration>
<scanIntervalSeconds>3</scanIntervalSeconds>
<webApp>
<contextPath>/</contextPath>
</webApp>
<httpConnector>
<port>8080</port>
</httpConnector>
<reload>automatic</reload>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<skipTests>true</skipTests>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</build>
添加shiro.ini文件(src/main/resources/shiro.ini)
# =======================
# Shiro INI configuration
# =======================
[main]
# Objects and their properties are defined here,
# Such as the securityManager, Realms and anything
# else needed to build the SecurityManager
[users]
admin = secret
lonestarr = vespa, goodguy, schwartz
darkhelmet = ludicrousspeed, badguy, schwartz
[roles]
admin = *
schwartz = lightsaber:*
goodguy = winnebago:drive:eagle5
[urls]
/user/** = authc
/login = anon
配置web.xml
<listener>
<listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
</listener>
<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>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
该
<listener>声明定义了一个ServletContextListener启动SecurityManagerWeb应用程序时启动Shiro环境(包括Shiro )的应用程序。默认情况下,这个侦听器会自动知道为我们的WEB-INF/shiro.ini文件寻找Shiro配置。该
<filter>声明定义了主人ShiroFilter。此过滤器将过滤所有对Web应用程序的请求,以便Shiro在允许请求到达应用程序之前执行必要的身份和访问控制操作。该
<filter-mapping>声明确保所有请求类型均由ShiroFilter。通常filter-mapping声明不指定<dispatcher>元素,但Shiro需要定义所有元素,以便可以过滤可能为Web应用程序执行的所有不同请求类型。
创建登陆页面
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="/login" method="post">
用户名:<input name="username" type="text"/>
密码:<input name="password" type="text"/>
<input type="submit" value="登陆" />
</form>
</body>
</html>
创建登陆servlet
package com.coder306;
import java.io.IOException;
import javax.servlet.ServletException;
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.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.subject.Subject;
public class Login extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
IniSecurityManagerFactory iniSecurityManagerFactory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager securityManager = iniSecurityManagerFactory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
Subject subject = SecurityUtils.getSubject();
if(!subject.isAuthenticated()) {
UsernamePasswordToken token = new UsernamePasswordToken(username,password);
try {
subject.login(token);
resp.sendRedirect("/user/index.jsp");
}catch (UnknownAccountException uae) {
resp.sendRedirect("/login.jsp");
}catch (IncorrectCredentialsException ice) {
resp.sendRedirect("/login.jsp");
}catch (LockedAccountException lae) {
resp.sendRedirect("/login.jsp");
}
}else {
resp.sendRedirect("/user/index.jsp");
}
}
}
web.xml配置servlet
<servlet>
<servlet-class>com.coder306.Login</servlet-class>
<servlet-name>login</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
运行webapp
mvn jetty:run
查看原文:http://www.coder306.cn/?p=200
第二章、Web中使用shiro(实现登陆)的更多相关文章
- 在web中使用shiro(会话管理,登出,shiro标签库的使用)
在shiro的主配置文件中配置,登出的请求经过的过滤器就可以了,在shiro的过滤器中有一个名称为logout的过滤 器专门为我们处理登出请求: 一.shiro会话管理器 shiro中也提供了类似于w ...
- web中集成shiro
Shiro提供了与Web集成的支持,其通过一个ShiroFilter入口来拦截需要安全控制的URL,然后进行相应的控制,ShiroFilter类似于如Strut2/SpringMVC这种web框架的前 ...
- 《Streaming Systems》第二章: 数据处理中的 What, Where, When, How
本章中,我们将通过对 What,Where,When,How 这 4 个问题的回答,逐步揭开流处理过程的全貌. What:计算什么结果? 也就是我们进行数据处理的目的,答案是转换(transforma ...
- Head First Servlets & JSP 学习笔记 第二章 —— Web应用体系结构
Servlet没有main()方法,所以Servlet受其他人控制,这个其他人就是容器!而Tomcat就是一种容器. 容器向Servlet提供Http请求和Http响应:容器来调用Servlet的do ...
- J2EE学习笔记-第二章(Web应用初步)
首先要理解一些概念的词语,到底这些是什么(当我读懂了后,会逐一填补完整,现在我真的有点混淆) web组件-相当于功能性的组件,就像是零件,汽车的轮胎,汽车的门,所有组件组合后,才能成为一辆车,有时候也 ...
- PHP:第二章——PHP中的break一continue一return语句
知识点一:break语句 break 结束当前 for,foreach,while,do-while 或者 switch 结构的执行. break 可以接受一个可选的数字参数来决定跳出 ...
- PHP:第二章——PHP中的while语句
<?php 语法格式: while(条件){ 代码块; } 说明:如果条件等价于true则重复执行代码块中内容,否则不执行 示例1: $i = ; ){ $i--; echo $i; } 注意: ...
- 第二章 eclipse中m2e插件问题
1.当引入一个maven项目到eclipse中时,这时候可能会出现找不到一个插件的问题,例如: <plugin> <groupId>org.apache.maven.plugi ...
- 第二章 python中重要的数据结构(下)
二.元组(tuple):不可变序列 跟list一样,也是一种序列,唯一不同的是,元组元素不能被修改,通常用(, ,)表示元组,也可以不加括号. #创建元组 >>> 1,2,3 (1, ...
随机推荐
- 前端星计划笔记-day1
前端 功能,美观,安全,无障碍,性能,兼容,体验 前端编程思想 WA doctype: 文档版本 浏览器决定渲染模式 语义化: 所有的标签都有自己的含义,属性 可读性 前端规范 whatwg css显 ...
- 基于 kubeadm 搭建高可用的kubernetes 1.18.2 (k8s)集群一 环境准备
本k8s集群参考了 Michael 的 https://gitee.com/pa/kubernetes-ha-kubeadm-private 这个项目,再此表示感谢! Michael的项目k8s版本为 ...
- C# 数据操作系列 - 19 FreeSql 入坑介绍
0. 前言 前几天FreeSql的作者向我推荐了FreeSql框架,想让我帮忙写个文章介绍一下.嗯,想不到我也能带个货了.哈哈,开个玩笑-看了下觉得设计的挺有意思的,所以就谢了这篇文章. 简单介绍一下 ...
- Java IO(十六)InputStreamReader 和 InputStreamWriter
Java IO(十六)InputStreamReader 和 InputStreamWriter 一.介绍 InputStreamReader 和 OutputStreamWriter 是字节流通向字 ...
- GDBT和XGBoost
https://www.cnblogs.com/pinard/p/6140514.html https://www.cnblogs.com/liuwu265/p/4690486.html https: ...
- & 异步使用场景
异步的使用场景: 1.不涉及共享资源,或对共享资源只读,即非互斥操作 2.没有时序上的严格关系 3.不需要原子操作,或可以通过其他方式控制原子性 4.常用于IO操作等耗时操作,因为比较影响客户体验和使 ...
- 使用python的socket模块进行网络编程
使用socket编程可以分成基于tcp和基于udp,tcp和udp两者最主要的区别是有无面向连接. 基于tcp的socket流程:
- html css javascript实现弹弹球
效果如图: 原创代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...
- 8.0 以上版本 mySQL数据库导致的命令行可连接,NaviCat不可连接的问题
错误代码: client does not support authentication 原因: 没有开启Mysql的远程连接配置 解决办法: 1 使用命令行进入数据库 C:\Users\wushao ...
- Java实现 LeetCode 792 自定义字符串排序(暴力)
792. 匹配子序列的单词数 给定字符串 S 和单词字典 words, 求 words[i] 中是 S 的子序列的单词个数. 示例: 输入: S = "abcde" words = ...