Shiro集成web环境[Springboot]

1.shiro官网查找依赖的jar,其中shiro-ehcache做授权缓存时使用,另外还需要导入ehcache的jar包

        <dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.3.2</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.ehcache/ehcache -->
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.6.3</version>
</dependency>

过滤器依赖的层级关系:



2.配置shrio的核心过滤器

@Configuration
public class ShiroFilter { @Bean
public ShiroFilterFactoryBean getShiroFilterFactoryBean(SecurityManager securityManager){
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(securityManager);
//shiro会对所有资源进行控制,默认不拦截 需要配置
Map<String,String> map = new HashMap<>();
//多个过滤器 AnonymousFilter 匿名过滤器 简称anon
// FormAuthenticationFilter 认证过滤器 简称authc
map.put("/**","authc");
//多个过滤器组成过滤器链
shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
//设置认证页面路径
shiroFilterFactoryBean.setLoginUrl("/main/login.jsp");
return shiroFilterFactoryBean;
} @Bean
public SecurityManager getSecurityManager(){
//web环境下securityManage的实现类为DefaultWebSecurityManager
SecurityManager securityManager = new DefaultWebSecurityManager();
return securityManager;
}
}

其中shiro过滤器中的SecurityManager 属性必须设置,否则报错如下:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getShiroFilterFactoryBean': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanInitializationException: SecurityManager property must be set.

注入方式:

  1. new SecurityManager 后set赋值给ShiroFilter 不推荐
  2. 自动注入,自动注入前应该考虑该类是否由spring工厂管理?没有管理 交由spring工厂管理
  3. 自动注入只在一个方法中使用 无需自动注入,java配置形式提供了方法形参的注入,该形参是基于类型的注入,凡是由spring工厂管理的类,所需的方法形参类型都可以由spring工厂提供

如图关系:

测试1:设置认证过滤器,访问index.jsp 无法到达且视图解析指向login.jsp 说明该请求没有经过认证

测试2:设置匿名过滤器,访问index.jsp可到到

Shiro过滤器

过滤器简称 对应的java类
anon org.apache.shiro.web.filter.authc.AnonymousFilter
authc org.apache.shiro.web.filter.authc.FormAuthenticationFilter
authcBasic org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter
perms org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter
port org.apache.shiro.web.filter.authz.PortFilter
rest org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter
roles org.apache.shiro.web.filter.authz.RolesAuthorizationFilter
ssl org.apache.shiro.web.filter.authz.SslFilter
user org.apache.shiro.web.filter.authc.UserFilter
logout org.apache.shiro.web.filter.authc.LogoutFilter

Shiro集成web环境[Springboot]-基础使用的更多相关文章

  1. Shiro集成web环境[Springboot]-认证与授权

    Shiro集成web环境[Springboot]--认证与授权 在登录页面提交登陆数据后,发起请求也被ShiroFilter拦截,状态码为302 <form action="${pag ...

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

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

  3. Spring集成web环境(使用封装好的工具)

    接上文spring集成web环境(手动实现) ##########代码接上文############# spring提供了一个监听器ContextLoaderListener对上述功能的封装,该监听器 ...

  4. Shiro在Web环境下集成Spring的大致工作流程

    1,Shiro提供了对Web环境的支持,其通过一个 ShiroFilter 入口来拦截需要安全控制的URL,然后进行相应的控制.      ①配置的 ShiroFilter 实现类为:org.spri ...

  5. 珠联壁合地设天造|M1 Mac os(Apple Silicon)基于vscode(arm64)配置搭建Java开发环境(集成web框架Springboot)

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_194 也许有人从未听说过Python,但是不会有人没听说过Java,它作为一个拥有悠久历史的老牌编程语言,常年雄踞TIOBE编程语 ...

  6. Shiro集成Web

    Shiro不仅可以集成到web中,也可以集成Spring. 1.在WEB中添加Shrio支持 2.WEB中INI配置 3.JSP/GSP标签 在WEB中添加Shrio支持 如果要想在web中使用Shr ...

  7. Shiro 集成 Web

    Web 集成 Shiro 的练习项目. Servlet + Shiro 项目结构 新建Maven项目,pom配置如下 <project xmlns="http://maven.apac ...

  8. Spring集成web环境(手动实现)

    1.创建UserDao接口及其实现类UserDaoImpl(接口代码省略) public class UserDaoImpl implements UserDao { @Override public ...

  9. 【Shiro】Apache Shiro架构之集成web

    Shiro系列文章: [Shiro]Apache Shiro架构之身份认证(Authentication) [Shiro]Apache Shiro架构之权限认证(Authorization) [Shi ...

随机推荐

  1. 【译】第10节---数据注解-Key

    原文:http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.asp ...

  2. HDU 5844 LCM Walk(数学逆推)

    http://acm.hdu.edu.cn/showproblem.php?pid=5584 题意: 现在有坐标(x,y),设它们的最小公倍数为k,接下来可以移动到(x+k,y)或者(x,y+k).现 ...

  3. 51nod 1052 最大M子段和

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1052 题意: 思路:设$dp[i][j]$表示前j个数构成i个字段时的最 ...

  4. WebPack基本概念以及基本使用

    入门Webpack,看这篇就够了 2017年9月18日更新,添加了一个使用webpack配置多页应用的demo,可以点击此处查看 2017年8月13日更新,本文依据webpack3.5.3将文章涉及代 ...

  5. mysql 事件 按月分表

    /****** 对象: Table Order_201512 脚本日期: 2015/12/18 11:44:23 ******/ /****** 字段数据长度 = 2599 字节 ******/ CR ...

  6. django使用表单

    假设你想从表单接收用户名数据,一般情况下,你需要在HTML中手动编写一个如下的表单元素: <form action="/your-name/" method="po ...

  7. [工具]cmd命令大全

    cmd命令大全(第一部分) winver---------检查Windows版本  wmimgmt.msc----打开windows管理体系结构(WMI)  wupdmgr--------window ...

  8. PostegreSQL模板数据库

    模板数据库 模板数据库就是创建新database时,PostgreSQL会基于模板数据库制作一份副本,其中会包含所有的数据库设置和数据文件. CREATE DATABASE 实际上是通过拷贝一个现有的 ...

  9. [JS]给String对象添加方法,使传入的字符串字符之间以空格分开输出

    看到一个这样子的面试题: 给String对象添加一个方法,传入一个string类型的参数,然后将string的每一个字符间加空格返回,例如:addSpace("hello world&quo ...

  10. CentOS/redhat使用光盘镜像源

    1,首先进行光盘的挂载,注意光盘挂载时不会自动建立目录的,    所以需要自己建立目录.    mkdir /mnt/cdrom    mount /dev/cdrom /mnt/cdrom  #de ...