html Input password encryption 前端 密码框加密

3 篇文章 0 订阅
2 篇文章 0 订阅

密码框加密

本代码需要Jquery 支持,并需要引入 JavaScript-MD5
支持自定义参数:
elemPassword:需要绑定加密的input 文本框(随便你写)
viewText:显示的字符(随便你写)
encryAttr:输入的字符串加密后绑定的属性(随便你写)
encryptionType:加密类型(需要可以自己扩展)
ignoreKey:忽略参数(前三位必须是 空格、Backspace、Delete)

一般默认调用即可,ignoreKey 参数最好不要动,必须保持数组前三位是 空格、Backspace、Delete,这三个涉及到事件的监听

 inputPasswordEncryption: function (options) {
                var option = $.extend(
                    {
                        elemPassword: '#password',
                        viewText: '●',
                        encryAttr: 'encry',
                        encryptionType: 'md5',
                        ignoreKey: [" ", "Backspace", "Delete", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "Escape", "Tab", "CapsLock", "Shift", "Control", "Alt", "Meta", "ContextMenu", "PrintScreen", "ScrollLock", "Pause", "Insert", "Home", "End", "PageUp", "PageDown", "NumLock", "Enter", "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight", "Process"]
                    }, options);

                var input = $(option.elemPassword),
                    encValue = "",
                    sourceValue = "",
                    cursortPosition = 0,
                    lastInputValue = "",
                    select = {
                        start: 0,
                        end: 0
                    },
                    keydownSelect = false,

                    initData = function () {
                        select.start = 0;
                        select.end = 0;
                        cursortPosition = 0;
                        lastInputValue = "";
                    },
                    getData = function (_this) {
                        select.start = _this.selectionStart;
                        select.end = _this.selectionEnd;
                        cursortPosition = getCursortPosition.call(_this, _this) - 1;
                        lastInputValue = input.val().substr(cursortPosition, 1);
                    },
                    getCursortPosition = function (ctrl) {
                        var CaretPos = 0;
                        if (document.selection) {
                            ctrl.focus();
                            var Sel = document.selection.createRange();
                            Sel.moveStart('character', -ctrl.value.length);
                            CaretPos = Sel.text.length;
                        }
                        else if (ctrl.selectionStart || ctrl.selectionStart == '0')
                            CaretPos = ctrl.selectionStart;
                        return (CaretPos);
                    },
                    updateSourceValue = function (_this, type) {
                        // 处理程序
                        var handleFunction = {
                            // 空格处理 (在索引前添加空格或者替换选中的字符)
                            Space: function () {
                                if (keydownSelect) {
                                    // 选中的有字符串,进行替换
                                    RemoveSourceValueGivenPositionStr();
                                }
                            },
                            Delete: function () {
                                if (keydownSelect) {
                                    // 选中的有字符串,进行替换
                                    RemoveSourceValueGivenPositionStr();
                                } else {
                                    var arr = sourceValue.split('');
                                    if (cursortPosition != -1) {
                                        if (arr.length - 1 != cursortPosition) {
                                            arr.splice(cursortPosition + 1, 1)
                                            sourceValue = arr.join('');
                                        }
                                    } else if (cursortPosition == -1 && arr.length > 0) {
                                        arr.splice(0, 1)
                                        sourceValue = arr.join('');
                                    }

                                }
                            },
                            Backspace: function () {
                                if (keydownSelect) {
                                    // 选中的有字符串,进行替换
                                    RemoveSourceValueGivenPositionStr();
                                } else {
                                    var arr = sourceValue.split('');
                                    if (cursortPosition != -1) {
                                        arr.splice(cursortPosition, 1)
                                        sourceValue = arr.join('');
                                    }
                                }
                            }
                        },
                            RemoveSourceValueGivenPositionStr = function () {
                                // 选中的有字符串,进行替换
                                var forNumber = select.end - select.start;
                                var arr = sourceValue.split('');
                                arr.splice(select.start, forNumber);
                                sourceValue = arr.join('');
                            }

                        switch (type) {
                            case option.ignoreKey[0]:
                                handleFunction.Space();
                                break;
                            case option.ignoreKey[1]:
                                handleFunction.Backspace();
                                break;
                            case option.ignoreKey[2]:
                                handleFunction.Delete();
                                break;
                            default:
                        }
                    };

                input.keydown(function (e) {
                    initData();
                    inputValue = input.val();
                    // 在keyDown时是否选中文本
                    keydownSelect = !(this.selectionStart == this.selectionEnd);
                    if (keydownSelect) {
                        getData(this);
                    }
                    if (option.ignoreKey.indexOf(e.key) > -1) {
                        cursortPosition = getCursortPosition.call(this, this) - 1;
                    }

                });
                input.keyup(function (e) {
                    if (option.ignoreKey.indexOf(e.key) > -1) {
                        updateSourceValue(this, e.key)
                    } else { // 不包含

                        // 是否选中
                        if (keydownSelect) {
                            var forNumber = select.end - select.start;
                            var arr = sourceValue.split('');
                            if (lastInputValue == "" || lastInputValue.length == 0) {
                                arr.splice(select.start, forNumber);
                            } else {
                                arr.splice(select.start, forNumber, e.key);
                            }
                            sourceValue = arr.join('');
                        } else {
                            getData(this);
                            // 没有选中
                            var arr = sourceValue.split('');
                            arr.splice(cursortPosition, 0, e.key)
                            sourceValue = arr.join('');
                        }
                    }
                    var result = "";
                    sourceValue.split('').forEach(function (value, index, array) {
                        result += option.viewText;
                    });
                    input.val(result);

                    if (sourceValue != "") {
                        switch (option.encryptionType.toLowerCase()) {
                            case "md5":
                                encValue = md5(sourceValue);
                                break;
                        }
                    } else {
                        encValue = "";
                    }
                    input.removeAttr(option.encryAttr).attr(option.encryAttr, encValue);
                });
            }
调用

inputPasswordEncryption();

使用Jasypt对MySQL密码进行加密,可以将其加密后的值存储在配置文件中,以确保密码的安全性。下面是一个示例: 1. 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency> ``` 2. 在application.properties文件中添加以下属性: ``` # MySQL数据库连接信息 spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root # 加密后的密码,前缀为ENC spring.datasource.password=ENC(encryptPassword) # Jasypt配置 jasypt.encryptor.password=jasyptPassword ``` 3. 创建一个加密工具类,用于执行加密操作: ``` import org.jasypt.encryption.StringEncryptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class JasyptEncryptor { private final StringEncryptor stringEncryptor; @Autowired public JasyptEncryptor(StringEncryptor stringEncryptor) { this.stringEncryptor = stringEncryptor; } public String encrypt(String input) { return stringEncryptor.encrypt(input); } public String decrypt(String encryptedMessage) { return stringEncryptor.decrypt(encryptedMessage); } } ``` 4. 在应用程序中使用加密工具类来加密密码: ``` import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @Component public class MyApp implements CommandLineRunner { private final JasyptEncryptor jasyptEncryptor; @Autowired public MyApp(JasyptEncryptor jasyptEncryptor) { this.jasyptEncryptor = jasyptEncryptor; } @Override public void run(String... args) { String password = "myPassword"; String encryptedPassword = jasyptEncryptor.encrypt(password); System.out.println("Encrypted Password: " + encryptedPassword); } } ``` 在此示例中,我们使用JasyptEncryptor类对密码进行加密,并将加密后的值存储在application.properties文件中。在应用程序中,我们可以使用JasyptEncryptor类来加密和解密密码,并将其用于数据库连接。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值