【视频&交流平台】
http://study.163.com/course/introduction.htm?courseId=1004329008&utm_campaign=commission&utm_source=400000000155061&utm_medium=share
http://study.163.com/course/introduction.htm?courseId=1004638001&utm_campaign=commission&utm_source=400000000155061&utm_medium=share
https://gitee.com/happyangellxq520/spring-boot
http://412887952-qq-com.iteye.com/blog/2321532
【原创文章,转载请注明出处】
在看此篇博客之前您可能需要先看下上面的博客:
Spring Boot之Shiro无状态(1)【从零开始学Spring Boot】
好了不废话了,看看这节的大纲:
(2)怎么配置无状态?;
(3)怎么测试是无状态设置成功了呢?
接下来看看具体的步骤吧。
(1)集成Shiro的基本配置;
Spring 继承Shiro最简单的配置就是需要注入ShiroFilterFactoryBean和DefaultWebSecurityManager。我们新建一个ShiroConfiguration类,代码如下:
package com.kfit.config;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* shiro配置类.
* @author Angel --守护天使
* @version v.0.1
* @date 2017年2月25日
*/
@Configuration
public class ShiroConfiguration {
@Bean
public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager){
ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
factoryBean.setSecurityManager(securityManager);
return factoryBean;
}
/**
* shiro安全管理器:
* 主要是身份认证的管理,缓存管理,cookie管理,
* 所以在实际开发中我们主要是和SecurityManager进行打交道的
* @return
*/
@Bean
public DefaultWebSecurityManager securityManager() {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
return securityManager;
}
}
以上只是集成的基本配置,实际上配置完之后还没啥鸟用,接下来才是重点呢。
(2)怎么配置无状态?;
对于无状态的配置,我们需要知道,我们要配置如下几个地方:
第一:SubjectContext在创建的时候,需要关闭session的创建,这个主要是由DefaultWebSubjectFactory的createSubject进行管理。
第二: 需要禁用使用Sessions 作为存储策略的实现,这个主要由securityManager的subjectDao的sessionStorageEvaluator进行管理的。
第三:需要禁用掉会话调度器,这个主要由sessionManager进行管理。
那么我们需要先定义一个StatelessDefaultSubjectFactory类,此类继承于DefaultWebSubjectFactory,我们重写createSubject的方法,通过SubjectContext关闭session的创建,具体实现代码如下:
package com.kfit.config;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.SubjectContext;
import org.apache.shiro.web.mgt.DefaultWebSubjectFactory;
/**
*
通过调用context.setSessionCreationEnabled(false)表示不创建会话;如果之后调用
Subject.getSession()将抛出DisabledSessionException异常。
* @author Angel --守护天使
* @version v.0.1
* @date 2017年2月25日
*/
public class StatelessDefaultSubjectFactory extends DefaultWebSubjectFactory{
@Override
public Subject createSubject(SubjectContext context) {
//不创建session.
context.setSessionCreationEnabled(false);
System.out.println("shiro.config.subjectFactory.createSubject.SessionCreationEnabled.false");
return super.createSubject(context);
}
}
接下来我们要调整下ShiroConfiguration,首先我们要注入StatelessDefaultSubjectFactory;其次就是将StatelessDefaultSubjectFactory交给DefaultWebSecurityManager进行管理;最后使用securityManager获取到subjectDao禁用session的存储策略,具体代码如下。(注意新加的代码为:Add.2.x)
package com.kfit.config;
import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.DefaultSessionManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.mgt.DefaultWebSubjectFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* shiro配置类.
* @author Angel --守护天使
* @version v.0.1
* @date 2017年2月25日
*/
@Configuration
public class ShiroConfiguration {
@Bean
public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager){
ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
factoryBean.setSecurityManager(securityManager);
return factoryBean;
}
/**
* shiro安全管理器:
* 主要是身份认证的管理,缓存管理,cookie管理,
* 所以在实际开发中我们主要是和SecurityManager进行打交道的
* @return
*/
@Bean
public DefaultWebSecurityManager securityManager() {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//Add.2.2
securityManager.setSubjectFactory(subjectFactory());
//Add.2.5
securityManager.setSessionManager(sessionManager());
/*
* 禁用使用Sessions 作为存储策略的实现,但它没有完全地禁用Sessions
* 所以需要配合context.setSessionCreationEnabled(false);
*/
//Add.2.3
((DefaultSessionStorageEvaluator)((DefaultSubjectDAO)securityManager.getSubjectDAO()).getSessionStorageEvaluator()).setSessionStorageEnabled(false);
return securityManager;
}
/**
* Add.2.1
* subject工厂管理器.
* @return
*/
@Bean
public DefaultWebSubjectFactory subjectFactory(){
StatelessDefaultSubjectFactory subjectFactory = new StatelessDefaultSubjectFactory();
return subjectFactory;
}
/**
* Add.2.4
* session管理器:
* sessionManager通过sessionValidationSchedulerEnabled禁用掉会话调度器,
* 因为我们禁用掉了会话,所以没必要再定期过期会话了。
* @return
*/
@Bean
public DefaultSessionManager sessionManager(){
DefaultSessionManager sessionManager = new DefaultSessionManager();
sessionManager.setSessionValidationSchedulerEnabled(false);
return sessionManager;
}
}
此时代码到这里的话,我们的session就是被关闭了。
(3)怎么测试是无状态设置成功了呢?
关说不测,不是好程序员,哈哈,我们还是要亲眼看见报错才能知道我们的代码才是正确了。
测试原理:如果是无状态的话,那么在调用代码:currentUser.getSession()是会抛出异常的。所以很好测试,直接在HellController中加入如下方法即可测试,代码如下:
/**
* 此方法执行的时候,会抛出异常:
* Session creation has been disabled for the current subject.
* @param session
* @return
*/
@RequestMapping("/hello3")
public String hello3(){
Subject currentUser = SecurityUtils.getSubject();
Session session = currentUser.getSession();
System.out.println(session);
return"hello3,Andy";
}
在浏览器中访问http://127.0.0.1:8080/hello3
正确的情况下是会抛出异常信息:
Session creation has been disabled for the current subject.
【视频&交流平台】
http://study.163.com/course/introduction.htm?courseId=1004523002
http://412887952-qq-com.iteye.com/blog/2321532
======================================
Spring Boot Shiro视频实战篇【已更新】
======================================
适合人群
有Spring Boot基础的人群。
使用技术
(1)spring boot(整合框架)
(2)spring mvc
(3)spring data jpa(持久化操作)
(4)shiro(安全框架)
(5)thymeleaf(模板引擎)
(6)ehcache(缓存管理)
(7)mysql(数据库)
(8)js/css/img(静态资源使用)
(9)kaptcha(验证码库)
课程目录
1. Spring Boot Shiro介绍
2. Spring Boot 搭建无Shiro的框架
3. Spring Boot Shiro拦截
4. Spring Boot Shiro身份认证准备工作
5. Spring Boot Shiro身份认证
6. Spring Boot Shiro权限控制
7. Spring Boot Shiro缓存
8. Spring Boot Shiro记住密码
9. Spring Boot Shiro登录成功之后下载favicon.ico
10. Spring Boot 在thymeleaf使用shiro标签
11. Spring Boot Shiro密码加密算法
12.Spring Boot Shiro使用JS-CSS-IMG
13. Spring Boot Shiro限制登录尝试次数
14.Spring Boot Shiro 验证码
相关推荐
从零开始学Spring Boot,Shiro教程,PDF电子书!2合一下载
Spring Boot整合Shiro搭建权限管理系统.pdfSpring Boot整合Shiro搭建权限管理系统.pdfSpring Boot整合Shiro搭建权限管理系统.pdfSpring Boot整合Shiro搭建权限管理系统.pdfSpring Boot整合Shiro搭建权限管理系统....
Spring Boot整合Shiro搭建权限管理系统.docxSpring Boot整合Shiro搭建权限管理系统.docxSpring Boot整合Shiro搭建权限管理系统.docxSpring Boot整合Shiro搭建权限管理系统.docxSpring Boot整合Shiro搭建权限管理系统...
Spring Boot学习之Shiro源码【学习狂神说,自己手动书写,可以实现正常所需的功能】 Spring Boot学习之Shiro源码【学习狂神说,自己手动书写,可以实现正常所需的功能】 Spring Boot学习之Shiro源码【学习狂神说,...
NULL 博文链接:https://412887952-qq-com.iteye.com/blog/2299732
Spring Boot整合Shiro搭建权限管理系统教学提纲.docxSpring Boot整合Shiro搭建权限管理系统教学提纲.docxSpring Boot整合Shiro搭建权限管理系统教学提纲.docxSpring Boot整合Shiro搭建权限管理系统教学提纲....
Spring Boot整合Shiro搭建权限管理系统教学提纲.pdfSpring Boot整合Shiro搭建权限管理系统教学提纲.pdfSpring Boot整合Shiro搭建权限管理系统教学提纲.pdfSpring Boot整合Shiro搭建权限管理系统教学提纲.pdfSpring ...
Spring Boot整合Shiro.zip
Spring Boot整合Shiro.zip
NULL 博文链接:https://412887952-qq-com.iteye.com/blog/2299777
spring boot+shiro 权限认证管理案例 shiro搭配缓存处理
NULL 博文链接:https://412887952-qq-com.iteye.com/blog/2299784
在线考试系统源码+项目说明(在线测试+辅助课程教学和学生学习,基于Spring Boot、Shiro、MyBatis、Redis、thymeleaf等框架).zip 在线考试系统源码+项目说明(在线测试+辅助课程教学和学生学习,基于Spring Boot、...
本demo为Spring boot整合shiro,以mybatis plus做dao层交互数据,实现了读取数据库用户数据实现用户登录,权限认证,读取数据库中用户对应的url请求,实现请求的过滤。自定义了relam和过滤器来实现这些功能
Spring Boot整合Shiro搭建权限管理系统 (2).docxSpring Boot整合Shiro搭建权限管理系统 (2).docx
Spring Boot整合Shiro搭建权限管理系统 (2).pdfSpring Boot整合Shiro搭建权限管理系统 (2).pdf
Spring Boot整合Shiro搭建的一套简单的权限管理系统,里面介绍了Shiro的基本用法,里面附上了代码,环境需要自己搭建(里面有图说明怎么搭建环境的),里面的代码直接复制粘贴就可以运行。感谢大家下载!