[내일배움캠프] #211208 💻 TIL 💻

이수영·2021년 12월 7일
0

MY TIL 

목록 보기
47/50
post-thumbnail

📚 Springboot

✍🏻 카카오메시지 api + 주기성

주기성을 위해 어떤 기술을 써야할까 고민이 많았다
카카오메시지 api 사용을 위해서는 access 토큰이 필요한 것이 핵심포인트였다 .. 하지만 access token 은 유효기간이 짧았고 로그인할때 발급받는 것 이므로 서버에서 재사용하는 것이 불가하였다.

=> 따라서 나는 자바스크립트에서 setInterval 함수를 쓰기로 결정!

📌 Source Code

✔ home.html ( 홈에 접속할 때 이 함수 실행 )

 sendMessage = setInterval(function() {
        console.log($(".test").css("background-color"))
        if ( $(".test").css("background-color")=='rgb(255, 0, 0)'){
          console.log('메시지 보내야됨')
          REST_Call('/message');
      }
        else{
          console.log('글 썼네')
        }
      }, 86400000);
  • 사용자가 오늘 글을 썼는 지 안썼는지 판별하는 flag가 존재한다
  • 나는 이것을 이용해 사용자가 글을 안썼을 경우 => 빨간색
    이 경우에는 /message 로 ajax 요청을 했다

✔ home.html ( 메시지 api 요청 )

function REST_Call (path)
    {

      let access_token=localStorage.getItem('access_token')
      console.log(access_token);
      $.ajax({
        type: "POST",
        url: path,
        contentType: "application/json",
        data: JSON.stringify({'token':access_token}),
        success: (data) => { alert('메시지가 전송되었습니다!'); }
      });
    }
  • 메시지 api에 필요한 access token 을 받아서 post 데이터로 서버에 넘겨준다

✔ Controller ( 메시지 api 실행 )

 @PostMapping("/message")
    public void message(@RequestBody SocialLoginDto socialLoginDto) {
    //access 토큰을 발급받아야함

        String token = socialLoginDto.getToken();

        log.info(token);
        String message= userService.sendmessage(token);

    }

✍ 로그인 JWT + Spring Security

📌 authentication

 try {
            authenticationManager.authenticate(
                    new UsernamePasswordAuthenticationToken(userDto.getUsername(), userDto.getPassword())
            );
  • 사용자가 입력한 아이디와 비밀번호로 인증정보객체 생성
  • 아직 인증이 완료된 객체가 아니며 AuthenticationManager 에서 authenticate 메소드의 파라미터로 넘겨서 검증 후에 Authentication 를 받습니다.
profile
Hongik Univ 💻

0개의 댓글