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
|
Line | |
---|
1 | package com.example.task.security;
|
---|
2 |
|
---|
3 |
|
---|
4 | import com.example.task.entity.StudentEntity;
|
---|
5 | import com.example.task.repository.StudentRepository;
|
---|
6 | import jakarta.servlet.ServletException;
|
---|
7 | import jakarta.servlet.http.HttpServletRequest;
|
---|
8 | import jakarta.servlet.http.HttpServletResponse;
|
---|
9 | import lombok.AllArgsConstructor;
|
---|
10 | import org.springframework.security.core.Authentication;
|
---|
11 | import org.springframework.security.core.userdetails.UserDetails;
|
---|
12 | import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
---|
13 | import org.springframework.stereotype.Component;
|
---|
14 |
|
---|
15 | import java.io.IOException;
|
---|
16 |
|
---|
17 | @Component
|
---|
18 | @AllArgsConstructor
|
---|
19 | public 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.