如何创建数据类实现特定于Spring Secuirty的UserDetails [英] How to create a data class implements Spring Secuirty specific UserDetails
问题描述
我正在尝试将一些spring-webflux
示例代码迁移到Kotlin.
I am trying to migrate some spring-webflux
sample codes to kotlin.
目前,我想转换 Spring Data Mongo 样本到Kotlin.有一个User
,原始的Data Mongo版本看起来是:
Currently I want to convert my Spring Data Mongo sample to kotlin. There is a User
, the original Data Mongo version looks:
@Data
@ToString
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Document
class User implements UserDetails {
@Id
private String id;
private String username;
private String password;
@Builder.Default()
private boolean active = true;
@Builder.Default()
private List<String> roles = new ArrayList<>();
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return AuthorityUtils.createAuthorityList(roles.toArray(new String[roles.size()]));
}
@Override
public boolean isAccountNonExpired() {
return active;
}
@Override
public boolean isAccountNonLocked() {
return active;
}
@Override
public boolean isCredentialsNonExpired() {
return active;
}
@Override
public boolean isEnabled() {
return active;
}
}
UserDetails
界面包括一些getXXX和isXXX方法,如何将override
添加到kotlin的username
和password
属性中?
The UserDetails
interface includes some getXXX and isXXX methods, how to add override
to the username
and password
proerpties in kotlin?
顺便说一句:目前,我在kotlin版本
BTW: Currently I removed UserDetails
in the kotlin version, check here:
@Document
data class User(
@Id var id: String? = null,
var username: String? = null,
var password: String? = null,
var active: Boolean = true,
var roles: List<String> = ArrayList()
)
更新:如何向其中添加,新问题是使用销毁时不起作用. UserDetails
接口?
The new problem is when use destruction, it does not work.
(id, username, password, active, roles) = <a user>
The User
is located https://github.com/hantsy/spring-reactive-sample/blob/master/kotlin-gradle/src/main/kotlin/com/example/demo/User.kt . Currently I commented out the version implemented UserDetails
. If I use the commented codes(User:UserDetails), the desstruction
codes in Beans.kt
will report errors in IDE. The errors occur on username
and password
: destructing declaration initializer of type User! must have a component2 function.
推荐答案
data class User(
// inherits from UserDetails
private val username: String,
private val password: String,
private val isEnabled: Boolean, //Disabled account can not log in
private val isCredentialsNonExpired: Boolean, //credential can be expired,eg. Change the password every three months
private val isAccountNonExpired: Boolean, //eg. Demo account(guest) can only be online 24 hours
private val isAccountNonLocked: Boolean, //eg. Users who malicious attack system,lock their account for one year
private val authorities: Set<GrantedAuthority>,
// inherits from Tree.If the user is not a tree structure, can be ignored
val id: Long,
val parentId: Long? = null,
val lft: Int? = null, // preorder tree traversal algorithm
val rgt: Int? = null, // preorder tree traversal algorithm
val depth: Int? = null, // or levels
val isLeaf: Boolean? = null,
val teamTotal: Int? = null, // The number of all subordinates
val childrenTotal: Int? = null, //The number of all directly under the subordinate
//Custom attributes
val tenantId: Long, //We are the platform + tenant model
val role: Set<Role>, // TENANT, SUBACCOUNT, DEFAULT_GROUP, MEMBER, GUEST
val platform: Platform, // PC, IOS, ANDROID, WAP, INTERNAL
val deviceToken: String? = null, //or machine code
val ip: InetAddress //Inet4Address or Inet6Address
) : UserDetails, Tree(id, parentId) {
override fun getUsername(): String = username
override fun getPassword(): String = password
override fun isEnabled(): Boolean = isEnabled
override fun isCredentialsNonExpired(): Boolean = isCredentialsNonExpired
override fun isAccountNonExpired(): Boolean = isAccountNonExpired
override fun isAccountNonLocked(): Boolean = isAccountNonLocked
override fun getAuthorities(): Set<out GrantedAuthority> = authorities
}
这篇关于如何创建数据类实现特定于Spring Secuirty的UserDetails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!