JAVA项目实现授权 (二)

版权声明:重在参与,贵在分享 https://blog.csdn.net/wohaqiyi/article/details/82986967

JAVA项目实现授权 (二)

   本篇文章只写具体的实现方法,如果想看实现的介绍,可以参考JAVA项目实现授权 (一) ,这里详细说明了一下实现步骤。直接贴代码了。。。

1、controller接口

  本类中外放两个接口,一个是验证授权的接口,一个是保存授权码的接口。这里能区分机器的只用到了硬盘序号 ,前一篇文章里,我还说用物理地址 ,这里就简单的用一个吧。

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import springboot.xjweb.license.utils.CheckAuthorizeCode;
import springboot.xjweb.license.utils.EncoderFile;
import springboot.xjweb.license.utils.LicenseCode;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class LicenseController {

    //行政区划,替代物理地址,加密只用了硬盘序号
    public String xzqh = "100000";

    //系统名称
    public String xm_name = "ZNXDPRO";

    //授权码文件保存位置
    private String licensePath = "d:/license/encoder.txt";

    /**
     * 单独的验证授权的方法,该方法中拿授权文件中的授权码,解密成一个
     * 系统名称+行政区划+硬盘序号+授权时间+授权截止时间串,
     * 然后获取本机的系统名称+行政区划+硬盘序号串,两者进行比较,如果验证通过,再判断时间是否
     * 到期,如果都通过,则返回验证通过。
     * 否则,使用本机的系统名称+行政区划+硬盘序号串,加密成一个申请码返回给前台
     * @return
     */
    @CrossOrigin
    @GetMapping(value= "/license" )
    public Object license(){
        Map resultMap = new HashMap();
        try {
            if (CheckAuthorizeCode.AuthorizeCode(licensePath,xm_name, xzqh)) {
                resultMap.put("success",true);
                resultMap.put("msg","授权成功");
                return resultMap;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        resultMap.put("success",false);
        resultMap.put("msg","系统未授权,请授权后登录");
        resultMap.put("license",LicenseCode.getApplyCode(xm_name,xzqh));
        return resultMap;
    }

    /**
     * 单独的授权码保存接口,拿到传入的授权码后,保存到服务器的一个文件中
     * @param code
     * @return
     */
    @CrossOrigin
    @GetMapping(value= "/importlicense/{code}" )
    public Object importlicense(@PathVariable String code){
        Map resultMap = new HashMap();
        File file = new File(licensePath);
        if(!file.exists()){
            file.getParentFile().mkdirs();
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        EncoderFile.mywrite(file,code);
        boolean flag = false;
        try {
            flag = CheckAuthorizeCode.AuthorizeCode(licensePath,xm_name, xzqh); //验证授权码是否正确
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(flag){ //授权成功
            resultMap.put("success",true);
            resultMap.put("msg","授权成功");
            return resultMap;
        }
        resultMap.put("success",false);
        resultMap.put("msg","授权码错误");
        return resultMap;
    }
}

2、验证授权的具体工具类

   本类里主要就是获取本机的硬盘序号,拼成一个串a,然后与从授权文件里拿到的授权码,授权码解析成一个串b,两个串进行比较,看看b串里是不是含有a 串,并且看一下b串里的截止时间是否过期,注意判断过期的这一步必须加上,原因代码里写了。

import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.io.IOException;

/**
 * Created by JAVA on 2018/7/24.
 */
public class CheckAuthorizeCode {

    /**
     * 验证授权方法
     * @param licensePath
     * @param xm_name
     * @param xzqh
     * @return
     * @throws Exception
     */
    public static boolean AuthorizeCode(String licensePath,String xm_name,String xzqh) throws Exception {
        File file = new File(licensePath);
        if(!file.exists()){
           return false; //如果授权文件都不存在,肯定还未授权直接返回false
        }
        //获取硬盘序号
        String code = LicenseCode.getBaseCode(xm_name,xzqh);
        //读取授权码文件,获取授权码
        String encoder = EncoderFile.myread(file).trim();
        if(StringUtils.isEmpty(encoder)){
            return false;
        }
        //解析授权码,与本机的系统名称+行政区划+硬盘序号进行比较,这里仅仅只是用的字符串contains方法
        String en = LicenseCode.getPlaintext(encoder);
        if (!en.contains(code)) {
            throw new Exception();
        }
        /**
         * 判断时间是否过期(这一步必须判断)
         * 因为系统名称+行政区划+硬盘号+当前时间构成了申请码
         * 系统名称+行政区划+硬盘号+授权时间+授权截止时间构成了授权码
         * 两者的不同,仅仅在于授权码多了一个截止时间。
         * 如果仅仅判断了是否包含,那会出现一个bug,直接把申请码当做授权码就能通过验证的
         * 这里没用到授权时间,你可以加密的时候不要当前时间。
         */
        String[] split = en.split(",");
        if (!DateUtils.authorize_date(split[4])) {
            throw new Exception();
        }
        return true;
    }
}

3、加密解密工具类

  加密解密工具类,采用jdk自带的crypto包里的,不需要引入依赖。

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

/**
 * 授权加密解密工具类,做成授权工具的话,主要就是用的这个类
 */
public class DESUtils {
	/**
	 * 密钥
	 */
	public static final String DEFAULT_KEY = "BOTWAVEE";



	public static String decrypt(String message) throws Exception {
		return java.net.URLDecoder.decode(decrypt(message, DEFAULT_KEY), "utf-8");
	}

	/**
	 * 解密
	 * @param message 加密后的内容
	 * @param key 密钥
	 * @return
	 * @throws Exception
	 */
	public static String decrypt(String message, String key) throws Exception {

		byte[] bytesrc = convertHexString(message);
		Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
		DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
		IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));

		cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);

		byte[] retByte = cipher.doFinal(bytesrc);
		return new String(retByte);
	}


	public static String encrypt(String message) throws Exception{

		return toHexString(encrypt(message, DEFAULT_KEY)).toUpperCase();
	}

	/**
	 * 加密
	 * 
	 * @param message
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static byte[] encrypt(String message, String key) throws Exception {
		Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

		DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));

		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
		IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
		cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);

		return cipher.doFinal(message.getBytes("UTF-8"));
	}

	public static byte[] convertHexString(String ss) {
		byte digest[] = new byte[ss.length() / 2];
		for (int i = 0; i < digest.length; i++) {
			String byteString = ss.substring(2 * i, 2 * i + 2);
			int byteValue = Integer.parseInt(byteString, 16);
			digest[i] = (byte) byteValue;
		}

		return digest;
	}

	public static void main(String[] args) throws Exception {
		String key = "BOTWAVEE";
		String jiami = "znxd,000000,7005-459C,2018-07-27,2022-02-02";


		System.out.println("加密数据:" + jiami);
		String a = encrypt(jiami);
		System.out.println("加密后的数据为:" + a);
		
		String b =decrypt(a);
		System.out.println("解密后的数据:" + b);

	}

	public static String toHexString(byte b[]) {
		StringBuffer hexString = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			String plainText = Integer.toHexString(0xff & b[i]);
			if (plainText.length() < 2)
				plainText = "0" + plainText;
			hexString.append(plainText);
		}

		return hexString.toString();
	}
}

4、其他

  还有好几个辅助的类没贴上,主要是上边三个,我还是直接把代码都放到了百度网盘上了,有需要的可以下载下来。
授权逻辑代码下载地址

5、思考

  这个授权方法,总感觉不是很好,因为加密是可逆的,并且申请码和授权码的加密方式都是一样的,这个不是很好。
  但是运用得当,还是可以的,虽然加密方法,网上就可以搜到,但是各自的盐不一样,另外,虽然申请码和授权码的加密解密方式都一样,但是授权码多了一个授权截止时间,只要在授权方法里别忘了判断授权截止时间是否过期,那也没问题,但是话说回来,我加密的源是系统名称+行政区划+硬盘序号+当前时间+授权截止时间 ,但是别人未必这样加密,可能最后还有别的内容,这样也不好破解。
  我本来想到的办法,其实是这样的,让申请码的加密采用对称加密,可以互逆。申请码可以通过另一种非对称加密变成授权码的一部分,再加上额外的截止时间采用对称加密加密成另一部分,最后,这两部分采用一定规律合成一个授权码,这样,不知道好不好。

猜你喜欢

转载自blog.csdn.net/wohaqiyi/article/details/82986967
今日推荐