• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

SpringBoot Security JDBC Authentication Faillure  RSS feed

 
Master Rancher
Posts: 920
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys

Me again ;
I'm having some trouble getting my Spring security to work.

My WebsecurityConfig:

Role Entity:

User Entity:


The stacktrace:

These are the queries I use:

Before I was using this for the roles:

but it gave me the same error.
Index out of range is the same as an index out of bound right? I don't have any ideas as of what is causing it...
 
Sheriff
Posts: 21556
100
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is documented in Spring in Action 5th edition, chapter 4.2.2. It describes how JDBC authentication works, as well as the default queries. These are defined in JdbcDaoImpl as the following constants:
ConstantValue
DEF_USERS_BY_USERNAME_QUERYselect username,password,enabled from users where username = ?
DEF_AUTHORITIES_BY_USERNAME_QUERYselect username,authority from authorities where username = ?
DEF_GROUP_AUTHORITIES_BY_USERNAME_QUERYselect g.id, g.group_name, ga.authority from groups g, group_members gm, group_authorities ga where gm.username = ? and g.id = ga.group_id and g.id = gm.group_id

As you can see, the users query expects (at least) 3 columns: the username, the password, and whether or not the user is enabled. You provide only the first two. You need to return the enabled flag as well; you can use 1 if you don't store this information:
 
Daniel Demesmaecker
Master Rancher
Posts: 920
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did as you suggested that took care of the error, but when trying to log in, nothing happens, I also can't see on the console that the querry is being executed...
 
Rob Spoor
Sheriff
Posts: 21556
100
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you try to put a breakpoint in class JdcbDaoImpl, method loadUserByUsername? That way you can see what query is being executed. If it doesn't even get there, then the problem is somewhere else, and not related to your queries (as they are not even used).
 
Daniel Demesmaecker
Master Rancher
Posts: 920
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where am I supposed to find that class? I never tried debugging a class that isn't written by myself.
My guess would be in my lib?
I searched for the class an according to git the path should be spring-security/core/src/main/java/org/springframework/security/core/userdetails/jdbc/JdbcDaoImpl.java, but I can't find that class...
But the error before makes me suspect that everything should be working. After I maded the changes you suggested, I first got an other error, my instance variable is called userName, i forget to change the tablename in my db, so it got saved as user_name, for some reasson the querry couldn't render that, but I dropped my db and changed the tablename to username.


B.T.W.: For everyone @ The CoderRanch a Happy New Year and best wishes for 2019
 
Rob Spoor
Sheriff
Posts: 21556
100
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on your IDE how you can open a class from one of your dependencies. In Eclipse you'd use CTRL+SHIFT+T, I think IntelliJ uses CTRL+N. Your IDE should take care of downloading the Spring sources as well, so it can show them instead of the class file.
 
Daniel Demesmaecker
Master Rancher
Posts: 920
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Opening isnt the prob, but to open it you First have to be able to locate it. It should be in my externe libary right? Or should i check my .m2 folder? What if it isn't there, my dependencies have bin downloaded automaticly so reimporting them would do  no good. Should I putt it manually on the classpath?
I used jdbc authentication before and never had those issues...
 
Rob Spoor
Sheriff
Posts: 21556
100
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should be in .m2 if you've added it as a maven dependency.
 
Daniel Demesmaecker
Master Rancher
Posts: 920
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't for the love of god find that class, it should be in org.springframework.security.core.userdetails.jdbc
but for me org.springframework.security.core.userdetails dosn't have a jdbc folder.
When I start up the application, I can see that my webConfig is being used

but somehow my security queries aren't executed. What's weird, when there is an error in the queries, I'm being warned so they are being read...
 
Rob Spoor
Sheriff
Posts: 21556
100
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Odd, I can find the class without problems. The class it's found in is <home>\.m2\repository\org\springframework\security\spring-security-core\5.0.8.RELEASE\spring-security-core-5.0.8.RELEASE.jar. And I've found it as far back as 3.2.5.RELEASE so it just should be there.
 
Daniel Demesmaecker
Master Rancher
Posts: 920
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not using springSecurity but springBoot.starter.security
I can see the jarfiles in the path you sugested, but can't explore them in my explorer, shouldn't they also be in the external libary folder of my project?
When I search with control +n like you sugested, I get no results
 
Daniel Demesmaecker
Master Rancher
Posts: 920
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I manually added it to the classpath and now found the interface and the impl.
I added a breakpoint, but it's never being called
 
Daniel Demesmaecker
Master Rancher
Posts: 920
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to be sure, it's this one right?

but it's indeed in package org.springframework.security.core.userdetails.jdbc;
and I'm using the springboot.starter, does that make a difference or...?
I tried changing my pom to use spring security instead of springboot.starter, but then my whol configClass fails...
 
Daniel Demesmaecker
Master Rancher
Posts: 920
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm making some progress lol.
Whiles debugging I know see that the method is called but my parameters aren't being returned
 
Daniel Demesmaecker
Master Rancher
Posts: 920
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The parameters were'nt passed cause I didn't have a username in my entity, I was using the emailfield, I thought it wouldn't mather cause they're both String, but it apperently dosn't work, I added an username to my entity and changed my db to enter the usernames in the table.
The usernames are esual to the emailadresses, now the value is passe but the resultset is still empty.
When I execute the query in datagrip I get the right values returned...
 
Daniel Demesmaecker
Master Rancher
Posts: 920
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally got it to work
 
Marshal
Posts: 62231
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:Finally got it to work

Well done
Do tell us what made it work.
 
Daniel Demesmaecker
Master Rancher
Posts: 920
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first prob was that the paremeters weren't passed on, like described above, of which I didn't get an exception, what's kind of weird.
I only noticed, cause of Rob's tip to debug the JdbcImpl.
I changed my security queries that often and cause I was in debugmode, I didn't noticed that after solving the parameters issue I got an exception for bad gramar in my querry, solving that finally also fixed the issue...
 
Always look on the bright side of life. At least this ad is really tiny:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!