Http에서 Cookie에Secure=true를 사용할 수 없다.

Sol's·2023년 2월 3일
0

팀프로젝트

목록 보기
18/25

팀프로젝트를 진행하며 로그인기능을 Cookie에 jwt를 담아 구현했습니다.

분명 localhost에서는 잘돌았는데 Ec2로 띄워 로그인을 해보니 Cookie값이 넘어오지 않았습니다.

원인을 알 수 없어 바로 검색에 들어갔습니다.

A cookie with the Secure attribute is only sent to the server with an encrypted request over the HTTPS protocol. 
It's never sent with unsecured HTTP (except on localhost), 
which means man-in-the-middle attackers can't access it easily.

쿠키는 Http에서는 보낼 수 없다고 합니다...
LocalHost에서만 가능하기때문에 테스트 할때는 잘 동작한 것입니다...

해결방안

Https를 사용해야하는데 임시방편으로 cookie.setSecure(true)를 주석처리하여 사용하였습니다.

@PostMapping("/view/v1/signin")
    public String login(UserLoginRequest userLoginRequest, HttpServletResponse response) throws UnsupportedEncodingException {

        UserLoginResponse tokens = userService.login(userLoginRequest);

        //cookie 설정은 스페이스가 안되기 때문에 Bearer 앞에 +를 붙인다. Security Filter에서 + -> " " 로 치환할 것이다.
        Cookie cookie = new Cookie("jwt", "Bearer+" + tokens.getAccessToken());


        cookie.setPath("/");
//        cookie.setSecure(true);
        //변경요소

        cookie.setHttpOnly(true);
        cookie.setMaxAge(60 * 25); //초단위 25분설정
        response.addCookie(cookie);

        return "redirect:/view/v1/crews";
    }

Nginx 프록시 서버에 SSL 인증서를 적용해 https로 열 수 있게 하는 방법이 있는것 같은데 더 알아보고 적용시켜보아야 겠습니다

배울게 너무 많다고 느껴지는 하루였습니다:)

참고자료

쿠키 설정 답변

도메인 연결하기 (feat. 🍪 쿠키오류 해결하기)

profile
배우고, 생각하고, 행동해라

0개의 댓글