From 742a32ec7169db793adb1efde6e2897e514a76fc Mon Sep 17 00:00:00 2001 From: jack <jack.su@pollex.com.tw> Date: 星期一, 06 一月 2025 17:59:49 +0800 Subject: [PATCH] [UPDATE] 更新YML --- pamapi/src/main/java/com/pollex/pam/security/provider/EServiceAuthenticationProvider.java | 136 +++++++++++++++++++++------------------------ 1 files changed, 64 insertions(+), 72 deletions(-) diff --git a/pamapi/src/main/java/com/pollex/pam/security/provider/EServiceAuthenticationProvider.java b/pamapi/src/main/java/com/pollex/pam/security/provider/EServiceAuthenticationProvider.java index 28a2a19..1860b1b 100644 --- a/pamapi/src/main/java/com/pollex/pam/security/provider/EServiceAuthenticationProvider.java +++ b/pamapi/src/main/java/com/pollex/pam/security/provider/EServiceAuthenticationProvider.java @@ -1,23 +1,20 @@ package com.pollex.pam.security.provider; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.pollex.pam.business.domain.Consultant; +import com.pollex.pam.business.enums.ConsultantDetailEnum; +import com.pollex.pam.business.repository.ConsultantRepository; +import com.pollex.pam.business.repository.EServiceErrorCodeRepository; +import com.pollex.pam.business.service.EServiceConnectService; +import com.pollex.pam.business.service.dto.EServiceResponse; +import com.pollex.pam.business.web.errors.ConsultantDisableException; +import com.pollex.pam.business.config.AppProperties; +import com.pollex.pam.business.security.token.EServiceAuthenticationToken; +import com.pollex.pam.business.web.errors.EServiceErrorException; import com.pollex.pam.config.ApplicationProperties; -import com.pollex.pam.domain.Consultant; -import com.pollex.pam.enums.ConsultantDetailEnum; -import com.pollex.pam.enums.CustomerDetailEnum; -import com.pollex.pam.repository.ConsultantRepository; -import com.pollex.pam.security.token.EServiceAuthenticationToken; -import com.pollex.pam.service.dto.EServiceRequest; -import com.pollex.pam.service.dto.EServiceResponse; -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.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.*; -import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; -import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; @@ -25,57 +22,86 @@ import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Component; -import org.springframework.web.client.RestTemplate; -import javax.net.ssl.SSLContext; -import java.security.KeyManagementException; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.security.cert.X509Certificate; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.security.GeneralSecurityException; +import java.util.*; + +import static java.util.Arrays.asList; @Component public class EServiceAuthenticationProvider { - private static final String E_SERVICE_LOGIN_SUCCESS_CODE = "0"; + private static final String E_SERVICE_LOGIN_SUCCESS_CODE = "true"; + private static final Logger log = LoggerFactory.getLogger(EServiceAuthenticationProvider.class); @Autowired - ApplicationProperties applicationProperty; + ApplicationProperties applicationProperties; @Autowired ConsultantRepository consultantRepository; + + @Autowired + EServiceConnectService eServiceConnectService; + + @Autowired + EServiceErrorCodeRepository eServiceErrorCodeRepository; public Authentication authenticate(EServiceAuthenticationToken authenticationToken) throws AuthenticationException { String account = authenticationToken.getPrincipal(); String credentials = authenticationToken.getCredentials(); - if(applicationProperty.isMockLogin()){ - return getConsultantToken(account, credentials); + + if(applicationProperties.isMockLogin()){ + return getConsultantTokenAndRecordLoginTime(account, credentials); } + + try { - ResponseEntity<EServiceResponse> responseEntity = loginByEService(account, credentials); + ResponseEntity<EServiceResponse> responseEntity = eServiceConnectService.loginByEService(account, credentials); if(HttpStatus.OK.equals(responseEntity.getStatusCode())) { EServiceResponse eServiceResponse = responseEntity.getBody(); + log.debug("eService response = {}", eServiceResponse); - if(E_SERVICE_LOGIN_SUCCESS_CODE.equals(eServiceResponse.getCode())){ - return getConsultantToken(account, credentials); + if(eServiceResponse == null) { + throw new RuntimeException("eService error!, response body is null"); } + + List<String> successCode = new ArrayList<>(asList("0","3","4","6")); + + if(successCode.contains(eServiceResponse.getCode())) { + return getConsultantTokenAndRecordLoginTime(account, credentials); + } else { + log.debug("account:{},error:{}",account,eServiceResponse.getMsg()); + eServiceErrorCodeRepository.findByErrorCode(eServiceResponse.getCode()). + ifPresent(eServiceErrorCode -> { + throw new EServiceErrorException(eServiceErrorCode.getErrorMessage()); + }); + + throw new EServiceErrorException("�頛詨�����炊嚗��頛詨"); + } + +// if(E_SERVICE_LOGIN_SUCCESS_CODE.equals(eServiceResponse.getIssuccess())){ +// +// return getConsultantTokenAndRecordLoginTime(account, credentials); +// } +// else { +// log.debug("account:{},error:{}",account,eServiceResponse.getMsg()); +// throw new EServiceErrorException("�頛詨�����炊嚗��頛詨"); +// } } - throw new AuthenticationCredentialsNotFoundException(""); - } catch (Exception e) { - throw new AuthenticationCredentialsNotFoundException(""); + throw new RuntimeException("eService http error!, response http status code = " + responseEntity.getStatusCode()); + } catch (GeneralSecurityException e) { + log.error("General Security SSL error!",e); + throw new RuntimeException("General Security SSL error!"); } } - private UsernamePasswordAuthenticationToken getConsultantToken(String account, String credential) { - Consultant consultant = consultantRepository.findOneByAgentNo(account).orElseThrow(() -> new UsernameNotFoundException("consultant is not in db, consultant agentNo = " + account)); + private UsernamePasswordAuthenticationToken getConsultantTokenAndRecordLoginTime(String account, String credential) throws ConsultantDisableException { + Consultant consultant = consultantRepository.findOneByAgentNo(account).orElseThrow(() -> new UsernameNotFoundException("撣唾��Ⅳ�隤�")); - List<GrantedAuthority> grantedAuths = Arrays.asList(new SimpleGrantedAuthority("ROLE_USER")); + List<GrantedAuthority> grantedAuths = Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER")); UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(account, credential, grantedAuths); Map<String, String> details = new HashMap<>(); @@ -85,39 +111,5 @@ authenticationToken.setDetails(details); return authenticationToken; - } - - private 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(applicationProperty.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); } } -- Gitblit v1.8.0