package ciai.security; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.Authentication; import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; import org.springframework.stereotype.Component; import ciai.model.Account; import ciai.model.Professor; import ciai.model.Student; import ciai.service.AppService; import tools.AccountRole; @Component public class AuthenticationSuccess extends SimpleUrlAuthenticationSuccessHandler { @Autowired AppService service; @Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { String username = request.getParameter("username"); Account ac = service.getAccountByUsername(username); if (ac != null) { AccountRole accRole = ac.getRole().getDescription(); if (accRole.equals(AccountRole.STUDENT)) { Student s = service.getStudentByAccount(ac); response.addHeader("redirect", "/student/" + s.getNumber()); } else if (accRole.equals(AccountRole.PROFESSOR) || accRole.equals(AccountRole.ASSISTANT)) { Professor p = service.getProfessorByAccount(ac); response.addHeader("redirect", "/professor/" + p.getId()); } } else { response.sendError(500, "Account doesn't exist"); } } }