在官网给的源码中,学习如何搭建第一个基于shiro的例子:

  • 开发工具 IDEA 2017.2.3
  • JDK 1.8
  • Spring 4.3.8
  • Shiro 1.3.2

搭建一个web的maven项目:

image.png

在pom.xml文件里添加

<dependencies><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-core</artifactId><version>1.3.2</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-spring</artifactId><version>1.3.2</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-ehcache</artifactId><version>1.4.0</version></dependency><dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache-core</artifactId><version>2.6.11</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-web</artifactId><version>1.3.2</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency><dependency><groupId>hsqldb</groupId><artifactId>hsqldb</artifactId><version>1.8.0.10</version><scope>runtime</scope></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.21</version><scope>runtime</scope></dependency><dependency><groupId>org.slf4j</groupId><artifactId>jcl-over-slf4j</artifactId><version>1.7.25</version><scope>runtime</scope></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version><scope>runtime</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>4.3.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.3.8.RELEASE</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version><scope>runtime</scope></dependency></dependencies>

修改web.xnk文件:配置springmvc前端控制器啊和shiro的拦截器

  <context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><!-- Shiro Filter is defined in the spring application context: --><filter><filter-name>shiroFilter</filter-name><filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class><init-param><param-name>targetFilterLifecycle</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>shiroFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>spring</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class></servlet><servlet-mapping><servlet-name>spring</servlet-name><url-pattern>/</url-pattern></servlet-mapping>

在资源文件夹新建applicationContext.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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!-- =========================================================Shiro Core Components - Not Spring Specific========================================================= --><!-- Shiro's main business-tier object for web-enabled applications(use DefaultSecurityManager instead when there is no web environment)--><!--1配置安全管理器--><bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"><property name="cacheManager" ref="cacheManager"/><!-- Single realm app.  If you have multiple realms, use the 'realms' property instead. --><property name="realm" ref="jdbcRealm"/></bean><!-- Let's use some enterprise caching support for better performance.  You can replace this with any enterprisecaching framework implementation that you like (Terracotta+Ehcache, Coherence, GigaSpaces, etc --><!--2配置缓存cacheManager需要加入相关依赖--><bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"><!-- Set a net.sf.ehcache.CacheManager instance here if you already have one.  If not, a new onewill be creaed with a default config:<property name="cacheManager" ref="ehCacheManager"/> --><!-- If you don't have a pre-built net.sf.ehcache.CacheManager instance to inject, but you wanta specific Ehcache configuration to be used, specify that here.  If you don't, a defaultwill be used.:<property name="cacheManagerConfigFile" value="classpath:some/path/to/ehcache.xml"/> --></bean><!-- Used by the SecurityManager to access security data (users, roles, etc).Many other realm implementations can be used too (PropertiesRealm,LdapRealm, etc. --><!--3配置安全数据源--><bean id="jdbcRealm" class="com.ximi.shiro.MyRealm"></bean><!-- =========================================================Shiro Spring-specific integration========================================================= --><!-- Post processor that automatically invokes init() and destroy() methodsfor Spring-configured Shiro objects so you don't have to1) specify an init-method and destroy-method attributes for every beandefinition and2) even know which Shiro objects require these methods to becalled. --><!--4配置LifecycleBeanPostProcessor: 可以子自动的调用配置在spring IOC容器中的shiro bean的生命周期方法--><bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/><!-- Enable Shiro Annotations for Spring-configured beans.  Only run afterthe lifecycleBeanProcessor has run: --><!--5配置DefaultAdvisorAutoProxyCreator:启用IOC容器中使用shiro的注解--><bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"depends-on="lifecycleBeanPostProcessor"/><bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"><property name="securityManager" ref="securityManager"/></bean><!-- Define the Shiro Filter here (as a FactoryBean) instead of directly in web.xml -web.xml uses the DelegatingFilterProxy to access this bean.  This allows usto wire things with more control as well utilize nice Spring things such asPropertiesPlaceholderConfigurer and abstract beans or anything else we might need: --><!--6配置shiroFilter:如果web.xml中不单独配置targetName属性的话,默认id要和web.xml中DelegatingFilterProxy的Filter name属性一致!!!!--><bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"><property name="securityManager" ref="securityManager"/><property name="loginUrl" value="/login.jsp"/><property name="successUrl" value="/index.jsp"/><property name="unauthorizedUrl" value="/unauthorized.jsp"/><!-- The 'filters' property is not necessary since any declared javax.servlet.Filter beandefined will be automatically acquired and available via its beanName in chaindefinitions, but you can perform overrides or parent/child consolidated configurationhere if you like: --><!-- <property name="filters"><util:map><entry key="aName" value-ref="someFilterPojo"/></util:map></property> --><!--配置那些页面需要保护访问那些页面需要什么权限anon:匿名authc:保护的(必须登陆之后才能访问)--><property name="filterChainDefinitions"><value>/login = anon# everything else requires authentication:/** = authc</value></property></bean>
</beans>

配置springmvc的组件:spring-servlet.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:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="com.ximi.shiro"/><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/"/><property name="suffix" value=".jsp"/></bean><mvc:annotation-driven/><mvc:default-servlet-handler/></beans>

配置log4j,控制台输出日志:log4j.properties

log4j.rootLogger=INFO, stdoutlog4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m %n# General Apache libraries
log4j.logger.org.apache=WARN# Spring
log4j.logger.org.springframework=WARN# Default Shiro logging
log4j.logger.org.apache.shiro=TRACE# Disable verbose logging
log4j.logger.org.apache.shiro.util.ThreadContext=WARN
log4j.logger.org.apache.shiro.cache.ehcache.EhCache=WARN

先实现身份验证

流程

  • 1、收集用户身份/凭证,即如用户名/密码
  • 2、调用 Subject.login 进行登录,如果失败将得到相应AuthenticationException 异常,根据异常提示用户错误信息;否则登录成功
  • 3、创建自定义的 Realm 类,继承org.apache.shiro.realm.AuthorizingRealm 类,实现doGetAuthenticationInfo() 方法

代码实现

1.编写一个简单的login表单页面
2.编写Controller,获取输入的参数,创建token对象,传给 Subject.login
3.编写自定义Realm类,实现doGetAuthenticationInfo() 方法里的逻辑

Shiro实战hello相关推荐

  1. Shiro 实战教程

    Shiro 实战教程 1.权限的管理 1.1 什么是权限管理 ​ 基本上涉及到用户参与的系统都要进行权限管理,权限管理属于系统安全的范畴,权限管理实现对用户访问系统的控制,按照安全规则或者安全策略控制 ...

  2. shiro实战系列(一)之入门实战

    一.什么是shiro? Apache Shiro 是一个强大而灵活的开源安全框架,它干净利落地处理身份认证,授权,企业会话管理和加密.   Apache Shiro 的首要目标是易于使用和理解.安全有 ...

  3. Session(数据)共享的前后端分离Shiro实战

    1,前言 本文期望描述如何使用Shiro构建基本的安全登录和权限验证.本文实战场景有如下特殊需求:1,在集群和分布式环境实现session共享:2,前端只使用HTML/CSS/JS.因此无法直接使用S ...

  4. Shiro 实战教程(全)

    目录 1.权限的管理 1.1 什么是权限管理 1.2 什么是身份认证 1.3 什么是授权 2.什么是shiro 3.shiro的核心架构 3.1 Subject 3.2 SecurityManager ...

  5. shiro实战系列(八)之安全管理器

    Apache Shiro 提供安全框架界独一无二的东西:一个完整的企业级 Session 解决方案,从最简单的命令行及智能手机 应用到最大的集群企业 Web 应用程序.   这对许多应用有着很大的影响 ...

  6. Shiro实战1-介绍

    什么是 Shiro 官网:http://shiro.apache.org/ shiro是一款主流的 Java 安全框架,不依赖任何容器,可以运行在 Java SE和 Java EE 项目中,它的主要作 ...

  7. 一文总结 Shiro 实战教程

    ✅作者简介:2022年博客新星 第八.热爱国学的Java后端开发者,修心和技术同步精进.

  8. 视频教程-Apache Shiro权限框架实战+项目案例视频课程-Java

    Apache Shiro权限框架实战+项目案例视频课程 拥有10余年项目实战经验. 2006-2011在nttdata从事对日软件开发类工作. 2011-2015在HP从事技术服务工作. 擅长于j2e ...

  9. 一篇文章搞定Shiro权限管理框架

    前言:前几天学习了SpringSecurity安全框架,这几天又接着学习shiro框架,这两者框架都是同一类产品,解决同一类问题,但是在官方推荐使用Shiro框架,因为它简单易学,所以这里有时间学习了 ...

最新文章

  1. mysql 替换某个字段中的某个字符
  2. 李宏毅老师机器学习和深度学习
  3. c# winform 程序打包部署
  4. MySQL中事物的详解
  5. Map 的Properties集合存储IO流对象
  6. MFC操作多个安卓设备(发送指令)
  7. 腾讯网游加速器大升级!5月31日起仅支持国服游戏加速 你用过吗?
  8. 一个人长途自驾旅行需要注意什么?
  9. 24岁女孩与30岁男人的精彩对白
  10. Java窗口之文本框、按钮、菜单
  11. php文章重复度检测,彻底解决网站存在的内容重复度的方法
  12. 光孝寺招聘员工!月薪15000,早九晚五,免费饭菜,有证者优先,工作六根清净,而且.......
  13. 阿里云ace认证内容有哪些?
  14. python print什么意思_python中print有什么用
  15. 为卿一袭白衣,倾尽江湖又何妨?
  16. python乘积函数_Python中乘法
  17. 假作真时真亦假,无为有处有还无
  18. 糊滤镜给人物脸部磨皮教程
  19. 软件测试学习中的一些有用网站
  20. PCI、PCI-X、PCI-E、PCI-E Card、Mini PCI-E、M.2、Add-in Card 它们有啥区别?这些概念你搞清楚了吗?

热门文章

  1. git ssh key生成
  2. EF连接ORACLE
  3. flask笔记3-模板
  4. 并发编程的 15 条建议(译)
  5. 基于OHCI的USB主机 —— UFI查询代码
  6. 使用模块化工具Rollup打包自己开发的JS库
  7. CryptoJS -- JS加密算法库
  8. 使用docker运行dotnetcore站点
  9. springboot logback自定义配置文件路径
  10. c语言 结构体练习之 实现产品销售记录的相关功能