본문 바로가기
Dev/Spring

[Spring Boot] 외부 정적 리소스 처리하기

by 펭귄안에 온천 2022. 3. 30.
728x90
반응형

정적 리소스

  • 정적 리소스는 이미 만들어져 있는 리소스 (html , css, js, 이미지 파일등)을 말한다.

application.properties

# 파일 업로드 경로
file.url=C:/DEV/project/01.HI/files/

WebConfig.java

@Value("${file.url}") 
  private String uploadImagePath; 

  @Override 
  public void addResourceHandlers(ResourceHandlerRegistry registry) { 

    registry.addResourceHandler("/upload/**") 
    .addResourceLocations("file:///"+uploadImagePath)  // 웹에서 이미지 호출시 'file:///' 설정됨
    .setCachePeriod(3600)
    .resourceChain(true)
    .addResolver(new PathResourceResolver());

  };

테스트

http://localhost:11411/upload/2021/01/01/text.PNG

http://localhost:11411/upload/text.PNG

뻘짓1

, *의 차이는 파일과 폴더의 차이

뻘짓2

스프링 시큐리티를 사용중이라면

시큐리티의 matchPatten에 "/upload/**" 추가 설정이 필요하다.

반응형