简体   繁体   English

带有用户名和 ID 的 Spring Security RememberMe 功能

[英]Spring Security RememberMe function with username and id

I have a Spring Boot application and the login handled by Spring security.我有一个 Spring Boot 应用程序和由 Spring 安全处理的登录。 I have added the remeberme() funtion to the HttpSecurity我已将 remeberme() 功能添加到 HttpSecurity

http.<...>.rememberMe().rememberMeParameter(REMEMBER_ME_PARAM).rememberMeCookieName(REMEMBER_ME_NAME).key(KEY_NAME).userDetailsService(userDetailsService);

and added the checkbox to login form.并将复选框添加到登录表单。 My users login by their email as username and belong to a special group.我的用户通过他们的电子邮件作为用户名登录并属于一个特殊组。 That works fine for normal users who just belong to one group.这适用于只属于一个组的普通用户。

But I have users which belong to several groups with the same email as username (they get a select of group when they login).但是我有属于多个组的用户,其电子邮件与用户名相同(他们登录时会选择一个组)。

This users have several database entries, each user with single ID but same email.这个用户有几个数据库条目,每个用户都有一个 ID 但相同的电子邮件。 So I do not have a "unique" username which can be used in RemeberMe Cookie and so it throws a 500 exception when I use remember me funtion with this users.因此,我没有可在 RemeberMe Cookie 中使用的“唯一”用户名,因此当我使用该用户的记住我功能时,它会引发 500 异常。

Is there any possibility to expand the remeberme cookie to also use the users ID for example?例如,是否有可能将 remeberme cookie 扩展为也使用用户 ID? So that I can get the email and ID of the cookie and search for it in database?这样我就可以获取cookie的电子邮件和ID并在数据库中搜索它?

I think you should change approach.我认为你应该改变方法。 Use another approach PersistentTokenBasedRememberMeServices Here 在这里使用另一种方法 PersistentTokenBasedRememberMeServices

The problem is we have one person with actually two users with the same username.问题是我们有一个人实际上有两个用户名相同的用户。 Eg two users in database , both with username/email test@test.de例如数据库中的两个用户,都使用用户名/电子邮件 test@test.de

First user has ID 1 and belongs to group Foo, second has ID 2 and belongs to group Bar.第一个用户的 ID 为 1,属于 Foo 组,第二个用户的 ID 为 2,属于组 Bar。 Though same username all users can have different names, passwords, ....虽然相同的用户名所有用户可以有不同的名称,密码,....

The person then logs in with test@test.de and gets a select option to choose wether he wants to login for group Foo or Bar.然后该人使用 test@test.de 登录并获得一个选择选项,以选择他是要登录 Foo 组还是 Bar 组。

The remember me function now just remembers the username in cookie, so I can not assign an explicit user.记住我功能现在只记住 cookie 中的用户名,所以我不能指定一个明确的用户。 As the users can also have different passwords I also cannot use the group select of the login.由于用户也可以有不同的密码,我也不能使用登录的组选择。

As far as I see does the PersistentTokenBasedRememberMeServices also just uses the username to identify the user?!据我所知 PersistentTokenBasedRememberMeServices 是否也只是使用用户名来识别用户?! So I need a possibility to add a second unique field (user or group id) to the saved/remembered users.所以我需要有可能向保存/记住的用户添加第二个唯一字段(用户或组 ID)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM