JavaSpring.libraries.spring-cloud.clientConfiguration.mustache Maven / Gradle / Ivy
package {{configPackage}};
{{#authMethods}}
{{#isBasic}}
import feign.auth.BasicAuthRequestInterceptor;
{{/isBasic}}
{{#-first}}
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
{{/-first}}
{{#isOAuth}}
import org.springframework.boot.context.properties.ConfigurationProperties;
{{/isOAuth}}
{{/authMethods}}
import org.springframework.boot.context.properties.EnableConfigurationProperties;
{{#authMethods}}
{{#-first}}
import org.springframework.context.annotation.Bean;
{{/-first}}
{{/authMethods}}
import org.springframework.context.annotation.Configuration;
{{#authMethods}}
{{#isOAuth}}
import org.springframework.cloud.security.oauth2.client.feign.OAuth2FeignRequestInterceptor;
import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
{{#isApplication}}
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
{{/isApplication}}
{{#isCode}}
import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails;
{{/isCode}}
{{#isImplicit}}
import org.springframework.security.oauth2.client.token.grant.implicit.ImplicitResourceDetails;
{{/isImplicit}}
{{#isPassword}}
import org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordResourceDetails;
{{/isPassword}}
{{/isOAuth}}
{{/authMethods}}
@Configuration
@EnableConfigurationProperties
public class ClientConfiguration {
{{#authMethods}}
{{#isBasic}}
@Value("${{openbrace}}{{#lambda.lowercase}}{{{title}}}{{/lambda.lowercase}}.security.{{{name}}}.username:{{closebrace}}")
private String {{{name}}}Username;
@Value("${{openbrace}}{{#lambda.lowercase}}{{{title}}}{{/lambda.lowercase}}.security.{{{name}}}.password:{{closebrace}}")
private String {{{name}}}Password;
@Bean
@ConditionalOnProperty(name = "{{#lambda.lowercase}}{{{title}}}{{/lambda.lowercase}}.security.{{{name}}}.username")
public BasicAuthRequestInterceptor {{{name}}}RequestInterceptor() {
return new BasicAuthRequestInterceptor(this.{{{name}}}Username, this.{{{name}}}Password);
}
{{/isBasic}}
{{#isApiKey}}
@Value("${{openbrace}}{{#lambda.lowercase}}{{{title}}}{{/lambda.lowercase}}.security.{{{name}}}.key:{{closebrace}}")
private String {{{name}}}Key;
@Bean
@ConditionalOnProperty(name = "{{#lambda.lowercase}}{{{title}}}{{/lambda.lowercase}}.security.{{{name}}}.key")
public ApiKeyRequestInterceptor {{{name}}}RequestInterceptor() {
return new ApiKeyRequestInterceptor({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{{keyParamName}}}", this.{{{name}}}Key);
}
{{/isApiKey}}
{{#isOAuth}}
@Bean
@ConditionalOnProperty("{{#lambda.lowercase}}{{{title}}}{{/lambda.lowercase}}.security.{{{name}}}.client-id")
public OAuth2FeignRequestInterceptor {{{name}}}RequestInterceptor(OAuth2ClientContext oAuth2ClientContext) {
return new OAuth2FeignRequestInterceptor(oAuth2ClientContext, {{{name}}}ResourceDetails());
}
@Bean
@ConditionalOnProperty("{{#lambda.lowercase}}{{{title}}}{{/lambda.lowercase}}.security.{{{name}}}.client-id")
public OAuth2ClientContext oAuth2ClientContext() {
return new DefaultOAuth2ClientContext();
}
{{#isCode}}
@Bean
@ConditionalOnProperty("{{#lambda.lowercase}}{{{title}}}{{/lambda.lowercase}}.security.{{{name}}}.client-id")
@ConfigurationProperties("{{#lambda.lowercase}}{{{title}}}{{/lambda.lowercase}}.security.{{{name}}}")
public AuthorizationCodeResourceDetails {{{name}}}ResourceDetails() {
AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails();
details.setAccessTokenUri("{{{tokenUrl}}}");
details.setUserAuthorizationUri("{{{authorizationUrl}}}");
return details;
}
{{/isCode}}
{{#isPassword}}
@Bean
@ConditionalOnProperty("{{#lambda.lowercase}}{{{title}}}{{/lambda.lowercase}}.security.{{{name}}}.client-id")
@ConfigurationProperties("{{#lambda.lowercase}}{{{title}}}{{/lambda.lowercase}}.security.{{{name}}}")
public ResourceOwnerPasswordResourceDetails {{{name}}}ResourceDetails() {
ResourceOwnerPasswordResourceDetails details = new ResourceOwnerPasswordResourceDetails();
details.setAccessTokenUri("{{{tokenUrl}}}");
return details;
}
{{/isPassword}}
{{#isApplication}}
@Bean
@ConditionalOnProperty("{{#lambda.lowercase}}{{{title}}}{{/lambda.lowercase}}.security.{{{name}}}.client-id")
@ConfigurationProperties("{{#lambda.lowercase}}{{{title}}}{{/lambda.lowercase}}.security.{{{name}}}")
public ClientCredentialsResourceDetails {{{name}}}ResourceDetails() {
ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails();
details.setAccessTokenUri("{{{tokenUrl}}}");
return details;
}
{{/isApplication}}
{{#isImplicit}}
@Bean
@ConditionalOnProperty("{{#lambda.lowercase}}{{{title}}}{{/lambda.lowercase}}.security.{{{name}}}.client-id")
@ConfigurationProperties("{{#lambda.lowercase}}{{{title}}}{{/lambda.lowercase}}.security.{{{name}}}")
public ImplicitResourceDetails {{{name}}}ResourceDetails() {
ImplicitResourceDetails details = new ImplicitResourceDetails();
details.setUserAuthorizationUri("{{{authorizationUrl}}}");
return details;
}
{{/isImplicit}}
{{/isOAuth}}
{{/authMethods}}
}