工具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>

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 org.springframework.stereotype.Controller;
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";}}

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";}
}

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"><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="org.apache.shiro.spring.web.ShiroFilterFactoryBean"><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</value></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>
</head>
<body>
首页
</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>

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>

运行结果

回车

回车

登录

root登录

输入网址

权限不同,可进入

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

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

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

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

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

  3. shiro学习(11):servelet实现权限认证二

    工具idea 先看看数据库 shiro_role_permission 数据 shiro_user shiro_user_role 数据 在pom.xml里面添加 <dependency> ...

  4. shiro学习(10):servelet实现权限认证一

    工具idea 先看看数据库 shiro_role_permission 数据 shiro_user shiro_user_role 数据 在pom.xml里面添加 <dependency> ...

  5. Python学习笔记--10.Django框架快速入门之后台管理admin(书籍管理系统)

    Python学习笔记--10.Django框架快速入门之后台管理 一.Django框架介绍 二.创建第一个Django项目 三.应用的创建和使用 四.项目的数据库模型 ORM对象关系映射 sqlite ...

  6. authorize(基于注解的权限认证框架)

    一.是什么 很多项目都会用到权限管理,目前流行的权限框架(Apache Shiro,Spring Security等)在使用的时候都觉得很繁琐,特别是在一些小型的项目中.有时候我会想,如果通过注解的方 ...

  7. 《白帽子讲WEB安全》学习笔记之第9章 认证与会话管理

    第9章 认证与会话管理 9.1 who am i? 认证包含了身份和身份认证两层含义. q  身份-我是谁? q  身份认证-这就是我. 认证的目的就是为了认出用户是谁?而授权的目的是为了决定用户能够 ...

  8. 再见Spring Security!推荐一款功能强大的权限认证框架,用起来够优雅!

    ‍ ‍在我们做SpringBoot项目的时候,认证授权是必不可少的功能!我们经常会选择Shiro.Spring Security这类权限认证框架来实现,但这些框架使用起来有点繁琐,而且功能也不够强大. ...

  9. SpringBoot学习:整合shiro(身份认证和权限认证),使用EhCache缓存

    一.在pom中引入依赖jar包 1 <properties> 2 <shiro.version>1.3.2</shiro.version> 3 </prope ...

最新文章

  1. 【FFmpeg】警告:[mpegts] H.264 bitstream error, startcode missing, size 0
  2. nginx源码编译、负载均衡及模块的扩展
  3. mysql hive 内置函数_Hive中与时间相关的内置函数
  4. codeforces D Good Substrings(hash)
  5. 剑指offer 算法 (综合)
  6. Windows下lex 与 yacc的使用
  7. Spring-Bean依赖注入(引用数据类型和集合数据类型)
  8. 从体验出发构建以增长为目标的视频服务体系
  9. 【系统知识点】linux入门基础命令
  10. 文件恢复原理Linux文件恢复工具-foremostextundelete
  11. 软件测试工程师经典面试题
  12. 云计算机盒子,网络盒子秒变PC电脑必备装备客厅云电脑
  13. 插入导频法实现载波通信matlab,插入导频法.ppt
  14. html5 判断手机横竖屏,移动端判断横竖屏的5种解决方案
  15. unity ps4 下碰到的坑
  16. C#MessageBox 自动关闭窗口
  17. 这就是裸金属服务器?
  18. Pytorch 实现强化学习策略梯度Reinforce算法
  19. 正则表达式贪婪模式、懒惰模式与独占模式浅析
  20. 数据结构复习题集(客观题)

热门文章

  1. 详解javascript中的call, apply
  2. 找到的程序集清单定义与程序集引用不匹配
  3. 学plc好还是python好_PLC是学西门子的好还是学三菱的?
  4. 山东计算机类好的民办大学,2021年山东所有民办大学名单及排名(教育部)
  5. 包打包和解析过程 unity_Unity学习—资源管理概览
  6. andriod studio 查看项目依赖_Intellij IDEA 中如何查看maven项目中所有jar包的依赖关系图...
  7. DataTables怎么给某一列加上click事件
  8. Exynos4412 Uboot 移植(五)—— Uboot 移植过程
  9. Linux c学习--从标准输入输出看流和缓冲区
  10. [react] 描述下在react中无状态组件和有状态组件的区别是什么?