Похожие чаты

Hey techies. I'm learning Spring Security. I saw somewhere being advised to

separate User.java from an implementation of UserDetails, say, MyUserDetails.java

My question is, say I have attributes: enabled, credentialsNonExpired, accountNonLocked, etc, do I need to persist these in the database?

And how would I 'link' my User.java with my MyUserDetails.java?

I know I may be 'overengineering' but I just want to wrap my head around this and get done with it.

9 ответов

35 просмотров

What was the reason for introducing that separation?

626d- Автор вопроса
Edwing123
What was the reason for introducing that separatio...

The S in SOLID. Single responsibility. User.java for user attributes. The other one for user-security-related.

> separate User from impl of UserDetails You may end up with changing auth mechanism to a different one (for example token-based), where you don't even need UserDetails at all. So you can throw it away. But you still need User to represent what info of a user is stored in DB.

626d- Автор вопроса
Dmytro Buryak
> separate User from impl of UserDetails You may ...

Thanks @dburyak Just a little more on this. Should I persist the userdetails in the database? That is, isEnabled, isCrendetialsExpired, etc? If so, how would you advise handling of identifiers? userId in User, and, say, userId as foreign key in UserDetails?

> My question is, say I have attributes: enabled, credentialsNonExpired, accountNonLocked, etc, do I need to persist these in the database? If you don't know what you're doing, then better just persist them in DB. It's a safer and future proof option. I highly recommend you to learn spring-security with some guidance and not by blindly trying something. Best resource I ever found about it was this book: https://www.manning.com/books/spring-security-in-action-second-edition Or any other form you like, but pick some *guided* tutorial. Something, where author knows what to explain, and in which order.

626d
Thanks @dburyak Just a little more on this. Shou...

We may have misunderstanding here. Under "User" I mean a domain object that you persist in database, which contains necessary information about the user. And under "UserDetails" an impl of an interface of spring-security concept, not related to any databases or anything. It's actually a part of something like "DaoAuthProvider" or something, but still, you don't necessarily have to store it anywhere. Hence, User - is for your domain, specific to whatever you develop. UserDetails - is an interface to make your domain work with spring-security

626d- Автор вопроса
Dmytro Buryak
> My question is, say I have attributes: enabled, ...

Thanks again You made a good point. Because most tutorials I've been watching/reading hardly go in depth on some nitty gritties. They all just hardcoded those values to true. Meaning, enabled is always true, credentialsNonExpired is always true. Made me wonder what if I want to implement logic where an admin would disable a user account. And hence my question.

626d- Автор вопроса
626d
Thanks again You made a good point. Because most ...

Alternatively, there may be a disconnect in my understanding vs how Spring actually handles those values. I'll reference the book you suggested for better understanding.

626d
Thanks @dburyak Just a little more on this. Shou...

I highly recommend to never use DB-specific ids in your business logic code. If you decide to change DB, and that DB uses something else for ids, then you'll have to rewrite TONS of code. Moreover, I guess your'e using SQL db at the moment, which is responsible of genearating IDs on its side. I.e. id generation is centralized. It's a huge obstacle for future development of the system. If you decide to eventually use decentralized IDs, you'll face the same problem - you're referencing objects by native centralized DB ids everywhere, and you'll have to rewrite tons of code.

Похожие вопросы

Обсуждают сегодня

а через ESC-код ?
Alexey Kulakov
29
30500 за редактор? )
Владимир
47
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
Чёт не понял, я ж правильной функцией воспользовался чтобы вывести отладочную информацию? но что-то она не ловится
notme
18
Привет)) уже кажется эту тему перемусолили, но вот я так и не понял. Я сейчас сижу на 27дюймов 2к мониторе. На Актуальной макоси, если я куплю 27д 4к монитор: - будет ли изобр...
Vladislav Piskunov
15
Hi guys, any problem with Pulsebrige? Trying to transfer from wETH to ETH. First it tells me to connect my metamask "through mobile app" not desktop. Then I did and confirmed ...
Snowflakecrypto
13
any reference of this implementation?
BitBuddha
29
У меня есть функция где происходит это: write_bit(buffer, 1); write_bit(buffer, 0); write_bit(buffer, 1); write_bit(buffer, 1); write_bit(buffer, 1); w...
~
13
Страшнейшая правда про списки ЦБ. С первых дней жизни P2P сферы, молодые человеки, начитавшись законодательной базы и "внутренних" документов, решили, что им противостоит сер...
Foxcool
3
&"C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.42.34433\bin\Hostx64\x64\dumpbin.exe" /EXPORTS C:\Users\Yeet\Tauon\vcpkg\installed\x64-window...
Martin Rys
6
Карта сайта