保誠-保戶業務員媒合平台
[update] 將EService登入後的處理機制抽出來,只將EService的連線放到Business中

修改2個檔案
新增1個檔案
92 ■■■■■ 已變更過的檔案
pamapi/src/main/java/com/pollex/pam/security/provider/CustomAuthenticationProvider.java 1 ●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/main/java/com/pollex/pam/security/provider/EServiceAuthenticationProvider.java 85 ●●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/main/java/com/pollex/pam/web/rest/EServiceResource.java 6 ●●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/main/java/com/pollex/pam/security/provider/CustomAuthenticationProvider.java
@@ -1,6 +1,5 @@
package com.pollex.pam.security.provider;
import com.pollex.pam.business.security.provider.EServiceAuthenticationProvider;
import com.pollex.pam.config.ApplicationProperties;
import com.pollex.pam.business.security.token.EServiceAuthenticationToken;
import com.pollex.pam.security.token.OtpAuthenticationToken;
pamapi/src/main/java/com/pollex/pam/security/provider/EServiceAuthenticationProvider.java
¤ñ¹ï·sÀÉ®×
@@ -0,0 +1,85 @@
package com.pollex.pam.security.provider;
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.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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import java.security.GeneralSecurityException;
import java.util.*;
@Component
public class EServiceAuthenticationProvider {
    private static final String E_SERVICE_LOGIN_SUCCESS_CODE = "true";
    private static final Logger log = LoggerFactory.getLogger(EServiceAuthenticationProvider.class);
    @Autowired
    ApplicationProperties applicationProperties;
    @Autowired
    ConsultantRepository consultantRepository;
    @Autowired
    EServiceConnectService eServiceConnectService;
    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);
            if(HttpStatus.OK.equals(responseEntity.getStatusCode())) {
                EServiceResponse eServiceResponse = responseEntity.getBody();
                log.debug("eService response = {}", eServiceResponse);
                if(E_SERVICE_LOGIN_SUCCESS_CODE.equals(eServiceResponse.getIssuccess())){
                    return getConsultantTokenAndRecordLoginTime(account, credentials);
                }
                else {
                    throw new EServiceErrorException(eServiceResponse.getMsg());
                }
            }
            throw new RuntimeException("eService http error!, response http status code = " + responseEntity.getStatusCode());
        } catch (GeneralSecurityException 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("該顧問資料並不存在於媒合平台系統中"));
        List<GrantedAuthority> grantedAuths = Arrays.asList(new SimpleGrantedAuthority("ROLE_USER"));
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(account, credential, grantedAuths);
        Map<String, String> details = new HashMap<>();
        details.put(ConsultantDetailEnum.ID.getValue(), consultant.getId().toString());
        details.put(ConsultantDetailEnum.NAME.getValue(), consultant.getName());
        details.put(ConsultantDetailEnum.AGENT_NO.getValue(), account);
        authenticationToken.setDetails(details);
        return authenticationToken;
    }
}
pamapi/src/main/java/com/pollex/pam/web/rest/EServiceResource.java
@@ -1,6 +1,7 @@
package com.pollex.pam.web.rest;
import com.pollex.pam.business.aop.logging.audit.AuditLoggingInject;
import com.pollex.pam.business.service.ConsultantService;
import com.pollex.pam.security.jwt.JWTFilter;
import com.pollex.pam.security.jwt.TokenProvider;
import com.pollex.pam.business.security.token.EServiceAuthenticationToken;
@@ -29,6 +30,9 @@
    @Autowired
    TokenProvider tokenProvider;
    @Autowired
    ConsultantService consultantService;
    @AuditLoggingInject(type = CONSULTANT_LOGIN)
    @PostMapping("/authenticate")
    public ResponseEntity<UserJWTController.JWTToken> authorize(@RequestBody EServiceLoginVM eServiceLoginVM) {
@@ -38,7 +42,9 @@
        );
        Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);
        consultantService.updateLoginTime(eServiceLoginVM.getUsername());
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        String jwt = tokenProvider.createToken(authentication, false);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add(JWTFilter.AUTHORIZATION_HEADER, "Bearer" + jwt);