Shiro集成Web
Shiro不仅可以集成到web中,也可以集成Spring。
在WEB中添加Shrio支持
如果要想在web中使用Shrio,需要在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>
在默认情况下,Shrio会从/WEB-INF/shiro.ini和classpath两个目录下去寻找ini配置文件。如果需要指定特定的位置,也可以通过添加context-parm的方式指定位置。
<context-param>
<param-name>shiroConfigLocations</param-name>
<param-value>YOUR_RESOURCE_LOCATION_HERE</param-value>
</context-param>
由于一般情况下,我们利用shiro来进行权限控制的时候,都希望让Shiro来拦截所有的请求,所以,最好是把shiro的mapping放在最前面。
配置监听器和过滤器之后,在启动的时候,EnviromentLoaderListener会实例化一个WebEnvironment实例,其中包括SecurityManager。并且是绑定到当前Servlet上下文的。如果需要获取当前的WebEnvironment的话,可以通过WebUtil.getRequiredWebEnvironment(servletContext)。
WEB中INI配置
在ini配置文件中,除了有[mian] [users] [roles]之外,还有一个节,叫做:[urls]
在[urls]中,你可以配置点对点的过滤器,提供了更加灵活的功能。
格式:
_URL_Ant_Path_Expression_ = _Path_Specific_Filter_Chain_
例子:
...
[urls] /index.html = anon
/user/create = anon
/user/** = authc
/admin/** = authc, roles[administrator]
/rest/** = authc, rest
/remoting/rpc/** = authc, perms["remote:invoke"]
其中,左边表示的请求的资源路径,以当前的上下文的根目录开始,也就是HttpServletRequest.getContextPath()的值。
两个星号代表后面是什么都可以,也可以还有几层路径,也就是说:
/user/name/age这个路径也会被/user/**过滤。但是需要注意的是,这些请求的配置是有先后顺序的,第一个匹配的会被调用。
等号右边的表示过滤器链。这些过滤器的名字表示的是在[main]中定义的过滤器,Shiro也自带了一些可以直接使用的过滤器。
在每个过滤器中还可以通过中括号来添加选项,过滤器的唤醒也是跟定义的顺序一样。
自动启动的过滤器
在Shrio自动启动的过滤器的名字都配置在org.apache.shiro.web.filter.mgt.DefaultFilter 这个枚举类中。
| 名字 | 类 | 作用 | 
| anon | org.apache.shiro.web.filter.authc.AnonymousFilter | 不进行任何的安全check, 允许是一个匿名用户 | 
| authc | org.apache.shiro.web.filter.authc.FormAuthenticationFilter | 需要是认证过的,否则的话重定向loginUrl中定义的路径 | 
| authcBasic | org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter | 需要是认证过的,如果没有的话则弹出登陆的对话框 | 
| logout | org.apache.shiro.web.filter.authc.LogoutFilter | 会立刻退出,并且重定向到redirectUrl中定义的url | 
| noSessionCreation | org.apache.shiro.web.filter.session.NoSessionCreationFilter | 不创建session | 
| perms | org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter | 是否有指定的权限 | 
| port | org.apache.shiro.web.filter.authz.PortFilter | 需要是通过指定的端口访问,如果不是的话则通过指定的port访问 | 
| rest | org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter | 
 会自动根据请求的方法构建一个权限字符串来验证是否有这个权限,head->read get->read put->update post->create 等等。  | 
| roles | org.apache.shiro.web.filter.authz.RolesAuthorizationFilter | 如果有指定的角色 | 
| ssl | org.apache.shiro.web.filter.authz.SslFilter | 只有是通过https访问的,否则自动跳转到https的443端口 | 
| user | org.apache.shiro.web.filter.authc.UserFilter | 如果是登陆了或者记住了用户则可以访问,否则重定向到login.url | 
他们分别可以实现不同的功能,比如:anon可以用来在不执行任何安全检测的前提下执行某些操作。
[main]
...
# Notice how we didn't define the class for the FormAuthenticationFilter ('authc') - it is instantiated and available already:
authc.loginUrl = /login.jsp
... [urls]
...
# make sure the end-user is authenticated. If not, redirect to the 'authc.loginUrl' above,
# and after successful authentication, redirect them back to the original account page they
# were trying to view:
/account/** = authc
...
如果要想让某个过滤器无效的话,最简单的方式是把他们移除出过滤器链,但是,还有一种不该表过滤器链的方式,那就是通过他们从OncePerRequestFilter中继承的功能,设置enable为false。
[main]
...
# configure Shiro's default 'ssl' filter to be disabled while testing:
ssl.enabled = false [urls]
...
/some/path = ssl, authc
/another/path = ssl, roles[admin]
...
基于表单的登陆
在web中,authc默认是一个FormAuthenticationFilter,它支持从表单中读取登陆信息和记住我。
但是用户名需要是username,密码需要是password,记住我需要是一个rememberMe的checkbox。
<form ...> Username: <input type="text" name="username"/> <br/>
Password: <input type="password" name="password"/>
...
<input type="checkbox" name="rememberMe" value="true"/>Remember Me?
...
</form>
如果不想使用默认的名字,则可以通过在ini中配置。
[main]
...
authc.loginUrl = /whatever.jsp
authc.usernameParam = somethingOtherThanUsername
authc.passwordParam = somethingOtherThanPassword
authc.rememberMeParam = somethingOtherThanRememberMe
...
JSP/GSP标签
如果要在JSP页面中使用Shiro的标签的话,则需要shiro-web.jar的支持。还需要添加下面的taglib。
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<shiro:guest>:只有当当前用户是游客的时候,才会显示标签中的内容。
<shiro:user>:只有当当前用户是登陆过的或记住了,才会显示标签中的内容,和guest相反。
<shiro:authenticated>:只有当是认证过的,才会显示当前标签中中的内容
<shiro:notAuthenticated>:只有当是没有认证过的,才会显示当前标签中中的内容跟authenticated相反。
<shiro:principal>:会打印出当前用户的用户名。
<shiro:principal type="类型">:会按指定类型打印出当前用户的用户名。
<shiro:principal propertype="名字">:打印用户中的某个属性。
<shiro:hasRole>:只有当当前用户有这个角色的时候才会显示标签内的内容。
<shiro:lacksRole name="角色名">:只有当当前用户没有这个角色的时候才会显示标签内的内容。
<shiro:hasAnyRole name="角色1,角色2...">:只有当当前用户有其中某个角色的时候才会显示标签内的内容。
<shiro:hasPermission>:只有当当前用户有这个权限的时候才会显示标签内的内容。
<shiro:lacksPermission name="角色名">:只有当当前用户没有这个权限的时候才会显示标签内的内容。
下面就通过一个简单的例子,来演示下在web中的权限控制。
首先编写ini文件
[main]
authc.loginUrl=/login.jsp
[users]
fuwh=123456 [urls]
/login.jsp=anon
/static/**=authc
其中,/login.jsp是不用登陆就可以访问的,/static/目录下的所有资源访问都需要是登陆状态,
如果没有登陆的话,则会跳转到login.jsp页面。login.jsp页面内容如下:
<%@ 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 type="text" name="username"/><br/>
密 码:<input type="password" name="password"/><br/>
<input type="checkbox" name="rememberMe" value="true"/>记住我  
<input type="submit" value="登陆">
</form>
</body>
</html>
编写LoginServlet
package com.fuwh.servlet; 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.UsernamePasswordToken;
import org.apache.shiro.subject.Subject; public class LoginServlet extends HttpServlet{ /**
*
*/
private static final long serialVersionUID = 1L; @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("doGet");
// this.doPost(req, resp);
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("doPost");
Subject subject=SecurityUtils.getSubject();
UsernamePasswordToken token=new UsernamePasswordToken(req.getParameter("username"),req.getParameter("password"));
try {
subject.login(token);
resp.sendRedirect(req.getContextPath()+"/static/success.jsp");
} catch (Exception e) {
// TODO: handle exception
resp.sendRedirect(req.getContextPath()+"/static/success.jsp");
} } }
在这个servlet中,不管登陆成功都跳转到/static/success.jsp页面,但是如果登陆不成功的话,则会被shiro拦截,跳转到login.jsp页面去了。
点此查看源码:https://github.com/oukafu/shiro
Shiro集成Web的更多相关文章
- Shiro集成web环境[Springboot]-认证与授权
		
Shiro集成web环境[Springboot]--认证与授权 在登录页面提交登陆数据后,发起请求也被ShiroFilter拦截,状态码为302 <form action="${pag ...
 - Shiro学习笔记四(Shiro集成WEB)
		
这两天由于家里出了点事情,没有准时的进行学习.今天补上之前的笔记 -----没有学不会的技术,只有不停找借口的人 学习到的知识点: 1.Shiro 集成WEB 2.基于角色的权限控制 3.基于权限的控 ...
 - Shiro集成web环境[Springboot]-基础使用
		
Shiro集成web环境[Springboot] 1.shiro官网查找依赖的jar,其中shiro-ehcache做授权缓存时使用,另外还需要导入ehcache的jar包 <dependenc ...
 - Shiro 集成 Web
		
Web 集成 Shiro 的练习项目. Servlet + Shiro 项目结构 新建Maven项目,pom配置如下 <project xmlns="http://maven.apac ...
 - 【Shiro】Apache Shiro架构之集成web
		
Shiro系列文章: [Shiro]Apache Shiro架构之身份认证(Authentication) [Shiro]Apache Shiro架构之权限认证(Authorization) [Shi ...
 - Shiro在Web环境下集成Spring的大致工作流程
		
1,Shiro提供了对Web环境的支持,其通过一个 ShiroFilter 入口来拦截需要安全控制的URL,然后进行相应的控制. ①配置的 ShiroFilter 实现类为:org.spri ...
 - Apache Shiro 集成-Cas
		
http://blog.csdn.net/peterwanghao/article/details/8825008 Shiro集成CAS是在1.2版本里新增的功能. Shiro-cas模块将应用作为C ...
 - cas+tomcat+shiro实现单点登录-4-Apache Shiro 集成Cas作为cas client端实现
		
目录 1.tomcat添加https安全协议 2.下载cas server端部署到tomcat上 3.CAS服务器深入配置(连接MYSQL) 4.Apache Shiro 集成Cas作为cas cli ...
 - spring-boot-2.0.3应用篇 - shiro集成
		
前言 上一篇:spring-boot-2.0.3源码篇 - 国际化,讲了如何实现国际化,实际上我工作用的模版引擎是freemaker,而不是thymeleaf,不过原理都是相通的. 接着上一篇,这一篇 ...
 
随机推荐
- RESTFul API设计指南及使用说明
			
RESTFul API设计指南及使用说明 一. 协议 API与用户的通信协议,使用HTTP协议. 二. 域名 应尽量将API部署在专用域名之下(http://api.example.com) 也可以将 ...
 - exports
			
暴露函数 var bar = require("./bar.js"); var msg = "你好"; var info = "呵呵"; f ...
 - [USACO4.1]麦香牛块Beef McNuggets
			
https://www.luogu.org/problemnew/show/P2737 给出n个数ai,求这n个数不能累加出的最大的数 最大的数无限大或能凑出所有的自然数则输出0 n<=10,a ...
 - 在wamp集成环境下安装laravel5.2.*框架
			
虽然官方一直强烈推荐使用homestead,但是这个相对麻烦一点,所以我还是选择使用wamp集成开发环境.还有这里我只讲解windows系统下的安装,其他例如mac或linux就不写了,此文章是面向刚 ...
 - css3动画transition详解
			
一.transition-property 语法: transition-property : none | all | [ <IDENT> ] [ ',' <IDENT> ] ...
 - CSS你所不知的伪元素的用法
			
你所不知的 CSS ::before 和 ::after 伪元素用法 博客分类: Div / Css / XML / HTML5 CSS 有两个说不上常用的伪类 :before 和 :after, ...
 - Python 简单聊天室
			
#coding=utf-8 from socket import * from threading import Thread import time udpSocket = socket(AF_IN ...
 - LeetCode & Q13-Roman to Integer-Easy
			
Math String Description: Given a roman numeral, convert it to an integer. Input is guaranteed to be ...
 - JS 上传图片时实现预览
			
网页中一张图片可以这样显示: <img src="http://www.letuknowit.com/images/wg.png"/>也可以这样显示:<img s ...
 - kubernetes入门(09)kubernetes的命令
			
Help执行<kubectl>或<kubectl help> | <kubectl --help>获得命令的帮助信息.kubectl的帮助信息.示例相当详细,而且简 ...