一、Shiro

Apache Shiro是一个Java安全框架。

1、官网:http://shiro.apache.org/

2、三个核心组件

Subject:即“当前操作用户”,可以指人、第三方进程、后台帐户或其他类似事物。Subject代表了当前用户的安全操作,SecurityManager则管理所有用户的安全操作。

SecurityManager:它是Shiro框架的核心,典型的Facade模式,Shiro通过SecurityManager来管理内部组件实例,并通过它来提供安全管理的各种服务。
  
Realm:Realm充当了Shiro与应用安全数据间的“桥梁”或者“连接器”。也就是说,当对用户执行认证(登录)和授权(访问控制)验证时,Shiro会从应用配置的Realm中查找用户及其权限信息。
从这个意义上讲,Realm实质上是一个安全相关的DAO:它封装了数据源的连接细节,并在需要时将相关数据提供给Shiro。当配置Shiro时,配置多个Realm是可以的,但是至少需要一个。

二、SpringBoot集成Shiro

1、依赖配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.9.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.wjy</groupId>
  12. <artifactId>shirodemo</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>shirodemo</name>
  15. <description>Demo project for Spring Boot</description>
  16.  
  17. <properties>
  18. <java.version>1.8</java.version>
  19. </properties>
  20.  
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-web</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-test</artifactId>
  33. <scope>test</scope>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-data-jpa</artifactId>
  38. </dependency>
  39. <dependency>
  40. <groupId>mysql</groupId>
  41. <artifactId>mysql-connector-java</artifactId>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.apache.shiro</groupId>
  45. <artifactId>shiro-spring</artifactId>
  46. <version>1.4.0</version>
  47. </dependency>
  48. <dependency>
  49. <groupId>org.springframework.boot</groupId>
  50. <artifactId>spring-boot-devtools</artifactId>
  51. <scope>runtime</scope>
  52. <optional>true</optional>
  53. </dependency>
  54. </dependencies>
  55.  
  56. <build>
  57. <plugins>
  58. <plugin>
  59. <groupId>org.springframework.boot</groupId>
  60. <artifactId>spring-boot-maven-plugin</artifactId>
  61. </plugin>
  62. </plugins>
  63. </build>
  64.  
  65. </project>

2、Shiro配置

  1. package com.wjy.shirodemo;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
  7. import org.apache.shiro.mgt.SecurityManager;
  8. import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
  9. import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
  10. import org.springframework.context.annotation.Bean;
  11. import org.springframework.context.annotation.Configuration;
  12.  
  13. @Configuration
  14. public class ShiroConfig {
  15. @Bean
  16. public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
  17. ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
  18. shiroFilterFactoryBean.setSecurityManager(securityManager);
  19.  
  20. Map<String, String> filterChainDefinitionMap = new HashMap<String, String>();
  21. //登录时访问的地址,即没有登录时访问任何页面跳转的地址
  22. shiroFilterFactoryBean.setLoginUrl("/login");
  23. //认证未通过访问的地址,即经过认证但是没有相应的权限时跳转的地址
  24. shiroFilterFactoryBean.setUnauthorizedUrl("/unauthc");
  25. //设置认证成功之后转向的地址
  26. shiroFilterFactoryBean.setSuccessUrl("/authc/index");
  27.  
  28. // /* anon表示不拦截 允许任何人访问
  29. filterChainDefinitionMap.put("/*", "anon");
  30. // /authc/index 必须登录才能访问
  31. filterChainDefinitionMap.put("/authc/index", "authc");
  32. // /authc/admin 需要有admin角色才能访问
  33. filterChainDefinitionMap.put("/authc/admin", "roles[admin]");
  34. // /authc/renewable 需要有Create,Update权限
  35. filterChainDefinitionMap.put("/authc/renewable", "perms[Create,Update]");
  36. // /authc/removable需要有Delete权限
  37. filterChainDefinitionMap.put("/authc/removable", "perms[Delete]");
  38. shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
  39. return shiroFilterFactoryBean;
  40. }
  41.  
  42. @Bean
  43. public HashedCredentialsMatcher hashedCredentialsMatcher() {
  44. HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
  45. // 散列算法
  46. hashedCredentialsMatcher.setHashAlgorithmName(PasswordHelper.ALGORITHM_NAME);
  47. // 散列次数
  48. hashedCredentialsMatcher.setHashIterations(PasswordHelper.HASH_ITERATIONS);
  49. return hashedCredentialsMatcher;
  50. }
  51.  
  52. @Bean
  53. public EnceladusShiroRealm shiroRealm() {
  54. EnceladusShiroRealm shiroRealm = new EnceladusShiroRealm();
  55. // 原来在这里
  56. shiroRealm.setCredentialsMatcher(hashedCredentialsMatcher());
  57. return shiroRealm;
  58. }
  59.  
  60. @Bean
  61. public SecurityManager securityManager() {
  62. DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
  63. securityManager.setRealm(shiroRealm());
  64. return securityManager;
  65. }
  66.  
  67. @Bean
  68. public PasswordHelper passwordHelper() {
  69. return new PasswordHelper();
  70. }
  71. }

其他代码参考:githup

验证:
(1)注册:http://localhost:8088/register?username=wjy&password=123456

(2)未登录而访问index:http://localhost:8088/authc/index 会跳转到登录页面: http://localhost:8088/login

(3)使用错误密码登录:http://localhost:8088/doLogin?username=wjy&password=123

(4)登录成功:http://localhost:8088/doLogin?username=wjy&password=123456 跳转到http://localhost:8088/authc/index

参考
30分钟学会如何使用Shiro 
30分钟了解Springboot整合Shiro 
Shiro核心设计思想

【Shiro学习之一】Shiro入门的更多相关文章

  1. Apache Shiro学习-2-Apache Shiro Web Support

     Apache Shiro Web Support  1. 配置 将 Shiro 整合到 Web 应用中的最简单方式是在 web.xml 的 Servlet ContextListener 和 Fil ...

  2. Shiro学习

    Shiro学习资源 Shiro官网,http://shiro.apache.org/index.html 学习网站链接,http://blog.java1234.com/blog/articles/4 ...

  3. Shiro学习总结(2)——Apache Shiro快速入门教程

    第一部分 什么是Apache Shiro 1.什么是 apache shiro : Apache Shiro是一个功能强大且易于使用的Java安全框架,提供了认证,授权,加密,和会话管理 如同 spr ...

  4. Apache shiro学习总结

    Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...

  5. Shiro学习笔记总结,附加" 身份认证 "源码案例(一)

    Shiro学习笔记总结 内容介绍: 一.Shiro介绍 二.subject认证主体 三.身份认证流程 四.Realm & JDBC reaml介绍 五.Shiro.ini配置介绍 六.源码案例 ...

  6. Apache Shiro 学习记录1

    最近几天在学习Apache Shiro......看了一些大神们的教程.....感觉收获不少.....但是毕竟教程也只是指引一下方向....即使是精品教程,仍然有很多东西都没有说明....所以自己也稍 ...

  7. Shiro学习笔记(5)——web集成

    Web集成 shiro配置文件shiroini 界面 webxml最关键 Servlet 測试 基于 Basic 的拦截器身份验证 Web集成 大多数情况.web项目都会集成spring.shiro在 ...

  8. shiro学习笔记_0100_shiro简介

    前言:第一次知道shiro是2016年夏天,做项目时候我要写springmvc的拦截器,申哥看到后,说这个不安全,就给我捣鼓了shiro,我就看了下,从此认识了shiro.此笔记是根据网上的视频教程记 ...

  9. 转:JAVAWEB开发之权限管理(二)——shiro入门详解以及使用方法、shiro认证与shiro授权

    原文地址:JAVAWEB开发之权限管理(二)——shiro入门详解以及使用方法.shiro认证与shiro授权 以下是部分内容,具体见原文. shiro介绍 什么是shiro shiro是Apache ...

  10. Shiro学习笔记四(Shiro集成WEB)

    这两天由于家里出了点事情,没有准时的进行学习.今天补上之前的笔记 -----没有学不会的技术,只有不停找借口的人 学习到的知识点: 1.Shiro 集成WEB 2.基于角色的权限控制 3.基于权限的控 ...

随机推荐

  1. excel中统计COUNTIFS的值为0

    excel中统计COUNTIFS的值为0 个人认为是由于导出的文件里面的字符个数问题 使用 =COUNTIFS(H1:H175,"微信支付") 这个的结果居然是0,找了很多办法 于 ...

  2. 阿里OSS前端直传

    第一次写博客,如有错误请多多指教. 先上代码吧: ossUpload = function (file, fun, funParameter) { //第一此请求后台服务器获取认证请求 $.ajax( ...

  3. MVC+Ninject+三层架构+代码生成 -- 总结(五、Ninject)

    1.在寫邏輯層前,需要弄好反轉控制,因框架沒有寫接口,所以Ninject只負責返回當前實例,有點類似共享設計模式. public sealed class IOCHelper { private st ...

  4. DevExpress的下拉框控件LookUpEdit的使用、添加item选项值、修改默认显示值

    场景 Winform控件-DevExpress18下载安装注册以及在VS中使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1 ...

  5. Spring @Import注解源码解析

    简介 Spring 3.0之前,创建Bean可以通过xml配置文件与扫描特定包下面的类来将类注入到Spring IOC容器内.而在Spring 3.0之后提供了JavaConfig的方式,也就是将IO ...

  6. git操作:撤销更改的文件

    在没有git add之前: #撤销所有更改 git checkout . #撤销指定文件的更改 git checkout -- myfile.txt 在git add之后: git reset HEA ...

  7. CTF-PHP一句话木马

    首先看一下题目 他是提示让你输入一个4位数的密码 使用burp进行密码爆破 我们使用burp来自动生成一个所有以4位数组成的密码 经过一段时间的爆破发现他的返回值都为192个字节,无法区别正确的密码. ...

  8. JavaWeb Listener之HttpSessionActivationListener ,session钝化、活化

    HttpSessionActivationListener    监听HttpSession对象的活化.钝化 钝化:将HttpSession对象从内存中转移至硬盘,存储为.session文件. 活化: ...

  9. idea使用过程中的一些常见问题,做个笔记

    :当实现这个接口方法时重载是不允许的. 首先我相信我的代码肯定没问题,因为我实现的接口确实有这个方法.在编程阶段就提示这个错误,于是我有理由相信应该是编译错误!通过google,解决办法so easy ...

  10. LVS (Linux虚拟服务器)模型及算法

    LVS(Linux Virtual Server)Linux虚拟服务器 LVS集群采用IP负载均衡技术和基于内容请求分发技术. 用户请求发给负载均衡调度器,由负载均衡调度器根据设定的调度算法将请求发给 ...