From f5b6fe34ff7121d38c266acb1aed89fb4a3905fd Mon Sep 17 00:00:00 2001 From: duLingLing Date: Wed, 27 Nov 2019 18:51:47 +0800 Subject: [PATCH] =?UTF-8?q?Add:=E5=AF=B9=E6=9E=81=E5=AE=A2=E5=95=86?= =?UTF-8?q?=E6=9C=8D=E6=8F=90=E4=BE=9B=E6=9F=A5=E8=AF=A2=E5=95=86=E6=88=B7?= =?UTF-8?q?=E8=AF=A6=E6=83=85API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../beans/ApiClientAccountLoginInfo.java | 52 ++++++++++ .../appclient/web/RetailClientController.java | 80 ++++++++++++++++ .../mappers/system/IndustryLookupMapper.java | 18 ++++ .../manage/merchants/core/ClientManager.java | 2 + .../core/impls/ClientManagerImpl.java | 13 +++ .../permission/utils/GeekLoginDESUtil.java | 94 +++++++++++++++++++ 6 files changed, 259 insertions(+) create mode 100644 src/main/java/au/com/royalpay/payment/manage/appclient/beans/ApiClientAccountLoginInfo.java create mode 100644 src/main/java/au/com/royalpay/payment/manage/appclient/web/RetailClientController.java create mode 100644 src/main/java/au/com/royalpay/payment/manage/mappers/system/IndustryLookupMapper.java create mode 100644 src/main/java/au/com/royalpay/payment/manage/permission/utils/GeekLoginDESUtil.java diff --git a/src/main/java/au/com/royalpay/payment/manage/appclient/beans/ApiClientAccountLoginInfo.java b/src/main/java/au/com/royalpay/payment/manage/appclient/beans/ApiClientAccountLoginInfo.java new file mode 100644 index 000000000..e23e1c1cf --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/appclient/beans/ApiClientAccountLoginInfo.java @@ -0,0 +1,52 @@ +package au.com.royalpay.payment.manage.appclient.beans; + +import au.com.royalpay.payment.manage.permission.utils.GeekLoginDESUtil; +import au.com.royalpay.payment.manage.signin.beans.LoginInfo; +import au.com.royalpay.payment.tools.utils.PasswordUtils; + +import javax.validation.constraints.NotEmpty; + +/** + * Created by yixian on 2016-12-14. + */ +public class ApiClientAccountLoginInfo { + @NotEmpty(message = "error.payment.valid.param_missing") + private String loginId; + @NotEmpty(message = "error.payment.valid.param_missing") + private String password; + + public LoginInfo ClientAccountLoginInfo(){ + LoginInfo info = new LoginInfo(); + info.setLoginId(loginId); + info.setPassword(GeekLoginDESUtil.getDecryptString(password)); + return info; + } + + public String getPasswordHashed(String salt) { + return PasswordUtils.hashPwd(password, salt); + } + + public String getLoginId() { + return loginId.toLowerCase(); + } + + public void setLoginId(String loginId) { + this.loginId = loginId; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + @Override + public String toString() { + return "ApiClientAccountLoginInfo{" + + "loginId='" + loginId + '\'' + + ", password='" + password + '\'' + + '}'; + } +} diff --git a/src/main/java/au/com/royalpay/payment/manage/appclient/web/RetailClientController.java b/src/main/java/au/com/royalpay/payment/manage/appclient/web/RetailClientController.java new file mode 100644 index 000000000..aaae410e5 --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/appclient/web/RetailClientController.java @@ -0,0 +1,80 @@ +package au.com.royalpay.payment.manage.appclient.web; + +import au.com.royalpay.payment.manage.appclient.beans.ApiClientAccountLoginInfo; +import au.com.royalpay.payment.manage.merchants.core.ClientManager; +import au.com.royalpay.payment.manage.signin.core.SignInStatusManager; +import au.com.royalpay.payment.manage.signin.events.ClientLoginEvent; +import au.com.royalpay.payment.tools.env.RequestEnvironment; +import au.com.royalpay.payment.tools.exceptions.BadRequestException; +import com.alibaba.fastjson.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.ApplicationEventPublisherAware; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import au.com.royalpay.payment.manage.permission.utils.GeekLoginDESUtil; + +import javax.annotation.Resource; +import javax.validation.Valid; + +/** + * @Author DuLingLing + * @create 2019/11/27 0027 14:30 + */ +@RestController +@RequestMapping("/api/v1.0/royalpay/client") +public class RetailClientController implements ApplicationEventPublisherAware { + Logger logger = LoggerFactory.getLogger(getClass()); + @Resource + private SignInStatusManager signInStatusManager; + @Resource + private ClientManager clientManager; + private ApplicationEventPublisher publisher; + + @Override + public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { + this.publisher = applicationEventPublisher; + } + + @PostMapping("/sigIn") + public JSONObject signIn(@RequestBody @Valid ApiClientAccountLoginInfo loginInfo) { + try { + String signKey = signInStatusManager.partnerSignIn(loginInfo.ClientAccountLoginInfo()); + JSONObject account = signInStatusManager.getCurrentClient(signKey); + JSONObject clientAllInfo = clientManager.getClientInfo(account.getInteger("client_id")); + publisher.publishEvent( + new ClientLoginEvent(this, account.getIntValue("client_id"), account.getString("account_id"), RequestEnvironment.getClientIp(), "GEEK_PASSWORD")); + return convertResponseClientInfo(clientAllInfo); + } catch (BadRequestException be) { + return responseError(be.getMessage()); + } catch (Exception e) { + return responseError(e.getMessage()); + } + } + + private JSONObject convertResponseClientInfo(JSONObject clientAllInfo) { + JSONObject response = new JSONObject(); + response.put("status", "SUCCESS"); + JSONObject industryInfo = clientManager.findByLookupCode(clientAllInfo.getString("industry")); + response.put("clientInfo", new JSONObject() {{ + put("client_moniker", clientAllInfo.getString("client_moniker")); + put("company_name", clientAllInfo.getString("company_name")); + put("logo_url", clientAllInfo.getString("client_moniker")); + put("credential_code", clientAllInfo.getString("credential_code")); + put("industry_code", clientAllInfo.getString("industry")); + put("industry_label", industryInfo.getString("lookup_value")); + }}); + return response; + } + + private JSONObject responseError(String message) { + JSONObject response = new JSONObject(); + response.put("status", "FAIL"); + response.put("message", message); + return response; + } + +} diff --git a/src/main/java/au/com/royalpay/payment/manage/mappers/system/IndustryLookupMapper.java b/src/main/java/au/com/royalpay/payment/manage/mappers/system/IndustryLookupMapper.java new file mode 100644 index 000000000..aa3b99ebe --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/mappers/system/IndustryLookupMapper.java @@ -0,0 +1,18 @@ +package au.com.royalpay.payment.manage.mappers.system; + +import cn.yixblog.support.mybatis.autosql.annotations.AutoMapper; +import cn.yixblog.support.mybatis.autosql.annotations.AutoSql; +import cn.yixblog.support.mybatis.autosql.annotations.SqlType; +import com.alibaba.fastjson.JSONObject; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator; + +/** + * @Author DuLingLing + * @create 2019/11/27 0027 16:20 + */ +@AutoMapper(tablename = "industry_lookup", pkName = "client_id", keyGenerator = Jdbc3KeyGenerator.class) +public interface IndustryLookupMapper { + @AutoSql(type = SqlType.SELECT) + JSONObject findByLookupCode(@Param("lookup_code") String code); +} diff --git a/src/main/java/au/com/royalpay/payment/manage/merchants/core/ClientManager.java b/src/main/java/au/com/royalpay/payment/manage/merchants/core/ClientManager.java index 35306f255..250439283 100644 --- a/src/main/java/au/com/royalpay/payment/manage/merchants/core/ClientManager.java +++ b/src/main/java/au/com/royalpay/payment/manage/merchants/core/ClientManager.java @@ -515,4 +515,6 @@ public interface ClientManager { */ JSONObject partnerIncrementalServiceInfo(String clientMoniker, String incrementalId); + JSONObject findByLookupCode(String code); + } diff --git a/src/main/java/au/com/royalpay/payment/manage/merchants/core/impls/ClientManagerImpl.java b/src/main/java/au/com/royalpay/payment/manage/merchants/core/impls/ClientManagerImpl.java index 7aed67259..666fcc129 100644 --- a/src/main/java/au/com/royalpay/payment/manage/merchants/core/impls/ClientManagerImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/merchants/core/impls/ClientManagerImpl.java @@ -302,6 +302,8 @@ public class ClientManagerImpl implements ClientManager, ManagerTodoNoticeProvid private MongoTemplate mongoTemplate; @Resource private ClientComplianceCompanyMapper clientComplianceCompanyMapper; + @Resource + private IndustryLookupMapper industryLookupMapper; @Resource @@ -6136,4 +6138,15 @@ public class ClientManagerImpl implements ClientManager, ManagerTodoNoticeProvid clientIncrementalMapper.update(existIncrementalService); } + @Override + public JSONObject findByLookupCode(String code){ + JSONObject industryInfo = industryLookupMapper.findByLookupCode(code); + if(industryInfo==null){ + return new JSONObject(){{ + put("lookup_code","未知行业"); + }}; + } + return industryInfo; + } + } diff --git a/src/main/java/au/com/royalpay/payment/manage/permission/utils/GeekLoginDESUtil.java b/src/main/java/au/com/royalpay/payment/manage/permission/utils/GeekLoginDESUtil.java new file mode 100644 index 000000000..a6b4b7e8a --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/permission/utils/GeekLoginDESUtil.java @@ -0,0 +1,94 @@ +package au.com.royalpay.payment.manage.permission.utils; + +import java.security.Key; +import java.security.SecureRandom; + +import javax.crypto.Cipher; +import javax.crypto.KeyGenerator; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import sun.misc.BASE64Decoder; +import sun.misc.BASE64Encoder; + +/** + * @Author DuLingLing + * @create 2019/11/27 0027 18:32 + */ +public class GeekLoginDESUtil { + + private static final Logger logger = LoggerFactory.getLogger(GeekLoginDESUtil.class); + + private static Key key; + //设置秘钥key + private static String KEY_STR="myKey"; + private static String CHARSETNAME="UTF-8"; + private static String ALGORITHM="DES"; + + static{ + try{ + //生成DES算法对象 + KeyGenerator generator=KeyGenerator.getInstance(ALGORITHM); + //运用SHA1安全策略 + SecureRandom secureRandom=SecureRandom.getInstance("SHA1PRNG"); + //设置上密钥种子 + secureRandom.setSeed(KEY_STR.getBytes()); + //初始化基于SHA1的算法对象 + generator.init(secureRandom); + //生成密钥对象 + key=generator.generateKey(); + generator=null; + }catch(Exception e){ + throw new RuntimeException(e); + } + } + + /** + * 获取加密的信息 + * @param str + * @return + */ + public static String getEncryptString(String str){ + //基于BASE64编码,接收byte[]并转换成String + BASE64Encoder base64Encoder=new BASE64Encoder(); + try { + // 按UTF8编码 + byte[] bytes = str.getBytes(CHARSETNAME); + // 获取加密对象 + Cipher cipher = Cipher.getInstance(ALGORITHM); + // 初始化密码信息 + cipher.init(Cipher.ENCRYPT_MODE, key); + // 加密 + byte[] doFinal = cipher.doFinal(bytes); + // byte[]to encode好的String并返回 + return base64Encoder.encode(doFinal); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** + * 获取解密之后的信息 + * + * @param str + * @return + */ + public static String getDecryptString(String str) { + // 基于BASE64编码,接收byte[]并转换成String + BASE64Decoder base64decoder = new BASE64Decoder(); + try { + // 将字符串decode成byte[] + byte[] bytes = base64decoder.decodeBuffer(str); + // 获取解密对象 + Cipher cipher = Cipher.getInstance(ALGORITHM); + // 初始化解密信息 + cipher.init(Cipher.DECRYPT_MODE, key); + // 解密 + byte[] doFinal = cipher.doFinal(bytes); + // 返回解密之后的信息 + return new String(doFinal, CHARSETNAME); + } catch (Exception e) { + throw new RuntimeException(e); + } + } +}