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. python学习 day016打卡 面向对象--成员

    本节主要内容: 1.类的成员 2.类的成员-变量 3.类的成员-方法 4.类的成员-属性 5.私有 一.类的成员: 能在类中写的内容就是类的成员. class 类名: #方法 def __init__ ...

  2. _event_worldstate_team

    EventId 事件ID ID WorldStateUI.dbc第10列数字部分 TeamId 事件玩家分组,攻守(防守为1,进攻为2),自定义阵营(_faction表自定义阵营ID),公会(公会gu ...

  3. 【三十二】thinkphp之连接数据库、实例化模型

    1.连接数据库 Thinlphp内置了抽象数据库访问层,把不同的数据操作封装起来.我们只需要调用公共的DB类进行操作即可.DB类会自动调用相应的数据库驱动来处理. 在应用目录/common/conf/ ...

  4. link 和 import的区别

    二者区别: link属于html标签,而@import是css提供的. 页面被加载时,link因为是HTM标签, 把认会同时被加载,而@import引用的css会等到页面加载结束后加载. link是h ...

  5. ArcFace 2.0 Demo [C++]

    环境: win10(10.0.16299.0)+ VS2017 sdk版本:ArcFace v2.0 OPENCV3.43版本 x64平台Debug.Release配置都已通过编译 下载地址:http ...

  6. git报错fatal: loose object ....(stored in .git/objects/....) is emtpy

    主要是非正常关机.把.git给破坏了 参考https://stackoverflow.com/questions/12571557/fixing-a-corrupt-loose-object-as-a ...

  7. Python Appium 开启Android测试之路

    1.获取 Android app的Activity 打开终端cmd,先cd进入到刚才下载的“新浪.apk”目录下,然后使用aapt dump badging xxx.apk命令获取包内信息.注意,启动 ...

  8. cyberduck的SSH登录

    1.通过配置SSH秘钥. 2.不点匿名(不要点匿名),如果非要填一个名字的话,你写root就行. 3.书签.

  9. Java 多线程 fork-join

    fork-join我们可以理解为分而治之,就是说当一个任务非常大的时候,我们可以按照一定的业务需求拆分为若干个小的任务,最后把这些小的任务再聚合起来. 下面就通过fork-join实现一个从1加到10 ...

  10. (转).Net中自定义类作为Dictionary的key详解

    在定义数据结构时,Dictionary提供了快速查找数据的功能,另外Dictionary< TKey, TValue >属于key-value键值对数据结构,提供了泛型的灵活性,是数据结构 ...