Align Code with Javadoc

Fixes: gh-6734
This commit is contained in:
Josh Cummings 2019-03-11 12:19:52 -06:00
parent cd326df659
commit 3ddcbde466
No known key found for this signature in database
GPG Key ID: 49EF60DD7FF83443
2 changed files with 14 additions and 14 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -37,16 +37,15 @@ public class SecureRandomFactoryBean implements FactoryBean<SecureRandom> {
public SecureRandom getObject() throws Exception { public SecureRandom getObject() throws Exception {
SecureRandom rnd = SecureRandom.getInstance(algorithm); SecureRandom rnd = SecureRandom.getInstance(algorithm);
// Request the next bytes, thus eagerly incurring the expense of default
// seeding and to prevent the see from replacing the entire state
rnd.nextBytes(new byte[1]);
if (seed != null) { if (seed != null) {
// Seed specified, so use it // Seed specified, so use it
byte[] seedBytes = FileCopyUtils.copyToByteArray(seed.getInputStream()); byte[] seedBytes = FileCopyUtils.copyToByteArray(seed.getInputStream());
rnd.setSeed(seedBytes); rnd.setSeed(seedBytes);
} }
else {
// Request the next bytes, thus eagerly incurring the expense of default
// seeding
rnd.nextBytes(new byte[1]);
}
return rnd; return rnd;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -15,14 +15,14 @@
*/ */
package org.springframework.security.core.token; package org.springframework.security.core.token;
import static org.assertj.core.api.Assertions.*;
import java.security.SecureRandom; import java.security.SecureRandom;
import org.junit.Test; import org.junit.Test;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.security.core.token.SecureRandomFactoryBean;
import static org.assertj.core.api.Assertions.assertThat;
/** /**
* Tests {@link SecureRandomFactoryBean}. * Tests {@link SecureRandomFactoryBean}.
@ -59,10 +59,11 @@ public class SecureRandomFactoryBeanTests {
"org/springframework/security/core/token/SecureRandomFactoryBeanTests.class"); "org/springframework/security/core/token/SecureRandomFactoryBeanTests.class");
assertThat(resource).isNotNull(); assertThat(resource).isNotNull();
factory.setSeed(resource); factory.setSeed(resource);
Object result = factory.getObject(); SecureRandom first = factory.getObject();
assertThat(result).isInstanceOf(SecureRandom.class); SecureRandom second = factory.getObject();
int rnd = ((SecureRandom) result).nextInt(); assertThat(first.nextInt())
assertThat(rnd).isNotEqualTo(0); .isNotEqualTo(0)
.isNotEqualTo(second.nextInt());
} }
} }