保誠-保戶業務員媒合平台
wayne
2022-02-22 1a8622c14a2b8686a5655523d22167354b3834cf
pamapi/src/main/java/com/pollex/pam/security/provider/EServiceAuthenticationProvider.java
@@ -6,8 +6,8 @@
import com.pollex.pam.enums.ConsultantDetailEnum;
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;
@@ -21,7 +21,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;
@@ -38,12 +37,15 @@
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.time.Instant;
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
@@ -52,16 +54,12 @@
    @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);
            return getConsultantTokenAndRecordLoginTime(account, credentials);
        }
        try {
@@ -70,12 +68,10 @@
                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);
                if(E_SERVICE_LOGIN_SUCCESS_CODE.equals(eServiceResponse.getIssuccess())){
                    return getConsultantTokenAndRecordLoginTime(account, credentials);
                }
                else {
                    loginRecordService.saveEServiceLoginFailRecord(account, eServiceResponse.getMsg());
                    throw new EServiceErrorException(eServiceResponse.getMsg());
                }
            }
@@ -88,8 +84,15 @@
        }
    }
    private UsernamePasswordAuthenticationToken getConsultantToken(String account, String credential) {
    private UsernamePasswordAuthenticationToken getConsultantTokenAndRecordLoginTime(String account, String credential) throws ConsultantDisableException {
        Consultant consultant = consultantRepository.findOneByAgentNo(account).orElseThrow(() -> new UsernameNotFoundException("該顧問資料並不存在於媒合平台系統中"));
        if(consultant.getStatus() == DISABLE) {
            throw new ConsultantDisableException();
        }
        consultant.setLatestLoginTime(Instant.now());
        consultantRepository.save(consultant);
        List<GrantedAuthority> grantedAuths = Arrays.asList(new SimpleGrantedAuthority("ROLE_USER"));
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(account, credential, grantedAuths);
@@ -105,7 +108,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())
@@ -140,7 +143,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));