保誠-保戶業務員媒合平台
wayne
2021-11-29 5e78d7a2a8e4c596cc6d61af6943c4e079e93b84
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.pollex.pam.service;
 
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
 
import javax.net.ssl.SSLContext;
 
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
 
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pollex.pam.config.ApplicationProperties;
import com.pollex.pam.service.dto.EServiceRequest;
import com.pollex.pam.service.dto.EServiceResponse;
import com.pollex.pam.web.rest.vm.OtpEmailLoginVM;
import com.pollex.pam.web.rest.vm.OtpSMSLoginVM;
import com.pollex.pam.web.rest.vm.VerifyOtpVM;
 
 
@Service
public class LoginService {
 
    private final ApplicationProperties applicationProperties;
 
    public LoginService(ApplicationProperties applicationProperties) {
        this.applicationProperties = applicationProperties;
    }
 
    public void otpLoginByPhone(OtpSMSLoginVM login) {
 
    }
 
    public void otpLoginByEmail(OtpEmailLoginVM login) {
 
    }
 
    public void verifyOtp(VerifyOtpVM verifyOtpParam) {
        // todo 要跟保誠otp的認證
 
        // 假設成功了,DB的確認
    }
 
    public ResponseEntity<EServiceResponse> loginByEService(String account, String paxxword) throws Exception{
        EServiceRequest dto = new EServiceRequest();
        dto.setFunc("ValidateUserLogin");
        dto.setId(account);
        dto.setPin(paxxword);
        dto.setPwd(paxxword);
        dto.setSys("epos");
 
        String dtoJson = new ObjectMapper().writeValueAsString(dto);
 
        RestTemplate restTemplate = getTrustAllRestTemplate();
 
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
 
        HttpEntity<String> entity = new HttpEntity<>(dtoJson, headers);
        return restTemplate.exchange(applicationProperties.geteServiceLoginUrl(), HttpMethod.POST, entity, EServiceResponse.class);
    }
 
    private RestTemplate getTrustAllRestTemplate() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
 
        SSLContext sslContext = SSLContexts.custom()
            .loadTrustMaterial(null, (X509Certificate[] x509Certs, String s) -> true)
            .build();
        SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier());
        CloseableHttpClient httpClient = HttpClients.custom()
            .setSSLSocketFactory(csf)
            .build();
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setHttpClient(httpClient);
        requestFactory.setConnectTimeout(300000);
        requestFactory.setReadTimeout(300000);
        return new RestTemplate(requestFactory);
    }
}