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实现权限认证的更多相关文章

  1. spring-boot整合shiro作权限认证

    spring-shiro属于轻量级权限框架,即使spring-security更新换代,市场上大多数企业还是选择shiro 废话不多说  引入pom文件 <!--shiro集成spring--& ...

  2. springboot集成shiro 实现权限控制(转)

    shiro apache shiro 是一个轻量级的身份验证与授权框架,与spring security 相比较,简单易用,灵活性高,springboot本身是提供了对security的支持,毕竟是自 ...

  3. SpringBoot集成Shiro实现权限控制

    Shiro简介 Apache Shiro是一个功能强大且易于使用的Java安全框架,用于执行身份验证,授权,加密和会话管理.使用Shiro易于理解的API,您可以快速轻松地保护任何应用程序-从最小的移 ...

  4. springboot集成shiro实现权限缓存和记住我

    到这节为止,我们已经实现了身份验证和权限验证.但是,如果我们登录之后多次访问http://localhost:8080/userInfo/userDel的话,会发现权限验证会每次都执行一次.这是有问题 ...

  5. springboot集成shiro实现身份认证

    github地址:https://github.com/peterowang/shiro pom文件 <dependencies> <dependency> <group ...

  6. SpringBoot集成JWT实现权限认证

    目录 一.JWT认证流程 二.SpringBoot整合JWT 三.测试 上一篇文章<一分钟带你了解JWT认证!>介绍了JWT的组成和认证原理,本文将介绍下SpringBoot整合JWT实现 ...

  7. SpringBoot集成Shiro 实现动态加载权限

    一.前言 本文小编将基于 SpringBoot 集成 Shiro 实现动态uri权限,由前端vue在页面配置uri,Java后端动态刷新权限,不用重启项目,以及在页面分配给用户 角色 . 按钮 .ur ...

  8. SpringBoot集成Shiro并用MongoDB做Session存储

    之前项目鉴权一直使用的Shiro,那是在Spring MVC里面使用的比较多,而且都是用XML来配置,用Shiro来做权限控制相对比较简单而且成熟,而且我一直都把Shiro的session放在mong ...

  9. SpringBoot学习笔记(五):SpringBoot集成lombok工具、SpringBoot集成Shiro安全框架

    SpringBoot集成lombok工具 什么是lombok? 自动生成setget方法,构造函数,打印日志 官网:http://projectlombok.org/features/index. 平 ...

随机推荐

  1. Data Grip 使用--->创建数据库连接

    1.  简介 Data Grip 是一款类似于Workbench的数据库设计工具,可以用来对常用的数据管理系统(MySQL/Oracle/Postgresql...)进行操作. 2.  利用DataG ...

  2. 洛谷P3386——二分图匹配

    题目:https://www.luogu.org/problemnew/show/P3386 二分图匹配模板,注意左部点只dfs未匹配点. 代码如下: #include<iostream> ...

  3. Oracle日期范围

    一.SQL语句: select to_date(to_char(to_date('2017-10-01', 'yyyy-mm-dd') + rownum - 1, 'yyyy-mm-dd'), 'yy ...

  4. 最小化安装linux CentOS-6.6后 部署fastdfs +下载地址 很干很干的干货

    参考:http://blog.itpub.net/7734666/viewspace-1292485/ 安装一些必要软件 yum -y install wget gcc perl mkdir ~/zy ...

  5. 0001_第一个测试小程序Login

    # -*- coding:utf-8 -*- user = raw_input("Username:") password = raw_input("Password:& ...

  6. Robot FrameWork基础学习(四) 元素定位

    元素定位 对于web自动化测试来说,就是操作页面的各种元素,在操作元素之间需要先找到元素,换句话说就是定位元素. Selenium2Library提供了非常丰富的定位器: 虽然提供了这么多种定位方式, ...

  7. 面试题: 数据库笔试 sql操作 已看 上课的练习题50sql

    2018/5/31 oracle数据库面试笔试试题总结http://www.yjbys.com/qiuzhizhinan/show-308759.html 1/4Oracle数据库1.基础测试选择在部 ...

  8. Uncommon Words from Two Sentences

    https://leetcode.com/problems/uncommon-words-from-two-sentences We are given two sentences A and B.  ...

  9. Windows下git的安装与配置

    表示git安装成功.

  10. cassandra的命令

    cassandra的命令: connect <hostname>/<port> (<username> '<password>')?;    Conne ...