博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一头扎进Shiro-集成Web
阅读量:3977 次
发布时间:2019-05-24

本文共 7049 字,大约阅读时间需要 23 分钟。

  本篇文章我们将讲解如何通过shiro实现对web请求的权限控制。

  首先向大家展示下shiro.ini文件的信息:

[main]authc.loginUrl=/loginroles.unauthorizedUrl=/unauthorized.jspperms.unauthorizedUrl=/unauthorized.jsp[users]java=123456,adminjack=123,teachermarry=234[roles]admin=user:createteacher=student:*[urls]/login=anon/admin=authc/student=roles[teacher]/teacher=perms["user:create"]
        
通过shiro官网,我们可以发现urls配置的请求对应的filter处理:

Filter Name Class
anon
authc
authcBasic
logout
noSessionCreation
perms
port
rest
roles
ssl
user
 
  /login的请求权限为匿名权限,解释为任何人都可以访问;/admin的请求权限为Form表单的身份验证通过权限,解释为只有登录成功的用户才可以访问/admin请求;/student的请求权限为用户必须为teacher角色;/teacher的请求权限为用户必须拥有user:create的权限。

   验证/login请求的authc控制

  我们直接在浏览器输入“http://localhost:8080/ShiroWeb/admin”请求地址,因为用户身份尚未验证通过,所以会跳转到“http://localhost:8080/ShiroWeb/login”登录页面。
  在登录页面,我们使用marry的用户名、密码登录成功后,再次输入“http://localhost:8080/ShiroWeb/admin”可以跳转到目标地址,证明/login请求的authc控制成功。

   验证/role请求的roles[teacher]控制

  在使用marry用户名登录成功后,在浏览器输入“http://localhost:8080/ShiroWeb/student”请求地址,浏览器会跳转到unauthorized.jsp页面,因为marry用户不是teacher权限,所以不可以访问/student请求。
  我们回到“http://localhost:8080/ShiroWeb/login”登录页面,使用jack用户名、密码登录成功后,再次输入“http://localhost:8080/ShiroWeb/student”可以跳转到目标地址,因为jack是teacher角色,teacher角色可以访问/student请求。

   验证/teacher请求的perms["user:create"]控制

  在使用jack用户名登录成功后,在浏览器输入“http://localhost:8080/ShiroWeb/teacher”请求地址,浏览器会跳转到unauthorized.jsp页面,因为jack是teacher角色,teacher角色的权限为student:*,没有user:create的权限,所以不可以访问/teacher请求。
  我们回到“http://localhost:8080/ShiroWeb/login”登录页面,使用java用户名、密码登录成功后,再次输入http://localhost:8080/ShiroWeb/teacher可以跳转到目标地址,因为java用户是admin角色,admin角色的权限为user:create,因此可以访问/teacher请求。
  上面我们讲解了shiro的验证流程,下面向大家分享实现过程。
  首先我们需要在项目的pom文件添加对相关jar包的依赖:
4.0.0
com.tgb.shiro
ShiroWeb
war
0.0.1-SNAPSHOT
ShiroWeb Maven Webapp
http://maven.apache.org
junit
junit
3.8.1
test
javax.servlet
javax.servlet-api
3.1.0
javax.servlet.jsp
javax.servlet.jsp-api
2.3.1
javax.servlet
jstl
1.2
log4j
log4j
1.2.17
commons-logging
commons-logging
1.2
org.apache.shiro
shiro-core
1.2.4
org.apache.shiro
shiro-web
1.2.4
org.slf4j
slf4j-api
1.7.12
ShiroWeb
  其次需要我们在web.xml配置shiro的拦截器,及对/login请求、/admin请求、/student请求的Servlet,配置信息如下:
ShrioWeb
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
org.apache.shiro.web.env.EnvironmentLoaderListener
ShiroFilter
org.apache.shiro.web.servlet.ShiroFilter
ShiroFilter
/*
loginServlet
com.tgb.servlet.LoginServlet
adminServlet
com.tgb.servlet.AdminServlet
studentServlet
com.tgb.servlet.StudentServlet
loginServlet
/login
adminServlet
/admin
studentServlet
/student
          
然后我们分别完成LoginServlet、AdminServlet、StudentServlet的请求处理
package com.tgb.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 {		System.out.println("login doget");		req.getRequestDispatcher("login.jsp").forward(req, resp);	}	@Override	protected void doPost(HttpServletRequest req, HttpServletResponse resp)			throws ServletException, IOException {		System.out.println("login dopost");		String userName = req.getParameter("userName");		String password = req.getParameter("password");		Subject subject = SecurityUtils.getSubject();		UsernamePasswordToken token = new UsernamePasswordToken(userName,				password);		try {			subject.login(token);			resp.sendRedirect("success.jsp");		} catch (Exception e) {			e.printStackTrace();			req.setAttribute("errorInfo", "用户名或密码错误");			req.getRequestDispatcher("login.jsp").forward(req, resp);		}	}}
package com.tgb.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class AdminServlet extends HttpServlet {	private static final long serialVersionUID = 1L;	public AdminServlet() {		super();	}	protected void doGet(HttpServletRequest request,			HttpServletResponse response) throws ServletException, IOException {		System.out.println("admin do get");	}	protected void doPost(HttpServletRequest request,			HttpServletResponse response) throws ServletException, IOException {		System.out.println("admin do post");	}}
package com.tgb.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class StudentServlet extends HttpServlet {	private static final long serialVersionUID = 1L;	public StudentServlet() {		super();	}	protected void doGet(HttpServletRequest request,			HttpServletResponse response) throws ServletException, IOException {		System.out.println("student do get");	}	protected void doPost(HttpServletRequest request,			HttpServletResponse response) throws ServletException, IOException {		System.out.println("student do post");	}}
  项目中用到的相关jsp比较简单,就不在博客上贴了,大家可以自定义创建。
  到此,我们详细讲解了shiro对web请求权限控制的验证流程及实现过程,希望可以帮助大家进一步了解shiro框架。

转载地址:http://dokui.baihongyu.com/

你可能感兴趣的文章
线程安全的单例模式
查看>>
fastjson深度源码解析- 序列化(五) - json内部注册序列化解析
查看>>
fastjson深度源码解析- 序列化(六) - json特定序列化实现解析
查看>>
fastjson深度源码解析- 词法和语法解析(二) - 基础类型实现解析
查看>>
fastjson深度源码解析- 词法和语法解析(三) - 针对对象实现解析
查看>>
fastjson深度源码解析- 反序列化(一) - 反序列化解析介绍
查看>>
fastjson深度源码解析- 反序列化(二) - 内部注册反序列化解析
查看>>
通过爱效率网站获取百度统计数据说明
查看>>
百度统计接口调用——登录接口
查看>>
百度统计接口调用——获取站点列表
查看>>
百度统计接口调用——获取站点访问数据
查看>>
Java WEB开发基础知识
查看>>
[Java Web开发系列课程]从0.5开始学习Java Web开发(一):学习大纲
查看>>
birt标签的使用
查看>>
2012年1月8号订火车票历程
查看>>
My97日期控件
查看>>
30分钟3300%性能提升——python+memcached网页优化小记
查看>>
confmgr项目,django,dwz相关问题
查看>>
linux文件共享
查看>>
linux下tar.gz、tar、bz2、zip等解压缩、压缩命令小结(tar包解压)
查看>>