2013-10-09 5 views
1

jacept가 (이전에 암호화 된) 속성 값을 해독하려고합니다. 궁극적으로 데이터베이스에 로그인하는 데 사용됩니다. 복호화는 Maven 프로파일을 소개 할 때를 제외하고는 정상적으로 작동합니다. 로컬/dev/prod 환경 설정 파일을 가지고 있습니다.Jasypt 암호화가 Maven 프로파일과 작동하지 않습니다.

여기 내 spring3 구성의 관련 부분이 있습니다. 이것은 코드 예제에서 가장 중요한 부분입니다. 이것은 암호 해독이 설정되는 방법과 암호 해독 된 문자열이 모범 사례 빈에 설정되는 방법입니다.

<bean id="jvmVariablesConfiguration" class="org.jasypt.encryption.pbe.config.EnvironmentPBEConfig" 
     p:password="secret_password_here"/> 

    <bean id="jvmConfigurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor" 
     p:config-ref="jvmVariablesConfiguration"/> 

    <bean id="jvmPropertyConfigurer" class="org.jasypt.spring3.properties.EncryptablePropertyPlaceholderConfigurer" 
     p:locations-ref="passwordProps"> 
    <constructor-arg ref="jvmConfigurationEncryptor"/> 
    </bean> 

    <util:list id="passwordProps"> 
    <value>classpath:database.properties</value>  
    </util:list> 

    <encryption:encryptable-properties id="dbProps" encryptor="jvmConfigurationEncryptor" location="classpath:database.properties"/> 

    <bean id="dummy" class="DummyPropertyTest"> 
    <property name="prop" value="${database.bar}"/> 
    </bean> 

내 maven poms 중 하나에서 프로필을 지정합니다. 이 자리 표시 자 속성이있는, /의 SRC/메인/자원

<dependency> 
    <groupId>org.jasypt</groupId> 
    <artifactId>jasypt-spring3</artifactId> 
    <version>1.9.1</version> 
</dependency> 

내가 메인 데이터베이스 속성 파일이 (database.properties) 설치 :

... 

    <profiles> 
    <profile> 
     <id>local</id> 
     <properties> 
     <build.profile.id>local</build.profile.id> 
     </properties> 
     <build> 
     <filters> 
      <filter>src/main/resources/properties/${build.profile.id}/database.properties</filter> 
     </filters> 
     <resources> 
      <resource> 
      <filtering>true</filtering> 
      <directory>src/main/resources</directory> 
      <includes> 
       <include>**/*.properties</include> 
       <include>**/*.xml</include> 
      </includes> 
      </resource> 
     </resources> 
     </build> 
    </profile> 

    <!--dev and prod profiles follow this in a similar pattern --> 

.... 

나는 버전 1.9.1을 jasypt 사용하고 있습니다 :

:

database.url=${database.url} 
database.username=${database.username} 
database.password=${database.password} 
database.dialect=${database.dialect} 
database.driver=${database.driver} 
database.show_sql=${database.show_sql} 
database.bar=${database.bar} 

그리고 여기가 /src/main/resources/properties/local/database.properties에있는 내 로컬 속성 파일의

database.url=jdbc:hsqldb:hsql://localhost/db 
database.username=sa 
database.password= 
database.dialect=MyHSQLDialect 
database.driver=org.hsqldb.jdbcDriver 
database.show_sql=true 
database.bar=ENC(RSuprdBgcpdheiWX0hJ45Q==) 

내 샘플 스프링 빈 코드는 여기에 설정된 속성을 읽는 것입니다. 모든 것이 작동하면 값은 해독 된 stdout에 인쇄됩니다. 나는 일반적으로 단지 별 특성 환경 재정의 단지 자리를 포함하는 주요 특성 파일에 상주하는 속성에 읽어 내 스프링 구성을 조정할 경우

import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class DummyPropertyTest { 

    private String prop; 

    public String getProp() { 
     return prop; 
    } 

    public void setProp(String prop) { 
     this.prop = prop; 
    } 

    @Value("#{dbProps['database.bar']}") 
    public String otherProp; 

    public String getOtherProp() { 
     return otherProp; 
    } 

    public static void main(String[] args) { 
     ApplicationContext ctx = new ClassPathXmlApplicationContext("/META-INF/spring/applicationContext-common.xml"); 
     DummyPropertyTest dpt = (DummyPropertyTest) ctx.getBean("dummy"); 

     System.out.println("what's my property being set??: "+dpt.getProp()); 
     System.out.println("otherProp:"+dpt.getOtherProp()); 
    } 
} 

, 다음 암호 해독 작동합니다. 그러나 암호 해독은 로컬 속성 파일에서 암호화 된 속성을 읽으려고하면 작동하지 않습니다. 나는 스프링 구성을 조정하려고 꽤 많은 것을 망쳤다. 클래스 패스 문제 일 뿐이라고 생각했지만, 도움이되지는 않았다.

아마도 암호화해야하는 속성에 대해서만 Spring이 속성 접두사와 접미사를 보는 방법을 재정의해야합니까? Jasypt의 EncryptablePropertyPlaceholderConfigurer는 Spring의 PropertyPlaceholderConfigurer에 대한 드롭 인 대체품이기 때문에 암호화 된 것뿐만 아니라 모든 속성에 적용되는 것처럼 보일 것입니다. 내가 설명했습니다으로 설정 파일을 두 개의 속성이있는 경우 여기에

이 프로그램의 출력입니다 : 내가 메인 프로퍼티 파일이있는 경우 여기 what's my property being set??: ENC(RSuprdBgcpdheiWX0hJ45Q==)

이 프로그램의 출력의 포함 된 암호화 된 특성 : what's my property being set??: sa

문제가 Spring 또는 Jayspt인지 확실하지 않습니다. 나는 그것이 메이븐이라고 생각하지 않는다. 지금 가능한 한 메이븐 프로필을 버리면 안된다.

명확성, 런타임 예제를 위해 편집 됨.* 업데이트

* : 나는 내가이하는 회원을 연결할 수 있습니다 내 테스트 콩에 <encryption:encryptable-properties id="dbProps" encryptor="jvmConfigurationEncryptor" location="classpath:database.properties"/>

그런 다음 Jasypt의 봄 구성 방법을 사용하는 경우 값이 제대로 해독되는 것을 확인할 수 있습니다 그것에 할당 된 재산 :

@Value("#{dbProps['database.bar']}") 
    public String otherProp; 

이것은 작동하는 것을 보인다. 하지만 실제로 데이터베이스 구성을 바꿀 수 있도록 PropertyOverride가 필요합니다.

답변

0

동료의 도움을 받아 해결책을 찾아 냈습니다. 문제는 프로필을 실제로 필터링하는 것이 었습니다. 다음은 수정 된 프로파일 설정입니다. 그 후 암호 해독은 꿈처럼 작동했습니다. 따라서 @Value 주석을 빈으로 직접 분리 할 필요가 없습니다 : Spring 구성에서 직접 속성을 설정하는 것이 좋습니다.

<profile> 
    <id>local</id> 
    <properties> 
    <build.profile.id>local</build.profile.id> 
    </properties> 
    <build> 
    <filters> 
     <filter>src/main/resources/properties/${build.profile.id}/database.properties</filter> 
     <filter>src/main/resources/properties/${build.profile.id}/cli.properties</filter> 
    </filters> 
    <resources> 
     <resource> 
     <filtering>true</filtering> 
     <directory>src/main/resources</directory> 
     <includes> 
      <include>**/*.properties</include> 
     </includes> 
     <excludes> 
      <exclude>**/*.xml</exclude> 
      <exclude>**/local/*.properties</exclude> 
      <exclude>**/dev/*.properties</exclude> 
      <exclude>**/prod/*.properties</exclude> 
     </excludes> 
     </resource> 
     <resource> 
     <filtering>false</filtering> 
     <directory>src/main/resources</directory> 
     <includes> 
      <include>**/*.xml</include> 
     </includes> 
     </resource> 
    </resources> 
    </build> 
</profile> 
관련 문제