So basically I’m trying to Login with user Email and password. using spring security. but it is not login and give an error “User account has expired”. i’m trying to solve that but not found any right solution so can any help to.
Note: I’m storing userdata in MySQL using encryption.
<code>**HomeController.java**
@Controller
public class HomeController {
Logger log = LoggerFactory.getLogger(HomeController.class);
@Autowired
private BCryptPasswordEncoder passwordEncoder;
@Autowired
UserRepository userRepository;
@GetMapping("/home")
public String home() {
return "index";
}
@GetMapping("/about")
public String aboutUs() {
return "about";
}
@GetMapping("/signup")
public String signup(Model model) {
model.addAttribute("User",new User());
return "signup";
}
@GetMapping("/login")
public String login() {
return "login";
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@RequestMapping(value = "/do_register" ,method = RequestMethod.POST)
public String registerUser(@Valid @ModelAttribute("User") User user,BindingResult bindingResult,@RequestParam(value = "acceptTerms",defaultValue = "false")boolean aggrement,
Model model,HttpSession session) {
User ans = null;
try {
if(aggrement == false){
System.out.println("You are not agrre with terms and condition");
throw new Exception("you have not accept terms and Condition.");
}
if(bindingResult.hasErrors()){
System.err.println("Error:"+bindingResult.toString());
// if any error occur so the all data are save to the form in signup page
model.addAttribute("User",user);
return "signup";
}
user.setRole("user");
log.info("Password::"+user.getPassword());
user.setPassword(passwordEncoder.encode(user.getPassword()));
LocalDate currentDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String dateString = currentDate.format(formatter);
user.setCreatedDate(dateString);
ans = userRepository.save(user);
User emptyUser = new User();
model.addAttribute("User",emptyUser);
session.setAttribute("message", new Message("Succesfully User Registerd", "alert-success"));
} catch (Exception e) {
e.printStackTrace();
model.addAttribute("User",user);
session.setAttribute("message", new Message("Something went Wrong!"+e.getMessage(),"alert-danger"));
return "signup";
}
return "signup";
}
}
**myConfig.java**
@Configuration
@EnableWebSecurity
public class myConfig {
@Bean
public UserDetailsService getUserDetailsService() {
return new userDetailServiceimpl();
}
@Bean
public BCryptPasswordEncoder encyptPassword() {
return new BCryptPasswordEncoder();
}
@Bean
public DaoAuthenticationProvider AuthenticationProvider() {
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
daoAuthenticationProvider.setUserDetailsService(this.getUserDetailsService());
daoAuthenticationProvider.setPasswordEncoder(encyptPassword());
return daoAuthenticationProvider;
}
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.csrf().disable().
authorizeHttpRequests().requestMatchers("/admin/**").hasRole("ADMIN")
.requestMatchers("/user/**").hasRole("user")
.anyRequest().permitAll()
.and().formLogin();
return http.build();
}
}
</code>
<code>**HomeController.java**
@Controller
public class HomeController {
Logger log = LoggerFactory.getLogger(HomeController.class);
@Autowired
private BCryptPasswordEncoder passwordEncoder;
@Autowired
UserRepository userRepository;
@GetMapping("/home")
public String home() {
return "index";
}
@GetMapping("/about")
public String aboutUs() {
return "about";
}
@GetMapping("/signup")
public String signup(Model model) {
model.addAttribute("User",new User());
return "signup";
}
@GetMapping("/login")
public String login() {
return "login";
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@RequestMapping(value = "/do_register" ,method = RequestMethod.POST)
public String registerUser(@Valid @ModelAttribute("User") User user,BindingResult bindingResult,@RequestParam(value = "acceptTerms",defaultValue = "false")boolean aggrement,
Model model,HttpSession session) {
User ans = null;
try {
if(aggrement == false){
System.out.println("You are not agrre with terms and condition");
throw new Exception("you have not accept terms and Condition.");
}
if(bindingResult.hasErrors()){
System.err.println("Error:"+bindingResult.toString());
// if any error occur so the all data are save to the form in signup page
model.addAttribute("User",user);
return "signup";
}
user.setRole("user");
log.info("Password::"+user.getPassword());
user.setPassword(passwordEncoder.encode(user.getPassword()));
LocalDate currentDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String dateString = currentDate.format(formatter);
user.setCreatedDate(dateString);
ans = userRepository.save(user);
User emptyUser = new User();
model.addAttribute("User",emptyUser);
session.setAttribute("message", new Message("Succesfully User Registerd", "alert-success"));
} catch (Exception e) {
e.printStackTrace();
model.addAttribute("User",user);
session.setAttribute("message", new Message("Something went Wrong!"+e.getMessage(),"alert-danger"));
return "signup";
}
return "signup";
}
}
**myConfig.java**
@Configuration
@EnableWebSecurity
public class myConfig {
@Bean
public UserDetailsService getUserDetailsService() {
return new userDetailServiceimpl();
}
@Bean
public BCryptPasswordEncoder encyptPassword() {
return new BCryptPasswordEncoder();
}
@Bean
public DaoAuthenticationProvider AuthenticationProvider() {
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
daoAuthenticationProvider.setUserDetailsService(this.getUserDetailsService());
daoAuthenticationProvider.setPasswordEncoder(encyptPassword());
return daoAuthenticationProvider;
}
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.csrf().disable().
authorizeHttpRequests().requestMatchers("/admin/**").hasRole("ADMIN")
.requestMatchers("/user/**").hasRole("user")
.anyRequest().permitAll()
.and().formLogin();
return http.build();
}
}
</code>
**HomeController.java**
@Controller
public class HomeController {
Logger log = LoggerFactory.getLogger(HomeController.class);
@Autowired
private BCryptPasswordEncoder passwordEncoder;
@Autowired
UserRepository userRepository;
@GetMapping("/home")
public String home() {
return "index";
}
@GetMapping("/about")
public String aboutUs() {
return "about";
}
@GetMapping("/signup")
public String signup(Model model) {
model.addAttribute("User",new User());
return "signup";
}
@GetMapping("/login")
public String login() {
return "login";
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@RequestMapping(value = "/do_register" ,method = RequestMethod.POST)
public String registerUser(@Valid @ModelAttribute("User") User user,BindingResult bindingResult,@RequestParam(value = "acceptTerms",defaultValue = "false")boolean aggrement,
Model model,HttpSession session) {
User ans = null;
try {
if(aggrement == false){
System.out.println("You are not agrre with terms and condition");
throw new Exception("you have not accept terms and Condition.");
}
if(bindingResult.hasErrors()){
System.err.println("Error:"+bindingResult.toString());
// if any error occur so the all data are save to the form in signup page
model.addAttribute("User",user);
return "signup";
}
user.setRole("user");
log.info("Password::"+user.getPassword());
user.setPassword(passwordEncoder.encode(user.getPassword()));
LocalDate currentDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String dateString = currentDate.format(formatter);
user.setCreatedDate(dateString);
ans = userRepository.save(user);
User emptyUser = new User();
model.addAttribute("User",emptyUser);
session.setAttribute("message", new Message("Succesfully User Registerd", "alert-success"));
} catch (Exception e) {
e.printStackTrace();
model.addAttribute("User",user);
session.setAttribute("message", new Message("Something went Wrong!"+e.getMessage(),"alert-danger"));
return "signup";
}
return "signup";
}
}
**myConfig.java**
@Configuration
@EnableWebSecurity
public class myConfig {
@Bean
public UserDetailsService getUserDetailsService() {
return new userDetailServiceimpl();
}
@Bean
public BCryptPasswordEncoder encyptPassword() {
return new BCryptPasswordEncoder();
}
@Bean
public DaoAuthenticationProvider AuthenticationProvider() {
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
daoAuthenticationProvider.setUserDetailsService(this.getUserDetailsService());
daoAuthenticationProvider.setPasswordEncoder(encyptPassword());
return daoAuthenticationProvider;
}
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.csrf().disable().
authorizeHttpRequests().requestMatchers("/admin/**").hasRole("ADMIN")
.requestMatchers("/user/**").hasRole("user")
.anyRequest().permitAll()
.and().formLogin();
return http.build();
}
}
SO i want to Login with UserEmail and password which enter by user.and redirect to the user dashboard.