前后端敏感数据加密方案及实现_03

文章目录

            • 1. vue 部分
            • 2. 前端工具 部分
            • 3. 后端控制层
            • 4. 后端逻辑层

1. vue 部分
<template><div class="login"><el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form"><h3 class="title">若依后台管理系统</h3><el-form-item prop="username"><el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号"><svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" /></el-input></el-form-item><el-form-item prop="password"><el-inputv-model="loginForm.password"type="password"auto-complete="off"placeholder="密码"@keyup.enter.native="handleLogin"><svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" /></el-input></el-form-item><el-form-item prop="code"><el-inputv-model="loginForm.code"auto-complete="off"placeholder="验证码"style="width: 63%"@keyup.enter.native="handleLogin"><svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" /></el-input><div class="login-code"><img :src="codeUrl" @click="getCode" class="login-code-img"/></div></el-form-item><el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox><el-form-item style="width:100%;"><el-button:loading="loading"size="medium"type="primary"style="width:100%;"@click.native.prevent="handleLogin"><span v-if="!loading">登 录</span><span v-else>登 录 中...</span></el-button></el-form-item></el-form><!--  底部  --><div class="el-login-footer"><span>Copyright © 2018-2021 ruoyi.vip All Rights Reserved.</span></div></div>
</template><script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'export default {name: "Login",data() {return {codeUrl: "",cookiePassword: "",loginForm: {username: "admin",password: "admin123",rememberMe: false,code: "",uuid: ""},loginRules: {username: [{ required: true, trigger: "blur", message: "用户名不能为空" }],password: [{ required: true, trigger: "blur", message: "密码不能为空" }],code: [{ required: true, trigger: "change", message: "验证码不能为空" }]},loading: false,redirect: undefined};},watch: {$route: {handler: function(route) {this.redirect = route.query && route.query.redirect;},immediate: true}},created() {this.getCode();this.getCookie();},methods: {getCode() {getCodeImg().then(res => {this.codeUrl = "data:image/gif;base64," + res.img;this.loginForm.uuid = res.uuid;});},getCookie() {const username = Cookies.get("username");const password = Cookies.get("password");const rememberMe = Cookies.get('rememberMe')this.loginForm = {username: username === undefined ? this.loginForm.username : username,password: password === undefined ? this.loginForm.password : decrypt(password),rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)};},handleLogin() {this.$refs.loginForm.validate(valid => {if (valid) {this.loading = true;if (this.loginForm.rememberMe) {Cookies.set("username", this.loginForm.username, { expires: 30 });Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });} else {Cookies.remove("username");Cookies.remove("password");Cookies.remove('rememberMe');}this.$store.dispatch("Login", this.loginForm).then(() => {this.$router.push({ path: this.redirect || "/" }).catch(()=>{});}).catch(() => {this.loading = false;this.getCode();});}});}}
};
</script><style rel="stylesheet/scss" lang="scss">
.login {display: flex;justify-content: center;align-items: center;height: 100%;background-image: url("../assets/images/login-background.jpg");background-size: cover;
}
.title {margin: 0px auto 30px auto;text-align: center;color: #707070;
}.login-form {border-radius: 6px;background: #ffffff;width: 400px;padding: 25px 25px 5px 25px;.el-input {height: 38px;input {height: 38px;}}.input-icon {height: 39px;width: 14px;margin-left: 2px;}
}
.login-tip {font-size: 13px;text-align: center;color: #bfbfbf;
}
.login-code {width: 33%;height: 38px;float: right;img {cursor: pointer;vertical-align: middle;}
}
.el-login-footer {height: 40px;line-height: 40px;position: fixed;bottom: 0;width: 100%;text-align: center;color: #fff;font-family: Arial;font-size: 12px;letter-spacing: 1px;
}
.login-code-img {height: 38px;
}
</style>
2. 前端工具 部分

jsencrypt.js

import JSEncrypt from 'jsencrypt/bin/jsencrypt.min'// 密钥对生成 http://web.chacuo.net/netrsakeypairconst publicKey = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdH\n' +'nzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ=='const privateKey = 'MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqhHyZfSsYourNxaY\n' +'7Nt+PrgrxkiA50efORdI5U5lsW79MmFnusUA355oaSXcLhu5xxB38SMSyP2KvuKN\n' +'PuH3owIDAQABAkAfoiLyL+Z4lf4Myxk6xUDgLaWGximj20CUf+5BKKnlrK+Ed8gA\n' +'kM0HqoTt2UZwA5E2MzS4EI2gjfQhz5X28uqxAiEA3wNFxfrCZlSZHb0gn2zDpWow\n' +'cSxQAgiCstxGUoOqlW8CIQDDOerGKH5OmCJ4Z21v+F25WaHYPxCFMvwxpcw99Ecv\n' +'DQIgIdhDTIqD2jfYjPTY8Jj3EDGPbH2HHuffvflECt3Ek60CIQCFRlCkHpi7hthh\n' +'YhovyloRYsM+IS9h/0BzlEAuO0ktMQIgSPT3aFAgJYwKpqRYKlLDVcflZFCKY7u3\n' +'UP8iWi1Qw0Y='// 加密
export function encrypt(txt) {const encryptor = new JSEncrypt()encryptor.setPublicKey(publicKey) // 设置公钥return encryptor.encrypt(txt) // 对数据进行加密
}// 解密
export function decrypt(txt) {const encryptor = new JSEncrypt()encryptor.setPrivateKey(privateKey) // 设置私钥return encryptor.decrypt(txt) // 对数据进行解密
}
3. 后端控制层
package com.ruoyi.web.controller.system;import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ISysMenuService;/*** 登录验证* * @author ruoyi*/
@RestController
public class SysLoginController
{@Autowiredprivate SysLoginService loginService;@Autowiredprivate ISysMenuService menuService;@Autowiredprivate SysPermissionService permissionService;@Autowiredprivate TokenService tokenService;/*** 登录方法* * @param loginBody 登录信息* @return 结果*/@PostMapping("/login")public AjaxResult login(@RequestBody LoginBody loginBody){AjaxResult ajax = AjaxResult.success();// 生成令牌String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),loginBody.getUuid());ajax.put(Constants.TOKEN, token);return ajax;}/*** 获取用户信息* * @return 用户信息*/@GetMapping("getInfo")public AjaxResult getInfo(){LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());SysUser user = loginUser.getUser();// 角色集合Set<String> roles = permissionService.getRolePermission(user);// 权限集合Set<String> permissions = permissionService.getMenuPermission(user);AjaxResult ajax = AjaxResult.success();ajax.put("user", user);ajax.put("roles", roles);ajax.put("permissions", permissions);return ajax;}/*** 获取路由信息* * @return 路由信息*/@GetMapping("getRouters")public AjaxResult getRouters(){LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());// 用户信息SysUser user = loginUser.getUser();List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getUserId());return AjaxResult.success(menuService.buildMenus(menus));}
}
4. 后端逻辑层
package com.ruoyi.framework.web.service;import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.exception.user.CaptchaException;
import com.ruoyi.common.exception.user.CaptchaExpireException;
import com.ruoyi.common.exception.user.UserPasswordNotMatchException;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory;/*** 登录校验方法* * @author ruoyi*/
@Component
public class SysLoginService
{@Autowiredprivate TokenService tokenService;@Resourceprivate AuthenticationManager authenticationManager;@Autowiredprivate RedisCache redisCache;/*** 登录验证* * @param username 用户名* @param password 密码* @param code 验证码* @param uuid 唯一标识* @return 结果*/public String login(String username, String password, String code, String uuid){String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;String captcha = redisCache.getCacheObject(verifyKey);redisCache.deleteObject(verifyKey);if (captcha == null){AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));throw new CaptchaExpireException();}if (!code.equalsIgnoreCase(captcha)){AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));throw new CaptchaException();}// 用户验证Authentication authentication = null;try{// 该方法会去调用UserDetailsServiceImpl.loadUserByUsernameauthentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));}catch (Exception e){if (e instanceof BadCredentialsException){AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));throw new UserPasswordNotMatchException();}else{AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));throw new CustomException(e.getMessage());}}AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));LoginUser loginUser = (LoginUser) authentication.getPrincipal();// 生成tokenreturn tokenService.createToken(loginUser);}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/518199.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

关于 Docker ,你必须了解的核心都在这里了!

来源 | fysuccess来源 | CSDN博客&#xff0c;责编 | Carol头图 | CSDN 下载自视觉中国Docker引擎Docker Engine是具有以下主要组件的客户端-服务器应用程序&#xff1a;服务器是一种长期运行的程序&#xff0c;称为守护程序进程&#xff08; dockerd命令&#xff09;。REST AP…

如何在视频里任意抠图?阿里工程师做到了!

阿里妹导读&#xff1a;现在的我们在手机上花费了越来越多的时间&#xff0c;其中&#xff0c;视频又格外地吸引我们的注意力。有很多好玩的视频&#xff0c;需要把前景物体从视频中分割出来&#xff0c;这需要花费创作者99%以上的时间。今天&#xff0c;阿里资深算法专家任海兵…

python-pyinstaller打包程序为exe

1.安装pyinstaller pip3/2 install pyinstaller2.打开代码路径的DOS窗口 3. 生成exe pyinstaller -Fw .\json_to_csv.py #不带黑框&#xff08;命令提示符&#xff09; pyinstaller -F .\json_to_csv.py #带黑框&#xff08;命令提示符&#xff09; -F 表示生成单个可执行…

启动vue项目失败,报错Failed at the node-sass@4.14.1 postinstall script.

在启动vue项目的时候报错 报错信息如下&#xff1a;npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! node-sass4.14.1 postinstall: node scripts/build.js npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the node-sass4.14.1 postinstall script. npm ERR! This i…

DLedger —基于 raft 协议的 commitlog 存储库

尊敬的阿里云用户&#xff1a; 您好&#xff01;为方便您试用开源 RocketMQ 客户端访问阿里云MQ&#xff0c;我们申请了专门的优惠券&#xff0c;优惠券可以直接抵扣金额。请填写下您公司账号信息&#xff0c;点击上图&#xff0c;了解更多哦。 一、DLedger引入目的 在 Rocke…

不服来战!青藤发起“雷火引擎”公测赛 百万赏金寻顶尖白帽

2020年春天&#xff0c;以5G、人工智能、云计算为代表的“新基建”蔚然成风&#xff0c;着眼国家数字经济体系建设&#xff0c;打造数字经济体系底座的“新基建”&#xff0c;无疑成为中国经济整体应对未来发展的核心方案。可以说&#xff0c;没有任何一个时期比现在更能够彰显…

UI2CODE系列文章|如何批量制造高质量样本

在 UI2CODE 项目中&#xff0c;我们大量使用了深度学习方法来做一些物体检测。而深度学习模型的训练&#xff0c;避免不了需要大量的样本&#xff0c;因此如何制造大量样本&#xff0c;来满足模型训练需要是我们必须要解决的一个问题。在这篇文章中&#xff0c;我们将介绍我们如…

python-list列表排序问题 sort 与 sorted

sort 与 sorted 区别&#xff1a; sort 是应用在 list 上的方法&#xff0c;sorted 可以对所有可迭代的对象进行排序操作。 list 的 sort 方法返回的是对已经存在的列表进行操作&#xff0c;而内建函数 sorted 方法返回的是一个新的 list&#xff0c;而不是在原来的基础上进行的…

javax.management.InstanceNotFoundException: org.springframework.boot:type=Admin,name=SpringApplicati

控制台service端打印javax.management.InstanceNotFoundException: org.springframework.boot:typeAdmin,nameSpringApplication 那个报的就点哪个,将红圈里的对勾去掉 就ok了&#xff01;

极测未来|淘宝千人千面内容下的智能评测技术与实践

背景挑战 全面个性化、内容化的淘宝&#xff0c;构造了基于内容的丰富的导购场景&#xff0c;包括猜你喜欢、有好货、每日好店、必买清单、哇哦视频、微淘、买家秀、头条、洋葱盒子….。个性化&#xff0c;给消费者带来更精准的货品分发。内容化为消费者带来更多惊喜和好的体验…

新时代 新营销 新增长, 纷享销客重磅发布CRM7.0产品,持续赋能企业数字化未来

2020年4月26日&#xff0c;主题为“新时代 新营销 新增长”的纷享销客2020销售增长大会暨春季战略与新品发布会&#xff0c;在GMIC大会期间成功举办。纷享销客创始人&CEO罗旭发表“新时代 新营销 新增长”的主题演讲&#xff0c;同时重磅发布纷享销客连接型CRM7.0新品。 这…

阿里云DLA新功能来袭 查询MongoDB嵌套字段更方便

背景 DLA之前对于MongoDB只支持简单类型的映射查询&#xff0c;对于嵌套字段无法直接查询&#xff0c;最近我们加入了对嵌套字段直接查询的支持&#xff0c;这篇文章给大家介绍一下。 MongoDB嵌套字段查询详解 首先我们在MongoDB里面准备一个带有嵌套数据结构的collection: …

python3-matplotlib基本使用(以折线图为例)

1、什么是matplotlib Matplotlib 是 Python 中最受欢迎的数据可视化软件包之一&#xff0c;支持跨平台运行&#xff0c;它是 Python 常用的 2D 绘图库&#xff0c;同时它也提供了一部分 3D 绘图接口。Matplotlib 通常与 NumPy、Pandas 一起使用&#xff0c;是数据分析中不可或…

flowable 开源项目

企业级工作流引擎开源项目 文章目录一、工作流引擎flowable1. flowable2. cims3. RuoYi-flowable4. springboot-flowable-modeler5. flowable-diagram二、工作流引擎activiti2.1. RuoYi-Vue-Process2.2. RuoYi-Process2.3. ruoyi-vue-activiti2.4. activiti7-workflow2.5. JeeS…

看!闲鱼在ServiceMesh的探索和实践

背景&#xff1a; 在阿里服务端开发以Java为主的大背景下&#xff0c;其他异构语言业务如何调用现有Java服务&#xff0c;如何与集团中间件打通&#xff0c;就成为使用非Java语言团队必须要解决的首要问题。 已有方案问题&#xff1a; 在ServiceMesh方案成熟之前&#xff0c…

编译错误 错误:PL/SQL: ORA-00932: 数据类型不一致: 应为 DATE, 但却获得 NUMBER 行

文章目录1. 现象2. 分析3. 解决方案通过存储过程将临时B表中的数据同步到轨迹表中 1. 现象 PROCEDURE LABS.ASSET_LOANP 编译错误错误&#xff1a;PL/SQL: ORA-00932: 数据类型不一致: 应为 DATE, 但却获得 NUMBER 行&#xff1a;17 文本&#xff1a;(select n.*, trunc(DBMS_R…

如何保证 HBase 服务的高可用?看看这份 HBase 可用性分析与高可用实践吧!

来源 | 阿丸笔记责编 | Carol头图 | CSDN 下载自视觉中国HBase作为一个分布式存储的数据库&#xff0c;它是如何保证可用性的呢&#xff1f;对于分布式系统的CAP问题&#xff0c;它是如何权衡的呢&#xff1f;最重要的是&#xff0c;我们在生产实践中&#xff0c;又应该如何保证…

python3-matplotlib绘制散点图、绘制条形图

matplotlib 支持的图形 https://matplotlib.org/stable/gallery/index.html 1、绘制散点图 from matplotlib import pyplot as plt from matplotlib import font_manager# y_3是三月每天的最高温度 y_10 是十月每天的最高温度 y_3 [11,17,16,11,12,11,12,6,6,7,8,9,12,…

编码规范 | Java函数优雅之道(上)

导读 随着软件项目代码的日积月累&#xff0c;系统维护成本变得越来越高&#xff0c;是所有软件团队面临的共同问题。持续地优化代码&#xff0c;提高代码的质量&#xff0c;是提升系统生命力的有效手段之一。软件系统思维有句话“Less coding, more thinking&#xff08;少编…

ORA-01858: 在要求输入数字处找到非数字字符 13行

文章目录1. 现象2. 分析3. 解决方案ORA-01858: 在要求输入数字处找到非数字字符13行 1. 现象 insert /*append*/ into ASSET_LOAN(select * from ASSET_LOANB );commit;2. 分析 由于ASSET_LOANB表和ASSET_LOAN表字段顺序不一致导致的 具体分析&#xff1a;由于ASSET_LOANB表倒数…