shiro学习(11):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.html=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);}}
}

logoutServlet

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 = "LogoutServlet",urlPatterns = "/logout.html")
public class LogoutServlet 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 {Subject subject = SecurityUtils.getSubject();subject.logout();req.getRequestDispatcher("/login.jsp").forward(req,resp);}
}

RoleServlt

package com.javaweb;import org.apache.shiro.SecurityUtils;
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 = "RoleServlet",urlPatterns = "/role.html")
public class RoleServlet 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 {Subject subject = SecurityUtils.getSubject();subject.logout();req.getRequestDispatcher("/role.jsp").forward(req,resp);}
}

index.jsp

<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!</h2>
<a href="logout.html">退出登录</a>
<shiro:hasPermission name="menu:list">
<a herf="">menu</a>
</shiro:hasPermission>
<shiro:hasRole name="admin">
<a herf="">role</a>
</shiro:hasRole>
</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>

role.jsp

<%--Created by IntelliJ IDEA.User: geyaoDate: 2019/11/30Time: 13:08To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>role.jsp
</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>

运行结果 输入网址

会跳转到

登录错误

登录成功

设置权限,对应用户看到对应功能 root用户

test用户

guest用户

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/423568.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[MOSS开发]:如何使用用户控件

如果是纯手工开发web part&#xff0c;其实还是比较困难的&#xff0c;因为这种类型的web part是以类库的形式出现&#xff0c;没有可视化的界面&#xff0c;完全由代码写出来&#xff0c;包含控件的样式&#xff0c;属性&#xff0c;事件等等。开发过自定义控件的朋友可能会感…

shiro学习(12)No WebApplicationContext found:

java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener or DispatcherServlet registered?org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:252) 解决 web.xml缺少了Spring监听器&#x…

Java 的Runnable和Callable的区别

Runnable和Callable的区别是&#xff0c;(1)Callable规定的方法是call(),Runnable规定的方法是run(). (2)Callable的任务执行后可返回值&#xff0c;而Runnable的任务是不能返回值得(3)call方法可以抛出异常&#xff0c;run方法不可以(4)运行Callable任务可以拿到一个Future对象…

Spring Boot----整合SpringCloud

首先比较一下Zookeeper和Eureka的区别&#xff1f; 1、CAP&#xff1a;C&#xff1a;强一致性&#xff0c;A&#xff1a;高可用性&#xff0c;P&#xff1a;分区容错性(分布式中必须有) CAP理论的核心是&#xff1a;一个分布式系统不可能同时很好的满足一致性&#xff0c;可用性…

[原创]利用Powerdesinger同步数据库的方法说明

本文主要介绍我在工作过程中如果利用PowerDesinger同步数据库设计PDM和物理数据库保持同步。PowerDesinger以下简称PD.我们经常在数据库生成后&#xff0c;在后续的开发中发现数据设计有遗漏&#xff0c;或者是少字段&#xff0c;或者是参照完整性不一致&#xff0c;那么我们都…

shiro学习(13):springMVC结合shiro完成认证

工具idea 先看看数据库 shiro_role_permission 数据 shiro_user shiro_user_role 数据 在pom.xml里面添加 <?xml version"1.0" encoding"UTF-8"?><project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3…

分布式环境下,怎么保证线程安全

转:https://blog.csdn.net/Faker_Wang/article/details/80798732 避免并发 在分布式环境中&#xff0c;如果存在并发问题&#xff0c;那么很难通过技术去解决&#xff0c;或者解决的代价很大&#xff0c;所以我们首先要想想是不是可以通过某些策略和业务设计来避免并发。比如…

用小程序·云开发两天搭建mini论坛丨实战

笔者最近涉猎了小程序相关的知识&#xff0c;于是利用周末时间开发了一款类似于同事的小程序&#xff0c;深度体验了小程序云开发模式提供的云函数、数据库、存储三大能力。关于云开发&#xff0c;可参考文档&#xff1a;小程序云开发。 个人感觉云开发带来的最大好处是鉴权流程…

shiro学习(14):springMVC结合shiro完成认证

工具idea 先看看数据库 shiro_role_permission 数据 shiro_user shiro_user_role 数据 在pom.xml里面添加 <?xml version"1.0" encoding"UTF-8"?><project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3…

mysql聚合函数rollup和cube

转:https://blog.csdn.net/liuxiao723846/article/details/48970443 一、with rollup&#xff1a; with rollup 通常和group by 语句一起使用&#xff0c;是根据维度在分组的结果集中进行聚合操作。——对group by的分组进行汇总。 假设用户需要对N个纬度进行聚合查询操作&am…

在C#里,如何执行cmd里的常用dos命令 (转)

http://blogger.org.cn/blog/more.asp?namenrzj&id4280 using System; using System.Diagnostics; namespace Tipo.Tools.Utility { /// <summary> /// 常用Dos命令操作 /// </summary> public class DosCommand { private Process processnull; pri…

Spring Boot----监控管理

用来监控spring 项目信息的 1、创建项目 1.1 启动项目 转载于:https://www.cnblogs.com/yanxiaoge/p/11400734.html

shiro学习(15):使用注解实现权限认证和后台管理

工具idea 先看看数据库 shiro_role_permission 数据 shiro_user shiro_user_role 数据 在pom.xml里面添加 <?xml version"1.0" encoding"UTF-8"?><project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3…

什么是索引?索引类型有几种,各有什么特点?

转发: 索引是对数据库表中一列或多列的值进行排序的一种结构&#xff0c;例如 employee 表的姓&#xff08;name&#xff09;列。如果要按姓查找特定职员&#xff0c;与必须搜索表中的所有行相比&#xff0c;索引会帮助您更快地获得该信息。 索引是一个单独的、物理的数据库结…

设置Netbeans 6.5为英文界面

进入netbeans 6.5/etc目录&#xff0c;编辑netbeans.conf文件&#xff0c;将其中的#command line switchs下面那行添加启动参数&#xff1a; -J-Duser.languagezh -J-Duser.countryUS变为&#xff1a;netbeans_default_options"-J-client -J-Xss2m -J-Xms32m -J-XX:PermSi…

用小程序·云开发打造功能全面的博客小程序丨实战

用小程序云开发将博客小程序常用功能“一网打尽” 本文介绍mini博客小程序的详情页的功能按钮如何实现&#xff0c;具体包括评论、点赞、收藏和海报功能&#xff0c;这里记录下整个实现过程和实际编码中的一些坑。 评论、点赞、收藏功能 实现思路 实现文章的一些操作功能&#…

shiro学习(16):使用注解实现权限认证和后台管理二

工具idea 先看看数据库 shiro_role_permission 数据 shiro_user shiro_user_role 数据 目录结构 在pom.xml里面添加 <?xml version"1.0" encoding"UTF-8"?><project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http…

Javascript玩转Prototype(一)——先谈C#原型模式

在《Javascript玩转继承&#xff08;二&#xff09;》中&#xff0c;我使用了原型继承法来实现Javascript的继承&#xff0c;那原型究竟奥秘何在。在这篇文章中&#xff0c;我就主要针对原型来展开讨论。 抛开Javascript&#xff0c;我们先来看我们熟悉的常规的面向对象语言。…

hive的row_number()、rank()和dense_rank()的区别以及具体使用

参考:https://blog.csdn.net/qq_20641565/article/details/52841345?locationNum5&fps1 2016年10月17日 20:05:21 阅读数&#xff1a;4931 row_number()、rank()和dense_rank()这三个是hive内置的分析函数&#xff0c;下面我们来看看他们的区别和具体的使用案例。 首先…

HDU 1176 免费馅饼 (动态规划、另类数塔)

免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 76293 Accepted Submission(s): 26722 Problem Description 都说天上不会掉馅饼&#xff0c;但有一天gameboy正走在回家的小径上&#xff0c;忽然天上掉…