springboot集成shiro实现权限认证
github:https://github.com/peterowang/shiro
基于上一篇:springboot集成shiro实现身份认证
1.加入UserController
package com.example.demo.web; import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; /**
* Created by BFD-593 on 2017/8/9.
*/
@Controller
@RequestMapping("/userinfo")
public class UserController {
/**
* 要查看必须有角色wangjing和有权限userinfo:view
* @return
*/
@RequestMapping("/userList")
@RequiresPermissions({"userinfo:view"})
@RequiresRoles({"wangjing"})
public String userInfo(){
return "userInfo";
} /**
* 用户添加必须有查看和删除权限;
* @return
*/
@RequestMapping("/userAdd")
@RequiresPermissions({"userinfo:view","userinfo:add"})
public String userInfoAdd(){
return "userAdd";
} /**
* 要删除必须有查看和删除权限
* @return
*/
@RequiresPermissions({"userinfo:view","userinfo:del"})
@RequestMapping("/userDel")
public String userInfoDel() {
return "userDel";
} }
2.在ShiroConfiguration中加入spring aop 对shiro注解的支持
/**
* 权限认证
* 需要开启Shiro AOP注解支持
* @RequiresPermissions({"userinfo:view"})
* @RequiresRoles({"wangjing"})等注解的支持
* @param securityManager
* @return
*/
@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){
AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
return authorizationAttributeSourceAdvisor;
}
这时候,我们再登录之后访问http://localhost:8080/userInfo/userDel
就会报org.apache.shiro.authz.
UnauthorizedException异常了。同时后台会打印权限验证的信息。
3.如果想让用户没权限时,进入无权限提示的指定页面可以这么设置在ShiroConfiguration中加入
/**
* 当用户无权限访问403页面而不抛异常,默认shiro会报UnauthorizedException异常
* @return
*/
@Bean
public SimpleMappingExceptionResolver resolver() {
SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver();
Properties properties = new Properties();
properties.setProperty("org.apache.shiro.authz.UnauthorizedException", "/403");
resolver.setExceptionMappings(properties);
return resolver;
}
4.我们也可以设置统一异常处理 添加package:exceptionResolver,添加类MyExceptionResolver
/**同时将该实现类以bean的方式,放到启动类中
* Created by BFD-593 on 2017/8/9.
*/
public class MyExceptionResolver implements HandlerExceptionResolver{
/**
* 统一异常处理,当出现runtimeException时,跳转到500页面。
* @param httpServletRequest
* @param httpServletResponse
* @param o
* @param e
* @return
*/
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
if(e instanceof RuntimeException){
ModelAndView mv = new ModelAndView("/500");
return mv;
}
return null;
}
}
然后在启动类中添加:
/**
* 统一异常处理
* @return
*/
@Bean
public MyExceptionResolver myExceptionResolver(){
return new MyExceptionResolver();
}
这时,当用户没权限时会跳转到403.html,当抛出runtimeexception时跳转到500.html(异常没有被捕获的时候哦...)
以上就是实现当用户访问请求时,验证用户是否拥有该请求的权限,但是,如果我们想页面上的某些标签,用户没有该权限时让他不展示,那该怎么办呢。这里由于用到的是thymeleaf模板,所以我们需要特殊处理:
1.在pom中添加
<!--thymeleaf中使用shiro标签-->
<dependency>
<groupId>com.github.theborakompanioni</groupId>
<artifactId>thymeleaf-extras-shiro</artifactId>
<version>1.2.1</version>
</dependency>
2.在ShiroConfiguration中添加
/**
* 整合thymeleaf中可以使用shiro标签
* @return
*/
@Bean
public ShiroDialect shiroDialect() {
return new ShiroDialect();
}
3.在html中修改
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org"
xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
添加:
<shiro:hasPermission name="userinfo:view">
<shiro:hasRole name="wangjing">
<span>这里是shiro权限 </span>
</shiro:hasRole>
</shiro:hasPermission>
表示当前用户拥有userinfo:view权限并且所属角色是wangjing时,可以看到此<span>标签中的内容。
至此,shiro权限介绍完毕
springboot集成shiro实现权限认证的更多相关文章
- spring-boot整合shiro作权限认证
spring-shiro属于轻量级权限框架,即使spring-security更新换代,市场上大多数企业还是选择shiro 废话不多说 引入pom文件 <!--shiro集成spring--& ...
- springboot集成shiro 实现权限控制(转)
shiro apache shiro 是一个轻量级的身份验证与授权框架,与spring security 相比较,简单易用,灵活性高,springboot本身是提供了对security的支持,毕竟是自 ...
- SpringBoot集成Shiro实现权限控制
Shiro简介 Apache Shiro是一个功能强大且易于使用的Java安全框架,用于执行身份验证,授权,加密和会话管理.使用Shiro易于理解的API,您可以快速轻松地保护任何应用程序-从最小的移 ...
- springboot集成shiro实现权限缓存和记住我
到这节为止,我们已经实现了身份验证和权限验证.但是,如果我们登录之后多次访问http://localhost:8080/userInfo/userDel的话,会发现权限验证会每次都执行一次.这是有问题 ...
- springboot集成shiro实现身份认证
github地址:https://github.com/peterowang/shiro pom文件 <dependencies> <dependency> <group ...
- SpringBoot集成JWT实现权限认证
目录 一.JWT认证流程 二.SpringBoot整合JWT 三.测试 上一篇文章<一分钟带你了解JWT认证!>介绍了JWT的组成和认证原理,本文将介绍下SpringBoot整合JWT实现 ...
- SpringBoot集成Shiro 实现动态加载权限
一.前言 本文小编将基于 SpringBoot 集成 Shiro 实现动态uri权限,由前端vue在页面配置uri,Java后端动态刷新权限,不用重启项目,以及在页面分配给用户 角色 . 按钮 .ur ...
- SpringBoot集成Shiro并用MongoDB做Session存储
之前项目鉴权一直使用的Shiro,那是在Spring MVC里面使用的比较多,而且都是用XML来配置,用Shiro来做权限控制相对比较简单而且成熟,而且我一直都把Shiro的session放在mong ...
- SpringBoot学习笔记(五):SpringBoot集成lombok工具、SpringBoot集成Shiro安全框架
SpringBoot集成lombok工具 什么是lombok? 自动生成setget方法,构造函数,打印日志 官网:http://projectlombok.org/features/index. 平 ...
随机推荐
- 《c# 实现p2p文件分享与传输系统》 二、 设计 - 续(NAT穿透)
c#实现P2P文件分享与传输系统 二.设计 - 续(NAT穿透) 首先要抱歉,因为这些日子较忙,没有写文章,这个系列拖了很久,现在开始继续. 上一篇文章介绍了p2p系统Tracker Server和 ...
- CF1076E:Vasya and a Tree
浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://codeforces.com/problemset/prob ...
- SHOI2001化工厂装箱员——记忆化搜索
题目:https://www.luogu.org/problemnew/show/P2530 太弱了不会用DP,于是暴搜: 每次传进一个数组c记录当前状态各种物品有多少个,枚举取哪种物品,返回最小值, ...
- 使用EA完成数据库设计
开始重构之后对于EA的了解也逐渐增多,今天就总结一下如何使用EA完成对数据库的设计.下边的图分别是截自机房收费系统和牛腩新闻开发的数据库,因为我第一遍写的时候是在机房合作的时候,而后建立牛腩新闻发布系 ...
- Grunt:GruntFile.js
ylbtech-Grunt:GruntFile.js 1.返回顶部 1. module.exports = function (grunt) { grunt.initConfig({ useminPr ...
- CF-828B
B. Black Square time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- VIM显示utf-8乱码
找到vim的根目录下的vimrc文件打开,加入下面三行,保存.重开vim即可. set encoding=utf-8 set langmenu=zh_CN.UTF-8 language message ...
- 扩展thinkphp5的redis类方法
笔者在开发时发现,thinkphp5的自带redis类方法,只有简单的读取缓存.写入缓存的基本方法,远不能满足我们业务的需求.redis本身支持五种数据类型,string(字符串).hash(哈希). ...
- 无监督学习:Linear Dimension Reduction(线性降维)
一 Unsupervised Learning 把Unsupervised Learning分为两大类: 化繁为简:有很多种input,进行抽象化处理,只有input没有output 无中生有:随机给 ...
- php如何运行
这篇文章,研究一下php代码是如何解释和执行以及PHP脚本运行的生命周期. 概述 PHP服务的启动.严格来说,PHP的相关进程是不需要手动启动的,它是随着Apache的启动而运行的.当然,如果有需要重 ...