springboot實(shí)現(xiàn)防盜鏈功能的示例代碼
防盜鏈(Hotlink Protection)是一種防止其他網(wǎng)站直接鏈接到你網(wǎng)站的資源(如圖片、視頻等),從而節(jié)省帶寬和保護(hù)內(nèi)容的有效手段。在Spring Boot應(yīng)用程序中實(shí)現(xiàn)防盜鏈功能,可以通過(guò)多種方式來(lái)達(dá)成,例如使用過(guò)濾器(Filter)、攔截器(Interceptor),或者通過(guò)配置Nginx等反向代理服務(wù)器。
以下是幾種實(shí)現(xiàn)防盜鏈的方法:
1. 使用過(guò)濾器(Filter)
你可以創(chuàng)建一個(gè)自定義過(guò)濾器,在請(qǐng)求到達(dá)實(shí)際資源之前檢查HTTP頭中的`Referer`字段。如果`Referer`不在允許的域名列表中,則返回403 Forbidden響應(yīng)或重定向到其他頁(yè)面。
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() {} }
然后你需要將這個(gè)過(guò)濾器注冊(cè)到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)建一個(gè)攔截器來(lái)執(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 {} }
接著,需要注冊(cè)該攔截器:
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)用程序是通過(guò)Nginx或其他反向代理服務(wù)器訪問(wèn)的,那么可以在Nginx配置文件中添加防盜鏈規(guī)則,這種方法通常更為高效:
location /resources/ { valid_referers none blocked yourdomain.com *.yourdomain.com; if ($invalid_referer) { return 403; } }
這三種方法都可以有效地防止其他網(wǎng)站直接鏈接到你的資源。選擇哪種方法取決于你的具體需求和技術(shù)棧。
以上就是springboot實(shí)現(xiàn)防盜鏈功能的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于springboot防盜鏈功能的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java返回前端樹形結(jié)構(gòu)數(shù)據(jù)的2種實(shí)現(xiàn)方式
近期項(xiàng)目有個(gè)需求,需要將組織機(jī)構(gòu)數(shù)據(jù)拼成樹型結(jié)構(gòu)返回至前端,下面這篇文章主要給大家介紹了關(guān)于java返回前端樹形結(jié)構(gòu)數(shù)據(jù)的2種實(shí)現(xiàn)方式,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05Springboot actuator生產(chǎn)就緒功能實(shí)現(xiàn)解析
這篇文章主要介紹了Springboot actuator生產(chǎn)就緒功能實(shí)現(xiàn)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05SpringBoot+Vue實(shí)現(xiàn)數(shù)據(jù)添加功能
這篇文章主要介紹了SpringBoot+Vue實(shí)現(xiàn)數(shù)據(jù)添加功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03java使用MulticastSocket實(shí)現(xiàn)基于廣播的多人聊天室
這篇文章主要為大家詳細(xì)介紹了java使用MulticastSocket實(shí)現(xiàn)基于廣播的多人聊天室,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01關(guān)于intellij idea打開就閃退或關(guān)閉詳細(xì)解決辦法
這篇文章主要介紹了關(guān)于intellij idea打開就閃退或關(guān)閉詳細(xì)解決辦法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03java常見報(bào)錯(cuò)及解決方案總結(jié)
這篇文章主要介紹了Java編程中常見錯(cuò)誤類型及示例,包括語(yǔ)法錯(cuò)誤、空指針異常、數(shù)組下標(biāo)越界、類型轉(zhuǎn)換異常、文件未找到異常、除以零異常、非法線程操作異常、方法未定義異常、死鎖和類未找到異常,文中通過(guò)代碼將解決方案介紹的非常詳細(xì),需要的朋友可以參考下2025-03-03SpringBoot中使用Redis?Stream實(shí)現(xiàn)消息監(jiān)聽示例
本文主要介紹了SpringBoot中使用Redis?Stream實(shí)現(xiàn)消息監(jiān)聽示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06JavaScript中new運(yùn)算符的實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了JavaScript中new運(yùn)算符的實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10