芋道源码yudao-cloud 二开日记(添加接口权限和页面固定路由)

06-01 1189阅读
今天的需求是单独开放两个接口,不用登录也能访问,于是看了下文档有几种方式,最快捷最方便的方式就是在方法上加注解@PermitAll,例如下面这样:
@GetMapping("/get/{configId}")
@PermitAll
public void getConfig(@PathVariable("configId") Long configId) throws Exception {
    // ...
}

但如果是多个方法都需要放开,那建议还是用下面这种配置,找到对应服务下的SecurityConfiguration.java文件,比如我要放开yudao-module-system模块下/system/mall/下面的所有接口,那么文件目录是:

yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/security/config/SecurityConfiguration.java

/**
 * System 模块的 Security 配置
 */
@Configuration(proxyBeanMethods = false, value = "systemSecurityConfiguration")
public class SecurityConfiguration {
    @Bean("systemAuthorizeRequestsCustomizer")
    public AuthorizeRequestsCustomizer authorizeRequestsCustomizer() {
        return new AuthorizeRequestsCustomizer() {
            @Override
            public void customize(ExpressionUrlAuthorizationConfigurer.ExpressionInterceptUrlRegistry registry) {
                // Swagger 接口文档
                registry.antMatchers("/v3/api-docs/**").permitAll()
                        .antMatchers("/webjars/**").permitAll()
                        .antMatchers("/swagger-ui").permitAll()
                        .antMatchers("/swagger-ui/**").permitAll();
                // Druid 监控
                registry.antMatchers("/druid/**").anonymous();
                // Spring Boot Actuator 的安全配置
                registry.antMatchers("/actuator").anonymous()
                        .antMatchers("/actuator/**").anonymous();
                // RPC 服务的安全配置
                registry.antMatchers(ApiConstants.PREFIX + "/**").permitAll();
                // 添加如下
                registry.antMatchers("/system/mall/**").permitAll();
            }
        };
    }
}

放开是没问题了,可一经测试,访问接口会出现租户标识未传递的异常,如下:

芋道源码yudao-cloud 二开日记(添加接口权限和页面固定路由)

因为我们这是个多租户的框架,默认每一次请求都会带上租户id去查询。只要在headers添加tenant-id:租户id就可以,如下:

芋道源码yudao-cloud 二开日记(添加接口权限和页面固定路由)

好了,接口是完成了,那么看看前端页面的配置。

我的需求是页面不用登陆也能访问页面,并且不需要管理的菜单栏,如下图:

芋道源码yudao-cloud 二开日记(添加接口权限和页面固定路由)

1.找到路由文件添加固定路由,如下图:

芋道源码yudao-cloud 二开日记(添加接口权限和页面固定路由)

2.并且添加不重定向的路由,如下图:

芋道源码yudao-cloud 二开日记(添加接口权限和页面固定路由)

3.ok,试着访问一下,页面是打开了,但是接口报错,如下图:

芋道源码yudao-cloud 二开日记(添加接口权限和页面固定路由)

还是我们刚刚上面测接口的问题,没有传租户id,那我们加上就是了,先看看请求拦截器怎么设置租户id,如下图:

芋道源码yudao-cloud 二开日记(添加接口权限和页面固定路由)

4.既然知道了是从localStorege获取的,那我们在请求接口之前存上是不是就OK,嘿嘿。。先看看在登录的情况下是怎么存储的

芋道源码yudao-cloud 二开日记(添加接口权限和页面固定路由)

5.知道了上面的存储结构,简单粗暴上代码。

onMounted(()=>{
  // 该页面设置固定租户id,否则请求会出现未传递租户id的异常
  const tenantCache = {
    "c":new Date().getTime(), // 当前时间戳
    "e":253402300799000, // 过期时间戳
    "v":"1" // 租户id
  }
  localStorage.setItem("tenantId",JSON.stringify(tenantCache));
  // 初始化接口
  init();
})

测试访问成功。。。完成

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们。

相关阅读

目录[+]

取消
微信二维码
微信二维码
支付宝二维码