Jun 28, 2024
pom.xml
user
) and password (logged in console)
application.properties
spring.security.user.name=user
, spring.security.user.password=user
@Configuration
and @EnableWebSecurity
SecurityFilterChain
bean
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) {
// security configurations
return http.build();
}
http.authorizeRequests().antMatchers("/public/**").permitAll().antMatchers("/admin/**").hasRole("ADMIN");
User
entityCommandLineRunner
to preload user datapasswordEncoder.encode(password)
UserDetails
User.withUsername(dbUser.getUsername()).password(dbUser.getPassword()).roles(dbUser.getRoles()).build();
@EnableMethodSecurity
annotation in configuration class@PreAuthorize
on methods to specify role-based access@PreAuthorize("hasRole('ADMIN')")