inblog logo
|
codingb
    Spring

    로그인 토큰 생성 (+Bearer)

    coding S's avatar
    coding S
    Apr 19, 2024
    로그인 토큰 생성 (+Bearer)

    [ 로그인 토큰 생성 ]

    public String 로그인(UserRequest.LoginDTO reqDTO){ User user = userJPARepository.findByUsernameAndPassword(reqDTO.getUsername(), reqDTO.getPassword()) .orElseThrow(() -> new Exception401("인증되지 않았습니다")); String jwt = JwtUtil.create(user); return jwt; }
    notion image
    //원래 코드
    @PostMapping("/login") public ResponseEntity<?> login(@RequestBody UserRequest.LoginDTO reqDTO) { String jwt = userService.로그인(reqDTO); return ResponseEntity.ok().header("Authorization", "Bearer "+jwt).body(new ApiUtil(null)); }
    💡
    헤더에 줄 때 문법 body는 new ApiUtil 해서 하면 됨...
    notion image
    💡
    들어갔죠?
     

    [ Bearer ]

    notion image
    토큰 기반 인증에서는 베리어가 들어옴. Bearer jwt 토큰이 들어옴 검증할 때에는 Bearer가 있으면 안됨.
    HTTP 헤더에서 JWT 토큰을 전송할 때, "Bearer"라는 단어를 접두사로 사용함. -> 서버 측에서 이 토큰을 처리할 때는, 실제 토큰 값만을 추출하기 위해 "Bearer " 접두사를 제거해야 함!
    notion image
    💡
    한 칸 띄워져 있으니까 한 칸 띄워서 작성하기!
     

    [ 한 글자만 수정해도 ]

    notion image
    한글자 수정했더니 바로 오류터짐! https://jwt.io/ 에서 다시 돌려야한다는 말
     

     
    이렇게 하면 서버 확장이나 스티키 이런거 필요 없음 프런트도 알아서 할거임
     
    Share article

    codingb

    RSS·Powered by Inblog