【Shiro】08 SpringBoot整合
需要的依赖的坐标:
<!-- Shiro依赖 -->
<dependency>
<groupId>com.github.theborakompanioni</groupId>
<artifactId>thymeleaf-extras-shiro</artifactId>
<version>2.0.0</version>
</dependency> <dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring-boot-web-starter</artifactId>
<version>1.5.3</version>
</dependency> <!-- Thymeleaf模版引擎 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency> <dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency> <!-- web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
【配置编写】
先编写自定义Realm:
package cn.dai.shiro; import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection; /**
* @author DaiZhiZhou
* @file Shiro
* @create 2020-08-01 22:44
*/
public class UserRealm extends AuthorizingRealm { @Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
return null;
} @Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { String principal = authenticationToken.getPrincipal().toString(); if ("xxx".equals(principal)) return new SimpleAuthenticationInfo(principal, "123456", this.getName()); return null;
} }
再编写配置类:
package cn.dai.config; import cn.dai.shiro.UserRealm;
import org.apache.shiro.realm.Realm;
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; /**
* @author DaiZhiZhou
* @file Shiro
* @create 2020-08-01 22:39
*/
@Configuration
public class ShiroConfiguration { @Bean("realm") /* 自定义Realm配置 */
public Realm getRealm() {
return new UserRealm();
} @Bean("defaultWebSecurityManager") /* 注入安全管理器 */
public DefaultWebSecurityManager getDefaultWebSecurityManager(Realm realm) {
DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
defaultWebSecurityManager.setRealm(realm);
return defaultWebSecurityManager;
} @Bean("shiroFilterFactoryBean") /* 注入Shiro过滤器工厂Bean */
public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager) {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
return shiroFilterFactoryBean;
}
}
配置Controller:
package cn.dai.controller; import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpSession; /**
* @author DaiZhiZhou
* @file Shiro
* @create 2020-08-01 22:53
*/
@Controller
public class AccessController { @RequestMapping("logout")
public String logout() {
SecurityUtils.getSubject().logout();
return "redirect:/loginview"; // 账号退出,重定向到登录页
} @RequestMapping("loginview")
public String login() {
return "login"; // 跳转登陆页面
} @RequestMapping("login")
public String login(String username, String password, HttpSession session) {
try {
Subject subject = SecurityUtils.getSubject();
subject.login(new UsernamePasswordToken(username, password));
return "redirect:/index"; // login方法执行没出现异常,登陆正常
} catch (UnknownAccountException unknownAccountException) {
unknownAccountException.printStackTrace();
System.out.println("用户名错误");
} catch (IncorrectCredentialsException incorrectCredentialsException) {
incorrectCredentialsException.printStackTrace();
System.out.println("密码错误");
} catch (Exception exception) {
exception.printStackTrace();
} return "redirect:/loginview";
} }
然后再更改权限控制:
package cn.dai.config; import cn.dai.shiro.UserRealm;
import org.apache.shiro.realm.Realm;
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; import java.util.HashMap; /**
* @author DaiZhiZhou
* @file Shiro
* @create 2020-08-01 22:39
*/
@Configuration
public class ShiroConfiguration { @Bean("realm") /* 自定义Realm配置 */
public Realm getRealm() {
return new UserRealm();
} @Bean("defaultWebSecurityManager") /* 注入安全管理器 */
public DefaultWebSecurityManager getDefaultWebSecurityManager(Realm realm) {
DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
defaultWebSecurityManager.setRealm(realm);
return defaultWebSecurityManager;
} @Bean("shiroFilterFactoryBean") /* 注入Shiro过滤器工厂Bean */
public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager) {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager); HashMap<String, String> map = new HashMap<>(); map.put("/login", "anon"); // 登录shiro控制程序,随意访问
map.put("/loginview", "anon"); // 登录页面随意访问
map.put("/logout", "anon"); //退出页面随意访问
map.put("/**", "authc"); // 其余资源都必须授权访问 shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
shiroFilterFactoryBean.setLoginUrl("/login");
return shiroFilterFactoryBean;
}
}
【Shiro】08 SpringBoot整合的更多相关文章
- 18.Shiro与Springboot整合下登陆验证UserService未注入的问题
Shiro与Springboot整合下登陆验证UserService未注入的问题 前言: 刚开始整合的情况下,UserService一执行,就会报空指针异常. 看了网上各位大神的讲解,什么不能用ser ...
- 【SpringBoot】08.SpringBoot整合jsp
SpringBoot整合jsp 1.修改pom文件加入两个坐标jstl标签库和jasper <project xmlns="http://maven.apache.org/POM/4. ...
- SpringBoot系列十二:SpringBoot整合 Shiro
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合 Shiro 2.具体内容 Shiro 是现在最为流行的权限认证开发框架,与它起名的只有最初 ...
- 补习系列(6)- springboot 整合 shiro 一指禅
目标 了解ApacheShiro是什么,能做什么: 通过QuickStart 代码领会 Shiro的关键概念: 能基于SpringBoot 整合Shiro 实现URL安全访问: 掌握基于注解的方法,以 ...
- SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例
1.前言 本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管理系统实例. 使用技术:SpringBoot.mybatis.shiro.thymeleaf.pagehelp ...
- SpringBoot整合Shiro实现基于角色的权限访问控制(RBAC)系统简单设计从零搭建
SpringBoot整合Shiro实现基于角色的权限访问控制(RBAC)系统简单设计从零搭建 技术栈 : SpringBoot + shiro + jpa + freemark ,因为篇幅原因,这里只 ...
- 转:30分钟了解Springboot整合Shiro
引自:30分钟了解Springboot整合Shiro 前言:06年7月的某日,不才创作了一篇题为<30分钟学会如何使用Shiro>的文章.不在意之间居然斩获了22万的阅读量,许多人因此加了 ...
- springboot整合Shiro功能案例
Shiro 核心功能案例讲解 基于SpringBoot 有源码 从实战中学习Shiro的用法.本章使用SpringBoot快速搭建项目.整合SiteMesh框架布局页面.整合Shiro框架实现用身份认 ...
- springboot系列(十)springboot整合shiro实现登录认证
关于shiro的概念和知识本篇不做详细介绍,但是shiro的概念还是需要做做功课的要不无法理解它的运作原理就无法理解使用shiro: 本篇主要讲解如何使用shiro实现登录认证,下篇讲解使用shiro ...
- SpringBoot整合Shiro实现权限控制,验证码
本文介绍 SpringBoot 整合 shiro,相对于 Spring Security 而言,shiro 更加简单,没有那么复杂. 目前我的需求是一个博客系统,有用户和管理员两种角色.一个用户可能有 ...
随机推荐
- RT-Thread Studio使用教程
介绍 RT-Thread Studio是官方出品的一款专门针对RT-Thread嵌入式开发.部署.调试.测试的集成开发环境,它基于Eclipse开源项目开发,极大的提高了嵌入式开发者的开发效率,目前最 ...
- 你知道键盘是如何工作的吗?(xv6键盘驱动程序)
键盘驱动程序 公众号:Rand_cs 键盘如何工作的前文曾经说过,当时是以 Linux 0.11 为基础讲的但不系统,本文以 xv6 的键盘驱动程序为例来系统地讲述键盘是如何工作的.关于驱动程序前文磁 ...
- log4net info 方法 根据不同业务创建不同的文件名
log4net info 方法 根据不同业务创建不同的文件名 <configuration> <!-- ... 其他配置 ... --> <configSections& ...
- FlashDuty Changelog 2023-10-30 | 告警路由与 Slack 应用
FlashDuty:一站式告警响应平台,前往此地址免费体验! 告警路由 什么是告警路由? FlashDuty已经与Zabbix.Prometheus等监控系统实现无缝集成,通过一个简单的webhook ...
- C# .NET core Avalonia 11.0版本,发布linux和MAC的简单记录
.net core 7.0+centos 7.0 cetnos目前运行在hyper V虚拟机里 虚拟机部署的注意事项 1 需要配置网络环境, 确保在同一局域网下 如果sftp无法连接 ctrl+shi ...
- mysql数据迁移-8.0.25
本文只简单描述一些逻辑迁移的问题,而且主要是针对开发过程中,小批量数据(例如100m之下的). 这几天装了个新的mysql8.0.25 64bit windows版本的. -- 看的出来oracle公 ...
- anaconda里虚拟环境安装jupyter notebook
安装jupyter notebook 打开anaconda prompt,进入虚拟环境 conda activate Pytorch_learning 下载安装jupyter notebook con ...
- 初识 SpringMVC,运行配置第一个Spring MVC 程序
1. 初识 SpringMVC,运行配置第一个Spring MVC 程序 @ 目录 1. 初识 SpringMVC,运行配置第一个Spring MVC 程序 1.1 什么是 MVC 2. Spring ...
- Java实现管线拓扑关系连通性分析
管线拓扑关系的连通性分析通常涉及图论(Graph Theory)中的概念,特别是无向图(Undirected Graph)的遍历算法,如深度优先搜索(DFS, Depth-First Search)或 ...
- 总结:软件开发的3个方向 与 嵌入式Linux学习路线(驱动方向)
--- title: 嵌入式Linux学习路线图(驱动方向) date: 2020-05-09 07:17:58 categories: tags: - embeded - summary - arm ...