package com.example.task.security; import com.example.task.entity.StudentEntity; import com.example.task.repository.StudentRepository; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import lombok.AllArgsConstructor; import org.springframework.security.core.Authentication; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.web.authentication.AuthenticationSuccessHandler; import org.springframework.stereotype.Component; import java.io.IOException; @Component @AllArgsConstructor public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler { private final StudentRepository studentRepository; @Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { UserDetails userDetails = (UserDetails) authentication.getPrincipal(); try { StudentEntity user = studentRepository.findByUsername(userDetails.getUsername()).orElseThrow(Exception::new); response.sendRedirect("/"); } catch (Exception e) { e.printStackTrace(); } } }