授权工具开发

106 阅读3分钟

开发一个授权工具

一、需求

开发一个用于授权的小工具,由于项目部署需要进行授权,生成授权文件的代码是一个main方法,所以有这么一个需求,要把授权的功能做成一个授权工具,以后移交给实施人员进行授权,因此需要开发一个授权工具

二、设计思路

本人熟悉java语言,并且授权功能是基于java开发的,所以我们需要利用java的awt功能开发一个授权界面,并且将授权功能迁移到该项目中即可

三、项目搭建
1、创建一个java项目

image-20230308181402105.png

image-20230308181519280.png

2、构建artifacts

image-20230308181847898.png

image-20230308181942784.png

3、打包

image-20230308182100880.png

image-20230308182136067.png

四、核心代码
package com.superred.th.license.active;
​
​
import com.superred.th.license.LicGen;
​
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
​
/**
 * @author hdqzz
 */
public class ToolFrame extends JFrame implements ActionListener {
    private static final long serialVersionUID = -1L;
    private static final int WIDTH = 800;
    private static final int HEIGHT = 600;
    private StringBuffer helpMessage = new StringBuffer()
            .append("操作步骤.\n")
            .append("1. 填写机器码\n")
            .append("2. 修改授权日志如下2024-12-31\n")
            .append("3. 点击按钮生成授权码\n")
            .append("4. 点击Tool中生成授权文件按钮生成授权文件\n\n");
​
​
    /**
     * 定义输入框字段
     *
     **/
    JTextField tfSystemId = new JTextField();
    JTextField tfExpireTime = new JTextField();
    JTextField tfActivationCode = new JTextField();
​
    /**
     * 定义按钮和菜单按钮
     **/
    JButton jbActive = new JButton("生成授权码");
    JTextArea taInfo = new JTextArea(helpMessage.toString());
​
    JMenuItem runMenu = new JMenuItem("生成授权码");
    JMenuItem saveProperitiesMenu = new JMenuItem("保存License");
    JMenuItem exitMenu = new JMenuItem("Exit");
​
​
    private String lastLicenseData = null;
​
    /**
     * 创建授权工具框架
     * @Author zxy
     * @Date 17:43 2023/3/8
     * @return
     **/
    public ToolFrame() {
        super("授权工具");
        initFrame();
        setDefaultCloseOperation(3);
        setSize(800, 600);
        setVisible(true);
    }
​
    /**
     * 初始化菜单上部授权区域和下部中心区域
     * @Author zxy
     * @Date 17:43 2023/3/8
     * @return void
     **/
    public void initFrame() {
        initMenu();
        initTopPanel();
        initCenterPanel();
    }
​
    /**
     * 组装菜单列表Operate和Tools菜单列表
     * @Author zxy
     * @Date 17:44 2023/3/8
     * @return void
     **/
    public void initMenu() {
        JMenuBar menubar = new JMenuBar();
​
        JMenu menu1 = new JMenu("Operate");
        JMenu menu2 = new JMenu("Tools");
​
        menu1.add(runMenu);
        menu1.addSeparator();
        menu1.add(exitMenu);
​
        menu2.add(this.saveProperitiesMenu);
​
        runMenu.addActionListener(this);
        exitMenu.addActionListener(this);
        saveProperitiesMenu.addActionListener(this);
​
        menubar.add(menu1);
        menubar.add(menu2);
​
        setJMenuBar(menubar);
    }
​
    public void initCenterPanel() {
        JScrollPane jsp = new JScrollPane(this.taInfo);
        jsp.setBounds(100, 190, 400, 300);
        getContentPane().add(jsp, "Center");
    }
​
    public void initTopPanel() {
        JPanel toppanel = new JPanel();
        toppanel.setLayout(null);
        JLabel lSystemId = new JLabel("机器编码:", 4);
        JLabel lExpireTime = new JLabel("授权期限:", 4);
        JLabel lActivationCode = new JLabel("生成授权码:", 4);
​
​
        lSystemId.setBounds(10, 50, 85, 25);
        tfSystemId.setBounds(100, 50, 300, 30);
​
        lExpireTime.setBounds(10, 90, 85, 25);
        tfExpireTime.setBounds(100, 90, 300, 30);
        tfExpireTime.setText("2024-12-31");
​
​
        lActivationCode.setBounds(10, 130, 85, 25);
        tfActivationCode.setBounds(100, 130, 300, 30);
        jbActive.setBounds(410, 130, 120, 30);
​
        jbActive.addActionListener(this);
​
        toppanel.add(lSystemId);
        toppanel.add(tfSystemId);
​
        toppanel.add(lExpireTime);
        toppanel.add(tfExpireTime);
​
​
        toppanel.add(lActivationCode);
        toppanel.add(tfActivationCode);
        toppanel.add(jbActive);
​
        JPanel tmpPanel = new JPanel();
        tmpPanel.add(toppanel, "Center");
​
        toppanel.setPreferredSize(new Dimension(600, 240));
        getContentPane().add(tmpPanel, "North");
    }
​
    /**
     * ActionListener监听器处理方法,用于处理不同按钮触发的监听方法
     * @Author zxy
     * @Date 17:47 2023/3/8
     * @param paramActionEvent 
     * @return void 
     **/
    @Override
    public void actionPerformed(ActionEvent paramActionEvent) {
        Object source = paramActionEvent.getSource();
​
        if ((source == this.jbActive) || (source == this.runMenu)) {
            /**
             * 生成授权码
             **/
            try {
                taInfo.append("机器编码:" + tfSystemId.getText().trim() + "\n");
                taInfo.append("授权期限:" + tfExpireTime.getText().trim() + "\n");
                String activeCode = LicGen.genLicStr(tfExpireTime.getText().trim(), tfSystemId.getText().trim());
                tfActivationCode.setText(activeCode == null ? "--" : activeCode);
                taInfo.append("机器编码:" + tfSystemId.getText().trim() + "\n");
                taInfo.append("授权期限:" + tfExpireTime.getText().trim() + "\n");
                taInfo.append("授权码:" + activeCode + "\n");
            } catch (Exception e) {
                e.printStackTrace();
            }
​
        } else if (source == saveProperitiesMenu) {
            /**
             *  生成授权文件
             **/
            try {
                String licPath = LicGen.genLic("C:\license\", tfExpireTime.getText().trim(), tfSystemId.getText().trim());
                taInfo.append("机器编码:" + tfSystemId.getText().trim() + "\n");
                taInfo.append("授权期限:" + tfExpireTime.getText().trim() + "\n");
                taInfo.append("授权文件路径:" + licPath + "\n");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (source == exitMenu) {
            /**
             * 退出功能
             **/
            System.exit(0);
        }
    }
​
    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        new ToolFrame();
    }
}
​