ServletResponseFilter对HTML加密,保护美工的劳动果实。

类别:Java 点击:0 评论:0 推荐:


Filter类代码如下:

package com.youk.wf.filter;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.youk.util.HtmlEncode;
/**
 <p>Title: Btoall</p>
 <p>Description: </p>
 <p>Copyright: Copyright (c) 2004</p>
 <p>Company: Youkone inc Ningbo.</p>
 @author victor wu([email protected])
 @version 1.0
 */

public class HtmlProtectFilter
    extends HttpServlet
    implements Filter {
    private FilterConfig filterConfig;
    HtmlEncode encode = new HtmlEncode();
//Handle the passed-in FilterConfig
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

//Process the request/response pair
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain filterChain) {
        try {
            HttpServletResponse httpResponse = (HttpServletResponse) response;
            HttpServletRequest httpRequest = (HttpServletRequest) request;
            String path =httpRequest.getRequestURI();
            //String mimitype = filterConfig.getServletContext().getMimeType(path);
            httpRequest.setCharacterEncoding("GBK");
            String ext = ".ww";
           
           if(path.length() > 2){
               path = path.substring(path.length()-3,path.length()).toLowerCase();
           }
            //需要乱序的资源;后2位;           
            if(!path.equals(ext)){
                filterChain.doFilter(httpRequest, response);
            }else{
                System.out.println("ext="+path);
                httpResponse.setContentType("text/html; charset=GBK");               
                ResponseWrapper wrapper = new ResponseWrapper(
                    httpResponse);
                filterChain.doFilter(httpRequest, wrapper); 
            }
           
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    /**
     *
     */
    public void destroy(){
       
    }


    /**
     *
     * <p>Title: SMS</p>
     * <p>Description: SMS</p>
     * <p>Copyright: Copyright (c) 2003</p>
     * <p>Company: Youk Inc.</p>
     * @author 吴锋彬([email protected]
     * @version 1.0
     */
    public class ResponseWrapper extends HttpServletResponseWrapper {
            private PrintWriter printWriter;
            private ResponseOutputStream outputStream;
   
   
            public ResponseWrapper(ServletResponse response) throws
    java.io.IOException {
                    super((HttpServletResponse) response);
   
                    outputStream = new ResponseOutputStream
    (response.getOutputStream());
                    printWriter = new PrintWriter(outputStream);
            }
   
   
            public ServletOutputStream getOutputStream() throws
    java.io.IOException {
                    return outputStream;
            }
   
   
            public PrintWriter getWriter() throws java.io.IOException {
                    return printWriter;
            }
           
           
    }
   

   
    /**
     *
     * @param args String[]
     */
    public static void main(String[] args){
      try{
        String cn = "你好";
        int h = cn.charAt(0);
        String s = new  Integer(h).toHexString(h);
        //String result = java.net.URLEncoder.encode(,"GBK");
        System.out.println(s);
    }catch(Exception ex){
          ex.printStackTrace();
      }
       
    }
   
   
   
    public class ResponseOutputStream extends ServletOutputStream {
        private OutputStream outputStream;
        private ByteArrayOutputStream arrayStream;
        private boolean closed = false;


        public ResponseOutputStream(OutputStream stream) {
                outputStream = stream;
                arrayStream = new ByteArrayOutputStream();
        }


        public void write(int i) throws java.io.IOException {
       
            arrayStream.write(i);
        }

        public void write(byte[] b)throws java.io.IOException{
            arrayStream.write(b);
        }

        public void write(byte[] b , int a , int bb)throws java.io.IOException{
            arrayStream.write(b,a,bb);
        }

        public void close() throws java.io.IOException {
                if (!closed) {
                  outputStream.close();
                  closed = true;
                }
         }

        public void flush() throws java.io.IOException {
                if (arrayStream.size() != 0) {
                        if (!closed) {
                                processStream();
                                arrayStream = new ByteArrayOutputStream();
                        }
                }
        }

        private void processStream() throws java.io.IOException {
            System.out.println("replace.");
            outputStream.write(replaceContent(arrayStream.toByteArray()));
            outputStream.flush();
        }
       
       
        byte[] replaceContent(byte[] bytesContent) {
            try{
                String s = new String(bytesContent, "GBK");
                if(s.toLowerCase().indexOf("<html>") != -1){
                String encodeHtml = encode.encode(s);
                String addS = "<script>s=\"" + encodeHtml + "\";";
                String sc = "document.write(unescape(s));</script>";
                return (addS+sc).getBytes();
                }
               
            }catch(Exception ex){              
            }
            return bytesContent;
        }
       

 

}


   
}


 weblogic8.1 on red hat / jboss3.2.5 on windows200 测试通过


我的qq:1265877

msn:[email protected]

欢迎和我联系

演示地址:http://www.youkone.com

 

本文地址:http://com.8s8s.com/it/it14655.htm