月球大数据地理空间分析展示平台-【后端】-月球后台服务
1
13693261870
2024-11-17 796b44ea813a1133beae4f3a67f1c0263510c0c7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.moon.server.service.all;
 
import com.moon.server.config.PropertiesConfig;
import com.moon.server.entity.all.StaticData;
import com.moon.server.helper.WebHelper;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
 
@Service
@SuppressWarnings("ALL")
public class SignService {
    @Autowired
    PropertiesConfig propertiesConfig;
 
    private final static String SUCCESS = "success";
 
    public void loginDruid(HttpServletRequest req, HttpServletResponse res) throws Exception {
        List<NameValuePair> list = new ArrayList<>();
        list.add(new BasicNameValuePair("loginUsername", propertiesConfig.getDruidUser()));
        list.add(new BasicNameValuePair("loginPassword", propertiesConfig.getDruidPwd()));
 
        String url = req.getRequestURL().toString().replace("/sign/toDruid", "/druid/submitLogin");
        CloseableHttpClient httpClient = HttpClients.custom().build();
 
        UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(list, StaticData.TEXT_ENCODER);
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(postEntity);
 
        CloseableHttpResponse closeResponse = httpClient.execute(httpPost);
        HttpEntity entity = closeResponse.getEntity();
 
        String rs = EntityUtils.toString(entity, StaticData.TEXT_ENCODER);
        if (!SUCCESS.equals(rs)) {
            return;
        }
 
        Header[] headers = closeResponse.getAllHeaders();
        for (int i = 0; i < headers.length; i++) {
            Header header = headers[i];
            if ("Set-Cookie".equals(header.getName())) {
                String val = header.getValue();
                String sessionId = val.substring(val.indexOf("=") + 1, val.indexOf(";"));
                WebHelper.saveCookie(StaticData.DRUID_COOKIE_KEY, sessionId, res);
                break;
            }
        }
    }
}