use of org.springframework.beans.factory.config.RuntimeBeanReference in project spring-security-oauth by spring-projects.
the class AuthorizationServerBeanDefinitionParser method parseEndpointAndReturnFilter.
@Override
protected AbstractBeanDefinition parseEndpointAndReturnFilter(Element element, ParserContext parserContext, String tokenServicesRef, String serializerRef) {
String clientDetailsRef = element.getAttribute("client-details-service-ref");
String oAuth2RequestFactoryRef = element.getAttribute("authorization-request-manager-ref");
String tokenEndpointUrl = element.getAttribute("token-endpoint-url");
String checkTokenUrl = element.getAttribute("check-token-endpoint-url");
String enableCheckToken = element.getAttribute("check-token-enabled");
String authorizationEndpointUrl = element.getAttribute("authorization-endpoint-url");
String tokenGranterRef = element.getAttribute("token-granter-ref");
String redirectStrategyRef = element.getAttribute("redirect-strategy-ref");
String userApprovalHandlerRef = element.getAttribute("user-approval-handler-ref");
String approvalPage = element.getAttribute("user-approval-page");
String errorPage = element.getAttribute("error-page");
String approvalParameter = element.getAttribute("approval-parameter-name");
String redirectResolverRef = element.getAttribute("redirect-resolver-ref");
String oAuth2RequestValidatorRef = element.getAttribute("request-validator-ref");
// Create a bean definition speculatively for the auth endpoint
BeanDefinitionBuilder authorizationEndpointBean = BeanDefinitionBuilder.rootBeanDefinition(AuthorizationEndpoint.class);
if (!StringUtils.hasText(clientDetailsRef)) {
parserContext.getReaderContext().error("ClientDetailsService must be provided", element);
return null;
}
if (!StringUtils.hasText(oAuth2RequestValidatorRef)) {
oAuth2RequestValidatorRef = "defaultOAuth2RequestValidator";
BeanDefinitionBuilder oAuth2RequestValidator = BeanDefinitionBuilder.rootBeanDefinition(DefaultOAuth2RequestValidator.class);
parserContext.getRegistry().registerBeanDefinition(oAuth2RequestValidatorRef, oAuth2RequestValidator.getBeanDefinition());
}
authorizationEndpointBean.addPropertyReference("oAuth2RequestValidator", oAuth2RequestValidatorRef);
if (!StringUtils.hasText(oAuth2RequestFactoryRef)) {
oAuth2RequestFactoryRef = "oAuth2AuthorizationRequestManager";
BeanDefinitionBuilder oAuth2RequestManager = BeanDefinitionBuilder.rootBeanDefinition(DefaultOAuth2RequestFactory.class);
oAuth2RequestManager.addConstructorArgReference(clientDetailsRef);
parserContext.getRegistry().registerBeanDefinition(oAuth2RequestFactoryRef, oAuth2RequestManager.getBeanDefinition());
}
ManagedList<BeanMetadataElement> tokenGranters = null;
if (!StringUtils.hasText(tokenGranterRef)) {
tokenGranterRef = "oauth2TokenGranter";
BeanDefinitionBuilder tokenGranterBean = BeanDefinitionBuilder.rootBeanDefinition(CompositeTokenGranter.class);
parserContext.getRegistry().registerBeanDefinition(tokenGranterRef, tokenGranterBean.getBeanDefinition());
tokenGranters = new ManagedList<BeanMetadataElement>();
tokenGranterBean.addConstructorArgValue(tokenGranters);
}
authorizationEndpointBean.addPropertyReference("tokenGranter", tokenGranterRef);
boolean registerAuthorizationEndpoint = false;
Element authorizationCodeElement = DomUtils.getChildElementByTagName(element, "authorization-code");
if (authorizationCodeElement != null && !"true".equalsIgnoreCase(authorizationCodeElement.getAttribute("disabled"))) {
// authorization code grant configuration.
String authorizationCodeServices = authorizationCodeElement.getAttribute("authorization-code-services-ref");
String clientTokenCacheRef = authorizationCodeElement.getAttribute("client-token-cache-ref");
BeanDefinitionBuilder authorizationCodeTokenGranterBean = BeanDefinitionBuilder.rootBeanDefinition(AuthorizationCodeTokenGranter.class);
if (StringUtils.hasText(tokenServicesRef)) {
authorizationCodeTokenGranterBean.addConstructorArgReference(tokenServicesRef);
}
if (!StringUtils.hasText(authorizationCodeServices)) {
authorizationCodeServices = "oauth2AuthorizationCodeServices";
BeanDefinitionBuilder authorizationCodeServicesBean = BeanDefinitionBuilder.rootBeanDefinition(InMemoryAuthorizationCodeServices.class);
parserContext.getRegistry().registerBeanDefinition(authorizationCodeServices, authorizationCodeServicesBean.getBeanDefinition());
}
authorizationEndpointBean.addPropertyReference("authorizationCodeServices", authorizationCodeServices);
authorizationCodeTokenGranterBean.addConstructorArgReference(authorizationCodeServices);
authorizationCodeTokenGranterBean.addConstructorArgReference(clientDetailsRef);
authorizationCodeTokenGranterBean.addConstructorArgReference(oAuth2RequestFactoryRef);
if (StringUtils.hasText(clientTokenCacheRef)) {
authorizationEndpointBean.addPropertyReference("clientTokenCache", clientTokenCacheRef);
}
if (StringUtils.hasText(oAuth2RequestFactoryRef)) {
authorizationEndpointBean.addPropertyReference("oAuth2RequestFactory", oAuth2RequestFactoryRef);
}
if (tokenGranters != null) {
tokenGranters.add(authorizationCodeTokenGranterBean.getBeanDefinition());
}
// end authorization code provider configuration.
registerAuthorizationEndpoint = true;
}
if (tokenGranters != null) {
Element refreshTokenElement = DomUtils.getChildElementByTagName(element, "refresh-token");
if (refreshTokenElement != null && !"true".equalsIgnoreCase(refreshTokenElement.getAttribute("disabled"))) {
BeanDefinitionBuilder refreshTokenGranterBean = BeanDefinitionBuilder.rootBeanDefinition(RefreshTokenGranter.class);
refreshTokenGranterBean.addConstructorArgReference(tokenServicesRef);
refreshTokenGranterBean.addConstructorArgReference(clientDetailsRef);
refreshTokenGranterBean.addConstructorArgReference(oAuth2RequestFactoryRef);
tokenGranters.add(refreshTokenGranterBean.getBeanDefinition());
}
Element implicitElement = DomUtils.getChildElementByTagName(element, "implicit");
if (implicitElement != null && !"true".equalsIgnoreCase(implicitElement.getAttribute("disabled"))) {
BeanDefinitionBuilder implicitGranterBean = BeanDefinitionBuilder.rootBeanDefinition(ImplicitTokenGranter.class);
implicitGranterBean.addConstructorArgReference(tokenServicesRef);
implicitGranterBean.addConstructorArgReference(clientDetailsRef);
implicitGranterBean.addConstructorArgReference(oAuth2RequestFactoryRef);
tokenGranters.add(implicitGranterBean.getBeanDefinition());
registerAuthorizationEndpoint = true;
}
Element clientCredentialsElement = DomUtils.getChildElementByTagName(element, "client-credentials");
if (clientCredentialsElement != null && !"true".equalsIgnoreCase(clientCredentialsElement.getAttribute("disabled"))) {
BeanDefinitionBuilder clientCredentialsGranterBean = BeanDefinitionBuilder.rootBeanDefinition(ClientCredentialsTokenGranter.class);
clientCredentialsGranterBean.addConstructorArgReference(tokenServicesRef);
clientCredentialsGranterBean.addConstructorArgReference(clientDetailsRef);
clientCredentialsGranterBean.addConstructorArgReference(oAuth2RequestFactoryRef);
tokenGranters.add(clientCredentialsGranterBean.getBeanDefinition());
}
Element clientPasswordElement = DomUtils.getChildElementByTagName(element, "password");
if (clientPasswordElement != null && !"true".equalsIgnoreCase(clientPasswordElement.getAttribute("disabled"))) {
BeanDefinitionBuilder clientPasswordTokenGranter = BeanDefinitionBuilder.rootBeanDefinition(ResourceOwnerPasswordTokenGranter.class);
String authenticationManagerRef = clientPasswordElement.getAttribute("authentication-manager-ref");
if (!StringUtils.hasText(authenticationManagerRef)) {
authenticationManagerRef = BeanIds.AUTHENTICATION_MANAGER;
}
clientPasswordTokenGranter.addConstructorArgReference(authenticationManagerRef);
clientPasswordTokenGranter.addConstructorArgReference(tokenServicesRef);
clientPasswordTokenGranter.addConstructorArgReference(clientDetailsRef);
clientPasswordTokenGranter.addConstructorArgReference(oAuth2RequestFactoryRef);
tokenGranters.add(clientPasswordTokenGranter.getBeanDefinition());
}
List<Element> customGrantElements = DomUtils.getChildElementsByTagName(element, "custom-grant");
for (Element customGrantElement : customGrantElements) {
if (!"true".equalsIgnoreCase(customGrantElement.getAttribute("disabled"))) {
String customGranterRef = customGrantElement.getAttribute("token-granter-ref");
tokenGranters.add(new RuntimeBeanReference(customGranterRef));
}
}
}
if (registerAuthorizationEndpoint) {
BeanDefinitionBuilder approvalEndpointBean = BeanDefinitionBuilder.rootBeanDefinition(WhitelabelApprovalEndpoint.class);
parserContext.getRegistry().registerBeanDefinition("oauth2ApprovalEndpoint", approvalEndpointBean.getBeanDefinition());
if (!StringUtils.hasText(clientDetailsRef)) {
parserContext.getReaderContext().error("A client details service is mandatory", element);
}
if (StringUtils.hasText(redirectStrategyRef)) {
authorizationEndpointBean.addPropertyReference("redirectStrategy", redirectStrategyRef);
}
if (StringUtils.hasText(userApprovalHandlerRef)) {
authorizationEndpointBean.addPropertyReference("userApprovalHandler", userApprovalHandlerRef);
}
authorizationEndpointBean.addPropertyReference("clientDetailsService", clientDetailsRef);
if (StringUtils.hasText(redirectResolverRef)) {
authorizationEndpointBean.addPropertyReference("redirectResolver", redirectResolverRef);
}
if (StringUtils.hasText(approvalPage)) {
authorizationEndpointBean.addPropertyValue("userApprovalPage", approvalPage);
}
if (StringUtils.hasText(errorPage)) {
authorizationEndpointBean.addPropertyValue("errorPage", errorPage);
}
parserContext.getRegistry().registerBeanDefinition("oauth2AuthorizationEndpoint", authorizationEndpointBean.getBeanDefinition());
}
// configure the token endpoint
BeanDefinitionBuilder tokenEndpointBean = BeanDefinitionBuilder.rootBeanDefinition(TokenEndpoint.class);
tokenEndpointBean.addPropertyReference("clientDetailsService", clientDetailsRef);
tokenEndpointBean.addPropertyReference("tokenGranter", tokenGranterRef);
authorizationEndpointBean.addPropertyReference("oAuth2RequestValidator", oAuth2RequestValidatorRef);
parserContext.getRegistry().registerBeanDefinition("oauth2TokenEndpoint", tokenEndpointBean.getBeanDefinition());
if (StringUtils.hasText(oAuth2RequestFactoryRef)) {
tokenEndpointBean.addPropertyReference("oAuth2RequestFactory", oAuth2RequestFactoryRef);
}
if (StringUtils.hasText(oAuth2RequestValidatorRef)) {
tokenEndpointBean.addPropertyReference("oAuth2RequestValidator", oAuth2RequestValidatorRef);
}
// Register a handler mapping that can detect the auth server endpoints
BeanDefinitionBuilder handlerMappingBean = BeanDefinitionBuilder.rootBeanDefinition(FrameworkEndpointHandlerMapping.class);
ManagedMap<String, TypedStringValue> mappings = new ManagedMap<String, TypedStringValue>();
if (StringUtils.hasText(tokenEndpointUrl) || StringUtils.hasText(authorizationEndpointUrl)) {
if (StringUtils.hasText(tokenEndpointUrl)) {
mappings.put("/oauth/token", new TypedStringValue(tokenEndpointUrl, String.class));
}
if (StringUtils.hasText(authorizationEndpointUrl)) {
mappings.put("/oauth/authorize", new TypedStringValue(authorizationEndpointUrl, String.class));
}
if (StringUtils.hasText(approvalPage)) {
mappings.put("/oauth/confirm_access", new TypedStringValue(approvalPage, String.class));
}
}
if (StringUtils.hasText(enableCheckToken) && enableCheckToken.equals("true")) {
// configure the check token endpoint
BeanDefinitionBuilder checkTokenEndpointBean = BeanDefinitionBuilder.rootBeanDefinition(CheckTokenEndpoint.class);
checkTokenEndpointBean.addConstructorArgReference(tokenServicesRef);
parserContext.getRegistry().registerBeanDefinition("oauth2CheckTokenEndpoint", checkTokenEndpointBean.getBeanDefinition());
if (StringUtils.hasText(checkTokenUrl)) {
mappings.put("/oauth/check_token", new TypedStringValue(checkTokenUrl, String.class));
}
}
if (!mappings.isEmpty()) {
handlerMappingBean.addPropertyValue("mappings", mappings);
}
if (StringUtils.hasText(approvalParameter) && registerAuthorizationEndpoint) {
if (!StringUtils.hasText(userApprovalHandlerRef)) {
BeanDefinitionBuilder userApprovalHandler = BeanDefinitionBuilder.rootBeanDefinition(DefaultUserApprovalHandler.class);
userApprovalHandler.addPropertyValue("approvalParameter", new TypedStringValue(approvalParameter, String.class));
authorizationEndpointBean.addPropertyValue("userApprovalHandler", userApprovalHandler.getBeanDefinition());
}
handlerMappingBean.addPropertyValue("approvalParameter", approvalParameter);
}
parserContext.getRegistry().registerBeanDefinition("oauth2HandlerMapping", handlerMappingBean.getBeanDefinition());
// We aren't defining a filter...
return null;
}
use of org.springframework.beans.factory.config.RuntimeBeanReference in project spring-framework by spring-projects.
the class GroovyBeanDefinitionReader method invokeBeanDefiningMethod.
/**
* This method is called when a bean definition node is called.
* @param beanName the name of the bean to define
* @param args the arguments to the bean. The first argument is the class name, the last
* argument is sometimes a closure. All the arguments in between are constructor arguments.
* @return the bean definition wrapper
*/
private GroovyBeanDefinitionWrapper invokeBeanDefiningMethod(String beanName, Object[] args) {
boolean hasClosureArgument = (args[args.length - 1] instanceof Closure);
if (args[0] instanceof Class) {
Class<?> beanClass = (Class<?>) args[0];
if (args.length >= 1) {
if (hasClosureArgument) {
if (args.length - 1 != 1) {
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, beanClass, resolveConstructorArguments(args, 1, args.length - 1));
} else {
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, beanClass);
}
} else {
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, beanClass, resolveConstructorArguments(args, 1, args.length));
}
}
} else if (args[0] instanceof RuntimeBeanReference) {
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName);
this.currentBeanDefinition.getBeanDefinition().setFactoryBeanName(((RuntimeBeanReference) args[0]).getBeanName());
} else if (args[0] instanceof Map) {
// named constructor arguments
if (args.length > 1 && args[1] instanceof Class) {
List constructorArgs = resolveConstructorArguments(args, 2, hasClosureArgument ? args.length - 1 : args.length);
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, (Class) args[1], constructorArgs);
Map namedArgs = (Map) args[0];
for (Object o : namedArgs.keySet()) {
String propName = (String) o;
setProperty(propName, namedArgs.get(propName));
}
} else // factory method syntax
{
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName);
//First arg is the map containing factoryBean : factoryMethod
Map.Entry factoryBeanEntry = (Map.Entry) ((Map) args[0]).entrySet().iterator().next();
// If we have a closure body, that will be the last argument.
// In between are the constructor args
int constructorArgsTest = hasClosureArgument ? 2 : 1;
// If we have more than this number of args, we have constructor args
if (args.length > constructorArgsTest) {
// factory-method requires args
int endOfConstructArgs = (hasClosureArgument ? args.length - 1 : args.length);
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, null, resolveConstructorArguments(args, 1, endOfConstructArgs));
} else {
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName);
}
this.currentBeanDefinition.getBeanDefinition().setFactoryBeanName(factoryBeanEntry.getKey().toString());
this.currentBeanDefinition.getBeanDefinition().setFactoryMethodName(factoryBeanEntry.getValue().toString());
}
} else if (args[0] instanceof Closure) {
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName);
this.currentBeanDefinition.getBeanDefinition().setAbstract(true);
} else {
List constructorArgs = resolveConstructorArguments(args, 0, hasClosureArgument ? args.length - 1 : args.length);
currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, null, constructorArgs);
}
if (hasClosureArgument) {
Closure callable = (Closure) args[args.length - 1];
callable.setDelegate(this);
callable.setResolveStrategy(Closure.DELEGATE_FIRST);
callable.call(new Object[] { currentBeanDefinition });
}
GroovyBeanDefinitionWrapper beanDefinition = currentBeanDefinition;
this.currentBeanDefinition = null;
beanDefinition.getBeanDefinition().setAttribute(GroovyBeanDefinitionWrapper.class.getName(), beanDefinition);
getRegistry().registerBeanDefinition(beanName, beanDefinition.getBeanDefinition());
return beanDefinition;
}
use of org.springframework.beans.factory.config.RuntimeBeanReference in project spring-framework by spring-projects.
the class GroovyBeanDefinitionReader method manageMapIfNecessary.
/**
* Checks whether there are any {@link RuntimeBeanReference}s inside the {@link Map}
* and converts it to a {@link ManagedMap} if necessary.
* @param map the original Map
* @return either the original map or a managed copy of it
*/
private Object manageMapIfNecessary(Map<?, ?> map) {
boolean containsRuntimeRefs = false;
for (Object element : map.values()) {
if (element instanceof RuntimeBeanReference) {
containsRuntimeRefs = true;
break;
}
}
if (containsRuntimeRefs) {
Map<Object, Object> managedMap = new ManagedMap<>();
managedMap.putAll(map);
return managedMap;
}
return map;
}
use of org.springframework.beans.factory.config.RuntimeBeanReference in project spring-framework by spring-projects.
the class GroovyBeanDefinitionReader method invokeMethod.
// INTERNAL HANDLING OF GROOVY CLOSURES AND PROPERTIES
/**
* This method overrides method invocation to create beans for each method name that
* takes a class argument.
*/
public Object invokeMethod(String name, Object arg) {
Object[] args = (Object[]) arg;
if ("beans".equals(name) && args.length == 1 && args[0] instanceof Closure) {
return beans((Closure) args[0]);
} else if ("ref".equals(name)) {
String refName;
if (args[0] == null)
throw new IllegalArgumentException("Argument to ref() is not a valid bean or was not found");
if (args[0] instanceof RuntimeBeanReference) {
refName = ((RuntimeBeanReference) args[0]).getBeanName();
} else {
refName = args[0].toString();
}
boolean parentRef = false;
if (args.length > 1) {
if (args[1] instanceof Boolean) {
parentRef = (Boolean) args[1];
}
}
return new RuntimeBeanReference(refName, parentRef);
} else if (this.namespaces.containsKey(name) && args.length > 0 && args[0] instanceof Closure) {
GroovyDynamicElementReader reader = createDynamicElementReader(name);
reader.invokeMethod("doCall", args);
} else if (args.length > 0 && args[0] instanceof Closure) {
// abstract bean definition
return invokeBeanDefiningMethod(name, args);
} else if (args.length > 0 && (args[0] instanceof Class || args[0] instanceof RuntimeBeanReference || args[0] instanceof Map)) {
return invokeBeanDefiningMethod(name, args);
} else if (args.length > 1 && args[args.length - 1] instanceof Closure) {
return invokeBeanDefiningMethod(name, args);
}
MetaClass mc = DefaultGroovyMethods.getMetaClass(getRegistry());
if (!mc.respondsTo(getRegistry(), name, args).isEmpty()) {
return mc.invokeMethod(getRegistry(), name, args);
}
return this;
}
use of org.springframework.beans.factory.config.RuntimeBeanReference in project spring-framework by spring-projects.
the class DefaultListableBeanFactoryTests method testSelfReference.
@Test
public void testSelfReference() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("spouse", new RuntimeBeanReference("self"));
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setPropertyValues(pvs);
lbf.registerBeanDefinition("self", bd);
TestBean self = (TestBean) lbf.getBean("self");
assertEquals(self, self.getSpouse());
}
Aggregations