EnableWebFluxSecurity contadiction with web dependency - spring-security

I am trying to annotate SecurityWebFilterChain class with #EnableWebFluxSecurity :
#EnableWebFluxSecurity
public class AccessIqWebSecurityConfigurerAdapter {
I am getting the following error while boot the app:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name
'org.springframework.security.config.annotation.web.reactive.WebFluxSecurityConfiguration':
Unsatisfied dependency expressed through field
'securityWebFilterChains'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'springSecurityFilterChain' defined in class
path resource
[com/config/AccessIqWebSecurityConfigurerAdapter.class]:
Bean instantiation via factory method failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to
instantiate
[org.springframework.security.web.server.SecurityWebFilterChain]:
Factory method 'springSecurityFilterChain' threw exception; nested
exception is java.lang.IllegalArgumentException:
clientRegistrationRepository cannot be null
When I remove compile("org.springframework.boot:spring-boot-starter-web"). It works. but I need compile("org.springframework.boot:spring-boot-starter-web") for swagger, Mockmvc etc. any idea?

As stated in the reference documentation, adding the spring-boot-starter-web dependency to your application will tell Spring Boot to configure it as a Spring MVC application.
You could give another hint to Spring Boot to make it still a Spring WebFlux application (through SpringApplication.setWebApplicationType(WebApplicationType.REACTIVE)), but it wouldn't help in your case:
You have to use a swagger integration that's compatible with WebFlux (I don't know if that exists)
You should consider WebTestClient instead of MockMvc

Related

ClassCastException for MappingFactory to ToMany while porting to grails 3

When trying to run command grails run-app --stacktrace, grails is throwing this error :
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is java.lang.ClassCastException: org.grails.datastore.mapping.model.MappingFactory$10 cannot be cast to org.grails.datastore.mapping.model.types.ToMany
I commented static hasMany from all the domain classes but error still persisted, until I replaced it with hibernate3 in my build.gradle
How can I solve this using hibernate4?
I found the solution, actually it had to do with embedded grails domain property.
As #deepen mention, this error is related with the mapping setup of the embedded domain classes. In my case, I was using multiples databases (mongodb gorm 6.0.12 + jtds (sql server)), and some of the documents had embedded properties (defined in the same class).
DomainA.groovy
class DomainA
static hasMany = [domainBs: DomainB]
static embedded = ['domainBs']
static mapWith = "mongo"
class DomainB
// if embedded: what's the point of this?
static belongsTo = [domainB: DomainA]
// if i'll use it as embedded, doesn't add nothing
static mapWith = "mongo"
In short, if I comment belongsTo and mapWith in the definition of the embedded class, the issue was solved. If not, must remember to mapWith embedded class with same datasource as container class.

Grails upgrade from 2.5.1 to 3.1.4

While migrating my grails 2 application to grails 3 I am facing some weird situation as:
I have two domain named Service.groovy and Test.groovy. Test domain has hasMany relation with Service e.g static hasMany = [services: Service].
Now while running the application it throws exception as
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'grailsApplicationPostProcessor' defined in
testproject.Application: Bean instantiation via factory method failed;
nested exception is
org.springframework.beans.BeanInstantiationException: Failed to
instantiate [grails.boot.config.GrailsApplicationPostProcessor]:
Factory method 'grailsApplicationPostProcessor' threw exception;
nested exception is java.lang.ClassCastException: com.test.Service
cannot be cast to java.lang.Class
I have created test project in grails 3 and it seems like new grails is not excepting Service.groovy as domain.
Please Kindly suggest.

gorm Inheritance causes DuplicateMappingException

My webapp behaves strangely when a child class is reloaded
package test
class Test {
Integer amount
//static mapping = {
// tablePerHierarchy true
// autoImport false
//}
}
and the child class
package test
class Test1 extends Test{
String cardNumber
}
new table is created after the parent class is reloaded
but grails console throws error if I reload child class
ERROR org.grails.plugins.AbstractGrailsPluginManager - Plugin [domainClass:3.0.9] could not reload changes to file [/home/gkiko/workspace/test/grails-app/domain/test/Test1.groovy]: Error creating bean with name 'org.grails.internal.SESSION_FACTORY_HOLDER-reloaded': Cannot create inner bean '(inner bean)#d22a35c' of type [org.grails.orm.hibernate.ConfigurableLocalSessionFactoryBean] while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#d22a35c': Invocation of init method failed; nested exception is org.hibernate.DuplicateMappingException: Duplicate class/entity mapping test.Test1
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.grails.internal.SESSION_FACTORY_HOLDER-reloaded': Cannot create inner bean '(inner bean)#d22a35c' of type [org.grails.orm.hibernate.ConfigurableLocalSessionFactoryBean] while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#d22a35c': Invocation of init method failed; nested exception is org.hibernate.DuplicateMappingException: Duplicate class/entity mapping test.Test1
.... some more stacktrace
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#d22a35c': Invocation of init method failed; nested exception is org.hibernate.DuplicateMappingException: Duplicate class/entity mapping test.Test1
How can I fix the problem?
Grails Version: 3.0.11
Groovy Version: 2.4.5
JVM Version: 1.7.0_91
You should restart your application if you are making any changes to your domain class. Although it is working fine in case you are modifying the parent class, but still the dynamic domain entities creation feature doesn't work perfectly.

Spring boot #ConditionalOnBean does not detect bean missing, throws exception "No qualifying bean of type"

Have this #Configuration class that requires a bean implementing JmsProperties which is declared in the #ConditionalOnBean
#Configuration
#ConditionalOnBean(JmsProperties.class)
public class JmsConfiguration {
#Inject
private JmsProperties properties;
...
}
Getting exception:
Caused by:
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [com.....JmsProperties] found for dependency:
Isn't the #ConditionalOnBean supposed to detect this missing bean and not try to inject the dependency in the first place?
Thanks in advance for any pointers
You've cross-posted on the Spring Boot tracker and the developers responded:
#ConditionalOnBean is evaluated after all configuration classes have been processed, i.e you can't use it to make a whole configuration class conditional on the presence of another bean. You can, however, use it where you have to make all of the configuration's beans conditional on the presence of another bean.
You must use #ComponentScan(basePackages="".
Without using it your app wont discover & register/instantiate all defined beans and hence you would end up with this type of exception i.e. No bean def found.

Grails-The different dependence Injection ways in grails

What's the difference of different way to inject dependence in grails:
ABCService abcService
def abcService
#Autowired
ABCService ABCService
Expected type is specified, if service with name abcService (or other Spring bean with such name) will have different class, then you'll get ClassCastException here
Just any bean with name abcService
Spring annotation, it's optional. But if you've marked a field but Grails/Spring cannot find such bean it will throw NoSuchBeanDefinitionException (previous two will get null if it doesn't exists) #Autowired could be combined with def type also
Basically Grails services are standard Spring beans, Grails just follows convention over configuration that for every class in services dir it will create a bean with name abcService that could be autowired into other beans. All other job is done by Spring. See also docs for Spring and Grails

Resources