当前位置: 首页 > news >正文

北京网站制作到诺然如何制作简易个人网站

北京网站制作到诺然,如何制作简易个人网站,wordpress蒸汽波主题,wordpress模板专题页下面是在Spring Boot项目中对属性文件中的账号密码进行加密的完整步骤&#xff0c;以MySQL的用户名为root&#xff0c;密码为123321为例&#xff1a; 步骤1&#xff1a;引入Jasypt依赖 在项目的pom.xml文件中&#xff0c;添加Jasypt依赖&#xff1a; <dependency><…

下面是在Spring Boot项目中对属性文件中的账号密码进行加密的完整步骤,以MySQL的用户名为root,密码为123321为例:

步骤1:引入Jasypt依赖

在项目的pom.xml文件中,添加Jasypt依赖:

<dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId><version>3.0.3</version>
</dependency>

步骤2:配置加密密码

src/main/resources/application.ymlapplication.properties中,配置Jasypt的加密密码。将其替换为自己的密匙,例如:

jasypt:encryptor:password: lfsun666

确保将lfsun666替换为自己的密匙。

步骤3:加密属性值

application.ymlapplication.properties

spring:datasource:username: rootpassword: 123321

步骤4:创建MyJasyptUtil类

创建一个用于加密和解密属性值的实用类,例如MyJasyptUtil。这个类将使用Jasypt的BasicTextEncryptor进行属性的加密和解密。以下是一个示例:

import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;/*** 用于加密和解密属性值的实用类,这个类将使用Jasypt的BasicTextEncryptor进行属性的加密和解密。*/
@Component
public class MyJasyptUtil {private final StringEncryptor stringEncryptor;@Autowiredpublic MyJasyptUtil(StringEncryptor stringEncryptor) {this.stringEncryptor = stringEncryptor;}public String encrypt(String input) {return stringEncryptor.encrypt(input);}public String decrypt(String input) {return stringEncryptor.decrypt(input);}
}

步骤5:在应用中使用MyJasyptUtil

使用MyJasyptUtil类来加密和解密属性值。示例:

import com.lfsun.demolfsunstudyjasypt.util.MyJasyptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;@Service
public class MyJasyptService {@Value("${spring.datasource.username}")private String username;@Value("${spring.datasource.password}")private String password;@Autowiredprivate MyJasyptUtil myJasyptUtil;/*** 保存加密后的凭据。*/public void saveCredentials() {String encryptedUsername = myJasyptUtil.encrypt(username);String encryptedPassword = myJasyptUtil.encrypt(password);System.out.println("加密后的用户名和密码: " + encryptedUsername + ":" + encryptedPassword);}/*** 检索解密后的凭据。*/public void retrieveCredentials() {String decryptedUsername = myJasyptUtil.decrypt(username);String decryptedPassword = myJasyptUtil.decrypt(password);System.out.println("解密后的用户名和密码: " + decryptedUsername + ":" + decryptedPassword);}
}

步骤6:测试
拿到加密后的用户名和密码然后替换到application.yml:

import com.lfsun.demolfsunstudyjasypt.service.MyJasyptService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;@SpringBootApplication
public class DemoLfsunStudyJasyptApplication {public static void main(String[] args) {/*** 在应用启动后立即创建了一个 MyJasyptService 实例并调用了 saveCredentials 方法。* 这并不是正确的方式来获取 Spring 管理的 Bean。** Spring Boot 应用的 Bean 生命周期和管理是由 Spring 容器控制的,不应该手动创建 Bean 实例。** 应该在 Spring 容器初始化后,使用 Spring 框架来获取 MyJasyptService Bean 并调用相应的方法。*//*SpringApplication.run(DemoLfsunStudyJasyptApplication.class, args);MyJasyptService myJasyptService = new MyJasyptService();myJasyptService.saveCredentials();*/// 正确的方式ConfigurableApplicationContext context = SpringApplication.run(DemoLfsunStudyJasyptApplication.class, args);MyJasyptService myJasyptService = context.getBean(MyJasyptService.class);// 拿到加密后的用户名和密码myJasyptService.saveCredentials();// 用加密后的用户名和密码去覆盖真实的密码// myJasyptService.retrieveCredentials();}}

取出密码:

        // 拿到加密后的用户名和密码// myJasyptService.saveCredentials();// 用加密后的用户名和密码去覆盖真实的密码myJasyptService.retrieveCredentials();

即可;

在这里插入图片描述

通过这些步骤,可以在Spring Boot项目中对属性文件中的账号密码进行加密,提高安全性。确保保护好加密密码(jasypt.encryptor.password)以防止数据泄漏。

http://www.yayakq.cn/news/961922/

相关文章:

  • vs做网站怎样添加图片西安做网站服务
  • 龙华网站 建设龙华信科中山网站建设乐云seo模板中心
  • 美工常用找素材网站深圳龙华建设公司
  • 网站制作排名广告设计模板素材图片
  • 网站制作公司徐州国外cdn
  • 微信扫码即可打开的网站如何做南京做网站的公司排名
  • 上海网站制作是什么两学一做网站按钮图片
  • 做vi设计的网站fotor懒设计在线设计
  • 什么网站可以找人做系统全国企业信息管理系统
  • 什么叫域名访问网站中铁建设集团有限公司华南分公司
  • 中国建设教育协会的网站全国信用企业公示系统
  • 我网站正在建设中做网站是不是太麻烦了
  • 怎么设置自己做的网站吗门户网站建站
  • 网站建设 推广人员html代码大全在线版
  • 精仿源码社区网站源码网业端云服务
  • 用腾讯云做淘宝客网站视频流程广告投放行业
  • 泰安网站的建设北京工程建设
  • 如何用word做简单的网站wordpress模板影视
  • 中国建设培训网站查询系统爱常德网
  • 营销网站的功能广告店
  • 沈阳网站关键词优化公司建设网站需要展示什么
  • 公司建网站多内容型网站
  • html框架做网站网站建设毕业设计摘要
  • 英国做deal的网站网站logo素材
  • 做介绍自己的短视频网站古董手表网站
  • 网站备案域名备案做门的网站
  • 关于大棚建设的网站小公司网络组建
  • 网站建设的预算营销软文范例大全100字
  • 河南网站备案中心河北建设信息网官网
  • 中国建设银行北京招聘信息网站网站建设教程 零基础