February 2019
Intermediate to advanced
442 pages
11h 46m
English
Spring Security needs users and their role details to perform the security check. In this approach, we will fetch the user and role details with a SQL query. We will define a query in the application.properties file as follows:
spring.queries.users-query= select username, password, enabled from users where username=?spring.queries.roles-query= select u.username, r.role from users u inner join user_role ur on(u.id=ur.user_id) inner join role r on(ur.role_id=r.id) where u.username=?
The first query fetches user details while second queries retrieve the list of roles for a given username. These properties can be read in the WebSecurityConfig class as follows:
@Value("\${spring.queries.users-query}")private val usersQuery: String? ...