Pārlūkot izejas kodu

BMD用户登录加密解密

njs 1 gadu atpakaļ
vecāks
revīzija
bb84315e65

+ 16 - 2
suishenbang-api/src/test/java/test/MyTest.java

@@ -2,6 +2,8 @@ package test;
 
 
 import com.dgtly.ApiApplication;
+import com.dgtly.common.utils.bean.EnDecoderUtil;
+import com.dgtly.common.utils.bean.HexUtils;
 import com.dgtly.system.domain.SysUser;
 import com.dgtly.system.service.ISysUserService;
 import com.dgtly.system.service.impl.SysUserServiceImpl;
@@ -17,6 +19,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringRunner;
 
+import java.nio.charset.StandardCharsets;
 import java.util.*;
 
 @RunWith(SpringRunner.class)
@@ -43,10 +46,21 @@ public class MyTest {
 
     @Test
     public void test() throws Exception {
-       String certificateUrl="/profile/pdf/2023/04/13/0584202872.pdf";
+    /*   String certificateUrl="/profile/pdf/2023/04/13/0584202872.pdf";
         String orders="6107665807";
-        esignSignService.tmsCertificate(orders, certificateUrl);
+        esignSignService.tmsCertificate(orders, certificateUrl);*/
+        String name="gujing.sm";
+
+        //base64进行加密解密,通常用作对二进制数据进行加密
+        byte[] base64Encrypt = EnDecoderUtil.base64Encrypt(name);
+     /*   String toHexString = HexUtils.toHexString(base64Encrypt);
+        System.out.println(toHexString);*/
+        String toHexString="5a3356716157356e4c6e4e74";
+        byte[] to = HexUtils.toByteArray(toHexString);
+        byte[] base64Decrypt = EnDecoderUtil.base64Decrypt(to);
+        System.out.println(new String(base64Decrypt));
     }
+
 }
       /*  Set<String> s = new LinkedHashSet<>();
         s.add("0110065150");

+ 24 - 0
suishenbang-common/src/main/java/com/dgtly/common/utils/bean/EnDecoderUtil.java

@@ -0,0 +1,24 @@
+package com.dgtly.common.utils.bean;
+
+import java.util.Base64;
+
+public class EnDecoderUtil {
+
+    /**
+     * base64加密
+     * @param content 待加密内容
+     * @return byte[]
+     */
+    public static byte[] base64Encrypt(final String content) {
+        return Base64.getEncoder().encode(content.getBytes());
+    }
+
+    /**
+     * base64解密
+     * @param encoderContent 已加密内容
+     * @return byte[]
+     */
+    public static byte[] base64Decrypt(final byte[] encoderContent) {
+        return Base64.getDecoder().decode(encoderContent);
+    }
+}

+ 160 - 0
suishenbang-common/src/main/java/com/dgtly/common/utils/bean/HexUtils.java

@@ -0,0 +1,160 @@
+package com.dgtly.common.utils.bean;
+
+
+import java.io.*;
+import java.text.SimpleDateFormat;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * 字节转换工具类
+ */
+public class HexUtils {
+
+    //10进制转十六进制
+    // 低字节在前(低字节序)
+    public static byte[] toLH(int n) {
+        byte[] b = new byte[4];
+        b[0] = (byte) (n & 0xff);
+        b[1] = (byte) (n >> 8 & 0xff);
+        b[2] = (byte) (n >> 16 & 0xff);
+        b[3] = (byte) (n >> 24 & 0xff);
+        return b;
+    }
+
+    //10进制转十六进制
+    //高字节在前(高字节序)
+    public static byte[] toHH(int n) {
+        byte[] b = new byte[4];
+        b[3] = (byte) (n & 0xff);
+        b[2] = (byte) (n >> 8 & 0xff);
+        b[1] = (byte) (n >> 16 & 0xff);
+        b[0] = (byte) (n >> 24 & 0xff);
+        return b;
+    }
+
+    //byte[]转String
+    public static String bytesToString(byte[] value){
+        StringBuilder stringBuilder=new StringBuilder(value.length*2);
+        for(byte b:value)
+        {
+            stringBuilder.append(String.format("%02X",new Integer(b & 0xFF)));
+        }
+
+        return stringBuilder.toString();
+    }
+
+    //打印byte[]
+    public static void printHexString( byte[] b) {
+        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        for (int i = 0; i < b.length; i++) {
+            String hex = Integer.toHexString(b[i] & 0xFF);
+            if (hex.length() == 1) {
+                hex = '0' + hex;
+            }
+            System.out.print(hex.toUpperCase()+" , ");
+        }
+        System.out.println();
+    }
+
+    //byte[]转double
+    public static double byteArrayToDouble(byte[] bytes){
+        long ff = 0xFF;
+        long bitLayoutLongValue = 0;
+        for (int i = 0; i < bytes.length; i++) {
+            bitLayoutLongValue |= (bytes[i] & ff) << (bytes.length - i - 1) * 8;
+        }
+        return Double.longBitsToDouble(bitLayoutLongValue);
+
+    }
+
+    //byte[]转file
+    public static void byteArrayToFile(byte[] src, File dest) {
+        OutputStream os = null;
+        try {
+            os = new BufferedOutputStream(new FileOutputStream(dest));
+            InputStream is = new ByteArrayInputStream(src);
+            byte[] flushBuffer = new byte[src.length];
+            int readLen = -1;
+            while ((readLen = is.read(flushBuffer)) != -1) {
+                os.write(flushBuffer, 0, readLen);
+            }
+            os.flush();
+        } catch (FileNotFoundException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            if (os != null) {
+                try {
+                    os.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+    }
+    /**
+     * byte数组转int类型
+     */
+    public static int byteArrayToInt(byte[] bytes) {
+        int ff = 0xFF;
+        int value = 0;
+        for (int i = 0; i < bytes.length; i++) {
+            value |= (bytes[i] & ff) << (bytes.length - i - 1) * 8;
+        }
+        return value;
+
+    }
+
+    /**
+     * byte数组转float类型
+     */
+    public static float byteArrayToFloat(byte[] b) {
+        return Float.intBitsToFloat(Integer.valueOf(bytesToString(b).trim(), 16));
+    }
+
+    /**
+     * String转byte数组
+     */
+    public static byte[] toByteArray(String hexString) {
+
+        hexString = hexString.toLowerCase();
+        final byte[] byteArray = new byte[hexString.length() / 2];
+        int k = 0;
+
+        for (int i = 0; i < byteArray.length; i++) {   //因为是16进制,最多只会占用4位,转换成字节需要两个16进制的字符,高位在先
+            byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
+            byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
+            byteArray[i] = (byte) (high << 4 | low);
+            k += 2;
+        }
+
+        return byteArray;
+    }
+    /**
+     * byte数组中取int数值,本方法适用于(低位在前,高位在后)的顺序,和和intToBytes()配套使用
+     */
+    public static int bytesToInt(byte[] src, int offset) {
+        int value;
+        value = (int) ((src[offset] & 0xFF)
+                | ((src[offset+1] & 0xFF)<<8)
+                | ((src[offset+2] & 0xFF)<<16)
+                | ((src[offset+3] & 0xFF)<<24));
+        return value;
+    }
+
+    /**
+     * byte数组中取int数值,本方法适用于(低位在后,高位在前)的顺序。和intToBytes2()配套使用
+     */
+    public static int bytesToInt2(byte[] src, int offset) {
+        int value;
+        value = (int) ( ((src[offset] & 0xFF)<<24)
+                |((src[offset+1] & 0xFF)<<16)
+                |((src[offset+2] & 0xFF)<<8)
+                |(src[offset+3] & 0xFF));
+        return value;
+    }
+
+
+}

+ 44 - 0
suishenbang-wxportal/suishenbang-wxportal-api/src/main/java/com/dgtly/wxportal/controller/WxController.java

@@ -7,6 +7,8 @@ import com.dgtly.common.core.controller.ApiBaseController;
 import com.dgtly.common.core.domain.AjaxResult;
 import com.dgtly.common.core.domain.ParameterObject;
 import com.dgtly.common.core.domain.Ztree;
+import com.dgtly.common.utils.bean.EnDecoderUtil;
+import com.dgtly.common.utils.bean.HexUtils;
 import com.dgtly.common.utils.http.HttpUtils;
 import com.dgtly.common.utils.security.EncryptPassWordClass;
 import com.dgtly.system.service.ISysConfigService;
@@ -553,4 +555,46 @@ public class WxController extends ApiBaseController {
         return AjaxResult.success();
     }
 
+    /**
+     * @description: BMD经销商登录
+     * @param:
+     * @return:
+     * @author: njs
+     * @date: 2023/5/4 9:30
+     */
+    @ApiOperation(value = "根据账户密码获取用户信息",notes = "参数:{'username':'1','token':'xxx'}" +
+            " 错误:303 无识别令牌" +
+            "错误:302  查无此人")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "params" , paramType = "body")
+    })
+    @PostMapping("/getUserBmdByNameAndToken")
+    public Object getUserBmdByNameAndToken(){
+        ParameterObject obj =  getParameterObject();
+        obj.checkParameterNotNull("username,token");
+        String username = obj.getString("username");
+        String token = obj.getString("token");
+        byte[] toByte = HexUtils.toByteArray(username);
+        byte[] base64Decrypt = EnDecoderUtil.base64Decrypt(toByte);
+        String name=new String(base64Decrypt);
+        SysUser user = sysUserService.selectUserByLoginName(name);
+        if(user==null){
+            return AjaxResult.error(302,"查无此人");
+        }
+        if(token ==null || ("").equals(token) || !("bmdCustomer").equals(token)){
+            return AjaxResult.error(303,"无识别令牌");
+        }
+       // String pass = EncryptPassWordClass.encryptPassword(user.getLoginName(),user.getLoginName(),user.getSalt());
+        List<Ztree>  author = sysUserOrderAuthorService.userAuthorTreeDataFmt(user.getUserId());
+            if (author.size() > 0 && author.get(0).getChildren().size() > 1) {
+                user.setAuthorType("TUC");
+            } else if (author.size() > 0 && author.get(0).getChildren().size() == 1) {//设置BUSINESS_UNIT
+                user.setAuthorType(author.get(0).getChildren().get(0).getCode());
+            }else{
+                user.setAuthorType("DIY");//默认diy
+            }
+            user.setAuthor(author);
+            return AjaxResult.success().putKV("sysUser",user);
+    }
+
 }