springboot整合shiro (四) shiro实现权限授权

1. ShiroConfig中添加授权过滤器

在这里插入图片描述

filterMap.put("/add", "perms[user:add]");
filterMap.put("/update", "perms[user:update]");
// 设置未授权页面
filterFactoryBean.setUnauthorizedUrl("/unAuth");

这个时候我们登录点击add或者update 就会跳转到未授权页面
在这里插入图片描述

2. 给用户授权

在这里插入图片描述
我们只需要一个 SimpleAhtuorizationInfo 并给他设置授权权限并返回即可
这样add页面就可以访问, update依旧没有权限

3. 关联数据库

在这里插入图片描述
数据库中写一个简单的权限
在这里插入图片描述
然后我们直接去获取用户的权限信息就可以实现权限拦截
数据库中有两个账号, admin这个账号只可以访问 add, 而 root 只可以访问 update

4. shiro和thymeleaf整合使用

1. 引入thymeleaf对shiro拓展的依赖
<!-- https://mvnrepository.com/artifact/com.github.theborakompanioni/thymeleaf-extras-shiro -->
<dependency>
    <groupId>com.github.theborakompanioni</groupId>
    <artifactId>thymeleaf-extras-shiro</artifactId>
    <version>2.0.0</version>
</dependency>

2. 在ShiroConfig中配置 ShiroDialect 用于 shiro和thymeleaf配合使用
/**
     * 配置 shiroDialect 用于thymeleaf和
     * @return
     */
    @Bean
    public ShiroDialect getShiroDialect() {
        return new ShiroDialect();
    }
3. 在thymeleaf中使用shiro标签

在这里插入图片描述
首页分别用div套上加上shiro:hasPermission 即可实现,当我们用admin登录只有添加显示, root则只有update, 否则啥也没有
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值