source: src/main/java/com/example/task/security/CustomAuthenticationSuccessHandler.java

Last change on this file was fdfbdde, checked in by Stojilkova Sara <sara.stojilkova.students.finki.ukim.mk>, 9 months ago

Initial commit

  • Property mode set to 100644
File size: 1.3 KB
RevLine 
[fdfbdde]1package com.example.task.security;
2
3
4import com.example.task.entity.StudentEntity;
5import com.example.task.repository.StudentRepository;
6import jakarta.servlet.ServletException;
7import jakarta.servlet.http.HttpServletRequest;
8import jakarta.servlet.http.HttpServletResponse;
9import lombok.AllArgsConstructor;
10import org.springframework.security.core.Authentication;
11import org.springframework.security.core.userdetails.UserDetails;
12import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
13import org.springframework.stereotype.Component;
14
15import java.io.IOException;
16
17@Component
18@AllArgsConstructor
19public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
20
21 private final StudentRepository studentRepository;
22
23
24 @Override
25 public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
26 UserDetails userDetails = (UserDetails) authentication.getPrincipal();
27 try {
28 StudentEntity user = studentRepository.findByUsername(userDetails.getUsername()).orElseThrow(Exception::new);
29 response.sendRedirect("/");
30 } catch (Exception e) {
31 e.printStackTrace();
32 }
33 }
34}
Note: See TracBrowser for help on using the repository browser.