java对api加密_java – Spring在属性文件中加密和解密API密钥

原始问题

我有一个位于Tomcat的属性文件和一个位于src / test / resources中的测试属性文件.

目前我有以下设置.我的属性文件加载到我的XML文件中

config.xml中

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

xmlns:cache="http://www.springframework.org/schema/cache"

xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd

http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

location="file:${catalina.base}/conf/omniatravel.properties"

ignore-unresolvable="true" />

测试-config.xml中

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

xmlns:cache="http://www.springframework.org/schema/cache"

xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd

http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

location="classpath:omniatravel_test.properties"

ignore-unresolvable="true" />

我可以通过将它放在我的Java文件中来访问这些值

public class SunnycarsClient extends WebServiceGatewaySupport {

@Value("${sunnycars.serviceUri}")

private String uri; // provided by the webservice

@Value("${sunnycars.operatingKey}")

private String key; // provide by the webservice

@Value("${sunnycars.passphrase}")

private String passphrase; // provided by the webservice

}

目前,operatingKey和passphrase作为平面文本存储在这些属性中.我想将它们存储为加密值,以最大限度地降低风险,并且仍然能够以我现在的方式访问.

更新1

所以我现在所做的就是将config.xml的内容替换为

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

xmlns:cache="http://www.springframework.org/schema/cache"

xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd

http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

class="org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer">

file:${catalina.base}/conf/omniatravel.properties

destroy-method="close">

${sunnycars.marshallerUri}

${sunnycars.serviceUri}

${sunnycars.contextPath}

${sunnycars.operatingKey}

${sunnycars.passphrase}

但我仍然不清楚如何从我的Java代码中访问这些内容.

同样在propeties文件中我应该用sunnycars.operatingKey = enc(ENCRYPTED_KEY)替换sunnycars.operatingKey = THE_KEY,但是如何获得ENCRYPTED_KEY值?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值