工具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.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>geyaoshiro</groupId><artifactId>geyaoshiro</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><name>geyaoshiro Maven Webapp</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.7</maven.compiler.source><maven.compiler.target>1.7</maven.compiler.target></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-core</artifactId><version>1.2.3</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.6.1</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.6.1</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>4.3.11.RELEASE</version></dependency><dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.2</version></dependency><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><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.2.8.RELEASE</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-spring</artifactId><version>1.4.0</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.2.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>4.2.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>4.2.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.2.8.RELEASE</version></dependency></dependencies><build><finalName>geyaoshiro</finalName><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><plugin><artifactId>maven-clean-plugin</artifactId><version>3.1.0</version></plugin><!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.22.1</version></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>3.2.2</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.5.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version></plugin></plugins></pluginManagement></build>
</project>

引入插件easyui

AuthorExceptionHandle

package com.geyao.shiro.exception;import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.servlet.ModelAndView;@ControllerAdvice
public class AuthExceptionHandler {@ExceptionHandler({UnauthorizedException.class})@ResponseStatus(HttpStatus.UNAUTHORIZED)public ModelAndView processUnauthenticatedException(NativeWebRequest request,UnauthorizedException e){return new ModelAndView("error","exception",e);}
}

controller包

LoginController

package controller;import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpServletRequest;@Controller
public class LoginController {@RequestMapping("gologin.html")public String goLogin(){Subject subject=SecurityUtils.getSubject();subject.logout();return "login";}@RequestMapping("logout.html")public String logout(){return "login";}@RequestMapping("login.html")public String login(String username, String password, HttpServletRequest request) {System.out.println("nihao");Subject subject = SecurityUtils.getSubject();UsernamePasswordToken token = new UsernamePasswordToken(username, password);try {subject.login(token);return "redirect:index.html";}catch (AuthenticationException e){e.printStackTrace();request.setAttribute("error","用户名或者密码错误");return "login";}}
}

PageController

package controller;import com.geyao.shiro.mvc.MyShiroFilterFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class PageController {@RequestMapping("index")public String index(){return "index";}@RequestMapping("error.html")public String error(){return "error";}@RequestMapping("do{path}.html")public String page(@PathVariable String path, Model model){model.addAttribute("path",path);return "test";}@Autowiredprivate MyShiroFilterFactory shiroFilterFactory;@RequestMapping("update.html")public String update(){shiroFilterFactory.update();return "index";}}

MenuController

package controller;import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
@RequestMapping("/menu")
public class MenuController {@RequestMapping("list.html")public String list(){return "menu_list";}@RequestMapping("go_edit.html")@RequiresPermissions("menu:edit")public String goEdit(){return "menu_edit";}
}

com.geyao.shiro.mvc包

MyshiroFilterFactory

package controller;import com.geyao.shiro.mvc.MyShiroFilterFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class PageController {@RequestMapping("index")public String index(){return "index";}@RequestMapping("error.html")public String error(){return "error";}@RequestMapping("do{path}.html")public String page(@PathVariable String path, Model model){model.addAttribute("path",path);return "test";}@Autowiredprivate MyShiroFilterFactory shiroFilterFactory;@RequestMapping("update.html")public String update(){shiroFilterFactory.update();return "index";}}

log4j.properties

### \u914D\u7F6E\u6839 ###
log4j.rootLogger = error,console ,fileAppender,dailyRollingFile,ROLLING_FILE,MAIL,DATABASE### \u8BBE\u7F6E\u8F93\u51FAsql\u7684\u7EA7\u522B\uFF0C\u5176\u4E2Dlogger\u540E\u9762\u7684\u5185\u5BB9\u5168\u90E8\u4E3Ajar\u5305\u4E2D\u6240\u5305\u542B\u7684\u5305\u540D ###
log4j.logger.org.apache=error
log4j.logger.java.sql.Connection=error
log4j.logger.java.sql.Statement=error
log4j.logger.java.sql.PreparedStatement=error
log4j.logger.java.sql.ResultSet=error### \u914D\u7F6E\u8F93\u51FA\u5230\u63A7\u5236\u53F0 ###
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern =  %d{ABSOLUTE} %5p %c{1}:%L - %m%n

shiro-web.ini

[users]
root = secret,admin
guest = guest,guest
test = 123456,guest,test[roles]
admin = *
guest=user:list
test=menu:list,menu:add

spring-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:classpath="http://www.springframework.org/schema/c"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"><context:component-scan base-package="com.geyao.shiro"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter></context:component-scan><bean id="iniRealm" class="org.apache.shiro.realm.text.IniRealm"><constructor-arg name="resourcePath" value="classpath:shiro-web.ini"/></bean><bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"><property name="securityManager" ref="securityManager"></property></bean><bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"><property name="realm" ref="iniRealm"></property></bean><bean id="shiroFilter" class="com.geyao.shiro.mvc.MyShiroFilterFactory"><property name="securityManager" ref="securityManager"></property><property name="loginUrl" value="/gologin.html"></property><property name="successUrl" value="/index.html"></property><property name="unauthorizedUrl" value="/error.html"></property><property name="filterChainDefinitions"><value>[urls]/login.html=anon/gologin.html=anon/index.html=authc/role.html=authc,roles[admin]/menu/**=authc,roles[admin,test]</value></property><property name="filters"><map><entry key="roles"><bean class="com.geyao.shiro.mvc.MyShiroFilter"></bean></entry></map></property></bean>
</beans>

error.jsp

<%--Created by IntelliJ IDEA.User: geyaoDate: 2019/11/30Time: 14:05To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
权限不足
</body>
</html>

index.jsp

<%--Created by IntelliJ IDEA.User: geyaoDate: 2019/11/30Time: 13:53To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title><meta charset="UTF-8"><link href="themes/black/easyui.css" rel="stylesheet" /><link href="themes/icon.css" rel="stylesheet" /><script src="jquery.min.js"></script><script src="jquery.easyui.min.js"></script><script src="locale/easyui-lang-jp.js"></script>
</head>
<!--<body class="easyui">
<div date-options="region:'north'" style="height: 50px"><a href="logout.html">退出</a>
</div>
<div data-options="region:'west'" title="菜单" style="width: 10%;"></div>
<div data-options="region:'center'" title="首页"></div> -->
<body class="easyui-layout"><div data-options="region:'north'" style="height:50px;"><a href="logout.html">退出</a>
</div>
<div data-options="region:'west'" title="菜单" style="width:10%;"><li><span>系统管理</span></li><ul class="easyui-tree" id="leftMenu"><li data-options="url:'/menu/list.html'"><span>菜单管理</span></li><li data-options="url:'/menu/list.html'"><span>角色管理</span></li><li data-options="url:'/menu/list.html'"><span>用户管理</span></li></ul>
</div><div data-options="region:'center'" title="首页"><div id="tabs" class="easyui-tabs"></div>
</div>
<script type="text/javascript">$(function(){$("#leftMenu").tree({onClick:function (node) {if ($("leftMenu").tree("isLeaf",node.target)) {var tabs = $("#tabs");var tab = tabs.tabs("getTab", node.text);if (tab) {tabs.tabs("select", node.text)} else {tabs.tabs("add", {title: node.text,href: node.url,closable: true})}}}})})
</script>
</body >
</html>

login.jsp

<%--Created by IntelliJ IDEA.User: geyaoDate: 2019/11/30Time: 13:46To 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>

menu_list

<%--Created by IntelliJ IDEA.User: geyaoDate: 2019/11/30Time: 20: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>
菜单页
</body>
</html>

test.jsp

<%--Created by IntelliJ IDEA.User: geyaoDate: 2019/12/1Time: 16:50To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
${path}
</body>
</html>

menu_edit

<%--Created by IntelliJ IDEA.User: geyaoDate: 2019/11/30Time: 21:33To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
编辑
</body>
</html>

springMVC-servlet

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"><mvc:annotation-driven /><!-- 定义要扫描 controller的包--><context:component-scan base-package="controller"></context:component-scan><!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 --><!--指定视图解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 视图的路径 --><property name="prefix" value="/WEB-INF/"/><!-- 视图名称后缀  --><property name="suffix" value=".jsp"/></bean><bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"></bean><bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"depends-on="lifecycleBeanPostProcessor"></bean>
</beans>

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>contextConfigLocation</param-name><param-value>classpath:spring-config.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>springMVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class></servlet>
<servlet-mapping><servlet-name>springMVC</servlet-name><url-pattern>*.html</url-pattern>
</servlet-mapping><filter><filter-name>shiroFilter</filter-name><filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class></filter><filter-mapping><filter-name>shiroFilter</filter-name><url-pattern>*.html</url-pattern></filter-mapping>
</web-app>

test.html

运行结果

shiro学习(23):动态添加验证规则3相关推荐

  1. shiro学习(22):动态添加验证规则2

    工具idea 先看看数据库 shiro_role_permission 数据 shiro_user shiro_user_role 数据 目录结构 在pom.xml里面添加 <?xml vers ...

  2. shiro学习(21):动态添加验证规则1

    工具idea 先看看数据库 shiro_role_permission 数据 shiro_user shiro_user_role 数据 目录结构 在pom.xml里面添加 <?xml vers ...

  3. ElementUI的DateTimePicker组件添加验证规则以及限制选择范围

    场景 DateTimePicker是ElementUI的日期和时间共存的选择器. 有时需要限制选择器的选择范围. 效果 实现 实现需要使用组件的picker-option属性,具体参照官方文档: ht ...

  4. shiro学习(20): 自定义过滤规则

    工具idea 先看看数据库 shiro_role_permission 数据 shiro_user shiro_user_role 数据 目录结构 在pom.xml里面添加 <?xml vers ...

  5. 后盾网lavarel视频项目---自定义验证和自定义验证规则

    后盾网lavarel视频项目---自定义验证和自定义验证规则 一.总结 一句话总结: 1.自定义验证就是用的自定义验证请求类:php artisan make:request AdminPost 2. ...

  6. PHP动态验证,php-动态更改验证规则

    我正在处理包含用户数据,特别是电话号码字段的表单.通常不需要电话号码,因此模型中唯一的验证规则是usphone规则.但是,如果用户正在提交此表格,则电话号码变得必不可少.我以为我可以在运行中简单地添加 ...

  7. v-for 循环生成多个表单元素 给动态生成的表单元素绑定值并且添加校验规则

    需求:点击新增按钮 能不断生成下级部门,所以我再data中定义了一个变量 空数组 subordinateDepartmentNum:[] 默认值设置为[] 给增加按钮添加点击事件 每点击一次按钮  s ...

  8. php字段验证规则,ThinkPHP 自动验证及验证规则详解

    ThinkPHP 自动验证及验证规则详解 ThinkPHP 自动验证 ThinkPHP 内置了数据对象的自动验证功能来完成模型的业务规则验证.自动验证是基于数据对象的,而大多情况下数据对象是基于 $_ ...

  9. shiro学习一 (开涛的跟我学系列 ) 身份验证

    原文来自于http://jinnianshilongnian.iteye.com/blog/2018398 1.简介 Apache Shiro是Java的一个安全框架.可以帮助我们完成:认证.授权.加 ...

最新文章

  1. 异步实现,查询大量数据时的加载
  2. 1031 Hello World for U
  3. c语言第四作业答案,C语言第一次作业及答案
  4. 疯狂ios讲义之实例:通过旋转手势旋转图片
  5. Python字符串isdigit()
  6. 智能判断图片中是否存在某物体_如果给猫披上象皮,神经网络将作何判断?
  7. MFC调用dos窗口使用printf,cout打印输出
  8. ArcEngine 渲染的使用【转载】
  9. python加法赋值运算符为_Python中什么是算术运算符、赋值运算符和复合运算符?...
  10. 全国计算机三级嵌入式资料
  11. IDEA汉化包安装和卸载
  12. RJ45和RJ11的线序以及用途
  13. shell脚本语法教程
  14. 陶森大学计算机专业收入水平,2016PayScale美国大学计算机专业本科毕业生薪酬排名...
  15. 华为鸿蒙家电物联网,华为“鸿蒙”来了:本身是为了做物联网,将比安卓速度快60%...
  16. 如何把视频转换为gif动图
  17. sql 内连接,左连接,右连接,全连接
  18. win7注册表常用设置
  19. Python分析薛之谦与李雨桐互撕微博
  20. week6:Diagnosing Bias vs. Variance难点记录

热门文章

  1. (转载)Android两种Tab分页的方式:TabActivity和ActivityGroup以及Android项目几种常见的应用架构...
  2. 我所知的javascript之prototype
  3. 计算机安全概论论文,计算机安全探讨论文毕业论文(7篇).doc
  4. boost python导出c++ map_使用Boost生成的Python模块:与C++签名不匹配
  5. html字符串转换jsx,javascript – 将React.element转换为JSX字符串
  6. phoenix的元数据一般存在哪里_Phoenix的一些问题
  7. 自动化测试遇到的难点_自动化测试过程中遇到的问题主要有什么?
  8. JavaScript将负数转换为正数
  9. 微信小程序入门一: 简 介、文本、事件、样式
  10. 终端乱码的终极解决方案