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;
    }
}
相关推荐
李白的粉8 小时前
基于springboot的新闻稿件管理系统
java·spring boot·毕业设计·课程设计·源代码·新闻稿件管理系统
零雲8 小时前
java面试:了解Redis的分布式限流算法么?
java·redis·面试
XiYang-DING8 小时前
【Java SE】JVM字符串常量池:位置、创建流程、对象个数与 `intern()`
java·开发语言·jvm
平生幻8 小时前
【数据结构】-复杂度
java·开发语言·数据结构
222you8 小时前
JUC当中的几个计数类
java·开发语言
xdl25998 小时前
如何快速搭建简单SpringBoot项目网页
java·spring boot·intellij-idea
k-l.8 小时前
【通过jdbc连接到kingbase数据库插入10w数据】
java·数据库
毕设源码-朱学姐8 小时前
【开题答辩全过程】以 基于java的书店用户管理系统的设计与实现为例,包含答辩的问题和答案
java·开发语言
gis分享者8 小时前
华为OD面试-Java、C++、Pyhton等多语言实现-目录
java·c++·华为od·面试·目录·od·机试
于先生吖8 小时前
2026 新版上门回收系统源码 JAVA 同城服务平台搭建指南
java·开发语言