강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

진짜 잘하고싶다님의 프로필 이미지
진짜 잘하고싶다

작성한 질문수

스프링부트 시큐리티 & JWT 강의

스프링부트 시큐리티 27강 - jwt토큰 서버 구축 완료

강사님 질문이있습니다 ㅠㅠ

작성

·

435

0

public class MyCustomDsl extends AbstractHttpConfigurer<MyCustomDsl, HttpSecurity> {
    @Override
    public void configure(HttpSecurity http) throws Exception {
        System.out.println("체크포인트!@@!@!@!@");
        AuthenticationManager authenticationManager = http.getSharedObject(AuthenticationManager.class);
        http
                .addFilter(new LoginFilter(authenticationManager))
                .addFilterAfter(jwtFilter, UsernamePasswordAuthenticationFilter.class);
    }
}

를 구현하고,

 

로그인이 완료 되었을 때,

 

@Override
    protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException {
        log.info("successfulAuthentication 함수 실행: 로그인 성공");
        PrincipalDetails principalDetails = (PrincipalDetails) authResult.getPrincipal();
        /*
        로그인 완료,
        1. 토큰 생성
        2. 쿠키 장착
         */
        response.setHeader("gd", "gdgd");
//        response.sendRedirect("/api/loginTest");
//        super.successfulAuthentication(request, response, chain, authResult);
    }

이 메서드 까지 구현했습니다.

 

제 원래 로직은

액세스 토큰, 리프레쉬 토큰을 생성하고

리프레쉬 토큰을 DB에 담고

액세스 토큰, 레프레쉬 토큰 두 개를 쿠키에 저장하는 로직입니다.

 

 

ResponseCookie build = ResponseCookie
        .from("accessToken", "gd")
        .path("/")
        .httpOnly(true)
        // 시간
        .maxAge(JwtUtil.REFRESH_TOKEN_EXPIRE_TIME)
        .sameSite("Lax")
        .build();
ResponseEntity.ok().header("Set-Cookie", build.toString())
        .body("ok");

대충 쿠키를 생성해서 그 쿠키를 헤더에 담는 방식을 사용 중인데,

 

이렇게 로그인 완료 메서드에서 구현할 방법이 없고 헤더에 저장할 방법이 없어서 너무 막막합니다..

 

Controller에서 그냥 구현하면 ResponseEntity를 이용해서 헤더에 고정적으로 담을 수 있는데

로그인 성공 메서드에서 어떻게 해야 이렇게 똑같이 구현을 할 수 있을까요..ㅠㅠㅜ?

답변 1

0

최주호님의 프로필 이미지
최주호
지식공유자

HttpServletResponse response 를 컨트롤러의 메서드 매개변수에 담아서 헤더에 담아주시면 됩니다!!

진짜 잘하고싶다님의 프로필 이미지
진짜 잘하고싶다

작성한 질문수

질문하기