Spring boot 加入shiro支持
在项目添加依赖
<!-- shiro spring. -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.0</version>
</dependency>
<!-- shiro core -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.4.0</version>
</dependency>
配置文件目录下新建spring文件夹,在文件夹内新建spring-shiro.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- ref对应我们写的realm myRealm -->
<property name="realm" ref="AuthRealm" />
<!-- 使用下面配置的缓存管理器 -->
<!-- <property name="cacheManager" ref="shiroEncacheManager" /> -->
</bean> <!-- 安全认证过滤器 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 调用我们配置的权限管理器 -->
<property name="securityManager" ref="securityManager" />
<!-- 配置我们的登录请求地址 -->
<property name="loginUrl" value="/toLogin" />
<!-- 配置我们在登录页登录成功后的跳转地址,如果你访问的是非/login地址,则跳到您访问的地址 -->
<property name="successUrl" value="/" />
<!-- 如果您请求的资源不再您的权限范围,则跳转到/403请求地址 -->
<property name="unauthorizedUrl" value="/html/403.html" />
<property name="filterChainDefinitions">
<value>
<!-- anon是允许通过 authc相反 -->
/statics/**=anon
/login=anon
/** = authc
</value>
</property>
</bean> <!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" /> <!-- AOP式方法级权限检查 -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
<property name="proxyTargetClass" value="true" />
</bean>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager" />
</bean> </beans>
在主入口加载spring-shiro.xml
@ImportResource({ "classpath:spring/spring-shiro.xml" })
在登录Controller内更改成
//构造登录参数
UsernamePasswordToken token = new UsernamePasswordToken(name, pwd);
try {
//交给Realm类处理
SecurityUtils.getSubject().login(token);
} catch (UnknownAccountException uae) {
map.put("msg", "未知用户");
return "login";
} catch (IncorrectCredentialsException ice) {
map.put("msg", "密码错误");
return "login";
} catch (AuthenticationException ae) {
// unexpected condition? error?
map.put("msg", "服务器繁忙");
return "login";
} return "redirect:/toIndex";
看5行就知道登录交给了Realm类处理了,所以我们要有Realm类
在可以被主入口扫描到的地方新建AuthRealm类并且继承AuthorizingRealm,重写doGetAuthenticationInfo(登录逻辑),重写doGetAuthorizationInfo(授权逻辑)
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection; public class AuthRealm extends AuthorizingRealm { @Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
// TODO Auto-generated method stub
return null;
} @Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
// TODO Auto-generated method stub
return null;
} }
登录验证在doGetAuthenticationInfo方法写入
// 获取Token
UsernamePasswordToken token2 = (UsernamePasswordToken) token;
//获取用户名
String userName = token2.getUsername();
//获取密码
String pwd = new String(token2.getPassword());
//下面我使用的是MyBatis-puls3.0
//查询条件对象
QueryWrapper<User> queryWrapper = new QueryWrapper();
//查询该用户
queryWrapper.eq("name", userName).or().eq("phone", userName);
//查询
User user = iUserService.getOne(queryWrapper);
//查回的对象为空
if (CommonUtil.isBlank(user)) {
//抛出未知的账户异常
throw new UnknownAccountException();
}
//查回的对象密码和输入密码不相等
if (!CommonUtil.isEquals(user.getPwd(), pwd)) {
//抛出凭证不正确异常
throw new IncorrectCredentialsException();
}
//上面都通过了就说明该用户存在并且密码相等
// 验证成功了
SecurityUtils.getSubject().getSession().setAttribute(Constant.SESSION_USER_KEY, user);
// 返回shiro用户信息
// token传过来的密码,一定要跟验证信息传进去的密码一致,加密的密码一定要加密后传过来 return new SimpleAuthenticationInfo(user, user.getPwd(), getName());
如果要设置权限,就在对应的Controllerf方法加上
@RequiresPermissions("/system/user/list")
再doGetAuthorizationInfo方法内写
//创建简单的授权信息对象
SimpleAuthorizationInfo simpleAuthorizationInfo=new SimpleAuthorizationInfo();
//授予权限
simpleAuthorizationInfo.addStringPermission("/system/user/list"); return simpleAuthorizationInfo;
当所有Controller都加了@RequiresPermissions注解后,如果访问到没有授权的Controller会报错。
Spring boot 加入shiro支持的更多相关文章
- Spring Boot 添加Shiro支持
前言: Shiro是一个权限.会话管理的开源Java安全框架:Spring Boot集成Shiro后可以方便的使用Session: 工程概述: (工程结构图) 一.建立Spring Boot工程 参照 ...
- 快速搭建Spring Boot + Apache Shiro 环境
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一.Apache Shiro 介绍及概念 概念:Apache Shiro是一个强大且易用的Java安全框 ...
- 基于Spring Boot和Shiro的后台管理系统FEBS
FEBS是一个简单高效的后台权限管理系统.项目基础框架采用全新的Java Web开发框架 —— Spring Boot 2.0.3,消除了繁杂的XML配置,使得二次开发更为简单:数据访问层采用Myba ...
- Spring Boot 集成Shiro和CAS
Spring Boot 集成Shiro和CAS 标签: springshirocas 2016-01-17 23:03 35765人阅读 评论(22) 收藏 举报 分类: Spring(42) 版 ...
- Spring Boot 添加JSP支持【转】
Spring Boot 添加JSP支持 大体步骤: (1) 创建Maven web project: (2) 在pom.xml文件添加依赖: (3) ...
- Spring Boot 整合 Shiro ,两种方式全总结!
在 Spring Boot 中做权限管理,一般来说,主流的方案是 Spring Security ,但是,仅仅从技术角度来说,也可以使用 Shiro. 今天松哥就来和大家聊聊 Spring Boot ...
- Spring Boot 之FilterRegistrationBean --支持web Filter 排序的使用(转)
Spring Boot 之FilterRegistrationBean --支持web Filter 排序的使用Spring 提供了FilterRegistrationBean类,此类提供setOr ...
- 解决Spring Boot集成Shiro,配置类使用Autowired无法注入Bean问题
如题,最近使用spring boot集成shiro,在shiroFilter要使用数据库动态给URL赋权限的时候,发现 @Autowired 注入的bean都是null,无法注入mapper.搜了半天 ...
- Springboot 系列(十七)迅速使用 Spring Boot Admin 监控你的 Spring Boot 程序,支持异常邮件通知
1. Spring Boot Admin 是什么 Spring Boot Admin 是由 codecentric 组织开发的开源项目,使用 Spring Boot Admin 可以管理和监控你的 S ...
随机推荐
- B/S 端基于 HTML5 + WebGL 的 VR 3D 机房数据中心可视化
前言 在 3D 机房数据中心可视化应用中,随着视频监控联网系统的不断普及和发展, 网络摄像机更多的应用于监控系统中,尤其是高清时代的来临,更加快了网络摄像机的发展和应用. 在监控摄像机数量的不断庞大的 ...
- 楼上请让路 RoarCTF2019 Writeup
Misc 签到题 RoarCTF{签到!!!} 黄金六年 文件尾部有一段base64,解码为16进制可以看到是一个压缩包 打开压缩包需要密码 使用pr抽帧 可以看到部分帧中有二维码,依次扫码即可得到k ...
- Orleans 知多少 | 3. Hello Orleans
1. 引言 是的,Orleans v3.0.0 已经发布了,并已经完全支持 .NET Core 3.0. 所以,Orleans 系列是时候继续了,抱歉,让大家久等了. 万丈高楼平地起,这一节我们就先来 ...
- 百万年薪python之路 -- 面向对象之三大特性
1.面向对象之三大特性 1.1封装 封装:就是把一堆代码和数据,放在一个空间,并且可以使用 对于面向对象的封装来说,其实就是使用构造方法将内容封装到 对象 中,然后通过对象直接或者self间接获取被封 ...
- 【Java必修课】四类方法删除List里面的所有null值
1 简介 万恶的null已经折磨程序员许久了,也带来了许多难以发现却造成严重损失的NullPointerException.我们需要尽可能的避免它,有一种简单的办法就是在它进入下轮处理前,我们就把它扼 ...
- 实战--dango自带的分页(极简)
注意,我将templates定义在项目的同级目录下: 在settings.py中配置 TEMPLATES = [ { 'BACKEND': 'django.template.backends.djan ...
- vue表单属性
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- MySQL、Oracle、SqlServer的区别
鉴于和数据库打交道日益频繁,遂决定写一篇关于Oracle.SqlServer.MySQL区别的个人观点. MySQL是大学时的主要学习对象,但刚参加工作时转到了SqlServer,现在主要接触的是Or ...
- 转载:全面理解面向对象的 JavaScript
来源:DeveloperWorks – 曾滢著 简介: JavaScript 函数式脚本语言特性以及其看似随意的编写风格,导致长期以来人们对这一门语言的误解,即认为 JavaScript 不是一门面向 ...
- K近邻(k-Nearest Neighbor,KNN)算法,一种基于实例的学习方法
1. 基于实例的学习算法 0x1:数据挖掘的一些相关知识脉络 本文是一篇介绍K近邻数据挖掘算法的文章,而所谓数据挖掘,就是讨论如何在数据中寻找模式的一门学科. 其实人类的科学技术发展的历史,就一直伴随 ...