SmartBi集成到第三方系统

最近项目有所涉及到SmartBi集成的相关事项,使用了Nginx代理配置优化,特此记录下。

复制代码
import lombok.extern.slf4j.Slf4j;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/***
 *@title SmartBiController
 *@description SmartBi集成到第三方系统
 *@author 
 *@version 1.0.0
 *@create 
 **/
@Slf4j
@Controller
@RequestMapping("/smartbi")
public class SmartBiController {

    @Value("${smartbi.loginUrl}")
    private String smartBiLoginUrl;
    @Value("${smartbi.url}")
    private String smartBiUrl;
    @Value("${smartbi.resiQy}")
    private String smartBiRidQy;
    @Value("${smartbi.resiGs}")
    private String smartBiRidGs;
    @Value("${smartbi.user}")
    private String smartBiUser;
    @Value("${smartbi.password}")
    private String smartBiPassword;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public void login(HttpServletResponse response) throws Exception {
        BasicCookieStore cookieStore = new BasicCookieStore();
        CloseableHttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();

        HttpPost loginRequest = new HttpPost(smartBiLoginUrl);
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("username", smartBiUser));
        params.add(new BasicNameValuePair("password", smartBiPassword));

        loginRequest.setEntity(new UrlEncodedFormEntity(params));
        try (CloseableHttpResponse httpResponse = httpClient.execute(loginRequest)){
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (statusCode != 200) {
                log.error("登录失败:{}", statusCode);
                throw new IllegalArgumentException("登录失败:" + httpResponse.getStatusLine().getReasonPhrase());
            }
            // 获取Cookie
            List<Cookie> cookies = cookieStore.getCookies();
            for (Cookie cookie : cookies) {
                javax.servlet.http.Cookie respCookie = new javax.servlet.http.Cookie(cookie.getName(), cookie.getValue());
                respCookie.setPath("/");
                response.addCookie(respCookie);
            }
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Cache-Control", "no-cache");
            response.setHeader("Pragma", "no-cache");
            response.setDateHeader("Expires", 0);
        }
    }

    @RequestMapping(value = "/getSmartBiUrl", method = RequestMethod.GET)
    public String getSmartBiUrl(@RequestParam(name = "type") String type) throws Exception {
        String resultUrl = "";
        if ("1".equals(type)) {
            resultUrl = smartBiUrl + "?resid=" + smartBiRidQy;
        } else {
            resultUrl = smartBiUrl + "?resid=" + smartBiRidGs;
        }
        log.info("登录SmartBi地址:{}", resultUrl);
        return resultUrl;
    }
}
相关推荐
瑞雪兆丰年兮1 分钟前
[从0开始学Java|第十八、十九天]API(常见API&对象克隆&正则表达式)
java·开发语言
KobeSacre2 分钟前
JVM G1 垃圾回收器
java·开发语言·jvm
摇滚侠22 分钟前
浏览器调试工具 检查元素 谷歌模拟器 控制台 断点调试
java·html
心之伊始37 分钟前
Spring Boot 接入 MCP 实战:用 Spring AI 调用本地工具的最小闭环
java·spring boot·agent·spring ai·mcp
Refrain_zc43 分钟前
无触摸屏场景下的蓝牙交互:Android 纯按键蓝牙扫描配对与 A2DP/Headset 连接
java·蓝牙
计算机安禾1 小时前
【算法设计与分析】第29篇:启发式与元启发式搜索方法综述
java·数据库·算法
DIY源码阁1 小时前
JavaSwing学生选课系统 - MySQL版
java·数据库·mysql·eclipse
砍材农夫1 小时前
物联网实战:Spring Boot + Netty 搭建 MQTT | MQTT 设备模拟器
java·spring boot·后端·物联网·struts·spring·netty
城管不管1 小时前
Agent——001
android·java·数据库·llm·prompt