| | |
| | | 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 java.security.GeneralSecurityException; |
| | | import java.util.*; |
| | | |
| | | import static java.util.Arrays.asList; |
| | | |
| | | @Component |
| | | public class EServiceAuthenticationProvider { |
| | | |
| | |
| | | @Autowired |
| | | EServiceConnectService eServiceConnectService; |
| | | |
| | | @Autowired |
| | | EServiceErrorCodeRepository eServiceErrorCodeRepository; |
| | | |
| | | public Authentication authenticate(EServiceAuthenticationToken authenticationToken) throws AuthenticationException { |
| | | String account = authenticationToken.getPrincipal(); |
| | | String credentials = authenticationToken.getCredentials(); |
| | | |
| | | |
| | | if(applicationProperties.isMockLogin()){ |
| | | return getConsultantTokenAndRecordLoginTime(account, credentials); |
| | | } |
| | | |
| | | |
| | | |
| | | try { |
| | | ResponseEntity<EServiceResponse> responseEntity = eServiceConnectService.loginByEService(account, credentials); |
| | |
| | | EServiceResponse eServiceResponse = responseEntity.getBody(); |
| | | log.debug("eService response = {}", eServiceResponse); |
| | | |
| | | if(E_SERVICE_LOGIN_SUCCESS_CODE.equals(eServiceResponse.getIssuccess())){ |
| | | 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("您輸入的資訊有誤,請重新輸入"); |
| | | } |
| | | else { |
| | | throw new EServiceErrorException(eServiceResponse.getMsg()); |
| | | } |
| | | |
| | | // 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 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 getConsultantTokenAndRecordLoginTime(String account, String credential) throws ConsultantDisableException { |
| | | Consultant consultant = consultantRepository.findOneByAgentNo(account).orElseThrow(() -> new UsernameNotFoundException("該顧問資料並不存在於媒合平台系統中")); |
| | | 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<>(); |