com.sap.cloud.yaas.servicesdk.springboot.security.BasicAuthenticationAutoConfiguration Maven / Gradle / Ivy
/*
* © 2017 SAP SE or an SAP affiliate company.
* All rights reserved.
* Please see http://www.sap.com/corporate-en/legal/copyright/index.epx for additional trademark information and
* notices.
*/
package com.sap.cloud.yaas.servicesdk.springboot.security;
import com.sap.cloud.yaas.servicesdk.security.basicauthorization.EnforceBasicAuthenticationFilter;
import com.sap.cloud.yaas.servicesdk.servletsupport.filters.ExcludableServletFilterWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.DelegatingFilterProxyRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import javax.servlet.Filter;
/**
* Auto Configuration class that registers YaaS Service SDK's {@link EnforceBasicAuthenticationFilter}.
*
* This filter enforces Basic Authorization, based on credentials configured in the properties.
*/
@Configuration
@ConditionalOnClass({ EnforceBasicAuthenticationFilter.class, DelegatingFilterProxyRegistrationBean.class })
@ConditionalOnWebApplication
@EnableConfigurationProperties(BasicAuthenticationProperties.class)
public class BasicAuthenticationAutoConfiguration
{
@Autowired
private BasicAuthenticationProperties properties;
/**
* Registers the {@link EnforceBasicAuthenticationFilter} that enforces Basic Authentication as defined in
* credentials. Includes Configuration of an {@link ExcludableServletFilterWrapper}.
*
* @return the configured {@link EnforceBasicAuthenticationFilter}
*/
@Lazy
@Bean(name = "basicAuthenticationFilter")
@ConditionalOnClass(name = "com.sap.cloud.yaas.servicesdk.servletsupport.filters.ExcludableServletFilterWrapper")
public Filter basicAuthenticationFilterExcludable()
{
return new ExcludableServletFilterWrapper(
new EnforceBasicAuthenticationFilter(properties.getRealm(), properties.getCredentials()),
properties.getExcludePaths());
}
/**
* Registers the {@link EnforceBasicAuthenticationFilter} that enforces Basic Authentication as defined in
* credentials.
*
* @return the configured {@link EnforceBasicAuthenticationFilter}
*/
@Lazy
@Bean(name = "basicAuthenticationFilter")
@ConditionalOnMissingClass("com.sap.cloud.yaas.servicesdk.servletsupport.filters.ExcludableServletFilterWrapper")
public Filter basicAuthenticationFilter()
{
return new EnforceBasicAuthenticationFilter(properties.getRealm(), properties.getCredentials());
}
/**
* Factory method that registers the {@link #basicAuthenticationFilter()} bean as a Filter for all requests.
*
* @return The basicAuthFilterRegistration bean.
*/
@Bean
@ConditionalOnMissingBean(name = "basicAuthenticationFilterRegistration")
public DelegatingFilterProxyRegistrationBean basicAuthenticationFilterRegistration()
{
final DelegatingFilterProxyRegistrationBean registration = new DelegatingFilterProxyRegistrationBean(
"basicAuthenticationFilter");
if (properties.getFilterOrder() != null)
{
registration.setOrder(properties.getFilterOrder());
}
return registration;
}
}