Shiro集成web环境[Springboot]-基础使用

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

原文地址:https://www.cnblogs.com/mzc1997/p/10225342.html

时间: 2024-08-09 16:35:19

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

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

Shiro集成web环境[Springboot]--认证与授权 在登录页面提交登陆数据后,发起请求也被ShiroFilter拦截,状态码为302 <form action="${pageContext.request.contextPath}/user/login" method="post"> Username:<input type="text" name="username"></br>

【Shiro】Apache Shiro架构之集成web

前面两节内容介绍了Shiro中是如何进行身份和权限的认证,但是只是单纯的进行Shiro的验证,简单一点的话,用的是.ini配置文件,也举了个使用jdbc realm的例子,这篇博文主要来总结一下Shiro是如何集成web的,即如何用在web工程中. 写在前面:本文没有使用web框架,比如springmvc或者struts2,用的是原始的servlet,使用的是.ini配置文件,旨在简单粗暴,说明问题.后面会写一些和框架整合的博文. 本文部分参考Apache Shiro的官方文档,文档地址:htt

Spring与Web环境集成

1.Spring与Web环境集成 1.1自定义监听器将Spring集成到web环境 1_需求:将spring集成到web开发环境中,将所有的bean对象创建交给spring,除了servlet,servlet可以理解为一个测试类.在servlet中获取ApplicationContext,获取对应的bean 环境搭建,这个是自己一步步取实现的,其实spring有提供简单的方法完成1.1的操作 <!--在pom文件中引入所需的依赖--> <!--Spring坐标--> <dep

我的全栈之路-C语言基础之集成开发环境搭建

我的全栈之路-C语言基础之集成开发环境搭建 我的全栈之路 2.1 C语言集成开发环境搭建 目前主流的操作系统(Windows,Linux,macOS)都有完善的C语言集成开发环境,用于编辑.编译.调试.打包部署C程序. 操作系统 开发工具 Windows10 1903 Visual Studio2019 macOS10.14 XCode10.3 Ubuntu18.04 QT5.13 Windows作为世界上最流行的桌面操作系统,当前最新版本为Windows10 1903,VisualStudio

java基础篇—java的集成开发环境(IDE)

从开始学习java,我们可能尝试过使用记事本编写我们的第一个java程序,比如打印"hello world!",但是慢慢的,随着对java功能的深入了解发现,面对图形,文件的输入和输出,web服务器和安卓应用程序开发等,使用集成开发环境更有助我们简单高效的编写各类java程序.目前有多种IDE支持Java 8(目前的java最新版),例如Intellij IDEA,开源软件Eclipse,NetBeans都是十分优秀的IDE.熟悉这些工具的使用对以后的学习和开发是有很大帮助的,但是作为

Java Web开发环境搭建基础[Windows篇]

1. 准备软件: JDK:(jdk-6u10-rc2-bin-b32-windows-i586-p-12_sep_2008.exe) Tomcat:(apache-tomcat-7.0.54-windows-x86.zip) Eclipse:(eclipse-java-helios-SR1-win32.zip) MySQL:(mysql-5.5.20-win32.msi) MySQL JDBC:(mysql-connector-java-5.1.31.zip) Navicat for MySQL

[零基础学python]集成开发环境(IDE)

当安装好python之后,其实就已经可以进行开发了.下面我们开始写第一行python代码. 值得纪念的时刻:Hello world 如果是用windows,请打开CMD,并执行python. 如果是UNIX类的,就运行shell,并执行python. 都会出现如下内容: Python 2.7.6 (default, Nov 13 2013, 19:24:16) [GCC 4.6.3] on linux2 Type "help", "copyright", "

【SpringBoot】集成 Web Flux

前言: 必需学会SpringBoot基础知识 简介: Takes an opinionated view of building production-ready Spring applications. Spring Boot favors convention over configuration and is designed to get you up and running as quickly as possible. 工具: JDK8 apache-maven-3.5.2 Inte

SpringBoot整合Shiro 集成Redis缓存(六)

简介:由于考虑到项目后期分布式部署,所以缓存由ehcache改为redis,而redis既有单机版部署,也有分布式部署,所以二者需要兼容. 1. maven依赖 <dependency> <groupId>org.crazycake</groupId> <artifactId>shiro-redis</artifactId> <version>3.1.0</version> </dependency> 2. 设