Spring Boot2 整合 Shiro ,两种方式全总结!
前言:在 Spring Boot 中做权限管理,一般来说,主流的方案是 Spring Security ,但是,仅仅从技术角度来说,也可以使用 Shiro。
文章目录
- 一、Spring Security 和 Shiro 的比较
- 二、原生的整合
- 2.1. 创建一个 Spring Boot 项目
- 2.1. 加入 Shiro 相关的依赖
- 2.2. 完整pom
- 2.3. 自定义核心组件 Realm
- 2.4. 配置 Shiro
- 2.5. 配置登录 Controller
- 三、测试验证
- 3.1. 首先访问 /hello 接口
- 3.2. 然后调用 /doLogin 接口完成登录
- 3.3. 再次访问 /hello 接口
- 四、使用 Shiro Starter
- 4.1. 创建工程,和上面的一样
- 4.2. pom
- 4.3. 创建 Realm
- 4.4. 配置 Shiro 基本信息
- 4.5. 配置 ShiroConfig
- 五、总结
今天gblfy就来和大家聊聊 Spring Boot 整合 Shiro 的话题!
一、Spring Security 和 Shiro 的比较
一般来说,Spring Security 和 Shiro 的比较如下:
Spring Security | Shiro |
---|---|
是一个重量级的安全管理框架 | 则是一个轻量级的安全管理框架 |
概念复杂,配置繁琐 | 概念简单、配置简单 |
功能强大 | 功能简单 |
虽然 Shiro 功能简单,但是也能满足大部分的业务场景。所以在传统的 SSM 项目中,一般来说,可以整合 Shiro。
在 Spring Boot 中,由于 Spring Boot 官方提供了大量的非常方便的开箱即用的 Starter ,当然也提供了 Spring Security 的 Starter ,使得在 Spring Boot 中使用 Spring Security 变得更加容易,甚至只需要添加一个依赖就可以保护所有的接口,所以,如果是 Spring Boot 项目,一般选择 Spring Security 。
这只是一个建议的组合,单纯从技术上来说,无论怎么组合,都是没有问题的。
在 Spring Boot 中整合 Shiro ,有两种不同的方案:
第一种就是原封不动的,将 SSM 整合 Shiro 的配置用 Java 重写一遍。
第二种就是使用 Shiro 官方提供的一个 Starter 来配置,但是,这个 Starter 并没有简化多少配置。
二、原生的整合
2.1. 创建一个 Spring Boot 项目
只需要添加 Web 依赖即可
2.1. 加入 Shiro 相关的依赖
<dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-web</artifactId><version>1.4.0</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-spring</artifactId><version>1.4.0</version></dependency>
2.2. 完整pom
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-web</artifactId><version>1.4.0</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-spring</artifactId><version>1.4.0</version></dependency>
2.3. 自定义核心组件 Realm
package com.gblfy.realm;import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;/*** @author gblfy* @Description https://www.gblfy.com* @Date 2019/12/8 15:45*/
public class MyRealm extends AuthorizingRealm {@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {return null;}@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {String username = (String) token.getPrincipal();if (!"gblfy".equals(username)) {throw new UnknownAccountException("账户不存在!");}return new SimpleAuthenticationInfo(username, "123", getName());}
}
在 Realm 中实现简单的认证操作即可,不做授权,授权的具体写法和 SSM 中的 Shiro 一样,不赘述。这里的认证表示用户名必须是 javaboy ,用户密码必须是 123 ,满足这样的条件,就能登录成功!
2.4. 配置 Shiro
package com.gblfy.config;import com.gblfy.realm.MyRealm;
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;import java.util.LinkedHashMap;
import java.util.Map;/*** @author gblfy* @Description https://www.gblfy.com* @Date 2019/12/8 15:45* <p>* 在这里进行 Shiro 的配置* Shiro 的配置主要配置 3 个 Bean* <p>* 1. 首先需要提供一个 Realm 的实例* 2. 需要配置一个 SecurityManager,在 SecurityManager 中配置 Realm* 3. 配置一个 ShiroFilterFactoryBean ,在 ShiroFilterFactoryBean 中指定路径拦截规则等*/
@Configuration
public class ShiroConfig {@BeanMyRealm myRealm() {return new MyRealm();}@BeanSecurityManager securityManager() {DefaultWebSecurityManager manager = new DefaultWebSecurityManager();manager.setRealm(myRealm());return manager;}@BeanShiroFilterFactoryBean shiroFilterFactoryBean() {ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();//指定 SecurityManagerbean.setSecurityManager(securityManager());//登录页面bean.setLoginUrl("/login");//登录成功页面bean.setSuccessUrl("/index");//访问未获授权路径时跳转的页面bean.setUnauthorizedUrl("/unauthorizedurl");//配置路径拦截规则,注意,要有序Map<String, String> map = new LinkedHashMap<>();map.put("/doLogin", "anon");map.put("/**", "authc");bean.setFilterChainDefinitionMap(map);return bean;}
}
在这里进行 Shiro 的配置主要配置 3 个 Bean :
①首先需要提供一个 Realm 的实例。
②需要配置一个 SecurityManager,在 SecurityManager 中配置 Realm。
③配置一个 ShiroFilterFactoryBean ,在 ShiroFilterFactoryBean 中指定路径拦截规则等。
④配置登录和测试接口。
其中,ShiroFilterFactoryBean 的配置稍微多一些,配置含义如下:
①setSecurityManager 表示指定 SecurityManager。
②setLoginUrl 表示指定登录页面。
③setSuccessUrl 表示指定登录成功页面。
④接下来的 Map 中配置了路径拦截规则,注意,要有序。
2.5. 配置登录 Controller
package com.gblfy.controller;import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author gblfy* @Description https://www.gblfy.com* @Date 2019/12/8 15:45*/
@RestController
public class LoginController {/*** 登录方法** @param username* @param password*/@PostMapping("/doLogin")public String doLogin(String username, String password) {Subject subject = SecurityUtils.getSubject();try {subject.login(new UsernamePasswordToken(username, password));return "登录成功!";} catch (AuthenticationException e) {e.printStackTrace();return "登录失败!";}}/*** 测试方法** @return*/@GetMapping("/hello")public String helloWord() {return "helloWord";}/*** 统一拦截登录页面** @return*/@GetMapping("/login")public String login() {return "please login!";}
}
三、测试验证
测试时,首先访问 /hello 接口,由于未登录,所以会自动跳转到 /login 接口
3.1. 首先访问 /hello 接口
http://localhost:8080/hello
3.2. 然后调用 /doLogin 接口完成登录
http://localhost:8080/doLogin?username=gblfy&password=123
3.3. 再次访问 /hello 接口
就可以成功访问了:
四、使用 Shiro Starter
上面这种配置方式实际上相当于把 SSM 中的 XML 配置拿到 Spring Boot 中用 Java 代码重新写了一遍,除了这种方式之外,我们也可以直接使用 Shiro 官方提供的 Starter 。
4.1. 创建工程,和上面的一样
4.2. pom
创建成功后,添加 shiro-spring-boot-web-starter ,这个依赖可以代替之前的 shiro-web 和 shiro-spring 两个依赖,pom.xml 文件如下:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-spring-boot-web-starter</artifactId><version>1.4.0</version></dependency>
4.3. 创建 Realm
这里的 Realm 和前面的一样,我就不再赘述。
4.4. 配置 Shiro 基本信息
接下来在 application.properties 中配置 Shiro 的基本信息:
shiro:sessionManager:sessionIdCookieEnabled: truesessionIdUrlRewritingEnabled: trueunauthorizedUrl: /unauthorizedurlweb:enabled: truesuccessUrl: /indexloginUrl: /login# 第一行表示是否允许将sessionId 放到 cookie 中
# 第二行表示是否允许将 sessionId 放到 Url 地址拦中
# 第三行表示访问未获授权的页面时,默认的跳转路径
# 第四行表示开启 shiro
# 第五行表示登录成功的跳转页面
# 第六行表示登录页面
4.5. 配置 ShiroConfig
package com.gblfy.config;import com.gblfy.realm.MyRealm;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @author gblfy* @Description https://www.gblfy.com* @Date 2019/12/8 15:4** 在这里进行 Shiro 的配置* Shiro 的配置主要配置 3 个 Bean 。** 1. 首先需要提供一个 Realm 的实例* 2. 需要配置一个 SecurityManager,在 SecurityManager 中配置 Realm* 3. 配置一个 ShiroFilterFactoryBean ,在 ShiroFilterFactoryBean 中指定路径拦截规则等*/
@Configuration
public class ShiroConfig {@BeanMyRealm myRealm() {return new MyRealm();}@BeanDefaultWebSecurityManager securityManager() {DefaultWebSecurityManager manager = new DefaultWebSecurityManager();manager.setRealm(myRealm());return manager;}@BeanShiroFilterChainDefinition shiroFilterChainDefinition() {DefaultShiroFilterChainDefinition definition = new DefaultShiroFilterChainDefinition();definition.addPathDefinition("/doLogin", "anon");definition.addPathDefinition("/**", "authc");return definition;}
}
这里的配置和前面的比较像,但是不再需要 ShiroFilterFactoryBean 实例了,替代它的是 ShiroFilterChainDefinition ,在这里定义 Shiro 的路径匹配规则即可。
这里定义完之后,接下来的登录接口定义以及测试方法都和前面的一致,我就不再赘述了。大家可以参考上文。
五、总结
本文主要向大家介绍了 Spring Boot 整合 Shiro 的两种方式,一种是传统方式的 Java 版,另一种则是使用 Shiro 官方提供的 Starter,两种方式,不知道大家有没有学会呢?
本文案例,我已经上传到 码云,欢迎大家 star:
https://gitee.com/gb_90/SpringBoot2_Practical_Column
Spring Boot2 整合 Shiro ,两种方式全总结!相关推荐
- Spring Boot2整合Shiro(1):身份认证
Spring Boot2整合Shiro(1):身份认证 前言 本文主要介绍了在Spring Boot2项目中整合Shiro实现登录认证.本文假设读者已经对Shiro和基于RBAC的权限控制系统有了基本 ...
- spring配置属性的两种方式
spring配置属性有两种方式,第一种方式通过context命名空间中的property-placeholder标签 <context:property-placeholder location ...
- apache shiro怎么升级_Spring Boot 整合 Shiro ,两种方式全总结!
在 Spring Boot 中做权限管理,一般来说,主流的方案是 Spring Security ,但是,仅仅从技术角度来说,也可以使用 Shiro. 一般来说,Spring Security 和 S ...
- Spring系列教程八: Spring实现事务的两种方式
2019独角兽企业重金招聘Python工程师标准>>> 一. Spring事务概念: 事务是一系列的动作,它们综合在一起才是一个完整的工作单元,这些动作必须全部完成,如果有一个失败的 ...
- spring 注入bean的两种方式
我们都知道,使用spring框架时,不用再使用new来实例化对象了,直接可以通过spring容器来注入即可. 而注入bean有两种方式: 一种是通过XML来配置的,分别有属性注入.构造函数注入和工厂方 ...
- Spring定义Bean的两种方式:<bean>和@Bean
前言: Spring中最重要的概念IOC和AOP,实际围绕的就是Bean的生成与使用. 什么叫做Bean呢?我们可以理解成对象,每一个你想交给Spring去托管的对象都可以称之为Bean. 今天通过S ...
- spring事务管理的两种方式
一.注解式事务 1.注解式事务在平时的开发中使用的挺多,工作的两个公司中看到很多项目使用了这种方式,下面看看具体的配置demo. 2.事务配置实例 (1).spring+mybatis 事务配置 &l ...
- Spring依赖注入的两种方式(根据实例详解)
1,Set注入 2,构造注入 Set方法注入: 原理:通过类的setter方法完成依赖关系的设置 name属性的取值依setter方法名而定,要求这个类里面这个对应的属性必须有setter方法. ...
- spring aop日志(两种方式)
第一种方式(全注解): <!-- 定义事务管理器(声明式的事务) --> <bean id="transactionManager" ...
最新文章
- react-antd项目中重新npm install 导致自动升级antd版本,引发的样式问题
- HDLBits 系列(42)根据仿真波形来设计电路之时序逻辑
- 皮一皮:今年的网友不够优秀啊。。。
- Spring Boot与数据访问
- linux的du使用方法
- 街霸5 android,MD街头霸王5免安装版
- 【转】crc16几种标准校验算法及c语言代码
- 强烈建议你把这5个跨境神器都收藏了
- H5 MediaDevices方法,调用摄像头、屏幕录像功能
- Jmeter介绍与使用
- 服务器数据库异常MySQL_服务器 mysql数据库异常
- Android 3.1更新后的警告
- 磁偏角测试仪TY3300永磁材料磁偏角测量
- rz waiting to receive. Starting zmodem transfer. Press Ctrl+C to cancel.
- 计算机辅助设计毕业论文,快速阀的计算机辅助设计毕业设计.doc
- AI面相手相V3.2.0无限多开版H5公众号版源码
- 转变技术指导重点江苏水稻穗期田管 国稻种芯百团计划行动
- 渗透技巧之403绕过_指纹识别
- 《强化学习周刊》第58期:RFQI、DRL-DBSCAN广义强化学习
- 下单小程序怎么做呢?