Add @EnableOAuth2Sso and spring.oauth2.sso.*
User can enable OAuth2 SSO by declaring the intent (@EnableOAuth2Sso) and also configuring the client properties (spring.oauth2.client.*). The spring.oauth2.sso.* are only needed to change the path for the login (defaults to /login) - any other security configuration for the protected resources can be added in a WebSecurityConfigurerAdapter which carries the @EnableOAuth2Sso annotation.
This commit is contained in:
@@ -17,15 +17,19 @@
|
||||
package org.springframework.boot.autoconfigure.security.oauth2;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.authserver.SpringSecurityOAuth2AuthorizationServerConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.client.SpringSecurityOAuth2ClientConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.resource.SpringSecurityOAuth2ResourceServerConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2RestOperationsConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.method.OAuth2MethodSecurityConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerProperties;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
||||
@@ -40,16 +44,25 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({ OAuth2AccessToken.class, WebMvcConfigurerAdapter.class })
|
||||
@ConditionalOnWebApplication
|
||||
@Import({ SpringSecurityOAuth2AuthorizationServerConfiguration.class,
|
||||
SpringSecurityOAuth2MethodSecurityConfiguration.class,
|
||||
SpringSecurityOAuth2ResourceServerConfiguration.class,
|
||||
SpringSecurityOAuth2ClientConfiguration.class })
|
||||
OAuth2MethodSecurityConfiguration.class,
|
||||
OAuth2ResourceServerConfiguration.class,
|
||||
OAuth2RestOperationsConfiguration.class })
|
||||
@AutoConfigureBefore(WebMvcAutoConfiguration.class)
|
||||
@EnableConfigurationProperties(ClientCredentialsProperties.class)
|
||||
public class SpringSecurityOAuth2AutoConfiguration {
|
||||
@EnableConfigurationProperties(OAuth2ClientProperties.class)
|
||||
public class OAuth2AutoConfiguration {
|
||||
|
||||
@Autowired
|
||||
private OAuth2ClientProperties credentials;
|
||||
|
||||
@Bean
|
||||
public ResourceServerProperties resourceServerProperties() {
|
||||
return new ResourceServerProperties(this.credentials.getClientId(),
|
||||
this.credentials.getClientSecret());
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnWebApplication
|
||||
protected static class ResourceServerOrderProcessor implements BeanPostProcessor {
|
||||
|
||||
@Override
|
||||
@@ -24,7 +24,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@ConfigurationProperties("spring.oauth2.client")
|
||||
public class ClientCredentialsProperties {
|
||||
public class OAuth2ClientProperties {
|
||||
|
||||
private String clientId;
|
||||
|
||||
@@ -20,11 +20,15 @@ import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.ClientCredentialsProperties;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.OAuth2ClientProperties;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -66,13 +70,34 @@ public class SpringSecurityOAuth2AuthorizationServerConfiguration extends
|
||||
|
||||
@Autowired(required = false)
|
||||
private TokenStore tokenStore;
|
||||
|
||||
@Configuration
|
||||
protected static class ClientDetailsLogger {
|
||||
|
||||
private static final Log logger = LogFactory
|
||||
.getLog(SpringSecurityOAuth2AuthorizationServerConfiguration.class);
|
||||
|
||||
@Autowired
|
||||
private OAuth2ClientProperties credentials;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
String prefix = "spring.oauth2.client";
|
||||
boolean defaultSecret = this.credentials.isDefaultSecret();
|
||||
logger.info(String.format(
|
||||
"Initialized OAuth2 Client\n\n%s.clientId = %s\n%s.secret = %s\n\n",
|
||||
prefix, this.credentials.getClientId(), prefix,
|
||||
defaultSecret ? this.credentials.getClientSecret() : "****"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnMissingBean(BaseClientDetails.class)
|
||||
protected static class BaseClientDetailsConfiguration {
|
||||
|
||||
@Autowired
|
||||
private ClientCredentialsProperties client;
|
||||
private OAuth2ClientProperties client;
|
||||
|
||||
@Bean
|
||||
@ConfigurationProperties("spring.oauth2.client")
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.security.oauth2.client;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerTokenServicesConfiguration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client;
|
||||
|
||||
/**
|
||||
* Configuration for OAuth2 Single Sign On (SSO). If there is an existing
|
||||
* {@link WebSecurityConfigurerAdapter} provided by the user and annotated with
|
||||
* <code>@EnableOAuth2Sso</code>, it is enhanced by adding an authentication filter and an
|
||||
* authentication entry point. If the user only has <code>@EnableOAuth2Sso</code> but not
|
||||
* on a WebSecurityConfigurerAdapter then one is added with all paths secured and with an
|
||||
* order that puts it ahead of the default HTTP Basic security chain in Spring Boot.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@EnableOAuth2Client
|
||||
@Import({ OAuth2SsoDefaultConfiguration.class, OAuth2SsoCustomConfiguration.class, ResourceServerTokenServicesConfiguration.class })
|
||||
public @interface EnableOAuth2Sso {
|
||||
|
||||
}
|
||||
@@ -15,12 +15,8 @@
|
||||
*/
|
||||
package org.springframework.boot.autoconfigure.security.oauth2.client;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
@@ -28,7 +24,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.ClientCredentialsProperties;
|
||||
import org.springframework.boot.context.embedded.FilterRegistrationBean;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -60,23 +55,7 @@ import org.springframework.security.oauth2.provider.authentication.OAuth2Authent
|
||||
@Configuration
|
||||
@ConditionalOnClass(EnableOAuth2Client.class)
|
||||
@ConditionalOnExpression("'${spring.oauth2.client.clientId:}'!=''")
|
||||
public class SpringSecurityOAuth2ClientConfiguration {
|
||||
|
||||
private static final Log logger = LogFactory
|
||||
.getLog(SpringSecurityOAuth2ClientConfiguration.class);
|
||||
|
||||
@Autowired
|
||||
private ClientCredentialsProperties credentials;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
String prefix = "spring.oauth2.client";
|
||||
boolean defaultSecret = this.credentials.isDefaultSecret();
|
||||
logger.info(String.format(
|
||||
"Initialized OAuth2 Client\n\n%s.clientId = %s\n%s.secret = %s\n\n",
|
||||
prefix, this.credentials.getClientId(), prefix,
|
||||
defaultSecret ? this.credentials.getClientSecret() : "****"));
|
||||
}
|
||||
public class OAuth2RestOperationsConfiguration {
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.security.oauth2.client;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.aopalliance.intercept.MethodInterceptor;
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
|
||||
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2SsoCustomConfiguration.WebSecurityEnhancerCondition;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.ImportAware;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Configuration for OAuth2 Single Sign On (SSO) when there is an existing
|
||||
* {@link WebSecurityConfigurerAdapter} provided by the user and annotated with
|
||||
* <code>@EnableOAuth2Sso</code>. The user-provided configuration is enhanced by adding an
|
||||
* authentication filter and an authentication entry point.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
@Configuration
|
||||
@Conditional(WebSecurityEnhancerCondition.class)
|
||||
public class OAuth2SsoCustomConfiguration implements ImportAware, BeanPostProcessor,
|
||||
BeanFactoryAware {
|
||||
|
||||
private Class<?> configType;
|
||||
|
||||
private BeanFactory beanFactory;
|
||||
|
||||
@Override
|
||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setImportMetadata(AnnotationMetadata importMetadata) {
|
||||
configType = ClassUtils.resolveClassName(importMetadata.getClassName(), null);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName)
|
||||
throws BeansException {
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName)
|
||||
throws BeansException {
|
||||
if (configType.isAssignableFrom(bean.getClass())
|
||||
&& bean instanceof WebSecurityConfigurerAdapter) {
|
||||
ProxyFactory factory = new ProxyFactory();
|
||||
factory.setTarget(bean);
|
||||
factory.addAdvice(new SsoSecurityAdapter(beanFactory));
|
||||
bean = factory.getProxy();
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
private static class SsoSecurityAdapter implements MethodInterceptor {
|
||||
|
||||
private SsoSecurityConfigurer configurer;
|
||||
|
||||
public SsoSecurityAdapter(BeanFactory beanFactory) {
|
||||
configurer = new SsoSecurityConfigurer(beanFactory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(MethodInvocation invocation) throws Throwable {
|
||||
if (invocation.getMethod().getName().equals("init")) {
|
||||
Method method = ReflectionUtils.findMethod(
|
||||
WebSecurityConfigurerAdapter.class, "getHttp");
|
||||
ReflectionUtils.makeAccessible(method);
|
||||
HttpSecurity http = (HttpSecurity) ReflectionUtils.invokeMethod(method,
|
||||
(WebSecurityConfigurerAdapter) invocation.getThis());
|
||||
configurer.configure(http);
|
||||
}
|
||||
return invocation.proceed();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected static class WebSecurityEnhancerCondition extends SpringBootCondition {
|
||||
|
||||
@Override
|
||||
public ConditionOutcome getMatchOutcome(ConditionContext context,
|
||||
AnnotatedTypeMetadata metadata) {
|
||||
String[] enablers = context.getBeanFactory().getBeanNamesForAnnotation(
|
||||
EnableOAuth2Sso.class);
|
||||
for (String name : enablers) {
|
||||
if (context.getBeanFactory().isTypeMatch(name,
|
||||
WebSecurityConfigurerAdapter.class)) {
|
||||
return ConditionOutcome
|
||||
.match("found @EnableOAuth2Sso on a WebSecurityConfigurerAdapter");
|
||||
}
|
||||
}
|
||||
return ConditionOutcome
|
||||
.noMatch("found no @EnableOAuth2Sso on a WebSecurityConfigurerAdapter");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.security.oauth2.client;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
|
||||
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
|
||||
import org.springframework.boot.autoconfigure.security.SecurityProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* If the user only has <code>@EnableOAuth2Sso</code> but not on a
|
||||
* WebSecurityConfigurerAdapter then one is added with all paths secured and with an order
|
||||
* that puts it ahead of the default HTTP Basic security chain in Spring Boot.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(OAuth2SsoProperties.class)
|
||||
public class OAuth2SsoDefaultConfiguration {
|
||||
|
||||
@Configuration
|
||||
@Conditional(NeedsWebSecurityCondition.class)
|
||||
protected static class WebSecurityConfiguration extends WebSecurityConfigurerAdapter
|
||||
implements Ordered {
|
||||
|
||||
@Autowired
|
||||
BeanFactory beanFactory;
|
||||
|
||||
@Autowired
|
||||
OAuth2SsoProperties sso;
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
http.antMatcher("/**").authorizeRequests().anyRequest().authenticated();
|
||||
new SsoSecurityConfigurer(beanFactory).configure(http);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
if (sso.getFilterOrder() != null) {
|
||||
return sso.getFilterOrder();
|
||||
}
|
||||
if (ClassUtils
|
||||
.isPresent(
|
||||
"org.springframework.boot.actuate.autoconfigure.ManagementServerProperties",
|
||||
null)) {
|
||||
// If > BASIC_AUTH_ORDER then the existing rules for the actuator
|
||||
// endpoints
|
||||
// will take precedence. This value is < BASIC_AUTH_ORDER.
|
||||
return SecurityProperties.ACCESS_OVERRIDE_ORDER - 5;
|
||||
}
|
||||
return SecurityProperties.ACCESS_OVERRIDE_ORDER;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class NeedsWebSecurityCondition extends SpringBootCondition {
|
||||
@Override
|
||||
public ConditionOutcome getMatchOutcome(ConditionContext context,
|
||||
AnnotatedTypeMetadata metadata) {
|
||||
String[] enablers = context.getBeanFactory().getBeanNamesForAnnotation(
|
||||
EnableOAuth2Sso.class);
|
||||
for (String name : enablers) {
|
||||
if (context.getBeanFactory().isTypeMatch(name,
|
||||
WebSecurityConfigurerAdapter.class)) {
|
||||
return ConditionOutcome
|
||||
.noMatch("found @EnableOAuth2Sso on a WebSecurityConfigurerAdapter");
|
||||
}
|
||||
}
|
||||
return ConditionOutcome
|
||||
.match("found no @EnableOAuth2Sso on a WebSecurityConfigurerAdapter");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2013-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.boot.autoconfigure.security.oauth2.client;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
@ConfigurationProperties("spring.oauth2.sso")
|
||||
public class OAuth2SsoProperties {
|
||||
|
||||
public static final String DEFAULT_LOGIN_PATH = "/login";
|
||||
|
||||
/**
|
||||
* Path to the login page, i.e. the one that triggers the redirect to the OAuth2
|
||||
* Authorization Server.
|
||||
*/
|
||||
private String loginPath = DEFAULT_LOGIN_PATH;
|
||||
|
||||
/**
|
||||
* The filter order to apply if not providing an explicit WebSecurityConfigurerAdapter
|
||||
* (in which case the order can be provided there instead).
|
||||
*/
|
||||
private Integer filterOrder;
|
||||
|
||||
public String getLoginPath() {
|
||||
return loginPath;
|
||||
}
|
||||
|
||||
public void setLoginPath(String loginPath) {
|
||||
this.loginPath = loginPath;
|
||||
}
|
||||
|
||||
public Integer getFilterOrder() {
|
||||
return filterOrder;
|
||||
}
|
||||
|
||||
public void setFilterOrder(Integer filterOrder) {
|
||||
this.filterOrder = filterOrder;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.security.oauth2.client;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.oauth2.client.OAuth2RestOperations;
|
||||
import org.springframework.security.oauth2.client.filter.OAuth2ClientAuthenticationProcessingFilter;
|
||||
import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices;
|
||||
import org.springframework.security.web.DefaultSecurityFilterChain;
|
||||
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
|
||||
import org.springframework.security.web.authentication.preauth.AbstractPreAuthenticatedProcessingFilter;
|
||||
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy;
|
||||
|
||||
class SsoSecurityConfigurer {
|
||||
|
||||
private BeanFactory beanFactory;
|
||||
|
||||
public SsoSecurityConfigurer(BeanFactory beanFactory) {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
public void configure(HttpSecurity http) throws Exception {
|
||||
OAuth2SsoProperties sso = beanFactory.getBean(OAuth2SsoProperties.class);
|
||||
// Delay the processing of the filter until we know the
|
||||
// SessionAuthenticationStrategy is available:
|
||||
http.apply(new OAuth2ClientAuthenticationConfigurer(oauth2SsoFilter(sso)));
|
||||
http.exceptionHandling().authenticationEntryPoint(
|
||||
new LoginUrlAuthenticationEntryPoint(sso.getLoginPath()));
|
||||
}
|
||||
|
||||
private OAuth2ClientAuthenticationProcessingFilter oauth2SsoFilter(
|
||||
OAuth2SsoProperties sso) {
|
||||
OAuth2RestOperations restTemplate = beanFactory
|
||||
.getBean(OAuth2RestOperations.class);
|
||||
ResourceServerTokenServices tokenServices = beanFactory
|
||||
.getBean(ResourceServerTokenServices.class);
|
||||
OAuth2ClientAuthenticationProcessingFilter filter = new OAuth2ClientAuthenticationProcessingFilter(
|
||||
sso.getLoginPath());
|
||||
filter.setRestTemplate(restTemplate);
|
||||
filter.setTokenServices(tokenServices);
|
||||
return filter;
|
||||
}
|
||||
|
||||
private static class OAuth2ClientAuthenticationConfigurer extends
|
||||
SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {
|
||||
private OAuth2ClientAuthenticationProcessingFilter filter;
|
||||
|
||||
public OAuth2ClientAuthenticationConfigurer(
|
||||
OAuth2ClientAuthenticationProcessingFilter filter) {
|
||||
this.filter = filter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(HttpSecurity builder) throws Exception {
|
||||
OAuth2ClientAuthenticationProcessingFilter ssoFilter = filter;
|
||||
ssoFilter.setSessionAuthenticationStrategy(builder
|
||||
.getSharedObject(SessionAuthenticationStrategy.class));
|
||||
builder.addFilterAfter(ssoFilter,
|
||||
AbstractPreAuthenticatedProcessingFilter.class);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.security.oauth2;
|
||||
package org.springframework.boot.autoconfigure.security.oauth2.method;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||
@@ -38,7 +38,7 @@ import org.springframework.security.oauth2.provider.expression.OAuth2MethodSecur
|
||||
@Configuration
|
||||
@ConditionalOnClass({ OAuth2AccessToken.class })
|
||||
@ConditionalOnBean(GlobalMethodSecurityConfiguration.class)
|
||||
public class SpringSecurityOAuth2MethodSecurityConfiguration implements
|
||||
public class OAuth2MethodSecurityConfiguration implements
|
||||
BeanFactoryPostProcessor {
|
||||
|
||||
@Override
|
||||
@@ -24,8 +24,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
|
||||
import org.springframework.boot.autoconfigure.security.SecurityProperties;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.ClientCredentialsProperties;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.resource.SpringSecurityOAuth2ResourceServerConfiguration.ResourceServerCondition;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerConfiguration.ResourceServerCondition;
|
||||
import org.springframework.boot.bind.RelaxedPropertyResolver;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
@@ -58,7 +57,7 @@ import org.springframework.util.StringUtils;
|
||||
@ConditionalOnWebApplication
|
||||
@ConditionalOnBean(ResourceServerConfiguration.class)
|
||||
@Import(ResourceServerTokenServicesConfiguration.class)
|
||||
public class SpringSecurityOAuth2ResourceServerConfiguration {
|
||||
public class OAuth2ResourceServerConfiguration {
|
||||
|
||||
@Autowired
|
||||
private ResourceServerProperties resource;
|
||||
@@ -69,19 +68,6 @@ public class SpringSecurityOAuth2ResourceServerConfiguration {
|
||||
return new ResourceSecurityConfigurer(this.resource);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
protected static class ResourceServerPropertiesConfiguration {
|
||||
|
||||
@Autowired
|
||||
private ClientCredentialsProperties credentials;
|
||||
|
||||
@Bean
|
||||
public ResourceServerProperties resourceServerProperties() {
|
||||
return new ResourceServerProperties(this.credentials.getClientId(),
|
||||
this.credentials.getClientSecret());
|
||||
}
|
||||
}
|
||||
|
||||
protected static class ResourceSecurityConfigurer extends
|
||||
ResourceServerConfigurerAdapter {
|
||||
|
||||
@@ -51,7 +51,7 @@ org.springframework.boot.autoconfigure.reactor.ReactorAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.redis.RedisAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.security.FallbackWebSecurityAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.security.oauth2.SpringSecurityOAuth2AutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.social.SocialWebAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.social.FacebookAutoConfiguration,\
|
||||
|
||||
@@ -29,8 +29,9 @@ import org.springframework.aop.support.AopUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.authserver.SpringSecurityOAuth2AuthorizationServerConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.method.OAuth2MethodSecurityConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerProperties;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.resource.SpringSecurityOAuth2ResourceServerConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
|
||||
@@ -112,8 +113,8 @@ public class SpringSecurityOAuth2AutoConfigurationTests {
|
||||
this.context.refresh();
|
||||
|
||||
this.context.getBean(SpringSecurityOAuth2AuthorizationServerConfiguration.class);
|
||||
this.context.getBean(SpringSecurityOAuth2ResourceServerConfiguration.class);
|
||||
this.context.getBean(SpringSecurityOAuth2MethodSecurityConfiguration.class);
|
||||
this.context.getBean(OAuth2ResourceServerConfiguration.class);
|
||||
this.context.getBean(OAuth2MethodSecurityConfiguration.class);
|
||||
|
||||
ClientDetails config = this.context.getBean(BaseClientDetails.class);
|
||||
AuthorizationEndpoint endpoint = this.context
|
||||
@@ -163,7 +164,7 @@ public class SpringSecurityOAuth2AutoConfigurationTests {
|
||||
|
||||
assertThat(
|
||||
this.context
|
||||
.getBeanNamesForType(SpringSecurityOAuth2ResourceServerConfiguration.class).length,
|
||||
.getBeanNamesForType(OAuth2ResourceServerConfiguration.class).length,
|
||||
is(0));
|
||||
|
||||
assertThat(
|
||||
@@ -183,7 +184,7 @@ public class SpringSecurityOAuth2AutoConfigurationTests {
|
||||
|
||||
assertThat(
|
||||
this.context
|
||||
.getBeanNamesForType(SpringSecurityOAuth2ResourceServerConfiguration.class).length,
|
||||
.getBeanNamesForType(OAuth2ResourceServerConfiguration.class).length,
|
||||
is(1));
|
||||
|
||||
assertThat(
|
||||
@@ -216,7 +217,7 @@ public class SpringSecurityOAuth2AutoConfigurationTests {
|
||||
|
||||
assertThat(
|
||||
this.context
|
||||
.getBeanNamesForType(SpringSecurityOAuth2ResourceServerConfiguration.class).length,
|
||||
.getBeanNamesForType(OAuth2ResourceServerConfiguration.class).length,
|
||||
is(1));
|
||||
|
||||
verifyAuthentication(config);
|
||||
@@ -247,7 +248,7 @@ public class SpringSecurityOAuth2AutoConfigurationTests {
|
||||
|
||||
assertThat(
|
||||
this.context
|
||||
.getBeanNamesForType(SpringSecurityOAuth2ResourceServerConfiguration.class).length,
|
||||
.getBeanNamesForType(OAuth2ResourceServerConfiguration.class).length,
|
||||
is(1));
|
||||
|
||||
verifyAuthentication(config);
|
||||
@@ -260,7 +261,7 @@ public class SpringSecurityOAuth2AutoConfigurationTests {
|
||||
MinimalSecureWebApplication.class);
|
||||
this.context.refresh();
|
||||
|
||||
this.context.getBean(SpringSecurityOAuth2MethodSecurityConfiguration.class);
|
||||
this.context.getBean(OAuth2MethodSecurityConfiguration.class);
|
||||
|
||||
ClientDetails config = this.context.getBean(ClientDetails.class);
|
||||
|
||||
@@ -283,7 +284,7 @@ public class SpringSecurityOAuth2AutoConfigurationTests {
|
||||
MinimalSecureWebApplication.class);
|
||||
this.context.refresh();
|
||||
|
||||
this.context.getBean(SpringSecurityOAuth2MethodSecurityConfiguration.class);
|
||||
this.context.getBean(OAuth2MethodSecurityConfiguration.class);
|
||||
|
||||
ClientDetails config = this.context.getBean(ClientDetails.class);
|
||||
|
||||
@@ -306,7 +307,7 @@ public class SpringSecurityOAuth2AutoConfigurationTests {
|
||||
MinimalSecureWebApplication.class);
|
||||
this.context.refresh();
|
||||
|
||||
this.context.getBean(SpringSecurityOAuth2MethodSecurityConfiguration.class);
|
||||
this.context.getBean(OAuth2MethodSecurityConfiguration.class);
|
||||
|
||||
ClientDetails config = this.context.getBean(ClientDetails.class);
|
||||
|
||||
@@ -403,7 +404,7 @@ public class SpringSecurityOAuth2AutoConfigurationTests {
|
||||
@Import({ UseFreePortEmbeddedContainerConfiguration.class,
|
||||
SecurityAutoConfiguration.class, ServerPropertiesAutoConfiguration.class,
|
||||
DispatcherServletAutoConfiguration.class,
|
||||
SpringSecurityOAuth2AutoConfiguration.class, WebMvcAutoConfiguration.class,
|
||||
OAuth2AutoConfiguration.class, WebMvcAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class })
|
||||
protected static class MinimalSecureWebApplication {
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.ClientCredentialsProperties;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.OAuth2ClientProperties;
|
||||
import org.springframework.boot.autoconfigure.social.FacebookAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.social.SocialWebAutoConfiguration;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
@@ -139,7 +139,7 @@ public class ResourceServerTokenServicesConfigurationTests {
|
||||
@Import({ ResourceServerTokenServicesConfiguration.class,
|
||||
ResourceServerPropertiesConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class })
|
||||
@EnableConfigurationProperties(ClientCredentialsProperties.class)
|
||||
@EnableConfigurationProperties(OAuth2ClientProperties.class)
|
||||
protected static class ResourceConfiguration {
|
||||
}
|
||||
|
||||
@@ -147,7 +147,7 @@ public class ResourceServerTokenServicesConfigurationTests {
|
||||
protected static class ResourceServerPropertiesConfiguration {
|
||||
|
||||
@Autowired
|
||||
private ClientCredentialsProperties credentials;
|
||||
private OAuth2ClientProperties credentials;
|
||||
|
||||
@Bean
|
||||
public ResourceServerProperties resourceServerProperties() {
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright 2013-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.boot.autoconfigure.security.oauth2.sso;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.sso.BasicOAuth2SsoConfigurationTests.TestConfiguration;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = TestConfiguration.class)
|
||||
@WebAppConfiguration
|
||||
@TestPropertySource(properties = { "spring.oauth2.client.clientId=client",
|
||||
"spring.oauth2.client.clientSecret=secret",
|
||||
"spring.oauth2.client.authorizationUri=http://example.com/oauth/authorize",
|
||||
"spring.oauth2.client.tokenUri=http://example.com/oauth/token",
|
||||
"spring.oauth2.resource.jwt.keyValue=SSSSHHH" })
|
||||
public class BasicOAuth2SsoConfigurationTests {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext context;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("springSecurityFilterChain")
|
||||
private Filter filter;
|
||||
|
||||
private MockMvc mvc;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
mvc = MockMvcBuilders.webAppContextSetup(context).addFilters(filter).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void homePageIsSecure() throws Exception {
|
||||
mvc.perform(get("/")).andExpect(status().isFound())
|
||||
.andExpect(header().string("location", "http://localhost/login"));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Import(OAuth2AutoConfiguration.class)
|
||||
@EnableOAuth2Sso
|
||||
@MinimalSecureWebConfiguration
|
||||
protected static class TestConfiguration {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright 2013-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.boot.autoconfigure.security.oauth2.sso;
|
||||
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.sso.CustomOAuth2SsoConfigurationTests.TestConfiguration;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = TestConfiguration.class)
|
||||
@WebAppConfiguration
|
||||
@TestPropertySource(properties = { "spring.oauth2.client.clientId=client",
|
||||
"spring.oauth2.client.clientSecret=secret",
|
||||
"spring.oauth2.client.authorizationUri=http://example.com/oauth/authorize",
|
||||
"spring.oauth2.client.tokenUri=http://example.com/oauth/token",
|
||||
"spring.oauth2.resource.jwt.keyValue=SSSSHHH" })
|
||||
public class CustomOAuth2SsoConfigurationTests {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext context;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("springSecurityFilterChain")
|
||||
private Filter filter;
|
||||
|
||||
private MockMvc mvc;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
mvc = MockMvcBuilders.webAppContextSetup(context).addFilters(filter).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void homePageIsBasicAuth() throws Exception {
|
||||
mvc.perform(get("/")).andExpect(status().isUnauthorized())
|
||||
.andExpect(header().string("WWW-Authenticate", startsWith("Basic")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void uiPageIsSecure() throws Exception {
|
||||
mvc.perform(get("/ui/")).andExpect(status().isFound())
|
||||
.andExpect(header().string("location", "http://localhost/login"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void uiTestPageIsAccessible() throws Exception {
|
||||
mvc.perform(get("/ui/test")).andExpect(status().isOk())
|
||||
.andExpect(content().string("test"));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableOAuth2Sso
|
||||
@Import(OAuth2AutoConfiguration.class)
|
||||
@MinimalSecureWebConfiguration
|
||||
protected static class TestConfiguration extends WebSecurityConfigurerAdapter {
|
||||
|
||||
@Override
|
||||
public void configure(HttpSecurity http) throws Exception {
|
||||
http.antMatcher("/ui/**").authorizeRequests().antMatchers("/ui/test")
|
||||
.permitAll().anyRequest().authenticated();
|
||||
}
|
||||
|
||||
@RestController
|
||||
public static class TestController {
|
||||
|
||||
@RequestMapping(value = "/ui/test")
|
||||
public String test() {
|
||||
return "test";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.security.oauth2.sso;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
@Configuration
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Import({ EmbeddedServletContainerAutoConfiguration.class,
|
||||
ServerPropertiesAutoConfiguration.class,
|
||||
DispatcherServletAutoConfiguration.class, WebMvcAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class,
|
||||
ErrorMvcAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class,
|
||||
SecurityAutoConfiguration.class }) @interface MinimalSecureWebConfiguration {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user