springboot+shiro入门学习(三)setUnauthorizedUrl的设置

本文详细介绍了如何在Spring Boot项目中使用Shiro进行权限控制,重点讲解了当用户无权限访问特定资源时,如何正确地跳转到指定页面。通过修改Shiro配置和增加全局异常处理,确保了权限验证流程的顺畅。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在上一篇中我们遗留了一个问题就是当没有权限时页面跳转的问题。

首先搭建一个jsp的环境

pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.shiro</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>shiro</name>
    <packaging>war</packaging>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.4.0</version>
        </dependency>

        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.1</version>
        </dependency>


        <!-- spring boot tomcat jsp 支持开启 -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <!--servlet支持开启-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>
        <!-- jstl 支持开启 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

applocation.yml文件:

server:
  port: 8202
spring:
  mvc:
    view:
      prefix: /WEB-INF/jsp/
      suffix: .jsp

访问jsp的Controller:

package com.shiro.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value = "jsp")
public class JspController {

    @RequestMapping(value = "test")
    public String aaa(){
        System.out.println("111111111111111111111");
        return "index";
    }
}

项目结构:

 首先测试一下http://localhost:8202/jsp/test能不能正常访问。如果能进行下面的步骤。

修改ShiroConfig类的shiroFilter方法:

我们新增了
shiroFilterFactoryBean.setUnauthorizedUrl("/jsp/test");

filterChainDefinitionMap.put("/test/admin","perms[user:view]");

@Bean
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();

        //设置安全管理器
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //shiroFilterFactoryBean.setLoginUrl("/index");
        shiroFilterFactoryBean.setUnauthorizedUrl("/jsp/test");

        //自定义过滤器
        Map<String, Filter> filterMap = new LinkedHashMap<>();
        filterMap.put("filter",new ShiroFilter());
        shiroFilterFactoryBean.setFilters(filterMap);

        //权限控制map
        Map<String,String> filterChainDefinitionMap = new LinkedHashMap<>();
        // 配置不会被拦截的链接 顺序判断
        filterChainDefinitionMap.put("/static/**", "anon");
        //配置退出 过滤器,其中的具体的退出代码Shiro已经替我们实现了
        filterChainDefinitionMap.put("/logout", "logout");
        filterChainDefinitionMap.put("/test/login","anon");
        filterChainDefinitionMap.put("/test/admin","perms[user:view]");
        //filterChainDefinitionMap.put("/test","authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }

同时将admin方法上面的@RequiresPermissions({"user:delete","user:update"})注释掉。

需要注意的是测试的时候一定要先执行login方法。

当前用户拥有的权限是:user:delete。所以访问时没有权限。即跳转到/jsp/test路径

继续修改代码:

将filterChainDefinitionMap.put("/test/admin","perms[user:view]");注释掉。同时在admin方法中添加@RequiresPermissions({"user:delete","user:update"})

会发现并没有跳转到/jsp/test路径去。具体原因访问https://blog.youkuaiyun.com/bicheng4769/article/details/86680955

新增一个MyControllerAdvice类来处理全局异常。

package com.shiro.MyException;

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

@ControllerAdvice
public class MyControllerAdvice {

    @ExceptionHandler(value = UnauthorizedException.class)
    public String aa(){
        return "index";
    }
}

我们再次访问test/admin方法。会发现能够跳转到/jsp/test路径去了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值