最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot實現(xiàn)防盜鏈功能的示例代碼

 更新時間:2024年12月18日 10:25:21   作者:Mercury_@22  
防盜鏈(Hotlink Protection)是一種防止其他網(wǎng)站直接鏈接到你網(wǎng)站的資源,從而節(jié)省帶寬和保護(hù)內(nèi)容的有效手段,下面我們就來看看如何使用springboot實現(xiàn)防盜鏈功能吧

防盜鏈(Hotlink Protection)是一種防止其他網(wǎng)站直接鏈接到你網(wǎng)站的資源(如圖片、視頻等),從而節(jié)省帶寬和保護(hù)內(nèi)容的有效手段。在Spring Boot應(yīng)用程序中實現(xiàn)防盜鏈功能,可以通過多種方式來達(dá)成,例如使用過濾器(Filter)、攔截器(Interceptor),或者通過配置Nginx等反向代理服務(wù)器。

以下是幾種實現(xiàn)防盜鏈的方法:

1. 使用過濾器(Filter)

你可以創(chuàng)建一個自定義過濾器,在請求到達(dá)實際資源之前檢查HTTP頭中的`Referer`字段。如果`Referer`不在允許的域名列表中,則返回403 Forbidden響應(yīng)或重定向到其他頁面。

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class HotlinkProtectionFilter implements Filter {

    private final String[] allowedDomains = {"yourdomain.com"};

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String referer = httpRequest.getHeader("Referer");

        // Allow if there's no Referer (like direct access or bookmarks)
        if (referer == null || Arrays.stream(allowedDomains).anyMatch(referer::contains)) {
            chain.doFilter(request, response);
        } else {
            HttpServletResponse httpResponse = (HttpServletResponse) response;
            httpResponse.sendError(HttpServletResponse.SC_FORBIDDEN, "Hotlinking not allowed");
        }
    }

    @Override
    public void destroy() {}
}

然后你需要將這個過濾器注冊到Spring的上下文中:

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WebConfig {

    @Bean
    public FilterRegistrationBean<HotlinkProtectionFilter> loggingFilter(){
        FilterRegistrationBean<HotlinkProtectionFilter> registrationBean = new FilterRegistrationBean<>();

        registrationBean.setFilter(new HotlinkProtectionFilter());
        registrationBean.addUrlPatterns("/resources/*"); // 替換為你的資源路徑

        return registrationBean;
    }
}

2. 使用攔截器(Interceptor)

如果你更傾向于MVC模式,可以創(chuàng)建一個攔截器來執(zhí)行相同的邏輯:

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class HotlinkProtectionInterceptor implements HandlerInterceptor {

    private final String[] allowedDomains = {"yourdomain.com"};

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String referer = request.getHeader("Referer");

        if (referer == null || Arrays.stream(allowedDomains).anyMatch(referer::contains)) {
            return true;
        } else {
            response.sendError(HttpServletResponse.SC_FORBIDDEN, "Hotlinking not allowed");
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {}

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {}
}

接著,需要注冊該攔截器:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private HotlinkProtectionInterceptor hotlinkProtectionInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(hotlinkProtectionInterceptor).addPathPatterns("/resources/**");
    }
}

3. 配置Nginx

如果你的應(yīng)用程序是通過Nginx或其他反向代理服務(wù)器訪問的,那么可以在Nginx配置文件中添加防盜鏈規(guī)則,這種方法通常更為高效:

location /resources/ {
    valid_referers none blocked yourdomain.com *.yourdomain.com;
    if ($invalid_referer) {
        return 403;
    }
}

這三種方法都可以有效地防止其他網(wǎng)站直接鏈接到你的資源。選擇哪種方法取決于你的具體需求和技術(shù)棧。

以上就是springboot實現(xiàn)防盜鏈功能的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于springboot防盜鏈功能的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

汝州市| 白河县| 临邑县| 维西| 航空| 光泽县| 长治县| 年辖:市辖区| 五华县| 闽清县| 屏边| 乳山市| 普定县| 东至县| 徐州市| 横山县| 宜黄县| 古蔺县| 白朗县| 汨罗市| 万源市| 霍州市| 霍林郭勒市| 当雄县| 大港区| 开阳县| 霞浦县| 阿荣旗| 井陉县| 当阳市| 布尔津县| 木里| 文水县| 南江县| 增城市| 九寨沟县| 富顺县| 富源县| 台中县| 鄂尔多斯市| 宿州市|