夸父追日

飞蛾扑火
本blog很少更新
 
 

常用链接

  • 我的随笔
  • 我的评论
  • 我的参与
  • 最新评论

留言簿(3)

  • 给我留言
  • 查看公开留言
  • 查看私人留言

随笔分类

  • class(1) (rss)
  • false (rss)
  • interface (rss)
  • new (rss)
  • null(1) (rss)
  • package(1) (rss)
  • private(2) (rss)
  • protected (rss)
  • public(2) (rss)
  • true (rss)

随笔档案

  • 2007年9月 (1)
  • 2006年4月 (1)
  • 2006年3月 (3)
  • 2006年2月 (1)
  • 2005年12月 (1)

文章分类

  • misc(2) (rss)
  • ruby(1) (rss)
  • spring(4) (rss)
  • web service(1) (rss)

文章档案

  • 2006年5月 (1)
  • 2006年4月 (1)
  • 2006年3月 (6)

友情链接

  • 一再沈醉 (rss)

搜索

  •  

最新评论

  • 1. re: 如何应对xss攻击?
  • <script>aaa</script>
  • --wd
  • 2. re: Hello,World
  • <script>alert('testAAAAAAAAAAAAAAAAAA')</script>
  • --asas
  • 3. re: Hello,World
  • </script><script>alert('testAAAAAAAAAAAAAAAAAA');</script>
  • --asas
  • 4. re: Hello,World[未登录]
  • <script>alert('test')</script>
  • --haha
  • 5. re: Hello,World
  • <script>alert('test')</script>
  • --cxzvcx

阅读排行榜

  • 1. 如何应对xss攻击?(3974)
  • 2. 用jarkata的commons-VFS监视文件夹的变化(2370)
  • 3. webwork2.2.2里面的富文本编辑器存在的一些问题(1870)
  • 4. 一种新的hibernate和spring结合方式(1847)
  • 5. 写的一个简单的代码生成器(1835)

评论排行榜

  • 1. 如何应对xss攻击?(7)
  • 2. Hello,World(7)
  • 3. 用jarkata的commons-VFS监视文件夹的变化(6)
  • 4. 写的一个简单的代码生成器(4)
  • 5. 一种新的hibernate和spring结合方式(1)

Powered by: 博客园
模板提供:沪江博客
BlogJava | 首页 | 发新随笔 | 发新文章 | 联系 | 聚合 | 管理

2007年9月24日

如何应对xss攻击?
什么是xss攻击?
请点击这里

怎么样避免xss攻击?
使用jsp tag来输出,比如<c:out/><s:property/>,这些tag默认都对html代码转义了

但是又引入了一个新的难题,怎么使用富文本编辑器?
富文本编辑器最终得到的内容是html代码,并且要原样输出.但是html代码是可以直接或者间接编辑的,这样的话就可以插入任何代码,可以用<script>标签,可以用<a href="javascript:xxx">,可以用css里面的expression,<img onload="xxx">等等.

解决方法有两个
1.在服务器端过滤这些script,很难全部过滤,不知道有没有现成的lib可以用
2.干脆不使用,像大部分论坛那样搞几个定义好的tag,比如[a][/a]插入超链接,这样不够灵活也做不到所见即所得,但是安全第一

不知道大家有什么好的解决方法?

posted @ 2007-09-24 11:33 quaff 阅读(3974) | 评论 (7) | 编辑 收藏
 

2006年4月10日

一种新的hibernate和spring结合方式

内容篇幅较长,请点击这里阅读全文。

posted @ 2006-04-10 21:03 quaff 阅读(1847) | 评论 (1) | 编辑 收藏
 

2006年3月26日

webwork2.2.2的富文本编辑器的不完美解决方法
先说怎么做,再稍微解释一下为什么这么做
1.webwork.properties里面设置
webwork.serve.static=false
webwork.multipart.parser=cos
2.在webapp的根目录下建一个文件夹webwork,把webwork.jar里面的/com/opensymphony/webwork/static和/template里面的文件和复制到自己建的webwork文件夹里面,在/webwork/richtexteditor里面建文件夹data(可能会自动建,没测试过)
3.写一个RichtexteditorConnector
package test;

import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.webwork.components.DefaultRichtexteditorConnector;


public class RichtexteditorConnector extends DefaultRichtexteditorConnector {
    
public static final Log _log = LogFactory
            .getLog(RichtexteditorConnector.
class);

    
private ServletContext servletContext;

    
public void setServletContext(ServletContext servletContext) {
        
this.servletContext = servletContext;
    }

    
protected String calculateActualServerPath(String actualServerPath,
            String type, String folderPath) 
throws Exception {
        String path 
= servletContext.getRealPath(actualServerPath);
        path 
= path.replace('\\', '/');
        makeDirIfNotExists(path);
        path 
= path.endsWith("/") ? path : path + "/";
        
return path + type + folderPath;
    }

    
protected Folder[] getFolders(String virtualFolderPath, String type)
            
throws Exception {
        String path 
= calculateActualServerPath(getActualServerPath(), type,
                virtualFolderPath);
        makeDirIfNotExists(path);
        java.io.File f 
= new java.io.File(path);
        java.io.File[] children 
= f.listFiles(new FileFilter() {
            
public boolean accept(java.io.File pathname) {
                
if (!pathname.isFile()) {
                    
return true;
                }
                
return false;
            }
        });

        List tmpFolders 
= new ArrayList();
        
for (int a = 0; a < children.length; a++) {
            tmpFolders.add(
new Folder(children[a].getName()));
        }

        
return (Folder[]) tmpFolders.toArray(new Folder[0]);
    }

    
protected FoldersAndFiles getFoldersAndFiles(String virtualFolderPath,
            String type) 
throws Exception {
        String path 
= calculateActualServerPath(getActualServerPath(), type,
                virtualFolderPath);
        makeDirIfNotExists(path);
        java.io.File f 
= new java.io.File(path);
        java.io.File[] children 
= f.listFiles();

        List directories 
= new ArrayList();
        List files 
= new ArrayList();
        
for (int a = 0; a < children.length; a++) {
            
if (children[a].isDirectory()) {
                directories.add(
new Folder(children[a].getName()));
            } 
else {
                
try {
                    files.add(
new File(children[a].getName(),
                            fileSizeInKBytes(children[a])));
                } 
catch (Exception e) {
                    _log.error(
"cannot deal with file " + children[a], e);
                }
            }
        }

        
return new FoldersAndFiles((Folder[]) directories
                .toArray(
new Folder[0]), (File[]) files.toArray(new File[0]));
    }

    
protected FileUploadResult fileUpload(String virtualFolderPath,
            String type, String filename, String contentType,
            java.io.File newFile) {
        
try {
            String tmpDir 
= calculateActualServerPath(getActualServerPath(),
                    type, virtualFolderPath);
            makeDirIfNotExists(tmpDir);
            String tmpFile 
= tmpDir + filename;
            
if (makeFileIfNotExists(tmpFile)) {
                
// already exists
                int a = 0;
                String ext 
= String.valueOf(a);
                tmpFile 
= calculateActualServerPath(getActualServerPath(),
                        type, virtualFolderPath)
                        
+ filename + ext;
                
while (makeFileIfNotExists(tmpFile)) {
                    a 
= a + 1;
                    ext 
= String.valueOf(a);
                    
if (a > 100) {
                        
return FileUploadResult.invalidFile();
                    }
                }
                copyFile(newFile, 
new java.io.File(tmpFile));
                
return FileUploadResult
                        .uploadCompleteWithFilenamChanged(filename 
+ ext);
            } 
else {
                copyFile(newFile, 
new java.io.File(tmpFile));
                
return FileUploadResult.uploadComplete();
            }
        } 
catch (Exception e) {
            _log.error(e.toString(), e);
            e.printStackTrace();
            
return FileUploadResult.invalidFile();
        }
    }

    
protected void unknownCommand(String command, String virtualFolderPath,
            String type, String filename, String contentType,
            java.io.File newFile) {
        
throw new RuntimeException("unknown command " + command);
    }

    
/**
     * 
     * 
@param path
     * 
@return true if file already exists, false otherwise.
     
*/
    
protected boolean makeDirIfNotExists(String path) {
        java.io.File dir 
= new java.io.File(path);
        
if (!dir.exists()) {
            _log.debug(
"make directory " + dir);
            
boolean ok = dir.mkdirs();
            
if (!ok) {
                
throw new RuntimeException("cannot make directory " + dir);
            }
            
return false;
        }
        
return true;
    }

    
protected boolean makeFileIfNotExists(String filePath) throws IOException {
        java.io.File f 
= new java.io.File(filePath);
        
if (!f.exists()) {
            _log.debug(
"creating file " + filePath);
            
boolean ok = f.createNewFile();
            
if (!ok) {
                
throw new RuntimeException("cannot create file " + filePath);
            }
            
return false;
        }
        
return true;
    }

}
4.xwork.xml加上
<package name="richtexteditor-browse" extends="webwork-default"
        namespace
="/webwork/richtexteditor/editor/filemanager/browser/default/connectors/jsp">
        
<action name="connector"
            class
="test.RichtexteditorConnector"
            method
="browse">
            
<param name="actualServerPath">
                /webwork/richtexteditor/data
            
</param>
            
<result name="getFolders" type="richtexteditorGetFolders" />
            
<result name="getFoldersAndFiles"
                type
="richtexteditorGetFoldersAndFiles" />
            
<result name="createFolder"
                type
="richtexteditorCreateFolder" />
            
<result name="fileUpload" type="richtexteditorFileUpload" />
        
</action>
</package>
<package name="richtexteditor-upload" extends="webwork-default"
        namespace
="/webwork/richtexteditor/editor/filemanager/upload">
        
<action name="uploader"
            class
="test.RichtexteditorConnector"
            method
="upload">
            
<param name="actualServerPath">
                /webwork/richtexteditor/data
            
</param>
            
<result name="richtexteditorFileUpload" />
        
</action>
</package>
5.配置标签
<ww:form action="test" method="post">
    <%request.setAttribute("contextPath",request.getContextPath());%>
    <ww:richtexteditor basePath="%{#request.contextPath}/webwork/richtexteditor/"         toolbarCanCollapse="false" width="700" label="description" name="content" defaultLanguage="zh-cn" />
    <ww:submit value="submit" />
</ww:form>
6.服务器端口设置为80

原因
1.
webwork.serve.static=false
/webwork/*这样的URL是可以直接访问不需要通过webwork,这样做是为了自己可以在里面建文件,并且可以方便的访问这些文件
webwork.multipart.parser=cos
设置为jakarta上传文件不成功,com.opensymphony.webwork.interceptor.FileUploadInterceptor解析MultiPartRequestWrapper不成功,原因不清楚,反正用cos就可以了,记得加上cos.jar
2.因为设置了webwork.serve.static=false需要这样做
3.覆盖webwork的DefaultRichtexteditorConnector,最关键的是覆盖calculateActualServerPath()方法,默认是把文件放到/WEB-INF/classes/com/opensymphony/webwork/static/richtexteditor/data/,我们需要放到/webwork/richtexteditor/data/里面,覆盖其他方法是因为默认对文件的访问都是通过
new File(new URI("file://"+filePath);来访问的,会有些问题,直接new File(filePath)就可以了
4.使用自己的RichtexteditorConnector,并且设置参数actualServerPath,其他参数不要改,webwork默认的是这样
5.basePath必须自己指定不能用默认的,虽然指定的值和默认的是一样,但是不自己指定的话它自动加上jsessionid,如/webwork/richtexteditor/;jsessionid=301gs94034pki/editor/fckeditor.html,因为设置了webwork.serve.static=false,所以服务器不能解析这个url
defaultLanguage="zh-cn",不指定的话中文默认是繁体
6.服务器端口必须为80不能为8080,因为fckeditor链接你上传的文件的时候,不会把端口加上去
posted @ 2006-03-26 16:41 quaff 阅读(1577) | 评论 (1) | 编辑 收藏
 

2006年3月24日

webwork2.2.2里面的富文本编辑器存在的一些问题
webwork2.2.2内置了fckeditor,用法
<ww:richtexteditor toolbarCanCollapse="false" width="700" label="描述" name="description"/>
xwork.xml加入:
<package name="richtexteditor-browse" extends="webwork-default" 
        namespace
="/webwork/richtexteditor/editor/filemanager/browser/default/connectors/jsp">
        
<action name="connector" 
            class
="com.opensymphony.webwork.components.DefaultRichtexteditorConnector" 
            method
="browse">
            
<result name="getFolders" type="richtexteditorGetFolders" />
            
<result name="getFoldersAndFiles" 
                type
="richtexteditorGetFoldersAndFiles" />
            
<result name="createFolder" type="richtexteditorCreateFolder" />
            
<result name="fileUpload" type="richtexteditorFileUpload" />
        
</action>
</package>
<package name="richtexteditor-upload" extends="webwork-default" 
        namespace
="/webwork/richtexteditor/editor/filemanager/upload">
        
<action name="uploader" 
            class
="com.opensymphony.webwork.components.DefaultRichtexteditorConnector" 
            method
="upload">
            
<result name="richtexteditorFileUpload" />
        
</action>
</package>



存在如下问题:

1.中文环境默认是繁体,需要去包里面用zh-cn.js替换zh.js
2.String path = "file://"+servletContext.getRealPath("/WEB-INF/classes"+actualServerPath),windows文件路径是\,webwork没有转义或者把\替换成/,对文件的访问都是通过new File(new URI(filePath))来访问,在我机子上会出例外
com.opensymphony.webwork.dispatcher.DispatcherUtils - Could not execute action
java.lang.IllegalArgumentException: URI has an authority component
 at java.io.File.<init>(File.java:326)
 at com.opensymphony.webwork.components.DefaultRichtexteditorConnector.makeDirIfNotExists(DefaultRichtexteditorConnector.java:171)

解决方法是自己写一个类继承com.opensymphony.webwork.components.DefaultRichtexteditorConnector然后覆盖它的方法

3.文件图片默认是保存在\WEB-INF\classes\com\opensymphony\webwork\static\richtexteditor\data里面,写入和读取都存在问题,写入的时候如果context是reloadable="true"则会引发context的reload,在context起来之后新加的文件比如test.jpg不能通过url来访问/webwork/data/test.jpg,这个应该是classloader不会在context启动之后加载这些文件

4.上传的表单里面没有action,虽然tag里面默认是
imageUploadURL="/webwork/richtexteditor/editor/filemanager/upload/uploader.action?Type=Image"
flashUploadURL="/webwork/richtexteditor/editor/filemanager/upload/uploader.action?Type=Flash"
linkUploadURL="/webwork/richtexteditor/editor/filemanager/upload/uploader.action?Type=File"
wikidocs里面说是"/webwork/static/richtexteditor/editor/filemanager/upload/uploader.action?Type=File"应该是多了个/static
但是实际上上传的form里面的action是空的,上传不了东西,这个可能需要去改fckeditor

posted @ 2006-03-24 19:07 quaff 阅读(1870) | 评论 (0) | 编辑 收藏
 

2006年3月2日

用jarkata的commons-VFS监视文件夹的变化
FileSystemManager fsManager = null; 
FileObject listendir 
= null; 
try { 
fsManager 
= VFS.getManager(); 
listendir 
= fsManager.resolveFile(new File( 
"./classes/").getAbsolutePath()); 
} 
catch (FileSystemException e) { 
log.error(
"监视文件夹出错了", e); 
e.printStackTrace(); 
} 
DefaultFileMonitor fm 
= new DefaultFileMonitor(new FileListener() { 
public void fileCreated(FileChangeEvent event) throws Exception { 
monitor(event); 
} 

public void fileDeleted(FileChangeEvent event) throws Exception { 
monitor(event); 
} 

public void fileChanged(FileChangeEvent event) throws Exception { 
monitor(event); 
} 

private void monitor(FileChangeEvent event) { 
FileObject fileObject 
= event.getFile(); 
FileName fileName 
= fileObject.getName(); 
if (fileName.getBaseName().endsWith(".class")) { 
reload(); 
} 
} 
}); 
fm.setRecursive(
true); 
fm.addFile(listendir); 
fm.start();

posted @ 2006-03-02 15:48 quaff 阅读(2370) | 评论 (6) | 编辑 收藏
 

2006年2月11日

写的一个简单的代码生成器
下载

用户只需要写models.xml,具体参数参照models.dtd,执行
java -cp .;freemarker.jar org.mycodegen.Main models.xml

生成的代码包括
1.POJO类,类的hibernate映射文件,DAO类,Manager类,spring的对hibernate封装的配置文件,如果你不使用webwork,只需要这些就行了
2.webwork的Action类,Action的资源文件,Action的校验文件,xwork.xml,
  模板支持freemarker和jsp两种,默认是jsp,列表功能(可以分页),删除功能,录入功能,查看功能,查询功能(查询条件自己写)

生成之后加上依赖的包就可以运行了,剩下的问题就是根据业务逻辑来修改界面和Action等

posted @ 2006-02-11 12:31 quaff 阅读(1835) | 评论 (4) | 编辑 收藏
 

2005年12月8日

Hello,World
留名帖
posted @ 2005-12-08 20:50 quaff 阅读(1080) | 评论 (7) | 编辑 收藏
 
仅列出标题