保誠-保戶業務員媒合平台
wayne
2021-11-23 a8b21cd919b76a548b77ef0ec2d8aee28c8e6b0e
[update] 修正發送OTP api,將兩個發送方式並成同一個

刪除2個檔案
修改3個檔案
新增3個檔案
165 ■■■■ 已變更過的檔案
pamapi/src/doc/登入API/客戶發EmailOTP.txt 15 ●●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/doc/登入API/客戶發OTP.txt 16 ●●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/doc/登入API/客戶發手機OTP.txt 15 ●●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/main/java/com/pollex/pam/enums/OtpLoginTypeEnum.java 6 ●●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/main/java/com/pollex/pam/service/OtpWebService.java 17 ●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/main/java/com/pollex/pam/web/rest/OtpResource.java 40 ●●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/main/java/com/pollex/pam/web/rest/TestLoginResource.java 32 ●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/main/java/com/pollex/pam/web/rest/vm/OtpLoginVM.java 24 ●●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/doc/µn¤JAPI/«È¤áµoEmailOTP.txt
Àɮפw§R°£
pamapi/src/doc/µn¤JAPI/«È¤áµoOTP.txt
¤ñ¹ï·sÀÉ®×
@@ -0,0 +1,16 @@
http post:
http://localhost:8080/api/otp/sendOtp
request body:
{
    "loginType":"SMS",          // "SMS":Otp發送手機,"EMAIL":Otp發email
    "account": "0912345678",    // è‹¥loginTypeå¡«SMS則該欄帶入手機、EMAIL則帶入郵件信箱
}
response body:
{
    "indexKey": "7c8c38a2",     // ç”¨æ–¼å¸¶å…¥otp認證時
    "success": true,            // Otp是否有成功發送
    "failCode": "",
    "failReason": ""
}
pamapi/src/doc/µn¤JAPI/«È¤áµo¤â¾÷OTP.txt
Àɮפw§R°£
pamapi/src/main/java/com/pollex/pam/enums/OtpLoginTypeEnum.java
¤ñ¹ï·sÀÉ®×
@@ -0,0 +1,6 @@
package com.pollex.pam.enums;
public enum OtpLoginTypeEnum {
    SMS,
    EMAIL
}
pamapi/src/main/java/com/pollex/pam/service/OtpWebService.java
@@ -22,7 +22,8 @@
    public OtpResponseDTO sendByPhone(String phone) throws ServiceException, RemoteException {
        OtpWebServicePortBindingStub stub = getOtpWebServicePortBindingStub();
        log.info("call OtpService snedOtpBySMS, ");
        log.info("call OtpService sendOtpBySMS, url = {}, systemType = {}, service password = {}, phone = {}",
            applicationProperty.getOtpWebServiceUrl(), applicationProperty.getOtpWebServiceSystemType(), applicationProperty.getOtpWebServicePassword(), phone);
        String[] result =
            stub.sendOtpBySMS(applicationProperty.getOtpWebServicePassword(), applicationProperty.getOtpWebServiceSystemType(), phone);
@@ -32,21 +33,19 @@
    public OtpResponseDTO sendByEmail(String email) throws ServiceException, RemoteException {
        OtpWebServicePortBindingStub stub = getOtpWebServicePortBindingStub();
        log.info("call OtpService sendByEmail, url = {}, systemType = {}, service password = {}, email = {}",
            applicationProperty.getOtpWebServiceUrl(), applicationProperty.getOtpWebServiceSystemType(), applicationProperty.getOtpWebServicePassword(), email);
        String[] result =
            stub.sendOtpByEmail(applicationProperty.getOtpWebServicePassword(), applicationProperty.getOtpWebServiceSystemType(), email);
        final OtpResponseDTO otpResponseDTO = new OtpResponseDTO(result);
        if(otpResponseDTO.isSuccess()) {
            return otpResponseDTO;
        }
        else {
            throw new RuntimeException("error code = " + otpResponseDTO.getFailCode() + ", error reason = " + otpResponseDTO.getFailReason());
        }
        return new OtpResponseDTO(result);
    }
    public OtpResponseDTO verifyOTP(String indexKey, String otpCode) throws ServiceException, RemoteException {
        OtpWebServicePortBindingStub stub = getOtpWebServicePortBindingStub();
        log.info("call OtpService verifyOTP, url = {}, systemType = {}, service password = {}, indexKey = {}, otpCode = {}",
            applicationProperty.getOtpWebServiceUrl(), applicationProperty.getOtpWebServiceSystemType(), applicationProperty.getOtpWebServicePassword(), indexKey, otpCode);
        String[] result =
            stub.verifyOtp(applicationProperty.getOtpWebServicePassword(), applicationProperty.getOtpWebServiceSystemType(), indexKey, otpCode);
@@ -54,7 +53,7 @@
        return new OtpResponseDTO(result);
    }
    private OtpWebServicePortBindingStub getOtpWebServicePortBindingStub() throws ServiceException {
    public OtpWebServicePortBindingStub getOtpWebServicePortBindingStub() throws ServiceException {
        OtpWebServiceLocator locator = new OtpWebServiceLocator();
        locator.setOtpWebServicePortEndpointAddress(applicationProperty.getOtpWebServiceUrl());
pamapi/src/main/java/com/pollex/pam/web/rest/OtpResource.java
@@ -1,15 +1,13 @@
package com.pollex.pam.web.rest;
import com.pollex.pam.config.ApplicationProperties;
import com.pollex.pam.enums.OtpLoginTypeEnum;
import com.pollex.pam.security.jwt.JWTFilter;
import com.pollex.pam.security.jwt.TokenProvider;
import com.pollex.pam.security.token.OtpAuthenticationToken;
import com.pollex.pam.service.OtpWebService;
import com.pollex.pam.service.dto.OtpResponseDTO;
import com.pollex.pam.web.rest.vm.OtpAccount;
import com.pollex.pam.web.rest.vm.OtpEmailLoginVM;
import com.pollex.pam.web.rest.vm.OtpSMSLoginVM;
import com.pollex.pam.web.rest.vm.VerifyOtpVM;
import com.pollex.pam.web.rest.vm.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -22,14 +20,8 @@
import org.springframework.web.bind.annotation.*;
import javax.xml.rpc.ServiceException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.rmi.RemoteException;
import java.util.Arrays;
import java.util.Random;
import java.util.UUID;
import static java.nio.charset.StandardCharsets.UTF_8;
@RestController
@RequestMapping("/api/otp")
@@ -49,29 +41,21 @@
    @Autowired
    TokenProvider tokenProvider;
    @PostMapping("/byPhone")
    public ResponseEntity<Object> sendOtpByPhone(@RequestBody OtpSMSLoginVM login) {
    @PostMapping("/sendOtp")
    public ResponseEntity<Object> sendOtp(@RequestBody OtpLoginVM login) {
        try {
            if(applicationProperty.isMockLogin()) {
                return new ResponseEntity<>(getMockOtpResponse(), HttpStatus.OK);
                return new ResponseEntity<>(getMockSendOtpResponse(), HttpStatus.OK);
            }
            OtpResponseDTO otpResponseDTO = otpWebService.sendByPhone(login.getPhone());
            return new ResponseEntity<>(otpResponseDTO, HttpStatus.OK);
        } catch (ServiceException | RemoteException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("connecting otp web service error");
        }
    }
    @PostMapping("/byEmail")
    public ResponseEntity<Object> sendOtpByEmail(@RequestBody OtpEmailLoginVM login) {
        try {
            if(applicationProperty.isMockLogin()) {
                return new ResponseEntity<>(getMockOtpResponse(), HttpStatus.OK);
            if(login.getLoginType() == OtpLoginTypeEnum.SMS) {
                return new ResponseEntity<>(otpWebService.sendByPhone(login.getAccount()), HttpStatus.OK);
            }
            else if(login.getLoginType() == OtpLoginTypeEnum.EMAIL) {
                return new ResponseEntity<>(otpWebService.sendByEmail(login.getAccount()), HttpStatus.OK);
            }
            OtpResponseDTO otpResponseDTO = otpWebService.sendByEmail(login.getEmail());
            return new ResponseEntity<>(otpResponseDTO, HttpStatus.OK);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("can not support this login type, loginType = " + login.getLoginType().name());
        } catch (ServiceException | RemoteException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("connecting otp web service error");
        }
@@ -93,7 +77,7 @@
        return new ResponseEntity<>(new UserJWTController.JWTToken(jwt), httpHeaders, HttpStatus.OK);
    }
    private OtpResponseDTO getMockOtpResponse() {
    private OtpResponseDTO getMockSendOtpResponse() {
        String indexKey = UUID.randomUUID().toString().substring(0, 8);
        return new OtpResponseDTO(new String[]{indexKey, "0", "", ""});
    }
pamapi/src/main/java/com/pollex/pam/web/rest/TestLoginResource.java
@@ -19,10 +19,14 @@
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import tw.com.softleader.otp.ws.OtpWebServicePortBindingStub;
import javax.xml.rpc.ServiceException;
import java.rmi.RemoteException;
// todo,僅為初期接login方便使用而用get的方式登入,目前已拆出OtpResource與EServiceResource,主要是用這兩個做登入
@Deprecated
@RestController
@RequestMapping("/api/testLogin")
public class TestLoginResource {
@@ -46,30 +50,26 @@
    @GetMapping("/bySMS")
    public ResponseEntity<OtpResponseDTO> sendOtpBySMS(@RequestParam("phone") String phone) throws ServiceException, RemoteException {
        otpWebService.sendByPhone(phone);
        return new ResponseEntity<>(HttpStatus.OK);
        final OtpResponseDTO otpResponseDTO = otpWebService.sendByPhone(phone);
        return new ResponseEntity<>(otpResponseDTO, HttpStatus.OK);
    }
    @GetMapping("/byEmail")
    public ResponseEntity<OtpResponseDTO> sendOtpByEmail(@RequestParam("email") String email) throws RemoteException, ServiceException {
        otpWebService.sendByEmail(email);
        return new ResponseEntity<>(HttpStatus.OK);
        final OtpResponseDTO otpResponseDTO = otpWebService.sendByEmail(email);
        return new ResponseEntity<>(otpResponseDTO, HttpStatus.OK);
    }
    @GetMapping("/verifyOtp")
    public ResponseEntity<UserJWTController.JWTToken> verifyOtp(@RequestParam("account") String account, @RequestParam("indexKey") String indexKey, @RequestParam("otpCode") String otpCode) throws ServiceException, RemoteException {
        OtpAccount otpAccount = new OtpAccount(account, indexKey);
        OtpAuthenticationToken authenticationToken = new OtpAuthenticationToken(
            otpAccount,
            otpCode
        );
    public ResponseEntity<OtpResponseDTO> verifyOtp(@RequestParam("account") String account, @RequestParam("indexKey") String indexKey, @RequestParam("otpCode") String otpCode) throws ServiceException, RemoteException {
        OtpWebServicePortBindingStub stub = otpWebService.getOtpWebServicePortBindingStub();
        log.info("call OtpService verifyOTP, systemType = {}, service password = {}, indexKey = {}, paxxword = {}",
            applicationProperty.getOtpWebServiceSystemType(), applicationProperty.getOtpWebServicePassword(), indexKey, otpCode);
        Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        String jwt = tokenProvider.createToken(authentication, false);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add(JWTFilter.AUTHORIZATION_HEADER, "Bearer" + jwt);
        return new ResponseEntity<>(new UserJWTController.JWTToken(jwt), httpHeaders, HttpStatus.OK);
        String[] result =
            stub.verifyOtp(applicationProperty.getOtpWebServicePassword(), applicationProperty.getOtpWebServiceSystemType(), indexKey, otpCode);
        return new ResponseEntity<>(new OtpResponseDTO(result), HttpStatus.OK);
    }
    @GetMapping("/byEService")
pamapi/src/main/java/com/pollex/pam/web/rest/vm/OtpLoginVM.java
¤ñ¹ï·sÀÉ®×
@@ -0,0 +1,24 @@
package com.pollex.pam.web.rest.vm;
import com.pollex.pam.enums.OtpLoginTypeEnum;
public class OtpLoginVM {
    private OtpLoginTypeEnum loginType;
    private String account;
    public OtpLoginTypeEnum getLoginType() {
        return loginType;
    }
    public void setLoginType(OtpLoginTypeEnum loginType) {
        this.loginType = loginType;
    }
    public String getAccount() {
        return account;
    }
    public void setAccount(String account) {
        this.account = account;
    }
}