|
|
|
@ -2,8 +2,10 @@ package com.bsmlab.dfx.dfxconsole.framework.config;
|
|
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import jakarta.servlet.FilterChain;
|
|
|
|
import jakarta.servlet.FilterChain;
|
|
|
|
|
|
|
|
import jakarta.servlet.ServletException;
|
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
|
|
|
|
|
import jakarta.servlet.http.HttpSession;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
@ -11,7 +13,10 @@ import org.springframework.security.authentication.AuthenticationServiceExceptio
|
|
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
|
import org.springframework.security.core.AuthenticationException;
|
|
|
|
import org.springframework.security.core.AuthenticationException;
|
|
|
|
|
|
|
|
import org.springframework.security.core.context.SecurityContext;
|
|
|
|
|
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
|
|
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
|
|
|
|
|
|
|
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashMap;
|
|
|
|
@ -40,14 +45,20 @@ public class JsonUsernamePasswordAuthenticationFilter extends UsernamePasswordAu
|
|
|
|
|
|
|
|
|
|
|
|
// 로그인 성공 후 결과 메시지 전달. 자동 호출됨.
|
|
|
|
// 로그인 성공 후 결과 메시지 전달. 자동 호출됨.
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException {
|
|
|
|
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException {
|
|
|
|
log.info("로그인 성공: {}", authResult.getName());
|
|
|
|
log.info("로그인 성공: {}", authResult.getName());
|
|
|
|
|
|
|
|
SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
|
|
|
|
|
|
|
|
securityContext.setAuthentication(authResult);
|
|
|
|
|
|
|
|
SecurityContextHolder.setContext(securityContext);
|
|
|
|
|
|
|
|
HttpSession httpSession = request.getSession(true);
|
|
|
|
|
|
|
|
httpSession.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, securityContext);
|
|
|
|
response.setStatus(HttpServletResponse.SC_OK);
|
|
|
|
response.setStatus(HttpServletResponse.SC_OK);
|
|
|
|
response.setContentType("application/json");
|
|
|
|
response.setContentType("application/json");
|
|
|
|
Map<String, Object> result = new HashMap<>();
|
|
|
|
Map<String, Object> result = new HashMap<>();
|
|
|
|
result.put("success", true);
|
|
|
|
result.put("success", true);
|
|
|
|
result.put("username", authResult.getName());
|
|
|
|
result.put("username", authResult.getName());
|
|
|
|
objectMapper.writeValue(response.getWriter(), result);
|
|
|
|
objectMapper.writeValue(response.getWriter(), result);
|
|
|
|
|
|
|
|
chain.doFilter(request, response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 로그인 실패 후 결과 메시지 전달. 자동 호출됨.
|
|
|
|
// 로그인 실패 후 결과 메시지 전달. 자동 호출됨.
|
|
|
|
|