DEV Community

Cover image for Scratch the edge using Spring WebFlux (Part 2)
Iman
Iman

Posted on

Scratch the edge using Spring WebFlux (Part 2)

Weknow Bob from the previous story and he wants to use his service right now, and he could do it but wait! this is his exclusive service, is he alone right now?
He and the whole world can use this service forever. let’s limit the alien’s access by Spring Security.
First thing first, we need a model for our users (lovely Bob is alone) so let’s create one as the follow in kotlin>com>{something you know}>{project name, that also you know}>model

@Document
data class User(
        @Id
        val id : ObjectId,
        @Indexed(unique = true)
        val name: String,
        val pass: String,
        val role: String
) : UserDetails {

    override fun getAuthorities(): MutableCollection<out GrantedAuthority> {
        return mutableListOf(SimpleGrantedAuthority(role))
    }

    override fun getPassword(): String {
        return pass
    }

    override fun getUsername(): String {
        return name
    }

    override fun isAccountNonExpired() = true

    override fun isAccountNonLocked() = true

    override fun isCredentialsNonExpired() = true

    override fun isEnabled() = true
}
Enter fullscreen mode Exit fullscreen mode

You can see two elegant things in one frame, the flexibility of Spring Security and the conciseness of Kotlin (by comparing two different types of method).

You can find complete story here.

Top comments (0)