1.导包

<!-- springboot 与 shiro 的集成-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.1</version>
</dependency> <!-- thymeleaf 与 shiro 集成-->
<dependency>
<groupId>com.github.theborakompanioni</groupId>
<artifactId>thymeleaf-extras-shiro</artifactId>
<version>2.0.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

2. 编写配置类

@Configuration
@ConfigurationProperties(prefix = "shiro")
@Data
public class ShiroConfig { private String loginUrl;
private String unauthorizedUrl;
private String successUrl;
private String logoutUrl; private String[] anons;
private String[] authcs; /**
* 配置securityManager
* @param userRealm
* @return
*/
@Bean
public SecurityManager securityManager(UserRealm userRealm){
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); securityManager.setRealm(userRealm); return securityManager;
} /**
* 配置shiroFilter
* @param securityManager
* @return
*/
@Bean
public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager){
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); shiroFilterFactoryBean.setSecurityManager(securityManager);
shiroFilterFactoryBean.setLoginUrl(loginUrl);
shiroFilterFactoryBean.setUnauthorizedUrl(unauthorizedUrl);
shiroFilterFactoryBean.setSuccessUrl(successUrl); Map<String,String> filterMap = new HashMap<>(); if(null != logoutUrl){
filterMap.put(loginUrl,"logout");
}
if(anons!=null && anons.length>0){
for(String anon:anons){
filterMap.put(anon,"anon");
}
}
if(authcs!=null && authcs.length>0){
for(String authc:authcs){
filterMap.put(authc,"authc");
}
} shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
return shiroFilterFactoryBean;
} /**
* 配置自定义Realm
* @return
*/
@Bean
public UserRealm userRealm(CredentialsMatcher credentialsMatcher){
UserRealm userRealm = new UserRealm(); userRealm.setCredentialsMatcher(credentialsMatcher); return userRealm;
} /**
* 配置凭证匹配器
* @return
*/
@Bean
public HashedCredentialsMatcher hashedCredentialsMatcher(){
HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher(); hashedCredentialsMatcher.setHashAlgorithmName("MD5");
hashedCredentialsMatcher.setHashIterations(10); return hashedCredentialsMatcher;
} /**
* 配置ShiroDialect,用于Thymeleaf和shiro标签的使用
* @return
*/
@Bean
public ShiroDialect shiroDialect(){ return new ShiroDialect();
} }

3. application.yml 配置 拦截链

# shiro
shiro:
login-url: /login.html
anons:
- /login.html
- /index.html
- doLogin
authcs:
- /**

Spring Boot 整合 Shiro+Thymeleaf的更多相关文章

  1. Spring Boot 整合 Shiro ,两种方式全总结!

    在 Spring Boot 中做权限管理,一般来说,主流的方案是 Spring Security ,但是,仅仅从技术角度来说,也可以使用 Shiro. 今天松哥就来和大家聊聊 Spring Boot ...

  2. Spring Boot2 系列教程(三十二)Spring Boot 整合 Shiro

    在 Spring Boot 中做权限管理,一般来说,主流的方案是 Spring Security ,但是,仅仅从技术角度来说,也可以使用 Shiro. 今天松哥就来和大家聊聊 Spring Boot ...

  3. Spring boot 整合 Mybatis + Thymeleaf开发web(二)

    上一章我把整个后台的搭建和逻辑给写出来了,也贴的相应的代码,这章节就来看看怎么使用Thymeleaf模板引擎吧,Spring Boot默认推荐Thymeleaf模板,之前是用jsp来作为视图层的渲染, ...

  4. spring boot整合shiro出现UnavailableSecurityManagerException

    spring boot自带spring security,spring security自然不用说是一个强大的安全框架,但是用惯了shiro,一时半会用不来spring security,所以要在sp ...

  5. spring boot整合shiro后,部分注解(Cache缓存、Transaction事务等)失效的问题

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/elonpage/article/details/78965176 前言 整合有缓存.事务的sprin ...

  6. Spring boot整合shiro框架

    ShiroConfiguration package com.energy.common.config; import java.util.LinkedHashMap; import java.uti ...

  7. 上手spring boot项目(二)之spring boot整合shiro安全框架

    题记:在学习了springboot和thymeleaf之后,想完成一个项目练练手,于是使用springboot+mybatis和thymeleaf完成一个博客系统,在完成的过程中出现的一些问题,将这些 ...

  8. Spring Boot 整合 Shiro实现认证及授权管理

    Spring Boot Shiro 本示例要内容 基于RBAC,授权.认证 加密.解密 统一异常处理 redis session支持 介绍 Apache Shiro 是一个功能强大且易于使用的Java ...

  9. spring boot 整合 shiro

    shrio官网:https://shiro.apache.org/ Apache Shiro是一个功能强大且易于使用的Java安全框架,可执行身份验证,授权,加密和会话管理.借助Shiro易于理解的A ...

随机推荐

  1. 简单理解Ext.DomQuery操作CSS3选择器

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Golang(Go语言)内置函数之copy用法

    该函数主要是切片(slice)的拷贝,不支持数组 将第二个slice里的元素拷贝到第一个slice里,拷贝的长度为两个slice中长度较小的长度值 示例: s := []int{1,2,3} fmt. ...

  3. monkeyrunner 进行多设备UI测试

    monkeyrunner进行多设备UI测试  首先你要连接好多个手机设置好已经连接好的手机的ip列表ipp = ['192xxx','192xxx']杀掉之前所有appium进程subprocess. ...

  4. SonarQube搭建和使用教程

    我想使用 SonarQube 查阅代码 请问怎么做,现在只有一个要审查代码的项目

  5. 牛客 Fruit Ninja 2018 ACM 上海大都会赛 (随机化算法)

    题目链接:Fruit Ninja 比赛链接:2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 题目描述 Fruit Ninja is a juicy action game enjoyed ...

  6. centos7简单安装配置mariadb

    CentOS 7下yum安装MariaDB yum install mariadb mariadb-server systemctl start mariadb #启动mariadb systemct ...

  7. 使用Python将字符串转换为格式化的日期时间字符串

    我正在尝试将字符串“20091229050936”转换为“2009年12月29日(UTC)” >>>import time >>>s = time.strptime ...

  8. react-router v4 理解

    1.Router (1)最基础的路由器,必须有history属性 (2)BrowserRouter和HashRouter都是由Router组件扩展而来 2.BrowserRouter (1)Brows ...

  9. redis主从复制和哨兵

    摘自:https://www.cnblogs.com/leeSmall/p/8398401.html 一.Redis主从复制 主从复制:主节点负责写数据,从节点负责读数据,主节点定期把数据同步到从节点 ...

  10. mysql 查询正在执行的sql

    select * from information_schema.`PROCESSLIST` where info is not null; 或者 -- use information_schema; ...