最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring數(shù)據(jù)源及配置文件數(shù)據(jù)加密實(shí)現(xiàn)過程詳解

 更新時(shí)間:2020年05月11日 10:37:08   作者:曾經(jīng)那個(gè)少年  
這篇文章主要介紹了Spring數(shù)據(jù)源及配置文件數(shù)據(jù)加密實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

The following example shows the corresponding XML configuration:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="${jdbc.driverClassName}"/>
  <property name="url" value="${jdbc.url}"/>
  <property name="username" value="${jdbc.username}"/>
  <property name="password" value="${jdbc.password}"/>
</bean>

<context:property-placeholder location="jdbc.properties"/>

Spring在第三方依賴包中包含了兩個(gè)數(shù)據(jù)源的實(shí)現(xiàn)類包,其一是:Apache的DBCP;其二是C3P0,可以在Spring配置文件中利用二者的任何一個(gè)配置數(shù)據(jù)源.

The next two examples show the basic connectivity and configuration for DBCP and C3P0. To learn about more options that help control the pooling features, see the product documentation for the respective connection pooling implementations.

The following example shows DBCP configuration:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="${jdbc.driverClassName}"/>
  <property name="url" value="${jdbc.url}"/>
  <property name="username" value="${jdbc.username}"/>
  <property name="password" value="${jdbc.password}"/>
</bean>

<context:property-placeholder location="jdbc.properties"/>

The following example shows C3P0 configuration:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
  <property name="driverClass" value="${jdbc.driverClassName}"/>
  <property name="jdbcUrl" value="${jdbc.url}"/>
  <property name="user" value="${jdbc.username}"/>
  <property name="password" value="${jdbc.password}"/>
</bean>

<context:property-placeholder location="jdbc.properties"/>

在jdbc.properties文件中定義屬性的值,如下:

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3309/sampledb

jdbc.username=root

jdbc.password=123456

但是這些屬性是以明文形式存放,那么任何擁有服務(wù)器登錄權(quán)限的人都可以查看這些機(jī)密信息,容易造成數(shù)據(jù)庫訪問權(quán)限的泄露.

這就要求對(duì)應(yīng)用程序配置文件對(duì)某些屬性進(jìn)行加密,讓Spring容器在讀取屬性文件后,在內(nèi)存中對(duì)屬性進(jìn)行解密,然后再將解密后的屬性賦給目標(biāo)對(duì)象.

這里提供一個(gè)加密解密工具(DES對(duì)稱加密解密)代碼:

package com.springboot.utils;

import java.security.Key;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.Base64.Decoder;
import java.util.Base64.Encoder;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;


public class DESUtils {
  //指定DES加密解密所用的密鑰
  private static Key key;
  private static String KEY_STR = "myKey";
  
  static {
    try {
      KeyGenerator generator = KeyGenerator.getInstance("DES");
      generator.init(new SecureRandom(KEY_STR.getBytes()));
      key = generator.generateKey();
      generator = null;
    }catch(Exception e) {
      throw new RuntimeException(e);
    }
  }
  
  public static String getEncryptString(String str) {
    Encoder encoder = Base64.getEncoder();
    try {
      byte[] strBytes = str.getBytes("UTF8");
      Cipher cipher = Cipher.getInstance("DES");
      cipher.init(Cipher.ENCRYPT_MODE, key);
      byte[] encryptStrBytes = cipher.doFinal(strBytes);
      return encoder.encodeToString(encryptStrBytes);
    }catch(Exception e) {
      throw new RuntimeException(e);
    }
  }
  
  public static String getDecryptString(String str) {
    Decoder decoder = Base64.getDecoder();
    try {
      byte[] strBytes = decoder.decode(str);
      Cipher cipher = Cipher.getInstance("DES");
      cipher.init(Cipher.DECRYPT_MODE, key);
      byte[] decryptStrBytes = cipher.doFinal(strBytes);
      return new String(decryptStrBytes,"UTF8");
    }catch(Exception e) {
      throw new RuntimeException(e);
    }
  }
  
  public static void main(String[] args) throws Exception{
    if(args == null || args.length < 1) {
      System.out.println("請(qǐng)輸入要加密的字符,用空格分隔.");
    }else {
      for(String arg : args) {
        System.out.println(arg + ":" + getEncryptString(arg));
      }
    }
  }
}

針對(duì)配置文件中加密信息的解密

package com.springboot.utils;

import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

public class EncryptPropertyPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer{
  private String[] encryptPropNames = {"userName","password"};
  
  private boolean isEncryptProp(String propertyName) {
    for(String encryptProName : encryptPropNames) {
      if(encryptProName.equals(propertyName)) {
        return true;
      }
    }
    return false;
  }
  @Override
  protected String convertProperty(String propertyName, String propertyValue) {
    if(isEncryptProp(propertyName)) {
      String decryptVal = DESUtils.getDecryptString(propertyValue);
      System.out.println("decryptVal = " + decryptVal);
      return decryptVal;
    }else {
      return propertyValue;
    }
  }
}

xml配置文件內(nèi)容

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:p="http://www.springframework.org/schema/p"
  xmlns:util="http://www.springframework.org/schema/util"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd 
    http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">


   
   <bean class="com.springboot.utils.EncryptPropertyPlaceholderConfigurer"
     p:location="classpath:application.properties"
     p:fileEncoding="utf-8"/>
   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close"
      p:driverClassName="${driverClassName}"
    p:url="${url}"
    p:username="${userName}"
    p:password="${password}"/>
</beans>

通過在控制臺(tái)運(yùn)行我們的加密代碼獲取加密后的密文

yusuwudeMacBook-Pro:classes yusuwu$ java com.springboot.utils.DESUtils root 123

獲取密文:

root:jxlNoW/DjKw=

123:RbtzyNE4tjY=

在application.properties中配置

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/springboot
userName=jxlNoW/DjKw=
password=RbtzyNE4tjY=

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java中如何將list轉(zhuǎn)為樹形結(jié)構(gòu)

    Java中如何將list轉(zhuǎn)為樹形結(jié)構(gòu)

    這篇文章主要介紹了Java中如何將list轉(zhuǎn)為樹形結(jié)構(gòu),本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-09-09
  • Java面試題沖刺第二十一天--JVM

    Java面試題沖刺第二十一天--JVM

    這篇文章主要為大家分享了最有價(jià)值的三道關(guān)于JVM的面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 深入Java線程中斷的本質(zhì)與編程原則的概述

    深入Java線程中斷的本質(zhì)與編程原則的概述

    本篇文章對(duì)Java線程中斷的本質(zhì)與編程原則進(jìn)行了詳細(xì)的概述,需要的朋友參考下
    2013-05-05
  • SpringBoot整合jasypt實(shí)現(xiàn)數(shù)據(jù)加密的步驟

    SpringBoot整合jasypt實(shí)現(xiàn)數(shù)據(jù)加密的步驟

    聽說過jasypt嗎?它可是一個(gè)超級(jí)流行的Java庫哦,提供了簡(jiǎn)單又高效的加密和解密接口,整合jasypt后,我們的SpringBoot應(yīng)用就能輕松處理敏感數(shù)據(jù)的加密和解密,而不必為復(fù)雜的加密算法頭疼啦,下面給大家介紹SpringBoot整合jasypt實(shí)現(xiàn)數(shù)據(jù)加密的步驟,感興趣的朋友一起看看吧
    2025-04-04
  • Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(18)

    Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(18)

    下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你
    2021-07-07
  • 數(shù)據(jù)庫阿里連接池 druid配置詳解

    數(shù)據(jù)庫阿里連接池 druid配置詳解

    本篇文章主要介紹了數(shù)據(jù)庫阿里連接池 druid配置詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • java中的xxxable和xxxator使用及說明

    java中的xxxable和xxxator使用及說明

    這篇文章主要介紹了java中的xxxable和xxxator使用及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Java?ServletContext與ServletConfig接口使用教程

    Java?ServletContext與ServletConfig接口使用教程

    ServletConfig對(duì)象,叫Servlet配置對(duì)象。主要用于加載配置文件的初始化參數(shù)。我們知道一個(gè)Web應(yīng)用里面可以有多個(gè)servlet,如果現(xiàn)在有一份數(shù)據(jù)需要傳給所有的servlet使用,那么我們就可以使用ServletContext對(duì)象了
    2022-09-09
  • Java中Switch的使用方法及新特性

    Java中Switch的使用方法及新特性

    在java中控制流程語句是由選擇語句、循環(huán)語句、跳轉(zhuǎn)語句構(gòu)成,選擇語句包括if和switch,在過多的使用if語句嵌套會(huì)使程序很難閱讀,這時(shí)就可以用到switch語句,這篇文章主要給大家介紹了關(guān)于Java中Switch的使用方法及新特性的相關(guān)資料,需要的朋友可以參考下
    2023-11-11
  • 詳解PipedInputStream和PipedOutputStream_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    詳解PipedInputStream和PipedOutputStream_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要為大家詳細(xì)介紹了管道PipedInputStream和PipedOutputStream,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05

最新評(píng)論

诏安县| 襄垣县| 武强县| 临城县| 荣成市| 敦煌市| 宽城| 满城县| 霸州市| 黑龙江省| 彭州市| 海盐县| 札达县| 汉源县| 滁州市| 德庆县| 汤原县| 钦州市| 苏州市| 安顺市| 洪洞县| 英山县| 通渭县| 房山区| 锡林浩特市| 丰城市| 南平市| 凌源市| 商城县| 永丰县| 老河口市| 塔河县| 林周县| 富民县| 南涧| 镇坪县| 岑巩县| 通化市| 札达县| 民权县| 江孜县|