Похожие чаты

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 ответов

9 просмотров

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.

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

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

я не магистр хаскеля, но разве не может лейзи тип конвертнуться в не-лейзи запросив вычисление содержимого прям при инициализации?
deadgnom32 λ madao
100
в сях есть множество как в питоне? для удаление дубликатов
Linus
25
Hi everyone, long time no see! 🐼 recently i want to update our Ergo Eco-System map, can everyone help to find out, which to delete and other new to add in? list it, that i can...
HEROKOO
13
This topic is discussed al lot. Please use the search function for the answers. The same question pops up every two weeks. First check this video https://www.youtube.com/watch...
Enrico
2
зеленые ноты вижу, а коричневые?
οἰφέω σκάπτω
19
If arrr gets implemented on cake wallet. Would that mean arrr/xmr swaps possible in cake wallet like atomic swaps on komodowallet ?
Keks Meister 🏴‍☠️
22
читать файл максимально быстро? странный вопрос))
zamtmn
53
Yeh scammer hai kya ? 🙄 mujhre bhi solana invest ke liye bol re thi kal
Amit
18
30 BCH videos in Hausa Language successfully delivered. In December 2023, I created a Flipstarter to produce Bitcoin Cash videos in the Hausa language for greater outreach an...
Munnir
9
Я хочу запустить свой проект в тг. Что-то между пирамидой и майнилкой. Еще подобного ничего не было. Уникальная идея. Нужен именно не бот, а приложение. С ввод, выводом тон...
Павел А.
6
Карта сайта