Spring Boot学习笔记:整合Shiro
Spring Boot如何和Shiro进行整合:
先自定义一个Realm继承AuthorizingRealm,并实现其中的两个方法,分别对应认证doGetAuthenticationInfo和授权doGetAuthorizationInfo。
创建一个ShiroConfig的类,加上@Configuration的注解,这个类就相当于一个shiro的配置文件,可以被spring读取。在ShiroConfig中创建一个方法,此方法的返回值为ShiroFilterFactoryBean,加上@Bean注解,这就就类似在xml文件中添加一个Bean一样,可以被spring管理。ShiroFilterFactoryBean依赖DefaultWebSecurityManager,需要在创建一个DefaultWebSecurityManager的Bean,创建方法和上面类似。DefaultWebSecurityManager依赖Realm,需要创建刚才定义的Realm的Bean。
下面是要继续完善ShiroFilterFactoryBean,设置ShiroFilterFactoryBean的一些属性,如下图:

filterChainDefinitionMap可以过滤哪些url需要某些角色或者权限才能访问。
loginUrl可以设置登陆的url,可以在创建一个登陆的Controller,为"/login",处理get请求,返回一个login.fil或者login.html的视图,视图中登陆的表单数据被提交到loginurl后,会自动被shiro拦截并调用Realm中的认证和授权方法(注意必须是post请求),这个两个方法走完之后,会回到controller,即用于处理post请求的对应的"/login"。网上很多教程中会另外在写一个controller接收登陆是提交表单的数据,然后调用subject.login方法,这个其实是没有必要的,接受post请求的controller并不用手动处理登陆逻辑,shiro的过滤器已经处理。
successUrl登陆成功后跳转的url
这样ShiroFilterFactoryBean就算基本配置好了,后面可以根据业务需求继续完善。
然后就是Realm中两个方法的说明:
doGetAuthenticationInfo方法用于认证,返回AuthenticationInfo,这个接口的实现类是SimpleAuthenticationInfo,构造函数中有四个参数SimpleAuthenticationInfo(Object principal, Object hashedCredentials, ByteSource credentialsSalt, String realmName)。这四个参数的作用分别为:
- principal: the 'primary' principal associated with the specified realm.
- hashedCredentials: the hashed credentials that verify the given principal.
- credentialsSalt: the salt used when hashing the given hashedCredentials
- realmName: the realm from where the principal and credentials were acquired.
登陆的流程:用户访问需要登录的资源,会跳转到登陆页面,即get /login,用户提交账户密码后,会被shiro拦截,在Realm的doGetAuthenticationInfo中接收到表单提交的参数,这个方法的参数是AuthenticationToken,这是一个接口,子接口是RememberMeAuthenticationToken HostAuthenticationToken,实现类是UsernamePasswordToken ,这里面可以接受表单提交的登陆账号和密码。
doGetAuthorizationInfo方法用于授权,返回AuthorizationInfo,这个接口的一个实现类是SimpleAuthorizationInfo,这个类中有三个属性 角色Set<String> roles 权限Set<String> stringPermissions 权限Set<Permission> objectPermissions。接受参数是 PrincipalCollection principalCollection,其中principalCollection.getPrimaryPrincipal()的返回值是SimpleAuthenticationInfo中的第一个参数Oject principal。
END
Spring Boot学习笔记:整合Shiro的更多相关文章
- Spring Boot 学习笔记--整合Thymeleaf
1.新建Spring Boot项目 添加spring-boot-starter-thymeleaf依赖 <dependency> <groupId>org.springfram ...
- Spring Boot 学习笔记--整合Redis
1.新建Spring Boot项目 添加spring-boot-starter-data-redis依赖 <dependency> <groupId>org.springfra ...
- Spring Boot学习笔记 - 整合Swagger2自动生成RESTful API文档
1.添加Swagger2依赖 在pom.xml中加入Swagger2的依赖 <!--swagger2--> <dependency> <groupId>io.spr ...
- Spring Boot 学习笔记(六) 整合 RESTful 参数传递
Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spring Boot 学习笔记(二) 整合 log4j2 Spring Boot 学习笔记 ...
- Spring Boot学习笔记2——基本使用之最佳实践[z]
前言 在上一篇文章Spring Boot 学习笔记1——初体验之3分钟启动你的Web应用已经对Spring Boot的基本体系与基本使用进行了学习,本文主要目的是更加进一步的来说明对于Spring B ...
- Spring Boot 最简单整合Shiro+JWT方式
简介 目前RESTful大多都采用JWT来做授权校验,在Spring Boot 中可以采用Shiro和JWT来做简单的权限以及认证验证,在和Spring Boot集成的过程中碰到了不少坑.便结合自身以 ...
- spring boot整合jsp的那些坑(spring boot 学习笔记之三)
Spring Boot 整合 Jsp 步骤: 1.新建一个spring boot项目 2.修改pom文件 <dependency> <groupId>or ...
- Spring Boot学习笔记(五)整合mybatis
pom文件里添加依赖 <!-- 数据库需要的依赖 --> <dependency> <groupId>org.mybatis.spring.boot</gro ...
- Spring Boot 学习笔记1——初体验之3分钟启动你的Web应用[z]
前言 早在去年就简单的使用了一下Spring Boot,当时就被其便捷的功能所震惊.但是那是也没有深入的研究,随着其在业界被应用的越来越广泛,因此决定好好地深入学习一下,将自己的学习心得在此记录,本文 ...
随机推荐
- buffers和cached的区别
原文:https://www.cnblogs.com/kevingrace/p/5991604.html buffers和cached解释 ============================== ...
- oracle user_tables没有新创建的表的问题
oracle 新创建表后,在user_tables没有,在user_tab_columns也没有,暂时未找到办法
- gdb调试嵌入式环境搭建
1.下载gdb源代码 http://ftp.gnu.org/gnu/gdb/ 2.编译 解压#tar zxvf gdb-7.9.1.tar.gz,cd到解压的目录中. 2.1编译arm-linux-g ...
- 纯CSS制作图形效果
下面所有的例子都是在demo.html的基础上添加相关样式实现的. <!DOCTYPE html> <html> <head> <meta charset=& ...
- 记一次sshd启动报错,Failed to start OpenSSH server daemon.
sshd -t [root@mysql5-slave proj]# sshd -t @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...
- log4j配置后行号乱码显示为?问号
debug="true" classpathref="accrual.path" > 首发于 http://blog.xfuse.cn 参考文档 htt ...
- idea导入项目
1. 2.导入项目 3.右键项目选择web 4.编辑添加tomcat 5.添加jar.包 6. 7.右键put into 8.安装tomcat 9.引入tomcat 10.把项目布署到tomcat
- 移动平台MOBA发热与帧率优化
MOBA项目的优化进入到了第二阶段,千元机,发热严重问题处理,及帧率进一步提升. 回顾之前的优化,当初我的 OPPO R9S不过8-10帧,后来经过了逻辑计算的一些优化后达到10-20帧. 再后来开启 ...
- Android Studio--按钮跳转新页
MainActivity.xml: <Button android:id="@+id/btnGo" android:layout_width="wrap_conte ...
- linux中open函数使用
open函数用来打开一个设备,他返回的是一个整型变量,如果这个值等于-1,说明打开文件出现错误,如果为大于0的值 参考格式 if(fd=open("/dev/ttys0",O_RD ...