• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do you do a constructor for a class that extends another class and implment a interface?

 
Ranch Hand
Posts: 1021
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi experts,

I have been trying very hard to work on the above without avail and I just can't get it right.  My fundamentals is so S H A K Y.......

Basically, I am using Spring Boot API where it says that I can


public class User
extends Object
implements UserDetails, CredentialsContainer

Models core user information retrieved by a UserDetailsService.

Developers may use this class directly, subclass it, or write their own UserDetails implementation from scratch.



So what I did is



I learnt from the past, we will have to bring in the super class variables(or the fields in that class - I hope I am clear about this part) but I do not know why it is not working out since there is a UserName there for sure...

Or did I misinterpret the instructions in the API, cos it mentioned subclass it so does it mean it cannot be a super class like what I have done ?

How do I subclass Spring Boot User class ?

I also learnt that I can override the method in the super class but there is not matter in Spring Boot ...I just need my User UserId to 'override' that of Spring Boot's.

Please let me know how to make this work.

Tks.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would recommend against having two classes in your project named "User", yours and the Spring one.

What parameters does the Spring...User constructor take? That's what you need to match with your super() call.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can only call getUserName() and getAuthorities() if these methods are static because at this point no object has been created yet. Where are these methods?
 
tangara goh
Ranch Hand
Posts: 1021
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:I would recommend against having two classes in your project named "User", yours and the Spring one.

What parameters does the Spring...User constructor take? That's what you need to match with your super() call.



I tried to use Super(and here i tried to pass in the Superclass member variable ) as per SpringBoot


User​(java.lang.String username, java.lang.String password, java.util.Collection<? extends GrantedAuthority> authorities



but it would not accept username. Why?



So, I constructed a superclass constructor myself and initialize it before this lines 'can work except can't accept username')


public User(int id, String password, UserStatus status, Role role, Collection<? extends GrantedAuthority>authorities) {
super(username, password, authorities);


 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see where you are getting userName or authorities from. userName should be one of the parameters to your User constructor and I'm thinking that authorities should be hard coded into the super() call unless you see passing them in also. Do authorities change from User to User in your program?
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the Spring User also have an "id" ? When a user is logging in they'd know their name, but would they know their internal "id"? I'm assuming that both id and name must be unique in your system.
 
tangara goh
Ranch Hand
Posts: 1021
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Does the Spring User also have an "id" ? When a user is logging in they'd know their name, but would they know their internal "id"? I'm assuming that both id and name must be unique in your system.



Hi Carey,

Sorry for the late reply but I was fire fighting with lots of issues that cropped up in the system.
Actually, I managed to extend the Spring Boot Security User.

But, I feel that something is not quite right since Spring is using loadUserByUserName and this would be read by the system so even if I have write my own getter for userId it won't wrapped around this method by SpringBoot.

How can I overcome the above ?

Tks.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it's using loadByUserName() then it should return the user with the id. You should not be giving it the id, it should be giving the id to you. That's if you really need it at all, you might just need the user name. I'm not familiar with Spring.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic