package com.example.manager_student.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/register").permitAll()
.antMatchers("/").hasRole("MEMBER")
.antMatchers("/admin").hasRole("ADMIN")
.and()
.formLogin()
.loginPage("/login")
.usernameParameter("email")
.passwordParameter("password")
.defaultSuccessUrl("/")
.failureUrl("/login?error")
.and()
.exceptionHandling()
.accessDeniedPage("/403");
}
}
package com.example.manager_student.config;
import java.util.HashSet;
import com.example.manager_student.domain.Role;
import com.example.manager_student.domain.User;
import com.example.manager_student.repository.RoleRepository;
import com.example.manager_student.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
@Component
public class DataSeedingListener implements ApplicationListener<ContextRefreshedEvent> {
@Autowired
private UserRepository userRepository;
@Autowired
private RoleRepository roleRepository;
@Autowired
private PasswordEncoder passwordEncoder;
@Override
public void onApplicationEvent(ContextRefreshedEvent arg0) {
// Roles
if (roleRepository.findByName("ROLE_ADMIN") == null) {
roleRepository.save(new Role("ROLE_ADMIN"));
}
if (roleRepository.findByName("ROLE_MEMBER") == null) {
roleRepository.save(new Role("ROLE_MEMBER"));
}
// Admin account
if (userRepository.findByEmail("[email protected]") == null) {
User admin = new User();
admin.setEmail("[email protected]");
admin.setPassword(passwordEncoder.encode("123456"));
HashSet<Role> roles = new HashSet<>();
roles.add(roleRepository.findByName("ROLE_ADMIN"));
roles.add(roleRepository.findByName("ROLE_MEMBER"));
admin.setRole(roles);
userRepository.save(admin);
}
// Member account
if (userRepository.findByEmail("[email protected]") == null) {
User user = new User();
user.setEmail("[email protected]");
user.setPassword(passwordEncoder.encode("123456"));
HashSet<Role> roles = new HashSet<>();
roles.add(roleRepository.findByName("ROLE_MEMBER"));
user.setRole(roles);
userRepository.save(user);
}
}
}
nó xuất thông báo lỗi
Error creating bean with name 'dataSeedingListener': Unsatisfied dependency expressed through field 'passwordEncoder'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'webSecurityConfig': Requested bean is currently in creation: Is there an unresolvable circular reference?