[update] 修正發送OTP api,將兩個發送方式並成同一個
¤ñ¹ï·sÀÉ®× |
| | |
| | | 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": "" |
| | | } |
¤ñ¹ï·sÀÉ®× |
| | |
| | | package com.pollex.pam.enums; |
| | | |
| | | public enum OtpLoginTypeEnum { |
| | | SMS, |
| | | EMAIL |
| | | } |
| | |
| | | |
| | | 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); |
| | |
| | | |
| | | 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); |
| | |
| | | return new OtpResponseDTO(result); |
| | | } |
| | | |
| | | private OtpWebServicePortBindingStub getOtpWebServicePortBindingStub() throws ServiceException { |
| | | public OtpWebServicePortBindingStub getOtpWebServicePortBindingStub() throws ServiceException { |
| | | OtpWebServiceLocator locator = new OtpWebServiceLocator(); |
| | | locator.setOtpWebServicePortEndpointAddress(applicationProperty.getOtpWebServiceUrl()); |
| | | |
| | |
| | | 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; |
| | |
| | | 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") |
| | |
| | | @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"); |
| | | } |
| | |
| | | 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", "", ""}); |
| | | } |
| | |
| | | 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 { |
| | |
| | | |
| | | @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") |
¤ñ¹ï·sÀÉ®× |
| | |
| | | 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; |
| | | } |
| | | } |