• ruoyi-cloud-plus添加一个不要认证的公开新页面


    版本
    RuoYiCloudPlusv2.1.2
    plus-uiVue3 ts

    以新增一个公开的课程搜索页面为例。

    一、前端

    1. 组件创建

    在view目录下创建一个页面的vue代码,比如

    src/views/customer/searchPage/index.vue
    
    • 1

    2. src/router/index.ts

    为其编制一个路由。在constantRoutes中添加一组dict信息。比如

    {
        path: '/courseSearch',
        component: () => import('@/views/customer/searchPage/index.vue'),
        hidden: true
    },
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3. src/permission.ts

    把页面加入前端的whiteList

    const whiteList = [
      '/login',
      '/register',
      '/social-callback',
      '/courseSearch'
    ];
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在浏览器输入http://localhost/courseSearch,至此这个页面已经不用登录就可以访问了。

    二、后端

    但是后端是有网关和认证模块的,虽然前端页面可以不用登陆了,但是如果这个页面还需要从后端获取数据,那么后端对应的controller也应该被open。

    1. 设计思想

    不同模块有不同的url前缀,比如

    routes:
       # 认证中心
       - id: ruoyi-auth
         uri: lb://ruoyi-auth
         predicates:
           - Path=/auth/**
         filters:
           - StripPrefix=1
       # 代码生成
       - id: ruoyi-gen
         uri: lb://ruoyi-gen
         predicates:
           - Path=/tool/**
         filters:
           - StripPrefix=1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    并且每个模块都有可能需要open一些controller,不需要认证。那么我们进行统一设定,比如课程模块,url前缀为course,那么/course/open/**就都是被公开的端点。于是在gateway只需要把/*/open/**加入白名单即可。

    2. ruoyi-gateway.yml

    在nacos中修改gateway的配置文件,把/*/open/**加入whites。

    security:
    	ignore:
    		whites:
    			- /auth/code
    			- /auth/logout
    			- /auth/login
    			- /auth/binding/*
    			- /auth/social/callback
    			- /auth/register
    			- /auth/tenant/list
    			- /resource/sms/code
    			- /*/v3/api-docs
    			- /*/error
    			- /csrf
    			- /*/open/**
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    3. 开发Controller

    在course模块中,新建一个CourseOpenController.java,内容示例如下

    package org.dromara.course.controller;
    
    import org.dromara.course.domain.bo.CourseCategoryBo;
    import org.dromara.course.domain.vo.CourseCategoryVo;
    import org.dromara.course.service.CourseCategoryService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    @RestController
    @RequestMapping("/open")
    public class CourseOpenController {
    
        @Autowired
        CourseCategoryService categoryService;
    
    
        @GetMapping("/category/list")
        public List<CourseCategoryVo> list(CourseCategoryBo bo) {
            return categoryService.queryList(bo);
        }
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    重启网关和课程模块即可。

    在这里插入图片描述

  • 相关阅读:
    【农业生产模拟】WOFOST模型与PCSE模型实践
    【附源码】计算机毕业设计JAVA演唱会购票系统
    Word处理控件Aspose.Words功能演示:使用C#或VB.NET在Word文档中进行邮件合并
    如何下载国外硕博论文?
    虚拟化之内存(Memory)
    树莓派之快速上手-变身个人Linux电脑
    Mybatis——Mybatis动态SQL的简介以及使用动态SQL对用户的优化
    C++基础第9章:序列与关联容器(2)——序列容器
    Java SE 19 新增特性
    java复习-多态性
  • 原文地址:https://blog.csdn.net/m0_51390969/article/details/138090135