Browse Source

Stop throwing checked exception from @PostConstruct in ResourceServerProperties

Closes gh-8916
pull/9072/head
Andy Wilkinson 9 years ago
parent
commit
4a00f90545
  1. 16
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/ResourceServerProperties.java
  2. 10
      spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/resource/ResourceServerPropertiesTests.java

16
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/ResourceServerProperties.java

@ -189,7 +189,7 @@ public class ResourceServerProperties implements BeanFactoryAware { @@ -189,7 +189,7 @@ public class ResourceServerProperties implements BeanFactoryAware {
}
@PostConstruct
public void validate() throws Exception {
public void validate() {
if (countBeans(AuthorizationServerEndpointsConfiguration.class) > 0) {
// If we are an authorization server we don't need remote resource token
// services
@ -203,9 +203,11 @@ public class ResourceServerProperties implements BeanFactoryAware { @@ -203,9 +203,11 @@ public class ResourceServerProperties implements BeanFactoryAware {
if (!StringUtils.hasText(this.clientId)) {
return;
}
BindingResult result = getBindingResult();
if (result.hasErrors()) {
throw new BindException(result);
try {
doValidate();
}
catch (BindException ex) {
throw new IllegalStateException(ex);
}
}
@ -214,7 +216,7 @@ public class ResourceServerProperties implements BeanFactoryAware { @@ -214,7 +216,7 @@ public class ResourceServerProperties implements BeanFactoryAware {
true, false).length;
}
private BindingResult getBindingResult() {
private void doValidate() throws BindException {
BindingResult errors = new BeanPropertyBindingResult(this,
"resourceServerProperties");
boolean jwtConfigPresent = StringUtils.hasText(this.jwt.getKeyUri())
@ -239,7 +241,9 @@ public class ResourceServerProperties implements BeanFactoryAware { @@ -239,7 +241,9 @@ public class ResourceServerProperties implements BeanFactoryAware {
}
}
}
return errors;
if (errors.hasErrors()) {
throw new BindException(errors);
}
}
public class Jwt {

10
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/resource/ResourceServerPropertiesTests.java

@ -77,7 +77,7 @@ public class ResourceServerPropertiesTests { @@ -77,7 +77,7 @@ public class ResourceServerPropertiesTests {
this.properties.getJwk().setKeySetUri("http://my-auth-server/token_keys");
this.properties.getJwt().setKeyUri("http://my-auth-server/token_key");
setListableBeanFactory();
this.thrown.expect(BindException.class);
this.thrown.expect(IllegalStateException.class);
this.thrown.expect(getMatcher("Only one of jwt.keyUri (or jwt.keyValue) "
+ "and jwk.keySetUri should be configured.", null));
this.properties.validate();
@ -89,7 +89,7 @@ public class ResourceServerPropertiesTests { @@ -89,7 +89,7 @@ public class ResourceServerPropertiesTests {
this.properties.getJwk().setKeySetUri("http://my-auth-server/token_keys");
this.properties.getJwt().setKeyValue("my-key");
setListableBeanFactory();
this.thrown.expect(BindException.class);
this.thrown.expect(IllegalStateException.class);
this.thrown.expect(getMatcher("Only one of jwt.keyUri (or jwt.keyValue) "
+ "and jwk.keySetUri should be configured.", null));
this.properties.validate();
@ -125,7 +125,7 @@ public class ResourceServerPropertiesTests { @@ -125,7 +125,7 @@ public class ResourceServerPropertiesTests {
public void validateWhenKeyConfigAbsentAndInfoUrisNotConfiguredShouldFail()
throws Exception {
setListableBeanFactory();
this.thrown.expect(BindException.class);
this.thrown.expect(IllegalStateException.class);
this.thrown.expect(getMatcher("Missing tokenInfoUri and userInfoUri and there"
+ " is no JWT verifier key", "tokenInfoUri"));
this.properties.validate();
@ -154,7 +154,7 @@ public class ResourceServerPropertiesTests { @@ -154,7 +154,7 @@ public class ResourceServerPropertiesTests {
this.properties.setTokenInfoUri("http://my-auth-server/check_token");
this.properties.setUserInfoUri("http://my-auth-server/userinfo");
setListableBeanFactory();
this.thrown.expect(BindException.class);
this.thrown.expect(IllegalStateException.class);
this.thrown.expect(getMatcher("Missing client secret", "clientSecret"));
this.properties.validate();
}
@ -208,7 +208,7 @@ public class ResourceServerPropertiesTests { @@ -208,7 +208,7 @@ public class ResourceServerPropertiesTests {
@Override
public boolean matches(Object item) {
BindException ex = (BindException) item;
BindException ex = (BindException) ((Exception) item).getCause();
ObjectError error = ex.getAllErrors().get(0);
boolean messageMatches = message.equals(error.getDefaultMessage());
if (field == null) {

Loading…
Cancel
Save