Boot, OAuth2, Zuul Proxy and Versions - spring

So, i've added in Zuul and to get the proxy working realised i needs to add in 'spring-cloud-security' which i did:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-security</artifactId>
<version>1.1.0.M2</version>
</dependency>
The trouble is that as soon as i do this i get this exception
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loadBalancedOauth2RestTemplate' defined in class path resource [org/springframework/cloud/security/oauth2/client/ OAuth2LoadBalancerClientAutoConfiguration$LoadBalancedOauth2RestTemplateConfig.class]: Unsatisfied dependency expressed through constructor argument with index 2 of typ[org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails]: : No qualifying bean of type [org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
if i do make a bean i get this exception (i'm specifying the resource server info outside of props/yml):
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.security.oauth2.client.OAuth2RestTemplate] is defined: expected single matching bean but found 2: userInfoRestTemplate,loadBalancedOauth2RestTemplate
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1126) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
I've been through this previous post:
https://github.com/spring-cloud/spring-cloud-security/issues/73
I'm using Spring Boot 1.3.3.RELEASE. I've tried (form the above posting) adding in to dep management:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Brixton.M3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
but to no avail.
I expect this is a 'versions' issue; what is the most recent set of libs & versions to use? I've been through the 'oauth2-vanilla-ui' sample in the AngularJS/Security tutorial. Is it because i'm not using props in the resource file?

Ok, so i managed to figure it out. So i upped the version of 'spring-cloud-security' to '1.1.0.M4' as I noticed some of the autoconf had changed. Also, i left my 'OAuth2ProtectedResourceDetails' as a bean and added an 'OAuth2RestTemplate' bean but, crucially, marked it as primary:
#Primary
#Bean(name = "platformClientRestTemplate")
public OAuth2RestTemplate clientRestTemplate() {
return new OAuth2RestTemplate(platformOAuth2ProtectedResourceDetails(), oauth2ClientContext);
}
this means the problem of multiple RestTemplates goes away (and ensures the rest template i want gets used).
This means i'm using (for anyone with version issues):
Boot 1.3.3.RELEASE
spring-cloud-security 1.1.0.M4
spring-cloud-starter-zuul 1.1.0.M3

Related

NoSuchBeanDefinitionException `HttpSecurity` after adding `spring-restdocs-mockmvc` dependency

I have a spring boot application which have different folders for repository, service and api controller. The DB is Postgress and we are using Gradle as our build tool.
The test cases for repository were working fine, but as soon as i added test cases for controller layer (by also adding the dependency
testImplementation("org.springframework.restdocs:spring-restdocs-mockmvc:2.0.6.RELEASE")
my repository tests start to fail with the below exception
Caused by: java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:124)
at org.springframework.test.context.junit.jupiter.SpringExtension.getApplicationContext(SpringExtension.java:283)
at org.springframework.test.context.junit.jupiter.SpringExtension.resolveParameter(SpringExtension.java:269)
at org.junit.jupiter.engine.execution.ExecutableInvoker.resolveParameter(ExecutableInvoker.java:216)
... 70 more
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'managementSecurityFilterChain' defined in class path resource [org/springframework/boot/actuate/autoconfigure/security/servlet/ManagementWebSecurityAutoConfiguration.class]: Unsatisfied dependency expressed through method 'managementSecurityFilterChain' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.config.annotation.web.builders.HttpSecurity' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at app//org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800)
at app//org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:541)
at app//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352)
at app//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195)
at app//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
at app//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
at app//org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at app//org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at app//org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at app//org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at app//org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953)
at app//org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
at app//org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
at app//org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734)
at app//org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408)
at app//org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
at app//org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:132)
at app//org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
at app//org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
... 74 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.config.annotation.web.builders.HttpSecurity' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at app//org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1799)
at app//org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1355)
at app//org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1309)
at app//org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887)
at app//org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791)
... 92 more
As soon as I remove the above dependency, my repository test cases work again but I need this dependency for my controller tests. Since we don't have modules, I cannot have individual gradle files and import the dependency there. Any suggestion how can I fix this?
I was thinking about excluding the specific module from spring-restdocs-mockmvc which is causing the issue but I don't know which module is the reason.

No qualifying bean of type 'com.netflix.client.config.IClientConfig' available:expected at least 1 bean which qualifies as autowire candidate

I'm creating a loadbalancer using Netflix round ribbon but i am not able to
auto wire IClientConfig this object.
I tried this also
#SpringBootApplication(scanBasePackages{"com.netflix.client.confg.IClientConfig"})
but this is also not working for me.
I'm getting this exception
org.spring framework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.netflix.client.config.IClientConfig' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
I suspect your dependencies are not configured correctly.
Is ribbon-core added as compile dependency in build.gradle or pom.xml?
please check if you have
runtime group: 'com.netflix.ribbon', name: 'ribbon-core', version: '2.7.17'
or
<dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon-core</artifactId>
<version>2.7.17</version>
<scope>runtime</scope>
</dependency>

Integration Test Started failing post Spring boot 2.2.0 upgrade with Swagger latest version 2.9.2 & HATEOS

Integration Test Started failing post Spring boot 2.2.0 upgrade with Swagger latest version 2.9.2 & HATEOS
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'linkDiscoverers'
defined in class path resource [org/springframework/hateoas/config/HateoasConfiguration.class]:
Unsatisfied dependency expressed through method 'linkDiscoverers' parameter 0;
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type 'org.springframework.plugin.core.PluginRegistry<org.springframework.hateoas.client.LinkDiscoverer,
org.springframework.http.MediaType>' available: expected single matching bean but
found 3: relProviderPluginRegistry,linkDiscovererRegistry,entityLinksPluginRegistry
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type 'org.springframework.plugin.core.PluginRegistry<org.springframework.hateoas.client.LinkDiscoverer,
org.springframework.http.MediaType>' available: expected single matching bean but found 3:
relProviderPluginRegistry,linkDiscovererRegistry,entityLinksPluginRegistry
The issue with springfox may take while to resolve issue but there is alternative available with latest swagger v3 have similar capabilities of springfox
https://github.com/springdoc/springdoc-openapi.git
Only disabled the SpringFox dependencies, added SpringDoc, and replaced the SpringFox bean, with a SpringDoc bean. Everything is working as expected. All I need to do is to convert a couple of annotations on each rest service so SpringDoc displays everything correctly. SpringDoc does work
I used this and it works for me
<dependency>
<groupId>org.springframework.plugin</groupId>
<artifactId>spring-plugin-core</artifactId>
<version>2.0.0.RELEASE</version>
</dependency

spring boot to Tomcat deployment data source issue

I am building an application with Spring Boot, Neo4j and MongoDB. I have used #Configuration annotation to load database properties from application.properties like this:
#Bean(name = "neoDriver")
#Profile("local")
public Driver getNeoDriver() {
return GraphDatabase.driver(
env.getProperty("datasource.neo4j.uri"),
AuthTokens.basic(env.getProperty("datasource.neo4j.username"), env.getProperty("datasource.neo4j.password")),
Config.build().toConfig());
}
The autowire code is
#Autowired
#Qualifier("neoDriver")
private Driver neoDriver;
when I run the application from IntelliJ, it runs fine; but when I try deploy the war file on Tomcat 8.5, it gives error.
Field neoDriver in com......repository.PositionRepositoryImpl required a bean of type 'org.neo4j.driver.v1.Driver' that could not be found.
Caused by:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'Controller': Unsatisfied dependency expressed through field 'positionService'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'Service': Unsatisfied dependency expressed through field 'positionRepository'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'RepositoryImpl': Unsatisfied dependency expressed through field 'neoDriver'; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.neo4j.driver.v1.Driver' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations:
{#org.springframework.beans.factory.annotation.Autowired(required=true), #org.springframework.beans.factory.annotation.Qualifier(value=neoDriver)}
Please let me know what did I do wrong when deploying this on Tomcat.
I bet you're not setting spring.profiles.active to local in your Tomcat deployment. Because your bean is marked with the #Profile annotation, so it will only be created if that profile is active.

Spring MVC: No qualifying bean of type found for dependency using #component

I have this class on a jar
package com.tdk.iot;
#Component
public class LogTemplate {
..
}
this is my application context file:
<context:component-scan base-package="com.tdk.iot" />
But when I start the application I have this error:
weblogic.application.ModuleException: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.tdk.iot.LogTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
You can try to declare the bean explicitly
<bean id="logTemplate" class="org.tdk.iot.LogTemplate"/>
Also make sure that your jar is really on classpath.

Resources