shiro学习(10):servelet实现权限认证一
工具idea
先看看数据库
shiro_role_permission
数据
shiro_user
shiro_user_role
数据
在pom.xml里面添加
<dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-web</artifactId><version>1.2.3</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.0.1</version><scope>provided</scope></dependency>
看看目录结构
shiro-web.ini
[users]
root = secret,admin
guest = guest,guest
test = 123456,guest,test[roles]
admin = *
guest=user:list
test=menu:list,menu:add[urls]
/login.html=anon
/index.html=authc
/role=authc,roles[admin]
/menu/**=authc,roles[admin],perms[menu:*]
com.javaweb
IndexServlet
package com.javaweb;import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;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 java.io.IOException;
@WebServlet(name = "indexServlet",urlPatterns = "/index.html")
public class IndexServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doPost(req,resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.getRequestDispatcher("/index.jsp").forward(req, resp);}
}
LoginSevlet
package com.javaweb;import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;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 java.io.IOException;@WebServlet(name = "loginServlet",urlPatterns = "/login.html")
public class LoginServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doPost(req,resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {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("/index.html");}catch (AuthenticationException e){e.printStackTrace();req.setAttribute("error","用户名或者密码错误");req.getRequestDispatcher("/login.jsp").forward(req,resp);}}
}
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
login.jsp
<%--Created by IntelliJ IDEA.User: geyaoDate: 2019/11/29Time: 21:09To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<form action="login.html" method="post">用户名:<input type="text" name="username"/><br/>密码:<input type="password" name="password"/><br/><input type="submit" value="登录"/>${error}
</form>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param><param-name>shiroEnvirinmentClass</param-name><param-value>org.apache.shiro.web.env.IniWebEnvironment</param-value>
</context-param><context-param><param-name>shiroConfigLocations</param-name><param-value>classpath:shiro-web.ini</param-value></context-param><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>*.html</url-pattern></filter-mapping>
</web-app>
运行结果 输入网址
会跳转到
登录错误
登录成功
shiro学习(10):servelet实现权限认证一相关推荐
- shiro利用mysql动态授权_SpringBoot+Shiro学习之数据库动态权限管理和Redis缓存
发现问题,需找解决思路. 之前我们整合Shiro,完成了登录认证和权限管理的实现,登录认证没什么说的,需要实现AuthorizingRealm中的doGetAuthenticationInfo方法进行 ...
- Shiro中进行角色与权限认证流程
场景 使用Shiro的JdbcRealm实现查询数据库进行身份认证: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/9010599 ...
- shiro学习(11):servelet实现权限认证二
工具idea 先看看数据库 shiro_role_permission 数据 shiro_user shiro_user_role 数据 在pom.xml里面添加 <dependency> ...
- shiro学习(3):用户权限
工具idea 首先创建maven项目 配置文件 <?xml version="1.0" encoding="UTF-8"?><project ...
- shiro 学习(一)
title: shiro学习(一) date: 2020-11-13 tags: spring springboot shiro categories: spring springboot shiro ...
- SpringBoot学习:整合shiro(身份认证和权限认证),使用EhCache缓存
一.在pom中引入依赖jar包 1 <properties> 2 <shiro.version>1.3.2</shiro.version> 3 </prope ...
- springboot shiro和freemarker集成之权限控制完全参考手册(跳过认证,登录由三方验证,全网首发)...
本文主要考虑单点登录场景,登录由其他系统负责,业务子系统只使用shiro进行菜单和功能权限校验,登录信息通过token从redis取得,这样登录验证和授权就相互解耦了. 用户.角色.权限进行集中式管理 ...
- Shiro学习总结(10)——Spring集成Shiro
1.引入Shiro的Maven依赖 [html] view plaincopy <!-- Spring 整合Shiro需要的依赖 --> <dependency> <gr ...
- Shiro学习笔记_02:shiro的认证+shiro的授权
Shiro 学习笔记 本文基于B站UP主[编程不良人]视频教程[2020最新版Shiro教程,整合SpringBoot项目实战教程]进行整理记录,仅用于个人学习交流使用. 视频链接:https://w ...
最新文章
- 张勇:做一个透明经济体的CEO
- mysql session变量_mysql 系统变量和session变量
- Leetcode 160. 相交链表 解题思路及C++实现
- 初识Zookeeper
- C++调用方式 入栈顺序
- revman软件_meta分析概述及RevMan软件安装教程
- Python快速计算Fibonacci数列中第n项的方法
- 稳定性测试怎么做,这篇文章彻底讲透了
- 自动驾驶中的深度学习
- Xv6 traps and system calls
- 项目过程管理(五)需求文档
- c语言数组的地址传递,c语言函数传递数组_c语言函数数组地址传递没有输出
- 2021SC@SDUSC Zxing开源代码(十四)Aztec二维码(三)
- HTTP 417 错误 – 预期结果失败 (Expectation failed)
- CAN总线物理值与原始值
- 如何使定时器1ms进入中断一次?
- 思科交换机创建子接口
- 第二个版图项目:CD4511
- 批量转换用户的共存模式--Skype for Business to Teams
- No qualifying bean of type xxx found for dependency expected at least 1 bean which qualifies as ...