When I perform a POST request to the “login-admin” endpoint, I want it to redirect to the “admin-dashboard” endpoint. However, I am encountering an issue where it does not redirect to “admin-dashboard” as intended. Instead, it redirects to a failure URL. I have already permitted the endpoints in the configuration, but the problem persists. I have checked everything, including the database and MVC setup, but I still haven’t found the issue. Can you identify any problem in my code because it is not working?
@Controller
public class MainController {
@Autowired
private AdminRepository adminRepository;
@Autowired
private UserRepository userRepository;
@Autowired
private PasswordEncoder passwordEncoder;
// Admin login page
@GetMapping("/login-admin")
public String adminPage() {
return "login-admin";
}
// Admin login process
@PostMapping("/login-admin")
public String loginAdmin(@RequestParam("username") String username,
@RequestParam("password") String password,
Model model) {
if (username.isEmpty() || password.isEmpty()) {
model.addAttribute("error", "username and password must be filled";
return "login-admin"; // Error handling for empty inputs
}
Admin admin = adminRepository.findByUsername(username);
if (admin == null || !passwordEncoder.matches(password, admin.getPassword())) {
model.addAttribute("error", "username or password is wrong");
return "login-admin"; // Incorrect login
}
// Successful login
return "redirect:/admin-dashboard"; // Redirect to admin dashboard
}
@GetMapping("/admin-dashboard")
public String adminDashboard() {
return "admin-dashboard";
}
}
here is the my thymeleaf code;
<form th:action="@{/login-admin}" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br><br>
<button type="submit">Login</button>
</form>
here is my service;
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Admin admin = adminRepository.findByUsername(username);
if (admin == null) {
System.out.println("Admin is not found");
throw new UsernameNotFoundException("Admin is not found: " + username);
} else {
System.out.println("Admin is found " + admin.getUsername());
}
return org.springframework.security.core.userdetails.User
.withUsername(admin.getUsername())
.password(admin.getPassword())
.authorities(admin.getRole())
.build();
}
Mahmut is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
Redirecting by returning a pure string works only for @Controller
annotated methods. For those annotated with @RestController
, you could use a RedirectView
.
@PostMapping("/login-admin")
public ModelAndView loginAdmin(..., Model model)
{
if (condition1) {
model.addAttribute(...);
return new ModelAndView("login-admin", model);
} else {
return new ModelAndView(new RedirectView("/login-admin"));
)
}
1