22
22
import org .springframework .boot .Bootstrapper ;
23
23
import org .springframework .boot .context .properties .bind .BindHandler ;
24
24
import org .springframework .boot .context .properties .bind .Binder ;
25
- import org .springframework .cloud .autoconfigure .EncryptionBootstrapAutoConfiguration ;
26
- import org .springframework .cloud .bootstrap .TextEncryptorConfigurationPropertiesBindHandlerAdvisor .TextEncryptorBindHandler ;
27
25
import org .springframework .cloud .bootstrap .encrypt .KeyProperties ;
28
26
import org .springframework .cloud .bootstrap .encrypt .RsaProperties ;
29
27
import org .springframework .cloud .context .encrypt .EncryptorFactory ;
30
28
import org .springframework .core .env .Environment ;
31
29
import org .springframework .security .crypto .encrypt .TextEncryptor ;
30
+ import org .springframework .security .rsa .crypto .KeyStoreKeyFactory ;
31
+ import org .springframework .security .rsa .crypto .RsaSecretEncryptor ;
32
32
import org .springframework .util .ClassUtils ;
33
33
import org .springframework .util .StringUtils ;
34
34
40
40
*/
41
41
public class TextEncryptorConfigBootstrapper implements Bootstrapper {
42
42
43
+ private static final boolean RSA_IS_PRESENT = ClassUtils
44
+ .isPresent ("org.springframework.security.rsa.crypto.RsaSecretEncryptor" , null );
45
+
43
46
@ Override
44
47
public void intitialize (BootstrapRegistry registry ) {
45
48
if (!ClassUtils .isPresent ("org.springframework.security.crypto.encrypt.TextEncryptor" , null )) {
46
49
return ;
47
50
}
48
51
49
52
registry .registerIfAbsent (KeyProperties .class , context -> context .get (Binder .class )
50
- .bind ("encrypt" , KeyProperties .class ).orElseGet (KeyProperties ::new ));
51
- registry .registerIfAbsent (RsaProperties .class , context -> context .get (Binder .class )
52
- .bind ("encrypt.rsa" , RsaProperties .class ).orElseGet (RsaProperties ::new ));
53
+ .bind (KeyProperties .PREFIX , KeyProperties .class ).orElseGet (KeyProperties ::new ));
54
+ if (RSA_IS_PRESENT ) {
55
+ registry .registerIfAbsent (RsaProperties .class , context -> context .get (Binder .class )
56
+ .bind (RsaProperties .PREFIX , RsaProperties .class ).orElseGet (RsaProperties ::new ));
57
+ }
53
58
registry .registerIfAbsent (TextEncryptor .class , context -> {
54
59
KeyProperties keyProperties = context .get (KeyProperties .class );
55
60
if (keysConfigured (keyProperties )) {
56
- if (ClassUtils . isPresent ( "org.springframework.security.rsa.crypto.RsaSecretEncryptor" , null ) ) {
61
+ if (RSA_IS_PRESENT ) {
57
62
RsaProperties rsaProperties = context .get (RsaProperties .class );
58
- return EncryptionBootstrapAutoConfiguration . rsaTextEncryptor (keyProperties , rsaProperties );
63
+ return rsaTextEncryptor (keyProperties , rsaProperties );
59
64
}
60
65
return new EncryptorFactory (keyProperties .getSalt ()).create (keyProperties .getKey ());
61
66
}
@@ -82,9 +87,11 @@ public void intitialize(BootstrapRegistry registry) {
82
87
if (keyProperties != null ) {
83
88
beanFactory .registerSingleton ("keyProperties" , keyProperties );
84
89
}
85
- RsaProperties rsaProperties = bootstrapContext .get (RsaProperties .class );
86
- if (rsaProperties != null ) {
87
- beanFactory .registerSingleton ("rsaProperties" , rsaProperties );
90
+ if (RSA_IS_PRESENT ) {
91
+ RsaProperties rsaProperties = bootstrapContext .get (RsaProperties .class );
92
+ if (rsaProperties != null ) {
93
+ beanFactory .registerSingleton ("rsaProperties" , rsaProperties );
94
+ }
88
95
}
89
96
TextEncryptor textEncryptor = bootstrapContext .get (TextEncryptor .class );
90
97
if (textEncryptor != null ) {
@@ -93,7 +100,23 @@ public void intitialize(BootstrapRegistry registry) {
93
100
});
94
101
}
95
102
96
- private boolean keysConfigured (KeyProperties properties ) {
103
+ public static TextEncryptor rsaTextEncryptor (KeyProperties keyProperties , RsaProperties rsaProperties ) {
104
+ KeyProperties .KeyStore keyStore = keyProperties .getKeyStore ();
105
+ if (keyStore .getLocation () != null ) {
106
+ if (keyStore .getLocation ().exists ()) {
107
+ return new RsaSecretEncryptor (
108
+ new KeyStoreKeyFactory (keyStore .getLocation (), keyStore .getPassword ().toCharArray ())
109
+ .getKeyPair (keyStore .getAlias (), keyStore .getSecret ().toCharArray ()),
110
+ rsaProperties .getAlgorithm (), rsaProperties .getSalt (), rsaProperties .isStrong ());
111
+ }
112
+
113
+ throw new IllegalStateException ("Invalid keystore location" );
114
+ }
115
+
116
+ return new EncryptorFactory (keyProperties .getSalt ()).create (keyProperties .getKey ());
117
+ }
118
+
119
+ public static boolean keysConfigured (KeyProperties properties ) {
97
120
if (hasProperty (properties .getKeyStore ().getLocation ())) {
98
121
if (hasProperty (properties .getKeyStore ().getPassword ())) {
99
122
return true ;
@@ -106,14 +129,14 @@ else if (hasProperty(properties.getKey())) {
106
129
return false ;
107
130
}
108
131
109
- private boolean hasProperty (Object value ) {
132
+ static boolean hasProperty (Object value ) {
110
133
if (value instanceof String ) {
111
134
return StringUtils .hasText ((String ) value );
112
135
}
113
136
return value != null ;
114
137
}
115
138
116
- private boolean isLegacyBootstrap (Environment environment ) {
139
+ static boolean isLegacyBootstrap (Environment environment ) {
117
140
boolean isLegacy = environment .getProperty ("spring.config.use-legacy-processing" , Boolean .class , false );
118
141
boolean isBootstrapEnabled = environment .getProperty ("spring.cloud.bootstrap.enabled" , Boolean .class , false );
119
142
return isLegacy || isBootstrapEnabled ;
@@ -126,7 +149,7 @@ private boolean isLegacyBootstrap(Environment environment) {
126
149
* @author Dave Syer
127
150
*
128
151
*/
129
- protected static class FailsafeTextEncryptor implements TextEncryptor {
152
+ public static class FailsafeTextEncryptor implements TextEncryptor {
130
153
131
154
@ Override
132
155
public String encrypt (String text ) {
0 commit comments