12345678910111213141516171819202122232425 |
- package org.example.sweater.controller;
- import jakarta.servlet.http.HttpServletRequest;
- import jakarta.servlet.http.HttpServletResponse;
- import org.example.sweater.domain.User;
- import org.springframework.security.core.Authentication;
- import org.springframework.stereotype.Component;
- import org.springframework.web.servlet.HandlerInterceptor;
- import java.security.Principal;
- @Component
- public class SessionInterceptor implements HandlerInterceptor {
- @Override
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
- Principal principal = request.getUserPrincipal();
- if (principal != null) {
- Authentication authentication = (Authentication) principal;
- User user = (User) authentication.getPrincipal();
- request.setAttribute("user", user);
- }
- return true;
- }
- }
|