본문 바로가기
공부 기록

[REACT x SPRING BOOT] 리소스 핸들러를 활용한 사진 출력하기

by 타태 2022. 2. 8.

 

2022.01.17 - [실전 공부/Java] - [FILE] 자바 + 스프링 부트 + Axios(rest api) 파일 업로드, 다운로드, 삭제 MIME 체크 등

 

 

 

[FILE] 자바 + 스프링 부트 + Axios(rest api) 파일 업로드, 다운로드, 삭제 MIME 체크 등

2022.01.01 - [실전 공부/Git] - [Git] 자주 사용하는 Git 명령어 요약 [Git] 자주 사용하는 Git 명령어 요약 2021.12.04 - [실전 공부/AWS&Docker&Linux] - [AWS_DB] AWS EC2에 올린 Docker Container DB에 DB..

ktae23.tistory.com

 

 

리소스 핸들러 등록

@Configuration
@EnableScheduling
@EnableTransactionManagement
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/image/**")
.addResourceLocations("file:/home/ubuntu/file-path")
.setCachePeriod(20);
}
}

- addResourceLocations에 등록한 주소로 오는 요청을

- addResourceLocations에 등록한 주소로 매핑해 줌

-  직접 파일 경로를 입력하기 보단 외부 설정 파일을 이용한 주입 권장

 

 

시큐리티 사용 시 필터 무시 요청 주소 등록

@RequiredArgsConstructor
@EnableWebSecurity
public class WebSecurity extends WebSecurityConfigurerAdapter {
@Override
public void configure(org.springframework.security.config.annotation.web.builders.WebSecurity web) throws Exception {
web.ignoring().antMatchers(
"/resources/**", "/dist/**", "/css/**", "/font-awesome/**", "/fonts/**",
"/image/**}
}

 

화면에 출력할 이미지 태그

<img src={`http://localhost:8080/image/${imageName}`}
width="100%"
alt="sampleImage" />}

 

반응형

댓글