- Spring Security(Third Edition)
- Mick Knutson Robert Winch Peter Mularien
- 153字
- 2025-04-04 17:54:29
Inserting custom authorities
We need to update DefaultCalendarService to insert the authorities for the user using our custom schema when we add a new CalendarUser class. This is because while we reused the schema for the user definition, we did not define custom authorities in our existing application. Update DefaultCalendarService, as follows:
//src/main/java/com/packtpub/springsecurity/service/DefaultCalendarService.java
import org.springframework.jdbc.core.JdbcOperations;
...
public class DefaultCalendarService implements CalendarService {
...
private final JdbcOperations jdbcOperations;
@Autowired
public DefaultCalendarService(EventDao eventDao,
CalendarUserDao userDao, JdbcOperations jdbcOperations) {
...
this.jdbcOperations = jdbcOperations;
}
...
public int createUser(CalendarUser user) {
int userId = userDao.createUser(user);
jdbcOperations.update(
"insert into calendar_user_authorities(calendar_user,authority)
values(?,?)", userId, "ROLE_USER");
return userId;
}
}
You may have noticed the JdbcOperations interface that is used for inserting our user. This is a convenient template provided by Spring that helps manage boilerplate code, such as connection and transaction handling. For more details, refer to the Appendix, Additional Reference Material of this book to find the Spring Reference.