diff --git a/designer-base/src/main/java/com/fr/design/data/datapane/connect/JDBCDefPane.java b/designer-base/src/main/java/com/fr/design/data/datapane/connect/JDBCDefPane.java index 5e8199d953..683b38e016 100644 --- a/designer-base/src/main/java/com/fr/design/data/datapane/connect/JDBCDefPane.java +++ b/designer-base/src/main/java/com/fr/design/data/datapane/connect/JDBCDefPane.java @@ -8,7 +8,7 @@ import com.fr.design.border.UITitledBorder; import com.fr.design.gui.ibutton.UIButton; import com.fr.design.gui.icombobox.UIComboBox; import com.fr.design.gui.ilable.UILabel; -import com.fr.design.gui.ipasswordfield.UIPassWordField; +import com.fr.design.gui.ipasswordfield.UIPasswordFieldWithFixedLength; import com.fr.design.gui.itextfield.UITextField; import com.fr.design.gui.ilable.ActionLabel; import com.fr.design.layout.FRGUIPaneFactory; @@ -24,8 +24,18 @@ import com.fr.general.ComparatorUtils; import com.fr.stable.StringUtils; -import javax.swing.*; -import java.awt.*; +import javax.swing.BorderFactory; +import javax.swing.JDialog; +import javax.swing.JFileChooser; +import javax.swing.JPanel; +import javax.swing.JPasswordField; +import javax.swing.SwingConstants; +import javax.swing.SwingUtilities; +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Component; +import java.awt.Dimension; +import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; @@ -108,7 +118,7 @@ public class JDBCDefPane extends JPanel { urlTextField = new UITextField(15); userNameTextField = new UITextField(15); userNameTextField.setName(USER_NAME); - passwordTextField = new UIPassWordField(15); + passwordTextField = new UIPasswordFieldWithFixedLength(15); dbtypeButton = new UIButton("."); dbtypeButton.setToolTipText(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Basic_Click_Get_Default_URL")); dbtypeButton.addActionListener(dbtypeButtonActionListener); diff --git a/designer-base/src/main/java/com/fr/design/gui/ipasswordfield/UIPasswordFieldWithFixedLength.java b/designer-base/src/main/java/com/fr/design/gui/ipasswordfield/UIPasswordFieldWithFixedLength.java new file mode 100644 index 0000000000..c1cb98ca3d --- /dev/null +++ b/designer-base/src/main/java/com/fr/design/gui/ipasswordfield/UIPasswordFieldWithFixedLength.java @@ -0,0 +1,106 @@ +package com.fr.design.gui.ipasswordfield; + +import com.fr.stable.StringUtils; +import org.jetbrains.annotations.NotNull; + +import javax.swing.text.Document; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; + +/** + * @author Yvan + * @version 10.0 + * Created by Yvan on 2020-08-11 + * 有固定长度的"*"回显的密码框,避免泄露密码长度 + */ +public class UIPasswordFieldWithFixedLength extends UIPassWordField { + /** + * 展示密码,为固定8位长度的特殊字符"*"组成 + */ + private static final String DISPLAY_PASSWORD = "********"; + + /** + * 实际密码 + */ + private String realPassword; + + /** + * 用于判断是否清空密码 + */ + private boolean clearPassword; + + public UIPasswordFieldWithFixedLength() { + this(null, null, 0); + } + + public UIPasswordFieldWithFixedLength(String text) { + this(null, text, 0); + } + + public UIPasswordFieldWithFixedLength(int columns) { + this(null, null, columns); + } + + public UIPasswordFieldWithFixedLength(String text, int columns) { + this(null, text, columns); + } + + public UIPasswordFieldWithFixedLength(Document doc, String txt, int columns) { + super(doc, txt, columns); + initRealPassword(txt); + } + + /** + * 为realPassword赋初值并添加一个鼠标单击事件 + */ + public void initRealPassword(String text) { + this.realPassword = text == null ? StringUtils.EMPTY : text; + this.clearPassword = true; + addShowFixedLengthPasswordListener(); + } + + /** + * 当鼠标点击密码框,第一次做出键入动作时,清空显示密码与实际密码,用户需要重新输入密码 + */ + private void addShowFixedLengthPasswordListener() { + this.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + UIPasswordFieldWithFixedLength.this.clearPassword = true; + } + }); + this.addKeyListener(new KeyAdapter() { + @Override + public void keyPressed(KeyEvent e) { + if (clearPassword) { + UIPasswordFieldWithFixedLength.this.setText(StringUtils.EMPTY); + UIPasswordFieldWithFixedLength.this.clearPassword = false; + UIPasswordFieldWithFixedLength.this.updateUI(); + } + } + }); + } + + @Override + public void setText(@NotNull String t) { + this.realPassword = t; + // 看到代码中有些场景是将密码置为空字符串的,所以在这里加个判断 + if (StringUtils.isEmpty(t)) { + super.setText(t); + } else { + super.setText(DISPLAY_PASSWORD); + } + } + + @Override + public char[] getPassword() { + //如果用户刚清空密码框,并输入了新密码,则返回输入内容,否则返回realPassword + String text = new String(super.getPassword()); + if (!StringUtils.isEmpty(text) && StringUtils.isEmpty(realPassword)) { + return text.toCharArray(); + } + return realPassword.toCharArray(); + } +} diff --git a/designer-base/src/main/java/com/fr/env/RemoteEnvPane.java b/designer-base/src/main/java/com/fr/env/RemoteEnvPane.java index bc32385b7c..54cfddc8d3 100644 --- a/designer-base/src/main/java/com/fr/env/RemoteEnvPane.java +++ b/designer-base/src/main/java/com/fr/env/RemoteEnvPane.java @@ -13,6 +13,7 @@ import com.fr.design.gui.icheckbox.UICheckBox; import com.fr.design.gui.icontainer.UIScrollPane; import com.fr.design.gui.ilable.UILabel; import com.fr.design.gui.ipasswordfield.UIPassWordField; +import com.fr.design.gui.ipasswordfield.UIPasswordFieldWithFixedLength; import com.fr.design.gui.itextfield.UITextField; import com.fr.design.i18n.Toolkit; import com.fr.design.layout.FRGUIPaneFactory; @@ -26,7 +27,17 @@ import com.fr.workspace.WorkContext; import com.fr.workspace.connect.WorkspaceConnectionInfo; import com.fr.workspace.engine.exception.WorkspaceAuthException; -import javax.swing.*; +import javax.swing.BorderFactory; +import javax.swing.BoxLayout; +import javax.swing.JDialog; +import javax.swing.JFileChooser; +import javax.swing.JPanel; +import javax.swing.JTextPane; +import javax.swing.SwingConstants; +import javax.swing.SwingUtilities; +import javax.swing.SwingWorker; +import javax.swing.ToolTipManager; +import javax.swing.UIManager; import javax.swing.border.EmptyBorder; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; @@ -96,7 +107,7 @@ public class RemoteEnvPane extends BasicBeanPane { /** * 密码 */ - private UIPassWordField passwordInput = new UIPassWordField(); + private UIPassWordField passwordInput = new UIPasswordFieldWithFixedLength(); /** * 是否记住密码 */ @@ -108,7 +119,7 @@ public class RemoteEnvPane extends BasicBeanPane { /** * https密钥 */ - private UIPassWordField certSecretKeyInput = new UIPassWordField(); + private UIPassWordField certSecretKeyInput = new UIPasswordFieldWithFixedLength(); /** * 选择证书文件按钮 */