B/S防止盗链原理

import java.io.IOException;

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

public class RequestServlet3 extends HttpServlet {

    // 防止盗链
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 分析原理:头信息referer 得知该次请求来源,判断是否为盗链
        String referer = request.getHeader("referer");
        response.setContentType("text/html;charset=utf-8");
        if (referer == null || !referer.startsWith("http://localhost")) {// 判断referer不能为null,必须该站点域名开始
            response.getWriter().println("

盗链资源,可耻!

");
        } else {
            response.getWriter().println("

欢迎访问...

");
        }
    }

你可能感兴趣的:(java代码片段)