加magic-api交流群请教群主解决了这一问题。
springboot版本2.3.6Release
Magic-api版本2.1.0
分为三步
1、配置编辑器
2、前端获取token
3、后端解析token
1、配置编辑器
参考:https://www.ssssssss.org/magic-api/pages/config/editor/#getmagictokenvalue
getMagicTokenValue: function(){// 自行获取magic-api需要的tokenreturn token; }
指定后台配置文件:
magic-api.editor-config=classpath:./magic-editor-config.js
2、前端获取token
magic-editor-config.js:
var MAGIC_EDITOR_CONFIG = {title: 'magic-api',checkUpdate: false,header: {skin: false, // 屏蔽皮肤按钮document: false, // 屏蔽文档按钮repo: true, // 屏蔽gitee和githubqqGroup: true // 屏蔽加入QQ群},getMagicTokenValue: function(){// 自行获取magic-api需要的token 这里我的magic-qpi在iframe里父级url中有tokenvar token = "";var currentTopHref = window.top.location.href;console.log('currentTopHref', currentTopHref);var reg = new RegExp("(^|&)token=([^&]*)(&|$)");var r = currentTopHref.match(reg);if (r != null){token = unescape(r[2]);}return token;}
}
3、后端解析token
CustomAuthorizationInterceptor.class:
@Component
public class CustomAuthorizationInterceptor implements AuthorizationInterceptor {/*** 配置是否需要登录*/@Overridepublic boolean requireLogin() {// 設置需要登錄 这里必须设置为true 不然后边鉴权时 magicUser为空return true;}@Overridepublic MagicUser getUserByToken(String token) throws MagicLoginException {//對登錄人token處理 獲取magicUser信息MagicUser magicUser = TokenUtil.getMagicUser(token);if(magicUser!=null){return magicUser;}throw new MagicLoginException("未登录");}/*** 是否拥有对该接口的增删改权限* 此方法可以不重写,则走默认的 boolean allowVisit(MagicUser magicUser, MagicHttpServletRequest request, Authorization authorization) 方法*/@Overridepublic boolean allowVisit(MagicUser magicUser, MagicHttpServletRequest request, Authorization authorization, MagicEntity entity) {// Authorization.SAVE 保存// Authorization.DELETE 删除// Authorization.VIEW 查询// Authorization.LOCK 锁定// Authorization.UNLOCK 解锁// 自行写逻辑判断是否拥有如果有,则返回true,反之为false// 这里存疑 entity.getCreateBy(), entity.getUpdateBy() 会为空return hasAuth(magicUser, entity.getCreateBy(), entity.getUpdateBy(), authorization);}/*** 是否拥有对该分组的增删改权限* 此方法可以不重写,则走默认的 boolean allowVisit(MagicUser magicUser, MagicHttpServletRequest request, Authorization authorization) 方法*/@Overridepublic boolean allowVisit(MagicUser magicUser, MagicHttpServletRequest request, Authorization authorization, Group group) {// Authorization.SAVE 保存// Authorization.DELETE 删除// Authorization.VIEW 查询// 自行写逻辑判断是否拥有如果有,则返回true,反之为false// 这里存疑 entity.getCreateBy(), entity.getUpdateBy() 会为空return hasAuth(magicUser, group.getCreateBy(), group.getUpdateBy(), authorization);}
}