User authentication failing after upgrading to 3.2.1 - spring

I just upgraded my app from Grails 3.2.0 to 3.2.1 due to some problems and the user authentication started failing. I'm using Grails Spring Security Core plugin version 3.1.1.
I'm getting the following exception:
org.springframework.security.authentication.InternalAuthenticationServiceException:
Cannot cast object 'User(email:user#example.com)' with class 'com.test.User' to class 'com.test.User'
at org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:126)
at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:144)
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:174)
at org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:94)
at grails.plugin.springsecurity.web.authentication.GrailsUsernamePasswordAuthenticationFilter.attemptAuthentication(GrailsUsernamePasswordAuthenticationFilter.groovy:53)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:212)
Caused by: org.codehaus.groovy.runtime.typehandling.GroovyCastException:
Cannot cast object 'User(email:user#example.com)' with class 'com.test.User' to class 'com.test.User'
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:405)
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:319)
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:232)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:603)
at com.test.User.findWhere(User.groovy)
at com.test.User$findWhere.call(Unknown Source)
at grails.plugin.springsecurity.userdetails.GormUserDetailsService.$tt__loadUserByUsername(GormUserDetailsService.groovy:60)
at grails.plugin.springsecurity.userdetails.GormUserDetailsService$_loadUserByUsername_closure1.doCall(GormUserDetailsService.groovy)
This is only failing when we deploy it on Apache Tomcat (using 8.5.6) but it is working fine on development with grails run-app.
Here are the modified dependencies in build.gradle (rest configuration is same as generated by create-app):
// "compile" changed to "provided"
provided "org.springframework.boot:spring-boot-starter-tomcat"
compile "org.grails.plugins:spring-security-core:3.1.1"
compile "org.grails.plugins:asynchronous-mail:2.0.0.RC4"
compile "org.mongodb:bson:3.3.0"
compile "org.codehaus.groovy.modules.http-builder:http-builder:0.7.1"
runtime "mysql:mysql-connector-java:5.1.39"
// https://github.com/spring-projects/spring-boot/issues/6761
runtime "com.google.code.gson:gson:2.5"
// Commented this to avoid issue (https://github.com/grails/grails-core/issues/10196)
//provided "org.codehaus.groovy:groovy-ant"
The same setup was working in the Grails 3.2.0.
Any idea about this exception?

Seems to be an issue with Grails 3.2.1 itself. Issue tracked grails/grails-core#10244.
Workaround is to override limitScanningToApplication in your grails-app/init/PACKAGE/Application.groovy
import grails.boot.GrailsApp
import grails.boot.config.GrailsAutoConfiguration
class Application extends GrailsAutoConfiguration {
static void main(String[] args) {
GrailsApp.run(Application, args)
}
#Override
boolean limitScanningToApplication() {
return false
}
}

Related

Java 17 gradle 7.3.3 Intellij Cannot cast org.gradle.api.file.Directory to java.io.File

I am migrating project from Java 8 to newer version.
I am not using module info.
springBootVersion=2.5.7
springCloudVersion=2020.0.5
I have an error in Intellij during build:
Cannot cast object 'property(org.gradle.api.file.Directory, undefined)' with class 'org.gradle.api.internal.file.DefaultFilePropertyFactory$DefaultDirectoryVar' to class 'java.io.File'
* Exception is:
com.intellij.openapi.externalSystem.model.ExternalSystemException: Cannot cast object 'property(org.gradle.api.file.Directory, undefined)' with class 'org.gradle.api.internal.file.DefaultFilePropertyFactory$DefaultDirectoryVar' to class 'java.io.File'
This issue occurs while using gradle-7.3.3,
when changed to gradle-7.4 issue changes to:
Cannot query the value of this property because it has no value available.
On gradle 7.3 the issue is:
class org.gradle.api.internal.file.DefaultFilePropertyFactory$DefaultDirectoryVar cannot be cast to class java.io.File (org.gradle.api.internal.file.DefaultFilePropertyFactory$DefaultDirectoryVar is in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader #6a4f787b; java.io.File is in module java.base of loader 'bootstrap')
class org.gradle.api.internal.file.DefaultFilePropertyFactory$DefaultDirectoryVar cannot be cast to class java.io.File (org.gradle.api.internal.file.DefaultFilePropertyFactory$DefaultDirectoryVar is in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader #6a4f787b; java.io.File is in module java.base of loader 'bootstrap')
I think the issue comes from this gradle block (idea part) that worked fine before:
afterEvaluate {
contracts {
baseClassForTests = "(...).contract.ContractBaseSpec"
basePackageForTests = '(...).contract.tests'
//targetFramework = TestFramework.SPOCK some other error here
testMode = 'EXPLICIT'
baseClassMappings {
baseClassMapping('.*event.*', '(...).contract.EventContractBaseSpec')
}
}
idea {
module {
testSourceDirs += project.contracts.generatedTestSourcesDir
generatedSourceDirs += project.contracts.generatedTestSourcesDir
}
}
}
After commenting it - build is ok.
Intellij 2022.1 EAP,
the same in 2021.3.2.
Java version setting in IntelliJ's Project setting does not make difference, I tried both: java 11 and java 17.
The same with build tools - gradle JVM java version setting.
As of https://docs.gradle.org/current/userguide/compatibility.html
Java 17 is compatible from gradle 7.3.
Running from terminal and from jenkins i see other issue:
sdk use java 17.0.2-open
Using java version 17.0.2-open in this shell.
./gradlew --version
------------------------------------------------------------
Gradle 7.3
------------------------------------------------------------
./gradlew clean build
Issue:
> BUG! exception in phase 'semantic analysis' in source unit '(.some-path-here...)/contract/ClientBuilder.groovy' Unsupported class file major version 61
Stacktrace: https://pastebin.pl/view/1449e795
in gradle:
jacoco {
toolVersion = "0.8.7"
}
as for this issue updating groovy from 3.0.3 to 3.0.9 fixes it and shows another issue.
Still why IntelliJ shows different issue?

spring boot jar file wont run

I have made an spring boot app and it works fine with maven but when I run it's jar file it gives an error like
java.lang.IllegalAccessException: class org.springframework.boot.loader.MainMethodRunner cannot access a member of class com.cafe2.user.UserApplication with modifiers "public static"
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:361)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:591)
at java.base/java.lang.reflect.Method.invoke(Method.java:558)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
at java.base/java.lang.Thread.run(Thread.java:829)
i have not such an error when i am running app with maven
it was really stupid the error was because of public in boot class i just add public and it works –
public class UserApplication {

Cannot run two #Input SubscribleChannels in spring-boot app

I'm developing a spring cloud kafka app with kafka streams binder. I have two #Input and one #Output as the following:
internal interface OfferChannel {
companion object {
const val CREATE_OFFER = "create-offer"
const val CREATE_OFFER_COUNTER = "create-offer-counter"
const val OFFER_CREATED = "offer-created"
}
#Input(OFFER_CREATED)
fun offerCreatedChannel(): SubscribableChannel
#Input(CREATE_OFFER_COUNTER)
fun createOfferAdminChannel(): SubscribableChannel
#Output(CREATE_OFFER)
fun createOfferChannel(): MessageChannel
}
During application startup I am getting the following error:
org.springframework.cloud.stream.binder.BinderException: Exception thrown while starting consumer:
at org.springframework.cloud.stream.binder.AbstractMessageChannelBinder.doBindConsumer(AbstractMessageChannelBinder.java:455)
at org.springframework.cloud.stream.binder.AbstractMessageChannelBinder.doBindConsumer(AbstractMessageChannelBinder.java:99)
at org.springframework.cloud.stream.binder.AbstractBinder.bindConsumer(AbstractBinder.java:144)
at org.springframework.cloud.stream.binding.BindingService.lambda$rescheduleConsumerBinding$0(BindingService.java:171)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.IllegalStateException: Only one LastSubscriberMessageHandler is allowed
at org.springframework.cloud.stream.binder.BinderErrorChannel.subscribe(BinderErrorChannel.java:44)
at org.springframework.cloud.stream.binder.AbstractMessageChannelBinder.registerErrorInfrastructure(AbstractMessageChannelBinder.java:704)
at org.springframework.cloud.stream.binder.AbstractMessageChannelBinder.registerErrorInfrastructure(AbstractMessageChannelBinder.java:627)
at org.springframework.cloud.stream.binder.kafka.KafkaMessageChannelBinder.createConsumerEndpoint(KafkaMessageChannelBinder.java:578)
at org.springframework.cloud.stream.binder.kafka.KafkaMessageChannelBinder.createConsumerEndpoint(KafkaMessageChannelBinder.java:135)
at org.springframework.cloud.stream.binder.AbstractMessageChannelBinder.doBindConsumer(AbstractMessageChannelBinder.java:402)
... 10 common frames omitted
Additionally, if I remove one of the #Input declarations the application starts successfully. Any suggestions what it going wrong here ?
Something is related to the version of the libraries.
For example my task was to switch to 2.4 kafka client with org.springframework.boot version 2.2.3.RELEASE. Firstly I updated to the latest 2.2.10.RELEASE version, you would say it's not a big deal since it's minor version change, well it appears that 2.2.10.RELEASE caused this bug and I had to revert it to 2.2.9.RELEASE.
Next step for my kafka version increase was to follow this tutorial:
https://docs.spring.io/spring-kafka/reference/html/#update-deps
So I set implementation "org.springframework.kafka:spring-kafka:2.4.9.RELEASE", well guess what? 2.4.9.RELEASE also caused the same problem, so I downgraded it to 2.4.8.RELEASE.
My working config (not using latest minor versions):
plugins {
id 'org.springframework.boot' version '2.2.9.RELEASE'
}
ext {
springKafkaVersion = '2.4.8.RELEASE'
kafkaVersion = '2.4.1'
}
dependencies {
implementation "org.springframework.kafka:spring-kafka:$springKafkaVersion"
implementation "org.apache.kafka:kafka-clients:$kafkaVersion"
implementation "org.apache.kafka:kafka-streams:$kafkaVersion"
testImplementation "org.springframework.kafka:spring-kafka-test:$springKafkaVersion"
testImplementation "org.apache.kafka:kafka-clients:$kafkaVersion:test"
}
So the suggestion is to look at your library versions and try to upgrade/downgrade accordingly.
I have the same error when updating spring boot version from 2.2.9 to 2.2.10, current work around is to use the old version. You can check your spring boot version and have a try

Spring Boot Application error: Exception in thread "main" java.lang.NoSuchMethodError

Why i'm getting this error while running simple SpringBoot Application.
Exception in thread "main" java.lang.NoSuchMethodError:
org.springframework.boot.SpringApplication.run(Ljava/lang/Class;[Ljava/lang/String;)Lorg/springframework/context/ConfigurableApplicationContext;
at
io.javabrain.springbootstarter.CourseApiApp.main(CourseApiApp.java:10)
package io.javabrain.springbootstarter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class CourseApiApp {
public static void main(String[] args) {
SpringApplication.run(CourseApiApp.class, args);
}
}
It should be running successfully
For gradle project in gradle\wrapper\gradle-wrapper.properties file change distributionUrl variable to:
distributionUrl=https://services.gradle.org/distributions/gradle-4.7-bin.zip
If it is don't helps download project from https://start.spring.io/ and do the same.
If you use maven, just download maven project from https://start.spring.io/ . It should work fine.

Resource injection in Spring Boot not working with gradle

I have a Configuration bean.
#Component
public class Config {
#Value("classpath:${app.file.name.srgbicc}")
public Resource icc;
#PostConstruct
void init(){
try {
tempdir = Files.createTempDir();
newIcc = copyIccFileToTemp();
}catch (IOException e){
throw new RuntimeException(e);
}
}
private File copyIccFileToTemp() throws IOException {
File tempIcc = new File(tempdir, icc.getFilename());
FileUtils.copyFile(icc.getFile(),tempIcc);
return tempIcc;
}
}
On icc.getFile() there is a FileNotFoundException
application.properties
app.file.name.srgbicc = sRGB.icc
I looked in my classpath and found the following situation:
build/classes/main (no icc file)
build/resources/main (icc file, application.properties)
When printed out my classpath during application start I only found ...myApp/build/classes/main.
No ../build/resources/main or ../src/main/resources entries there.
So I was wondering why is the resources not on the classpath?
according to http://docs.spring.io/spring-boot/docs/1.1.5.RELEASE/reference/htmlsingle/#build-tool-plugins-gradle-running-applications
this should be.
Of course if I put the icc file in build/classes/main its all working as expected, but it is not supposed to be there. right?
I tried to run the application with gradle run or gradle bootRun in intellij I use static main. The good thing is that the application fails consistently independent of how I start it.
My Project layout
myApp
src
main
java
pkg
Config.java
resources
sRGB.icc
application.properties
For the reference:
IntelliJ 13
spring-boot 1.1.5
Fgradle 2.0
Update
Here is a stripped down example of my code
https://gist.github.com/Vad1mo/bb5d23ca251341236fbd
I removed #PostConstruct in the config.init and run the application with gradle build all test are success when i rund from intellij the tests fail. this is strange that i know have different behavior
I solved the problem now.
What helped me was just to do a simple clean build and rebuild project in intellij. I was using to much eclipse and maven so I expected it to happen automagically.

Resources