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;
    }
}
相关推荐
小bo波1 天前
从"任意文件复制"深挖Java I/O:字符流与字节流的本质抉择
java·nio·io流·后端开发·文件复制
nanxun8862 天前
记一次诡异的 Docker 容器"串包"故障排查
java
用户1563068103512 天前
Day01 | Java 基础(Java SE)
java
行者全栈架构师2 天前
Maven dependency:tree 的 8 个高级用法
java·后端
行者全栈架构师3 天前
IDEA 中 Maven 项目的 15 个红色报错快速解决方法
java·后端
令人头秃的代码0_03 天前
mac(m5)平台编译openjdk
java
唐青枫4 天前
Java JDBC 实战指南:从 Connection 到事务和连接池
java
一个做软件开发的牛马4 天前
MyBatis-Plus 从零实战:完整搭建可运行 Demo,BaseMapper 零 SQL、Wrapper 条件构造、分页插件与代码生成器详解
java·后端
用户3721574261354 天前
Java 处理 PDF 图片:提取 PDF 中的图片,并压缩 PDF 图片体积
java
用户3721574261354 天前
Java 打印 Word 文档:从基础打印到高级设置
java