This is a module of a security method that's provided by Spring Security. We can provide a role in a particular function so that role-based users can access the function.
The following annotation is used to enable this feature:
@EnableGlobalMethodSecurity(securedEnabled = true)
Here's an example of how to enable method security in the SpringSecurityApplication.kt class, which is the main application class of our demo project:
@SpringBootApplication
@EnableGlobalMethodSecurity(securedEnabled = true)
class SpringSecurityApplication{
fun main(args: Array<String>) {
runApplication<SpringSecurityApplication>(*args)
}
}
Now you can create the method resources, such as the following code:
@Secured
class CustomService{
@Secured
fun secure(): String{
return "The is Secured..."
}
}
Here, we created a secured class named CustomService using the @Secured annotation, and then created a secured function that will return a spring. The @Secured annotation is used to specify a list of roles on a function.