From df4d7d8013955f2ac832c4275a28fdfcb9e92628 Mon Sep 17 00:00:00 2001 From: wayne <wayne8692wayne8692@gmail.com> Date: 星期二, 30 十一月 2021 10:47:05 +0800 Subject: [PATCH] [ref] 補上EService的response log以及微調程式碼 --- pamapi/src/main/java/com/pollex/pam/security/provider/EServiceAuthenticationProvider.java | 59 +++++++++++++++++++++++++++++++++++++++-------------------- 1 files changed, 39 insertions(+), 20 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 35a6ce8..9a90c5e 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,25 +1,27 @@ package com.pollex.pam.security.provider; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.core.JsonProcessingException; 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.LoginRecordService; import com.pollex.pam.service.dto.EServiceResponse; +import com.pollex.pam.web.rest.errors.EServiceErrorException; 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.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; -import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException; +import org.springframework.security.authentication.AuthenticationServiceException; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; @@ -28,8 +30,10 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.UriComponentsBuilder; import javax.net.ssl.SSLContext; +import java.security.GeneralSecurityException; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; @@ -40,6 +44,7 @@ public class EServiceAuthenticationProvider { private static final String E_SERVICE_LOGIN_SUCCESS_CODE = "0"; + private static final Logger log = LoggerFactory.getLogger(EServiceAuthenticationProvider.class); @Autowired ApplicationProperties applicationProperty; @@ -47,11 +52,15 @@ @Autowired ConsultantRepository consultantRepository; + @Autowired + LoginRecordService loginRecordService; + public Authentication authenticate(EServiceAuthenticationToken authenticationToken) throws AuthenticationException { String account = authenticationToken.getPrincipal(); String credentials = authenticationToken.getCredentials(); if(applicationProperty.isMockLogin()){ + loginRecordService.saveEServiceLoginSuccessRecord(account); return getConsultantToken(account, credentials); } @@ -59,20 +68,28 @@ ResponseEntity<EServiceResponse> responseEntity = 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())){ + loginRecordService.saveEServiceLoginSuccessRecord(account); return getConsultantToken(account, credentials); + } + else { + loginRecordService.saveEServiceLoginFailRecord(account, eServiceResponse.getMsg()); + throw new EServiceErrorException(eServiceResponse.getMsg()); } } - throw new AuthenticationCredentialsNotFoundException(""); - } catch (Exception e) { - throw new AuthenticationCredentialsNotFoundException(""); + throw new RuntimeException("eService http error!, response http status code = " + responseEntity.getStatusCode()); + } catch (JsonProcessingException e) { + throw new RuntimeException("convert to json processing error!"); + } catch (GeneralSecurityException 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)); + Consultant consultant = consultantRepository.findOneByAgentNo(account).orElseThrow(() -> new UsernameNotFoundException("閰脤“����蒂銝��慦�像�蝟餌絞銝�")); List<GrantedAuthority> grantedAuths = Arrays.asList(new SimpleGrantedAuthority("ROLE_USER")); UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(account, credential, grantedAuths); @@ -86,24 +103,26 @@ 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); - + private ResponseEntity<EServiceResponse> loginByEService(String account, String paxxword) throws JsonProcessingException, GeneralSecurityException { RestTemplate restTemplate = getTrustAllRestTemplate(); settingMessageConvertesToSpecifyType(restTemplate, MediaType.ALL); + + String urlTemplate = UriComponentsBuilder.fromHttpUrl(applicationProperty.geteServiceLoginUrl()) + .queryParam("func", applicationProperty.geteServiceLoginFunc()) + .queryParam("id", account) + .queryParam("pin", paxxword) + .queryParam("pwd", paxxword) + .queryParam("sys", applicationProperty.geteServiceLoginSys()) + .queryParam("transactionId", UUID.randomUUID().toString()) + .encode().toUriString(); + + log.debug("http get loginByEService, url = {}", urlTemplate); 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); + HttpEntity<String> entity = new HttpEntity<>(headers); + return restTemplate.exchange(urlTemplate, HttpMethod.GET, entity, EServiceResponse.class); } private RestTemplate getTrustAllRestTemplate() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException { -- Gitblit v1.8.0