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

