posts - 64,comments - 22,trackbacks - 0
     摘要: Servlet 3.0作为Java EE6规范体系中一员,随着Java EE6规范一起发布。该版本在前一版本(Servlet 2.5)的基础上提供了若干新特性用于简化Web应用的开发和部署。  阅读全文
posted @ 2013-01-16 11:08 hellxoul 阅读(52) | 评论 (0)编辑 收藏
     摘要: n 可以用来改变根据自己需要改变加载的优先级!  阅读全文
posted @ 2013-01-16 10:50 hellxoul 阅读(57) | 评论 (0)编辑 收藏
    只有注册用户登录后才能阅读该文。阅读全文
posted @ 2013-01-16 10:46 hellxoul 阅读(59) | 评论 (0)编辑 收藏
    只有注册用户登录后才能阅读该文。阅读全文
posted @ 2013-01-16 10:44 hellxoul 阅读(55) | 评论 (0)编辑 收藏
    只有注册用户登录后才能阅读该文。阅读全文
posted @ 2012-12-21 11:03 hellxoul 阅读(71) | 评论 (0)编辑 收藏
接下来,我们将重点讨论一下Struts2中的拦截器的内部结构和执行顺序,并结合源码进行分析。

Interceptor结构 Top

让我们再来回顾一下之前我们曾经用过的一张Action LifeCycle的图:



图中,我们可以发现,Struts2的Interceptor一层一层,把Action包裹在最里面。这样的结构,大概有以下一些特点:

1. 整个结构就如同一个堆栈,除了Action以外,堆栈中的其他元素是Interceptor

2. Action位于堆栈的底部。由于堆栈"先进后出"的特性,如果我们试图把Action拿出来执行,我们必须首先把位于Action上端的Interceptor拿出来执行。这样,整个执行就形成了一个递归调用

3. 每个位于堆栈中的Interceptor,除了需要完成它自身的逻辑,还需要完成一个特殊的执行职责。这个执行职责有3种选择:

1) 中止整个执行,直接返回一个字符串作为resultCode

2) 通过递归调用负责调用堆栈中下一个Interceptor的执行

3) 如果在堆栈内已经不存在任何的Interceptor,调用Action


Struts2的拦截器结构的设计,实际上是一个典型的责任链模式的应用。首先将整个执行划分成若干相同类型的元素,每个元素具备不同的逻辑责任,并将他们纳入到一个链式的数据结构中(我们可以把堆栈结构也看作是一个递归的链式结构),而每个元素又有责任负责链式结构中下一个元素的执行调用。

这样的设计,从代码重构的角度来看,实际上是将一个复杂的系统,分而治之,从而使得每个部分的逻辑能够高度重用并具备高度可扩展性。所以,Interceptor结构实在是Struts2/Xwork设计中的精华之笔。

Interceptor执行分析 Top

Interceptor的定义

我们来看一下Interceptor的接口的定义:

Java代码  收藏代码
  1. public interface Interceptor extends Serializable {  
  2.   
  3.     /** 
  4.      * Called to let an interceptor clean up any resources it has allocated. 
  5.      */  
  6.     void destroy();  
  7.   
  8.     /** 
  9.      * Called after an interceptor is created, but before any requests are processed using 
  10.      * {@link #intercept(com.opensymphony.xwork2.ActionInvocation) intercept} , giving 
  11.      * the Interceptor a chance to initialize any needed resources. 
  12.      */  
  13.     void init();  
  14.   
  15.     /** 
  16.      * Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the 
  17.      * request by the {@link ActionInvocation} or to short-circuit the processing and just return a String return code. 
  18.      * 
  19.      * @return the return code, either returned from {@link ActionInvocation#invoke()}, or from the interceptor itself. 
  20.      * @throws Exception any system-level error, as defined in {@link com.opensymphony.xwork2.Action#execute()}. 
  21.      */  
  22.     String intercept(ActionInvocation invocation) throws Exception;  
  23. }  


Interceptor的接口定义没有什么特别的地方,除了init和destory方法以外,intercept方法是实现整个拦截器机制的核心方法。而它所依赖的参数ActionInvocation则是我们之前章节中曾经提到过的著名的Action调度者

我们再来看看一个典型的Interceptor的抽象实现类:

Java代码  收藏代码
  1. public abstract class AroundInterceptor extends AbstractInterceptor {  
  2.       
  3.     /* (non-Javadoc) 
  4.      * @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation) 
  5.      */  
  6.     @Override  
  7.     public String intercept(ActionInvocation invocation) throws Exception {  
  8.         String result = null;  
  9.   
  10.         before(invocation);  
  11.         // 调用下一个拦截器,如果拦截器不存在,则执行Action  
  12.         result = invocation.invoke();  
  13.         after(invocation, result);  
  14.   
  15.         return result;  
  16.     }  
  17.       
  18.     public abstract void before(ActionInvocation invocation) throws Exception;  
  19.   
  20.     public abstract void after(ActionInvocation invocation, String resultCode) throws Exception;  
  21.   
  22. }  


在这个实现类中,实际上已经实现了最简单的拦截器的雏形。或许大家对这样的代码还比较陌生,这没有关系。我在这里需要指出的是一个很重要的方法 invocation.invoke()。这是ActionInvocation中的方法,而ActionInvocation是Action调度者,所 以这个方法具备以下2层含义:

1. 如果拦截器堆栈中还有其他的Interceptor,那么invocation.invoke()将调用堆栈中下一个Interceptor的执行。

2. 如果拦截器堆栈中只有Action了,那么invocation.invoke()将调用Action执行。

所以,我们可以发现,invocation.invoke()这个方法其实是整个拦截器框架的实现核心。基于这样的实现机制,我们还可以得到下面2个非常重要的推论:

1. 如果在拦截器中,我们不使用invocation.invoke()来完成堆栈中下一个元素的调用,而是直接返回一个字符串作为执行结果,那么整个执行将被中止。

2. 我们可以以invocation.invoke()为界,将拦截器中的代码分成2个部分,在invocation.invoke()之前的代码,将会在 Action之前被依次执行,而在invocation.invoke()之后的代码,将会在Action之后被逆序执行。

由此,我们就可以通过invocation.invoke()作为Action代码真正的拦截点,从而实现AOP。

Interceptor拦截类型

从上面的分析,我们知道,整个拦截器的核心部分是invocation.invoke()这个函数的调用位置。事实上,我们也正式根据这句代码的调用位置,来进行拦截类型的区分的。在Struts2中,Interceptor的拦截类型,分成以下三类:

1. before

before拦截,是指在拦截器中定义的代码,它们存在于invocation.invoke()代码执行之前。这些代码,将依照拦截器定义的顺序,顺序执行

2. after

after拦截,是指在拦截器中定义的代码,它们存在于invocation.invoke()代码执行之后。这些代码,将一招拦截器定义的顺序,逆序执行

3. PreResultListener

有的时候,before拦截和after拦截对我们来说是不够的,因为我们需要在Action执行完之后,但是还没有回到视图层之前,做一些事 情。Struts2同样支持这样的拦截,这种拦截方式,是通过在拦截器中注册一个PreResultListener的接口来实现的。

Java代码  收藏代码
  1. public interface PreResultListener {  
  2.   
  3.     /** 
  4.      * This callback method will be called after the Action execution and before the Result execution. 
  5.      * 
  6.      * @param invocation 
  7.      * @param resultCode 
  8.      */  
  9.     void beforeResult(ActionInvocation invocation, String resultCode);  
  10. }  


在这里,我们看到,Struts2能够支持如此多的拦截类型,与其本身的数据结构和整体设计有很大的关系。正如我在之前的文章中所提到的:

downpour 写道
因为Action是一个普通的Java类,而不是一个Servlet类,完全脱离于Web容器,所以我们就能够更加方便地对Control层进行合理的层次设计,从而抽象出许多公共的逻辑,并将这些逻辑脱离出Action对象本身。


我们可以看到,Struts2对于整个执行的划分,从Interceptor到Action一直到Result,每一层都职责明确。不仅如此,Struts2还为每一个层次之前都设立了恰如其分的插入点。使得整个Action层的扩展性得到了史无前例的提升。

Interceptor执行顺序

Interceptor的执行顺序或许是我们在整个过程中最最关心的部分。根据上面所提到的概念,我们实际上已经能够大致明白了Interceptor的执行机理。我们来看看Struts2的Reference对Interceptor执行顺序的一个形象的例子。

如果我们有一个interceptor-stack的定义如下:

Xml代码  收藏代码
  1. <interceptor-stack name="xaStack">  
  2.   <interceptor-ref name="thisWillRunFirstInterceptor"/>  
  3.   <interceptor-ref name="thisWillRunNextInterceptor"/>  
  4.   <interceptor-ref name="followedByThisInterceptor"/>  
  5.   <interceptor-ref name="thisWillRunLastInterceptor"/>  
  6. </interceptor-stack>  


那么,整个执行的顺序大概像这样:



在这里,我稍微改了一下Struts2的Reference中的执行顺序示例,使得整个执行顺序更加能够被理解。我们可以看到,递归调用保证了各种各样的拦截类型的执行能够井井有条。

请注意在这里,每个拦截器中的代码的执行顺序,在Action之前,拦截器的执行顺序与堆栈中定义的一致;而在Action和Result之后,拦截器的执行顺序与堆栈中定义的顺序相反。

源码解析 Top

接下来我们就来看看源码,看看Struts2是如何保证拦截器、Action与Result三者之间的执行顺序的。

之前我曾经提到,ActionInvocation是Struts2中的调度器,所以事实上,这些代码的调度执行,是在 ActionInvocation的实现类中完成的,这里,我抽取了DefaultActionInvocation中的invoke()方法,它将向我 们展示一切。

Java代码  收藏代码
  1. /** 
  2.  * @throws ConfigurationException If no result can be found with the returned code 
  3.  */  
  4. public String invoke() throws Exception {  
  5.     String profileKey = "invoke: ";  
  6.     try {  
  7.         UtilTimerStack.push(profileKey);  
  8.               
  9.         if (executed) {  
  10.             throw new IllegalStateException("Action has already executed");  
  11.         }  
  12.         // 依次调用拦截器堆栈中的拦截器代码执行  
  13.         if (interceptors.hasNext()) {  
  14.             final InterceptorMapping interceptor = (InterceptorMapping) interceptors.next();  
  15.             UtilTimerStack.profile("interceptor: "+interceptor.getName(),   
  16.                     new UtilTimerStack.ProfilingBlock<String>() {  
  17.                         public String doProfiling() throws Exception {  
  18.                          // 将ActionInvocation作为参数,调用interceptor中的intercept方法执行  
  19.                             resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);  
  20.                             return null;  
  21.                         }  
  22.             });  
  23.         } else {  
  24.             resultCode = invokeActionOnly();  
  25.         }  
  26.   
  27.         // this is needed because the result will be executed, then control will return to the Interceptor, which will  
  28.         // return above and flow through again  
  29.         if (!executed) {  
  30.             // 执行PreResultListener  
  31.             if (preResultListeners != null) {  
  32.                 for (Iterator iterator = preResultListeners.iterator();  
  33.                     iterator.hasNext();) {  
  34.                     PreResultListener listener = (PreResultListener) iterator.next();  
  35.                           
  36.                     String _profileKey="preResultListener: ";  
  37.                     try {  
  38.                             UtilTimerStack.push(_profileKey);  
  39.                             listener.beforeResult(this, resultCode);  
  40.                     }  
  41.                     finally {  
  42.                             UtilTimerStack.pop(_profileKey);  
  43.                     }  
  44.                 }  
  45.             }  
  46.   
  47.             // now execute the result, if we're supposed to  
  48.             // action与interceptor执行完毕,执行Result  
  49.             if (proxy.getExecuteResult()) {  
  50.                 executeResult();  
  51.             }  
  52.   
  53.             executed = true;  
  54.         }  
  55.   
  56.         return resultCode;  
  57.     }  
  58.     finally {  
  59.         UtilTimerStack.pop(profileKey);  
  60.     }  
  61. }  


从源码中,我们可以看到,我们之前提到的Struts2的Action层的4个不同的层次,在这个方法中都有体现,他们分别是:拦截器 (Interceptor)、Action、PreResultListener和Result。在这个方法中,保证了这些层次的有序调用和执行。由此我 们也可以看出Struts2在Action层次设计上的众多考虑,每个层次都具备了高度的扩展性和插入点,使得程序员可以在任何喜欢的层次加入自己的实现机制改变Action的行为。

在这里,需要特别强调的,是其中拦截器部分的执行调用:

Java代码  收藏代码
  1. resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);  


表面上,它只是执行了拦截器中的intercept方法,如果我们结合拦截器来看,就能看出点端倪来:

Java代码  收藏代码
  1. public String intercept(ActionInvocation invocation) throws Exception {  
  2.     String result = null;  
  3.   
  4.         before(invocation);  
  5.         // 调用invocation的invoke()方法,在这里形成了递归调用  
  6.         result = invocation.invoke();  
  7.         after(invocation, result);  
  8.   
  9.         return result;  
  10. }  


原来在intercept()方法又对ActionInvocation的invoke()方法进行递归调用,ActionInvocation 循环嵌套在intercept()中,一直到语句result = invocation.invoke()执行结束。这样,Interceptor又会按照刚开始执行的逆向顺序依次执行结束。
posted @ 2012-12-14 10:41 hellxoul 阅读(245) | 评论 (0)编辑 收藏

自己制作ssl证书:自己签发免费ssl证书,为nginx生成自签名ssl证书

    这里说下Linux 系统怎么通过openssl命令生成 证书。

  首先执行如下命令生成一个key
openssl genrsa -des3 -out ssl.key 1024
    然后他会要求你输入这个key文件的密码。不推荐输入。因为以后要给nginx使用。每次reload nginx配置时候都要你验证这个PAM密码的。
    由于生成时候必须输入密码。你可以输入后 再删掉。

mv ssl.key xxx.key
openssl rsa -in xxx.key -out ssl.key
rm xxx.key
    然后根据这个key文件生成证书请求文件
openssl req -new -key ssl.key -out ssl.csr
    以上命令生成时候要填很多东西 一个个看着写吧(可以随便,毕竟这是自己生成的证书)

    最后根据这2个文件生成crt证书文件
openssl x509 -req -days 365 -in ssl.csr -signkey ssl.key -out ssl.crt
    这里365是证书有效期 推荐3650哈哈。这个大家随意。最后使用到的文件是key和crt文件。

    如果需要用pfx 可以用以下命令生成
openssl pkcs12 -export -inkey ssl.key -in ssl.crt -out ssl.pfx

    在需要使用证书的nginx配置文件的server节点里加入以下配置就可以了。
ssl on;
ssl_certificate /home/ssl.crt;
ssl_certificate_key /home/ssl.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;
    然后重启nginx就大功告成了

posted @ 2012-11-29 16:50 hellxoul 阅读(2055) | 评论 (0)编辑 收藏

Nginx + https + 免费SSL证书配置指南

时间:2010-02-24 20:15:42   类别:技术   访问:11,529 views   RSS 2.0   评论  

请参考 Nginx Wiki http://wiki.nginx.org/NginxHttpSslModule

生成证书

$ cd /usr/local/nginx/conf
$ openssl genrsa -des3 -out server.key 1024
$ openssl req -new -key server.key -out server.csr
$ cp server.key server.key.org
$ openssl rsa -in server.key.org -out server.key
$ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

编辑 nginx.conf

server {
    server_name YOUR_DOMAINNAME_HERE;
    listen 443;
    ssl on;
    ssl_certificate /usr/local/nginx/conf/server.crt;
    ssl_certificate_key /usr/local/nginx/conf/server.key;
}

OK, 完成了。但这样证书是不被信任的,自己玩玩还行,要被信任请看下面。

以下内容转载自
http://goo.gl/YOb5
http://goo.gl/Gftj

HTTPS(全称:Hypertext Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版。即HTTP下加入SSL层,HTTPS的安全基础是SSL,因此加密的详细内容 请看SSL。

它是一个URI scheme(抽象标识符体系),句法类同http:体系。用于安全的HTTP数据传输。https:URL表明它使用了HTTP,但HTTPS存在不同 于HTTP的默认端口及一个加密/身份验证层(在HTTP与TCP之间)。这个系统的最初研发由网景公司进行,提供了身份验证与加密通讯方法,现在它被广 泛用于万维网上安全敏感的通讯,例如交易支付方面。

1、自行颁发不受浏览器信任的SSL证书:
HTTPS的SSL证书可以自行颁发,Linux下的颁发步骤如下:

openssl genrsa -des3 -out api.bz.key 1024
openssl  req -new -key api.bz.key -out api.bz.csr
openssl rsa -in api.bz.key  -out api.bz_nopass.key

Nginx + https + 免费SSL证书配置指南 7f13bbb85e123811 thumb

nginx.conf 的SSL证书配置,使用 api.bz_nopass.key,在启动Nginx是无需输入SSL证书密码,而使用 api.bz.key 则需要输入密码:

server {
server_name sms.api.bz;
listen  443;
index index.html index.htm index.php;
root  /data0/htdocs/api.bz;
ssl on;
ssl_certificate api.bz.crt;
ssl_certificate_key api.bz_nopass.key;
......
}

自行颁发的SSL证书虽然能够实现加密传输功能,但得不到浏览器的信任,会出现以下提示:
Nginx + https + 免费SSL证书配置指南 6c3f2b38523ed259

posted @ 2012-11-29 14:44 hellxoul 阅读(382) | 评论 (0)编辑 收藏
    只有注册用户登录后才能阅读该文。阅读全文
posted @ 2012-11-08 12:52 hellxoul 阅读(39) | 评论 (0)编辑 收藏
    只有注册用户登录后才能阅读该文。阅读全文
posted @ 2012-10-29 13:01 hellxoul 阅读(68) | 评论 (0)编辑 收藏
仅列出标题
共7页: 上一页 1 2 3 4 5 6 7 下一页