sourcecode

Spring Boot을 사용하여 Dropbox 폴더에 있는 정적 콘텐츠를 처리하려면 어떻게 해야 합니까?

codebag 2023. 3. 4. 14:54
반응형

Spring Boot을 사용하여 Dropbox 폴더에 있는 정적 콘텐츠를 처리하려면 어떻게 해야 합니까?

Spring Boot 웹 애플리케이션을 사용하고 있으며, Linode VPS(~/Dropbox/이미지)의 공유 Dropbox 디렉토리에 있는 정적 콘텐츠를 서비스하고 싶습니다.Spring Boot에서 자동으로 스태틱콘텐츠가 처리되는 것을 확인했습니다.

"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/",

물론 Dropbox 디렉토리는 클래스 패스에 없습니다.

Apache가 Dropbox 폴더에 있는 이미지를 처리하도록 설정할 수 있지만, Spring Security를 이용하여 인증된 사용자만 정적 콘텐츠에 액세스할 수 있도록 제한하고 싶습니다.

독자적인 스태틱자원 핸들러를 추가할 수 있습니다(디폴트를 덮어씁니다).

@Configuration
public class StaticResourceConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("file:/path/to/my/dropbox/");
    }
}

이에 대한 문서는 Spring Boot에 있지만 실제로는 바닐라 Spring MVC 기능일 뿐입니다.

또, 스프링 부트 1.2(내 생각에)부터, 간단하게 설정할 수 있습니다.spring.resources.staticLocations.

스프링 부트(스프링 경유)를 통해 기존 리소스 핸들러에 쉽게 추가할 수 있게 되었습니다.Dave Syers의 답변을 참조하십시오.기존 정적 리소스 핸들러에 추가하려면 기존 경로를 재정의하지 않는 리소스 핸들러 경로를 사용하십시오.

아래의 두 "또한" 메모는 여전히 유효합니다.

. . .

[편집: 아래 접근방식은 유효하지 않습니다]

기본 정적 리소스 핸들러를 확장하려면 다음과 같은 기능이 있습니다.

@Configuration
@AutoConfigureAfter(DispatcherServletAutoConfiguration.class)
public class CustomWebMvcAutoConfig extends
                    WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter {

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    String myExternalFilePath = "file:///C:/Temp/whatever/m/";

    registry.addResourceHandler("/m/**").addResourceLocations(myExternalFilePath);

    super.addResourceHandlers(registry);
  }

}

문의처super.addResourceHandlers는 디폴트 핸들러를 설정합니다.

기타:

  • 외부 파일 경로의 후행 슬래시에 주의해 주세요(URL 매핑에 대한 예상에 따라 다름).
  • WebMvcAutoConfigurationAdapter의 소스 코드를 검토하십시오.

@Dave Syers의 답변을 바탕으로 스프링 부트 프로젝트에 다음 클래스를 추가합니다.

@Configuration
public class StaticResourceConfiguration extends WebMvcConfigurerAdapter {

 private static final Logger LOG = LoggerFactory.getLogger(StaticResourceConfiguration.class);

 @Value("${static.path}")
 private String staticPath;

 @Override
 public void addResourceHandlers(ResourceHandlerRegistry registry) {

    if(staticPath != null) {
        LOG.info("Serving static content from " + staticPath);
        registry.addResourceHandler("/**").addResourceLocations("file:" + staticPath);
    }
 }

 // see https://stackoverflow.com/questions/27381781/java-spring-boot-how-to-map-my-my-app-root-to-index-html
 @Override
 public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("redirect:/index.html");
 }
}

이를 통해 파라미터를 사용하여 스프링 부트 앱을 시작할 수 있습니다.--static.path맘에 들다

java -jar spring-app-1.0-SNAPSHOT.jar --static.path=/path/to/my/static-files/

이것은 개발 및 테스트에 매우 편리합니다.

소유물이 있다spring.resources.staticLocations로 설정할 수 있습니다.application.properties. 기본 위치보다 우선됩니다.org.springframework.boot.autoconfigure.web.ResourceProperties.

  • OS: Windows 10
  • 스프링 부트: 2.1.2

c:/images의 정적 콘텐츠를 제공하고 싶었다.

이 속성을 추가하면 효과가 있었습니다.

spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:///C:/images/

Spring Boot Doc 부록 A에서 속성의 원래 값을 찾았습니다.

이를 통해 c:/images/image.jpg는 http://localhost:8080/image.jpg로 액세스할 수 있게 됩니다.

@마크 셰퍼

슬래시슬래시)를 추가해 ./[ ] :

spring.resources.static-locations=file:/opt/x/y/z/static/

★★★★★★★★★★★★★★★★★.http://<host>/index.html이제 도달 가능합니다.

@DaveSyer를 기반으로 @kalietech 및 @asmaier는 springboot v2+ 방법에 응답합니다.

@Configuration
@AutoConfigureAfter(DispatcherServletAutoConfiguration.class)
public class StaticResourceConfiguration implements WebMvcConfigurer  {

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    String myExternalFilePath = "file:///C:/temp/whatever/m/";

     registry.addResourceHandler("/m/**").addResourceLocations(myExternalFilePath);

  }

}

를 제공하다

는 ㅇㅇㅇㅇㅇㅇㅇㅇ다를 넣었습니다.spring.resources.static-location=file:../frontend/buildapplication.properties

index.html.build 표시

도 있습니다.

spring.resources.static-location=file:/User/XYZ/Desktop/frontend/build

마찬가지로 Dropbox 폴더 경로를 추가할 수 있습니다.

현재 Spring-Boot 버전 1.5.3의 경우 파라미터는 다음과 같습니다.

spring.resources.static-locations

구성한 업데이트

'spring.displaces.static-displaces=파일:/opt/x/y/z/staticlaces

전화를 걸 때 이 폴더에 있는 내 인덱스.인덱스를 받을 수 있을 것으로 기대됩니다.

http://<host>/index.html

이것은 효과가 없었다.다음 URL에 폴더 이름을 포함해야 했습니다.

http://<host>/static/index.html

FWIW는 spring.resources.static-locations★★★★★★★★★★★★★★★★★★:

report.location=file:/Users/bill/report/html/
spring.thymeleaf.prefix=${report.location}

WebMvcConfigurerAdapter는 현재 권장되지 않습니다(WebMvcConfigurerAdapter 참조).Java 8 기본 메서드 때문에 WebMvcConfigr만 구현하면 됩니다.

폴더를 ServletContext의 루트에 배치할 수 있습니다.

그런 다음 application.yml에서 이 디렉토리에 대한 상대 경로 또는 절대 경로를 지정합니다.

spring:
  resources:
    static-locations: file:some_temp_files/

이 폴더의 리소스는 다운로드용으로 다음 위치에서 사용할 수 있습니다.

http://<host>:<port>/<context>/your_file.csv

언급URL : https://stackoverflow.com/questions/21123437/how-do-i-use-spring-boot-to-serve-static-content-located-in-dropbox-folder

반응형