若依分离板集成积木报表就不说了需要的请移步:若依前后分离版集成积木报表-CSDN博客
考虑到前端摸鱼不干活,所以一般都是前后端都干,我这里前后端都搞上,你们直接抄,抄完接着去摸鱼,代码不美观,轻喷
一、需要进行token传递的接口
集成积木主要有两个接口需要进行token传递
1. /jmreport/list 查看积木列表
2. /jmreport/view/* 查看积木报表报表积木详情
二、后端配置:
1、创建 JimuReportTokenService 实现 JmReportTokenServiceI接口,文件路径:/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/JimuReportTokenService.java
JimuReportTokenService.java文件内容,直接C+V即可
package com.ruoyi.framework.web.service;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.jeecg.modules.jmreport.api.JmReportTokenServiceI;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.tool.TokenUtils;
import lombok.AllArgsConstructor;
/**
*
* @ClassName: JimuReportTokenService
* @author fzz
*
*/
@Component
@AllArgsConstructor
public class JimuReportTokenService implements JmReportTokenServiceI {
private final TokenService tokenService;
@Override
public String getToken(HttpServletRequest request) {
return TokenUtils.getTokenByRequest(request);
}
@Override
public String getToken() {
return JmReportTokenServiceI.super.getToken();
}
@Override
public String getUsername(String s) {
LoginUser loginUser = tokenService.getLoginUser(s);
return loginUser.getUsername();
}
@Override
public Boolean verifyToken(String s) {
LoginUser loginUser = tokenService.getLoginUser(s);
if (StringUtils.isNotNull(loginUser)) {
tokenService.verifyToken(loginUser);
return true;
}
return false;
}
@Override
public Map<String, Object> getUserInfo(String token) {
return JmReportTokenServiceI.super.getUserInfo(token);
}
@Override
public HttpHeaders customApiHeader() {
return JmReportTokenServiceI.super.customApiHeader();
}
@Override
public String getTenantId() {
return JmReportTokenServiceI.super.getTenantId();
}
}
2、添加tool包, 创建TokenUtils.java ,文件路径:/ruoyi-framework/src/main/java/com/ruoyi/framework/web/tool/TokenUtils.java
文件内容,直接C+V即可
package com.ruoyi.framework.web.tool;
import javax.servlet.http.HttpServletRequest;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import lombok.AllArgsConstructor;
/**
*
* @ClassName: TokenUtils
* @author fzz
*
*/
public class TokenUtils {
/**
* 获取 request 里传递的 token
*
* @param request
* @return
*/
public static String getTokenByRequest(HttpServletRequest request) {
String parameter = request.getParameter("token");
String header = request.getHeader("token");
if (parameter == null && header == null) {
parameter = request.getHeader("Authorization");
}
return parameter != null ? parameter : header;
}
}
3、TokenService.java文件重载getLoginUser方法
/**
* 获取用户身份信息
*
* @return 用户信息
*/
public LoginUser getLoginUser(String token) {
if (StringUtils.isNull(token)) {
return null;
}
token = token.replace(Constants.TOKEN_PREFIX, "");
if (StringUtils.isNotEmpty(token)) {
try {
Claims claims = parseToken(token);
// 解析对应的权限以及用户信息
String uuid = (String) claims.get(Constants.LOGIN_USER_KEY);
String userKey = getTokenKey(uuid);
LoginUser user = redisCache.getCacheObject(userKey);
return user;
} catch (Exception e) {
}
}
return null;
}
OK,现在如果再次访问/jmreport/list接口查看积木列表时如果不带token时就会报错
4、定义拦截器
这个需要一个拦截器去监听/jmreport/view/*接口必须携带token
创建JimuInterceptor.java文件,路径:/ruoyi-framework/src/main/java/com/ruoyi/framework/interceptor/JimuInterceptor.java
文件内容,直接复制
package com.ruoyi.framework.interceptor;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.framework.web.service.TokenService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @ClassName: JimuInterceptor
* @Description: 监听/jmreport/view/*接口,必须携带token
* @author fzz
*
*/
@Component
@AllArgsConstructor
public class JimuInterceptor implements HandlerInterceptor {
private final TokenService tokenService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
response.setContentType("text/html; charset=UTF-8");
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String token = request.getParameter("token");
LoginUser loginUser = tokenService.getLoginUser(token);
if (loginUser != null) {
return true;
}
JSONObject jsonObject = new JSONObject();
jsonObject.put("code", 200);
jsonObject.put("msg", "参数错误或无权访问数据");
response.getWriter().println(jsonObject);
return false;
}
}
5、对接口进行拦截
打开ResourcesConfig.java文件,路径:/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ResourcesConfig.java
找到addInterceptors方法,添加registry.addInterceptor(jimuInterceptor).addPathPatterns("/jmreport/view/*");
/**
* 自定义拦截规则
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**");
//新增自定义规则
registry.addInterceptor(jimuInterceptor).addPathPatterns("/jmreport/view/*");
}
OK,这样再去访问/jmreport/view 就会也需要token了,如果不带token时就会报错
后端配置到此结束,接下来是前端配置
三、前端配置:
1.内嵌菜单,之前有写过,需要的移步:若依前后分离版集成积木报表嵌入菜单-CSDN博客
访问项目就OK了,如果还是提示需要token的话 ,请打开前端项目:\ruoyi-ui\src\views\system\jeecg\index.vue找到indexUrl,查看是否添加了token,如果没添加 在这里添加上即可,代码如下
created() {
indexUrl().then(res => {
this.src = res + '?token=' + getToken()
})
},