Spring 安全中的 2 个 UserDomainClass
2 UserDomainClass in Spring Security
在现有的 Grails 应用程序中,我有 2 个用户域,比如 UserAdmin 和 UserBasic。这两个域都有很少的公共字段和一些不同的字段(相对于域),目前 login/logout 分别使用两种类型的用户的会话来维护。我想在现有应用程序中集成 spring-security。
最好的方法是什么?考虑到这两个领域也有不同的领域。我们可以在一个域中继承两个域并将其用作 Spring 安全用户 class 吗?请提出建议。
Spring 安全核心使用 UserDetails
接口的实现之一作为经过身份验证的用户的投影。 Grails 提供了例如GrailsUser
class:
请记住,就 Grails 应用程序布局而言,此 class 不是 "domain" class - 它不会持久保存在数据库中,它只是绑定到当前会话的用户。
如果您有 2 个不同的域 classes 在您的应用程序中代表用户,您可以尝试提供自己的 UserDetailsService
实现,例如
class CustomUserDetailsService implements UserDetailsService {
@Override
UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
// 1. Check if expected user is type of UserBasic
// 2. If not, check if expected user is type of UserAdmin
// 3. If nothing found, throw an exception
// 4. Otherwise create new GrailsUser instance using UserBasic or UserAdmin data
// 5. Return created GrailsUser instance
return null
}
}
然后您必须通过添加或修改 grails-app/conf/spring/resources.groovy
中的条目来注入您的实现,例如
// Place your Spring DSL code here
beans = {
// other beans goes here
// ...
userDetailsService(CustomUserDetailsService)
}
这只是一个概念,您可以从中着手。
在现有的 Grails 应用程序中,我有 2 个用户域,比如 UserAdmin 和 UserBasic。这两个域都有很少的公共字段和一些不同的字段(相对于域),目前 login/logout 分别使用两种类型的用户的会话来维护。我想在现有应用程序中集成 spring-security。
最好的方法是什么?考虑到这两个领域也有不同的领域。我们可以在一个域中继承两个域并将其用作 Spring 安全用户 class 吗?请提出建议。
Spring 安全核心使用 UserDetails
接口的实现之一作为经过身份验证的用户的投影。 Grails 提供了例如GrailsUser
class:
请记住,就 Grails 应用程序布局而言,此 class 不是 "domain" class - 它不会持久保存在数据库中,它只是绑定到当前会话的用户。
如果您有 2 个不同的域 classes 在您的应用程序中代表用户,您可以尝试提供自己的 UserDetailsService
实现,例如
class CustomUserDetailsService implements UserDetailsService {
@Override
UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
// 1. Check if expected user is type of UserBasic
// 2. If not, check if expected user is type of UserAdmin
// 3. If nothing found, throw an exception
// 4. Otherwise create new GrailsUser instance using UserBasic or UserAdmin data
// 5. Return created GrailsUser instance
return null
}
}
然后您必须通过添加或修改 grails-app/conf/spring/resources.groovy
中的条目来注入您的实现,例如
// Place your Spring DSL code here
beans = {
// other beans goes here
// ...
userDetailsService(CustomUserDetailsService)
}
这只是一个概念,您可以从中着手。