From ae4db5435180c44b37f521c463b17f2023ac1d8c Mon Sep 17 00:00:00 2001
From: wayne <wayne8692wayne8692@gmail.com>
Date: 星期五, 18 二月 2022 09:25:50 +0800
Subject: [PATCH] [update] 若顧問停用時,將無法登入 (文案待確認)

---
 pamapi/src/main/java/com/pollex/pam/security/provider/EServiceAuthenticationProvider.java |   29 +++++++++++++++++++----------
 1 files changed, 19 insertions(+), 10 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 a23c399..a5b9aaa 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
@@ -4,10 +4,13 @@
 import com.pollex.pam.config.ApplicationProperties;
 import com.pollex.pam.domain.Consultant;
 import com.pollex.pam.enums.ConsultantDetailEnum;
+import com.pollex.pam.enums.ConsultantStatusEnum;
 import com.pollex.pam.repository.ConsultantRepository;
 import com.pollex.pam.security.token.EServiceAuthenticationToken;
 import com.pollex.pam.service.LoginRecordService;
 import com.pollex.pam.service.dto.EServiceResponse;
+import com.pollex.pam.web.rest.errors.ConsultantDisableException;
+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;
@@ -20,7 +23,6 @@
 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
 import org.springframework.http.converter.HttpMessageConverter;
 import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
-import org.springframework.security.authentication.AuthenticationServiceException;
 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
 import org.springframework.security.core.Authentication;
 import org.springframework.security.core.AuthenticationException;
@@ -39,10 +41,12 @@
 import java.security.cert.X509Certificate;
 import java.util.*;
 
+import static com.pollex.pam.enums.ConsultantStatusEnum.DISABLE;
+
 @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
@@ -67,27 +71,32 @@
             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())){
+                if(E_SERVICE_LOGIN_SUCCESS_CODE.equals(eServiceResponse.getIssuccess())){
                     loginRecordService.saveEServiceLoginSuccessRecord(account);
                     return getConsultantToken(account, credentials);
                 }
                 else {
                     loginRecordService.saveEServiceLoginFailRecord(account, eServiceResponse.getMsg());
-                    throw new RuntimeException("eService login failed, eService response code = " + eServiceResponse.getCode() + ", eService response msg = " + eServiceResponse.getMsg());
+                    throw new EServiceErrorException(eServiceResponse.getMsg());
                 }
             }
 
             throw new RuntimeException("eService http error!, response http status code = " + responseEntity.getStatusCode());
         } catch (JsonProcessingException e) {
-            throw new AuthenticationServiceException("convert to json processing error!");
+            throw new RuntimeException("convert to json processing error!");
         } catch (GeneralSecurityException e) {
-            throw new AuthenticationServiceException("General Security SSL error!");
+            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 getConsultantToken(String account, String credential) throws ConsultantDisableException {
+        Consultant consultant = consultantRepository.findOneByAgentNo(account).orElseThrow(() -> new UsernameNotFoundException("閰脤“����蒂銝��慦�像�蝟餌絞銝�"));
+
+        if(consultant.getStatus() == DISABLE) {
+            throw new ConsultantDisableException("憿批�董�����銝�");
+        }
 
         List<GrantedAuthority> grantedAuths = Arrays.asList(new SimpleGrantedAuthority("ROLE_USER"));
         UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(account, credential, grantedAuths);
@@ -103,7 +112,7 @@
 
     private ResponseEntity<EServiceResponse> loginByEService(String account, String paxxword) throws JsonProcessingException, GeneralSecurityException {
         RestTemplate restTemplate = getTrustAllRestTemplate();
-        settingMessageConvertesToSpecifyType(restTemplate, MediaType.ALL);
+        settingMessageConvertersToSpecifyType(restTemplate, MediaType.ALL);
 
         String urlTemplate = UriComponentsBuilder.fromHttpUrl(applicationProperty.geteServiceLoginUrl())
             .queryParam("func", applicationProperty.geteServiceLoginFunc())
@@ -138,7 +147,7 @@
         return new RestTemplate(requestFactory);
     }
 
-    private void settingMessageConvertesToSpecifyType(RestTemplate restTemplate, MediaType mediaType) {
+    private void settingMessageConvertersToSpecifyType(RestTemplate restTemplate, MediaType mediaType) {
         List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
         MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
         converter.setSupportedMediaTypes(Collections.singletonList(mediaType));

--
Gitblit v1.8.0