본문 바로가기
카테고리 없음

항해 7주차 회고

2022. 2. 27.

S3업로더란

S3이란 AWS에서 제공하는 Simple Storage Service 이다.

해당 S3Uploader를 사용하려면 aws.yml 파일에 설정을 해줘야한다.

cloud:
  aws:
    credentials:
      accessKey: 엑세스키
      secretKey: 시크릿키
    s3:
      bucket: 버킷 이름
    region:
      static: ap-northeast-2
    stack:
      auto: false

특히나 해당 accessKey와 secretKey를 github에 업로드하면 절대 안된다!!

가디언 문제때문에 문제가 생긴다.

github 뿐만아니라 온라인에 올리지를 말자!

 

 

또한 AmazonS3Config 파일을 설정해 줘야한다.

package com.sparta.velogclone.config;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

public class AmazonS3Config {

    @Value("${cloud.aws.credentials.access-key}")
    private String accessKey;

    @Value("${cloud.aws.credentials.secret-key}")
    private String secretKey;

    @Value("${cloud.aws.region.static}")
    private String region;

    @Bean
    public AmazonS3Client amazonS3Client() {
        BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKey, secretKey);
        return (AmazonS3Client) AmazonS3ClientBuilder.standard()
                .withRegion(region)
                .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
                .build();
    }
}

 

 

 

 

Filter란

JWT에서 Filter를 사용한다.

필터는 HTTP 요청과 응답을 변경할 수 있는 재사용 가능한 코드이다.

클라이언트가 서버에서 주는 request는 서버가 filter를 거쳐서 받게 되고
반대로 서버가 클라이언트에게 주는 response는 filter를 거쳐서 받게된다.

 

그러므로 요청이 들어오면 컨트롤러로 들어가기 전부터

filter가 먼저 발동한다.

그러므로 컨트롤러에 있는 파라미터에중 UserDetailImpl에 user정보가 들어갈 수 있었던 것이다.

// 게시글 작성
@PostMapping("/api/posting/{imageId}")
@ApiOperation(value = "게시물 등록", notes = "게시물에 이미지 파일을 첨부해서 등록한다")
public String savePost(
        @PathVariable Long imageId,
        @RequestBody PostRequestDto postRequestDto,
        @AuthenticationPrincipal UserDetailsImpl userDetails
) {
    if(userDetails != null) {
        User user = userDetails.getUser();
        ImageFile imageFile = postService.savePost(postRequestDto, user, imageId);
        return imageFile.getFilePath();
    } else throw new LoginUserNotFoundException("로그인한 유저 정보가 없습니다.");
}