快速掌握shiro安全框架(五)之异常处理、前端权限控制

1、无权限时候会报错,对于前后端来说都不是很好的事情,故此我们可以做异常处理,无权限时候可以进行自定义处理,在controller中创建ExceptionController

package com.wxg.springbootshiro.controller;

import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class ExceptionController {

    @ResponseBody
    @ExceptionHandler(UnauthorizedException.class)
    public String unauthorizedException(Exception ex){
        return "无权限";
    }
    @ResponseBody
    @ExceptionHandler(AuthorizationException.class)
    public String authorizationException(Exception ex){
        return "权限认证失败";
    }
}

2、验证

 3、前端权限控制需要在pom中引入extras依赖

<dependency>
    <groupId>com.github.theborakompanioni</groupId>
    <artifactId>thymeleaf-extras-shiro</artifactId>
    <version>2.0.0</version>
</dependency>

4、在ShiroConfig配置类中,添加以下Bean

@Bean
public ShiroDialect shiroDialect(){
 return new ShiroDialect();
}

5、在main.html中添加角色和权限校验

6、验证:期望是张三有所以权限及角色,可以看到所有,李四没有相应的角色和权限,李四登录后看不到该入口

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值