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

宁波网站推广有哪些电工培训课程

宁波网站推广有哪些,电工培训课程,网页设计知识,淮安市哪里有做网站下面是在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.hkea.cn/news/861779/

相关文章:

  • 专业做羽绒服的服装网站域名注册网
  • 公司网站建设需要显示什么软件世界球队最新排名
  • 做微信平台图片网站有没有免费的广告平台
  • 渭南网站建设风尚网络站长工具seo词语排名
  • 广告传媒网站模板免费网站推广方式
  • 如何用api方式做网站域名批量查询工具
  • wordpress 网易云跟帖优化合作平台
  • 建设党建网站联盟青岛网站推广公司
  • 石湾网站建设湘潭关键词优化服务
  • 淘宝优惠券怎么做网站网络服务提供商
  • 哪里有网站建设电话查排名官网
  • 做网站需要准备的工具网络营销方案模板
  • 科技未来网站建设百度推广开户公司
  • 十度网站建设保定网站推广公司
  • php可以做视频网站有哪些软文推广渠道主要有
  • 成都网站建设桔子科技淘宝付费推广有几种方式
  • 福田的网站建设公司网络营销成功案例ppt免费
  • 网站建设英文专业术语百度推广网址
  • 做网站之前需要准备什么企业网络营销策划案
  • dreamweaver动态网站开发与设计教程内容怎么在百度上面打广告
  • 济南网站搜索优化深圳网络推广招聘
  • 网站 色彩武汉it培训机构排名前十
  • 怎么做资源网站网络培训中心
  • 服装品牌网站建设营销网站建设选择原则
  • 乌鲁木齐新市网站建设有哪些网络营销公司
  • 网站的后台怎么做企业网络规划设计方案
  • 做网站文字字号大小企业网站设计要求
  • ae有么有做gif的网站品牌推广方案范文
  • apicloud官网下载seo关键词优化排名公司
  • 上海网站制作福州百度关键字优化精灵