package license.aliyuniot;
import com.formdev.flatlaf.FlatDarculaLaf;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.swing.*;
import javax.swing.border.*;
/**
* @author 1
*/
public class AliyunIot extends JFrame {
public static void main(String[] args) {
FlatDarculaLaf.install();
new AliyunIot().setVisible(true);
}
public AliyunIot() {
initComponents();
//655 540
setTitle("三元组生成连接地址"); // 标题
setResizable(false); // 固定窗体
//setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); //退出窗口不适用
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口 ,退出进程
//显示屏幕中央
int width = 400; //宽度
int height = 300; // 高度
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
/** 屏幕宽度 */
int screenWidth = screenSize.width;
/** 屏幕高度 */
int screenHeight = screenSize.height;
setLocation((screenWidth - width) / 2, (screenHeight - height) / 2);
setMinimumSize(new Dimension(width,height)); //窗体大小
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String productKey = textField1.getText();
String deviceName = textField2.getText();
String deviceSecret = textField3.getText();
if(productKey == null || "".equals(productKey)){
JOptionPane.showMessageDialog(null,"productKey 不能为空");
return;
}else if(deviceName == null || "".equals(deviceName)){
JOptionPane.showMessageDialog(null,"deviceName 不能为空");
return;
}else if(deviceSecret == null || "".equals(deviceSecret)){
JOptionPane.showMessageDialog(null,"deviceSecret 不能为空");
return;
}
try {
Map<String,String> map = re(productKey,deviceName,deviceSecret);
if(map == null){
JOptionPane.showMessageDialog(null,"生成失败");
return ;
}
textField11.setText(map.get("address"));
textField12.setText(map.get("clientId"));
textField13.setText(map.get("userName"));
textField14.setText(map.get("passwd"));
} catch (Exception ex) {
JOptionPane.showMessageDialog(null,"deviceSecret 不能为空");
return;
}
JOptionPane.showMessageDialog(null,"生成成功");
}
});
}
public Map<String,String> re(String productKey , String deviceName , String deviceSecret) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
// String productKey = "";
// String deviceName = "";
// String deviceSecret = "";
String clientId = UUID.randomUUID().toString().replace("-", "").toUpperCase();
String region = "cn-shanghai";
String data = "clientId" + clientId + "deviceName" + deviceName + "productKey" + productKey;
Map<String,String> map = new HashMap<>();
// System.out.println("address : " + productKey + ".iot-as-mqtt." + region + ".aliyuncs.com");
// System.out.println("clientId : " + clientId + "|securemode=3,signmethod=hmacsha1|");
// System.out.println("userName : " + deviceName + "&" + productKey);
// System.out.println("passwd : " + re(data, deviceSecret).toUpperCase());
map.put("address",productKey + ".iot-as-mqtt." + region + ".aliyuncs.com");
map.put("clientId",clientId + "|securemode=3,signmethod=hmacsha1|");
map.put("userName",deviceName + "&" + productKey);
map.put("passwd",re(data, deviceSecret).toUpperCase());
return map;
}
private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
private static String toHexString(byte[] bytes) {
Formatter formatter = new Formatter();
for (byte b : bytes) {
formatter.format("%02x", b);
}
return formatter.toString();
}
public static String re(String data, String key) throws SignatureException, NoSuchAlgorithmException, InvalidKeyException, InvalidKeyException, NoSuchAlgorithmException {
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);
Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
try {
mac.init(signingKey);
} catch (InvalidKeyException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null,"签名验签失败");
return null;
}
return toHexString(mac.doFinal(data.getBytes()));
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
panel3 = new JPanel();
label1 = new JLabel();
label2 = new JLabel();
label3 = new JLabel();
textField1 = new JTextField();
textField2 = new JTextField();
textField3 = new JTextField();
button1 = new JButton();
panel4 = new JPanel();
label11 = new JLabel();
textField11 = new JTextField();
textField12 = new JTextField();
label12 = new JLabel();
label13 = new JLabel();
textField13 = new JTextField();
textField14 = new JTextField();
label14 = new JLabel();
//======== this ========
Container contentPane = getContentPane();
contentPane.setLayout(null);
//======== panel3 ========
{
panel3.setBorder(new TitledBorder("\u963f\u91cc\u4e91Iot\uff0c\u4e09\u5143\u7ec4\u4fe1\u606f"));
panel3.setLayout(null);
//---- label1 ----
label1.setText("productKey\uff1a");
panel3.add(label1);
label1.setBounds(new Rectangle(new Point(50, 45), label1.getPreferredSize()));
//---- label2 ----
label2.setText("deviceName \uff1a");
panel3.add(label2);
label2.setBounds(new Rectangle(new Point(40, 85), label2.getPreferredSize()));
//---- label3 ----
label3.setText("deviceSecret\uff1a");
panel3.add(label3);
label3.setBounds(45, 120, 95, 20);
panel3.add(textField1);
textField1.setBounds(150, 45, 295, 24);
panel3.add(textField2);
textField2.setBounds(150, 80, 295, 24);
panel3.add(textField3);
textField3.setBounds(150, 115, 295, 24);
{
// compute preferred size
Dimension preferredSize = new Dimension();
for(int i = 0; i < panel3.getComponentCount(); i++) {
Rectangle bounds = panel3.getComponent(i).getBounds();
preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
}
Insets insets = panel3.getInsets();
preferredSize.width += insets.right;
preferredSize.height += insets.bottom;
panel3.setMinimumSize(preferredSize);
panel3.setPreferredSize(preferredSize);
}
}
contentPane.add(panel3);
panel3.setBounds(35, 25, 565, 190);
//---- button1 ----
button1.setText("ok");
contentPane.add(button1);
button1.setBounds(465, 235, 74, 24);
//======== panel4 ========
{
panel4.setBorder(new TitledBorder("\u8fde\u63a5\u5730\u5740"));
panel4.setLayout(null);
//---- label11 ----
label11.setText("address \uff1a");
panel4.add(label11);
label11.setBounds(75, 40, 63, 17);
panel4.add(textField11);
textField11.setBounds(150, 35, 295, 24);
panel4.add(textField12);
textField12.setBounds(150, 70, 295, 24);
//---- label12 ----
label12.setText("clientId \uff1a");
panel4.add(label12);
label12.setBounds(80, 75, 58, 17);
//---- label13 ----
label13.setText("userName\uff1a");
panel4.add(label13);
label13.setBounds(65, 105, 72, 17);
panel4.add(textField13);
textField13.setBounds(150, 105, 295, 24);
panel4.add(textField14);
textField14.setBounds(150, 140, 295, 24);
//---- label14 ----
label14.setText("passwd \uff1a");
panel4.add(label14);
label14.setBounds(75, 140, 60, 17);
{
// compute preferred size
Dimension preferredSize = new Dimension();
for(int i = 0; i < panel4.getComponentCount(); i++) {
Rectangle bounds = panel4.getComponent(i).getBounds();
preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
}
Insets insets = panel4.getInsets();
preferredSize.width += insets.right;
preferredSize.height += insets.bottom;
panel4.setMinimumSize(preferredSize);
panel4.setPreferredSize(preferredSize);
}
}
contentPane.add(panel4);
panel4.setBounds(35, 270, 565, 205);
{
// compute preferred size
Dimension preferredSize = new Dimension();
for(int i = 0; i < contentPane.getComponentCount(); i++) {
Rectangle bounds = contentPane.getComponent(i).getBounds();
preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
}
Insets insets = contentPane.getInsets();
preferredSize.width += insets.right;
preferredSize.height += insets.bottom;
contentPane.setMinimumSize(preferredSize);
contentPane.setPreferredSize(preferredSize);
}
pack();
setLocationRelativeTo(getOwner());
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
private JPanel panel3;
private JLabel label1;
private JLabel label2;
private JLabel label3;
private JTextField textField1;
private JTextField textField2;
private JTextField textField3;
private JButton button1;
private JPanel panel4;
private JLabel label11;
private JTextField textField11;
private JTextField textField12;
private JLabel label12;
private JLabel label13;
private JTextField textField13;
private JTextField textField14;
private JLabel label14;
// JFormDesigner - End of variables declaration //GEN-END:variables
}