防止重定向漏洞的方法是创建一份合法URL列表,用户只能从中进行选择,进行重定向操作。

    1. public class RedirectServlet extends HttpServlet {
    2. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
    3. ...
    4. String query = request.getQueryString();
    5. if (query.contains("url")) {
    6. String url = request.getParameter("url");
    7. if(safeUrls.contains(url)){
    8. response.sendRedirect(url);
    9. }
    10. ...
    11. }
    12. }
    13. }