Spring Boot-Thymeleaf redirect problem (When doing post request) [closed]

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();
}

New contributor

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật