﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-像一颗晨土-随笔分类-ajax</title><link>http://www.blogjava.net/aichan/category/9028.html</link><description /><language>zh-cn</language><lastBuildDate>Tue, 27 Feb 2007 12:35:36 GMT</lastBuildDate><pubDate>Tue, 27 Feb 2007 12:35:36 GMT</pubDate><ttl>60</ttl><item><title>Spring AOP 在DWR安全上的应用</title><link>http://www.blogjava.net/aichan/archive/2006/05/24/47735.html</link><dc:creator>艾尘</dc:creator><author>艾尘</author><pubDate>Tue, 23 May 2006 16:26:00 GMT</pubDate><guid>http://www.blogjava.net/aichan/archive/2006/05/24/47735.html</guid><wfw:comment>http://www.blogjava.net/aichan/comments/47735.html</wfw:comment><comments>http://www.blogjava.net/aichan/archive/2006/05/24/47735.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/aichan/comments/commentRss/47735.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/aichan/services/trackbacks/47735.html</trackback:ping><description><![CDATA[<font size="1"><font size="2">在<a href="http://www.blogjava.net/aichan/archive/2006/05/10/45321.html">上一篇文章</a>里提到了可以让 DWR自动往Service里面注入一个与Servlet相关的对象，作为参数。只是这样，要每个Service都加上这样的一个参数，奇丑无比！想了 想，决定就让DWR污染一下，Service保留原样。只是增加一个MethodBeforeAdvice（正是它让DWR的API污染了一下。），来对 Service的方法进行拦截，可以在Service的调用之前对操作进行所谓的身份验证，授权之类的操作。完整的拦截模块几个类文件加个Spring配 置文件搞定。</font></font>
<p style="text-align: left;"><font size="1"><font size="2">&nbsp;&nbsp;&nbsp;&nbsp;  实现拦截功能的类有：</font></font></p>
<p style="text-align: left;"><font size="1"><font size="2">&nbsp;&nbsp;&nbsp; 一、MainInteceptor，主拦截器，所以DWR的远程调用都会被拦截，当然， 调用是细到方法级的，可配置的，该类实现了Spring AOP的MethodBeforeAdvice接口，该类有一个集合成员变量，成员为IInteceptor。</font></font></p>
<p style="text-align: left;"><font size="1"><font size="2">&nbsp;&nbsp;&nbsp; 二、IInteceptor，是一个接口，仅有一个execute(AopContext context)函数。该接口是拦截器（与前面的主拦截器不同，本接口定义的拦截器是可以由用户去实现，并且可以有多个）。实现接口只需要实现方法。这些 拦截器会被主拦截器回调。 比如要实现一个身份验证的拦截，SecuityInteceptor，在配置文件中把这个拦截器设置为主拦截器的属性即可获得回调。</font></font></p>
<p style="text-align: left;"><font size="1"><font size="2">&nbsp;&nbsp;&nbsp;&nbsp; 三、AopContext，Aop上下文。在主拦截器调用IInteceptor的对象时，把这个上下文对象作为参数来调用子拦截器。从该上下文可获得一系列信息，如HttpSession，HttpRequest等。甚至你可以自已设置属性。&nbsp;</font></font></p>
<p style="text-align: left;"><font size="1"><font size="2">&nbsp;&nbsp;&nbsp;  &nbsp; 下面看一些代码片断:<br /><strong> MainInteceptor:</strong></font></font></p>
<p style="text-align: left;"><font size="1"><font size="2">&nbsp;private List&lt;IInterceptor&gt; interceptors;//定义一系列的子拦截器</font></font></p>
<p style="text-align: left;"><font size="1"><font size="2">public void setInterceptors(List&lt;IInterceptor&gt; interceptors) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;this.interceptors = interceptors;<br />&nbsp;&nbsp;&nbsp; }</font></font></p>
<p style="text-align: left;"><font size="1"><font size="2">在before(Method method, Object[] params, Object target)方法里：</font></font></p>
<p style="text-align: left;"><font size="1"><font size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WebContext ctx = WebContextFactory.get();//唯一被DWR污染的地方<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;HttpSession session = ctx.getSession();<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;AopContext context = new AopContext(); &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; context.setSession(session);<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;for(Iterator it = interceptors.iterator(); it.hasNext();){<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;IInterceptor interceptor = (IInterceptor) it.next();<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;interceptor.execute(context);<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;} <br /></font></font></p>
<p style="text-align: left;"><strong><font size="1"><font size="2">&nbsp;</font></font><font size="1"><font size="2">IInterceptor：</font></font></strong></p>
<p style="text-align: left;"><font size="1"><font size="2">&nbsp;public interface IInterceptor {<br />&nbsp;&nbsp;&nbsp; public void execute(AopContext context);<br />}</font></font></p>
<p style="text-align: left;"><font size="1"><font size="2">AopContext就不必贴出来了， 随自已定义些什么属性，不过就内置了一个Map，用来保存数据罢了。</font></font></p>
<p style="text-align: left;"><font size="1"><font size="2">下面来看看配置文件：</font></font></p>
<p style="text-align: left;"><font size="1"><font size="2">&nbsp;&lt;beans&gt;</font></font></p>
<p style="text-align: left;"><font size="1"><font size="2">&nbsp;&nbsp;&nbsp; &lt;!--将要暴露给DWR的Service--&gt;<br />&nbsp;&nbsp; &nbsp; &lt;bean id=&quot;bookManager&quot; class=&quot;org.springframework.aop.framework.ProxyFactoryBean&quot;&gt;<br />&nbsp;&nbsp;&nbsp; &lt;property name=&quot;proxyInterfaces&quot;&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;value&gt;net.jf.ajax.business.BookManager&lt;/value&gt;<br />&nbsp;&nbsp;&nbsp; &lt;/property&gt;<br />&nbsp;&nbsp;&nbsp; &lt;property name=&quot;target&quot;&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ref local=&quot;bookManagerImpl&quot;/&gt;<br />&nbsp;&nbsp;&nbsp; &lt;/property&gt;<br />&nbsp;&nbsp;&nbsp; &lt;property name=&quot;interceptorNames&quot;&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;list&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;value&gt;dwrAdvisor&lt;/value&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/list&gt;<br />&nbsp;&nbsp;&nbsp; &lt;/property&gt;<br />&nbsp; &lt;/bean&gt;<br /><br />&nbsp; &lt;bean id=&quot;bookManagerImpl&quot; class=&quot;net.jf.ajax.business.impl.BookManagerImpl&quot;/&gt;<br />&lt;!--装配器？如果看不懂，先看看Spring的Aop吧 ：P--&gt;<br />&nbsp; &lt;bean id=&quot;dwrAdvisor&quot; class=&quot;org.springframework.aop.support.RegexpMethodPointcutAdvisor&quot;&gt;<br />&nbsp;&nbsp;&nbsp; &lt;property name=&quot;advice&quot;&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ref local=&quot;dwrInterceptor&quot;/&gt;<br />&nbsp;&nbsp;&nbsp; &lt;/property&gt;<br />&nbsp;&nbsp;&nbsp; &lt;property name=&quot;patterns&quot;&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;list&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;value&gt;.*.*&lt;/value&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/list&gt;<br />&nbsp;&nbsp;&nbsp; &lt;/property&gt;<br />&nbsp; &lt;/bean&gt;</font></font></p>
<font size="2"><br /></font>
<p style="text-align: left;"><font size="1"><font size="2">&lt;!--主拦截器，给它设置子拦截器--&gt;<br />&nbsp; &lt;bean id=&quot;dwrInterceptor&quot; class=&quot;net.jf.ajax.iterceptor.MainInterceptor&quot;&gt;<br />&nbsp; &nbsp;&nbsp;&nbsp; &lt;property name=&quot;interceptors&quot;&gt;<br />&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;list&gt;<br />&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;ref bean=&quot;test&quot;/&gt;<br />&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/list&gt;<br />&nbsp; &nbsp;&nbsp;&nbsp; &lt;/property&gt;<br />&nbsp; &lt;/bean&gt;<br />&nbsp; &lt;!--其中一个子拦截器的实现--&gt;<br />&nbsp; &lt;bean id=&quot;test&quot; class=&quot;net.jf.ajax.iterceptor.TestInterceptor&quot;/&gt;<br />&lt;/beans&gt;</font></font><br /></p>
<p><font size="2">就 这样，在配置DWR的配置文件时，配置&lt;creator&gt;时使用Spring的Creator就可以直接使用上面的Service了。当 DWR远程请求时，在配置范围内的方法的调用都会被主拦截器拦截，并且遍历、执行所有子拦截器。原有的Service不需要改动，只需要多加一个 Spring的配置文件，将原有的Service再加一层Aop的轻纱。</font></p>
<p><font size="2">这是一种实现方法。如果有别的方法让DWR更安全、有效，请一定告知。：） <br /></font></p><img src ="http://www.blogjava.net/aichan/aggbug/47735.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/aichan/" target="_blank">艾尘</a> 2006-05-24 00:26 <a href="http://www.blogjava.net/aichan/archive/2006/05/24/47735.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于DWR与Servlet、安全</title><link>http://www.blogjava.net/aichan/archive/2006/05/10/45321.html</link><dc:creator>艾尘</dc:creator><author>艾尘</author><pubDate>Tue, 09 May 2006 17:18:00 GMT</pubDate><guid>http://www.blogjava.net/aichan/archive/2006/05/10/45321.html</guid><wfw:comment>http://www.blogjava.net/aichan/comments/45321.html</wfw:comment><comments>http://www.blogjava.net/aichan/archive/2006/05/10/45321.html#Feedback</comments><slash:comments>6</slash:comments><wfw:commentRss>http://www.blogjava.net/aichan/comments/commentRss/45321.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/aichan/services/trackbacks/45321.html</trackback:ping><description><![CDATA[使用DWR做Remote，完全绕过传统的MVC框架，如Struts、Webwork，单用一个DWRServlet来做控制器。DWR向客户端 暴露了服务端的服务接口，很有可能有没有任何限制的情况下被客户端调用所暴露的接口。如果使用传统的MVC框架，可以很方便地解决很多问题诸如身份验证、 权限控制等。而DWR提供的功能是给客户端暴露服务接口。上面所涉及的问题却少有牵涉。不过，解决方案还是有的。其中之一就是使用AOP，自已实现一些拦 截功能，例如结合Spring，使用DWR的Spring整合功能，给客户端提供一个Spring的Bean，而这个Bean是经过代理的 （Proxy）。实际上已经保证了身份认证等动作完成了。而我们要多做的是，写一些Spring的Bean来作拦截器。再在原有的服务上再加上一些 AOP。当然，DWR对于安全的还是提供了些设施的，基于J2EE的安全策略之上。感觉不是十分良好，所以没用，也没深入研究。：P
<p>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 很多时候，在做身份验证及授权的时候可能会用到应用的环境，如ServletContext，Session等。那么在DWR中的服务或拦截器需要用到Session这些东西的时候，获取是一件很简单的事。通常有两个办法<br />&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; 一、使用DWR的API。其实是一个静态的方法，极其不推荐。所以示例也就免了。<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 二、让DWR自已注入需要的元素，这里讲的元素仅限于：</p>
<ul>
    <li>HttpServletRequest</li>
    <li>HttpServletResponse</li>
    <li>HttpSession</li>
    <li>ServletContext</li>
    <li>ServletConfig</li>
</ul>
<p>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 做法是在服务里定义方法的时候，把以上的元素作为参数。在方法体内直接使用即可。而不必担心它的来源，来源是DWR会自已根据参数的类型注入。在客户端调 用的时候不需要提供这个参数。ServletContext之类的东西作为ThreadLocal的变量保存起来的。简单的示例。</p>
<p>1、服务代码&nbsp;</p>
<p><code><font color="#7f0055"><strong>package&nbsp;</strong></font><font color="#000000">net.jf.ajax.session;</font><br /><br /><font color="#7f0055"><strong>import&nbsp;</strong></font><font color="#000000">javax.servlet.http.HttpSession;</font><br /><br /><font color="#7f0055"><strong>public&nbsp;class&nbsp;</strong></font><font color="#000000">Store&nbsp;</font><font color="#000000">{</font><br /><font color="#ffffff">&nbsp;&nbsp;</font><font color="#7f0055"><strong>public&nbsp;</strong></font><font color="#7f0055"><strong>void&nbsp;</strong></font><font color="#000000">setAttribute</font><font color="#000000">(</font><font color="#000000">String&nbsp;name,String&nbsp;value,HttpSession&nbsp;session</font><font color="#000000">){</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">session.setAttribute</font><font color="#000000">(</font><font color="#000000">name,value</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;</font><font color="#000000">}</font><br /><font color="#ffffff">&nbsp;&nbsp;</font><font color="#7f0055"><strong>public&nbsp;</strong></font><font color="#000000">String&nbsp;getAttribute</font><font color="#000000">(</font><font color="#000000">String&nbsp;name,HttpSession&nbsp;session</font><font color="#000000">){</font><br /><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>return&nbsp;</strong></font><font color="#000000">(</font><font color="#000000">String</font><font color="#000000">)&nbsp;</font><font color="#000000">session.getAttribute</font><font color="#000000">(</font><font color="#000000">name</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#ffffff">&nbsp;&nbsp;</font><font color="#000000">}</font><br /><font color="#000000">}</font></code> <font size="3" /></p>
<p><font size="1"><font size="2">2、spring配置文件</font></font></p>
<p style="text-align: left;"><font size="1"><font size="2">&nbsp;&nbsp;&nbsp; &nbsp; &lt;beans&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;bean id=&quot;store&quot; class=&quot;net.jf.ajax.session.Store&quot;&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/bean&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/beans&gt;</font></font></p>
<p style="text-align: left;"><font size="1"><font size="2">3、dwr.xml&nbsp;</font></font></p>
<p style="text-align: left;"><font size="1"><font size="2">&lt;dwr&gt;<br />&nbsp; &lt;allow&gt;<br />&nbsp;&nbsp;&nbsp; &lt;create creator=&quot;spring&quot; javascript=&quot;store&quot;&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&lt;param name=&quot;beanName&quot; value=&quot;store&quot;/&gt;<br />&nbsp;&nbsp;&nbsp; &lt;/create&gt;<br />&nbsp; &lt;/allow&gt;<br />&lt;/dwr&gt;</font></font></p>
<p style="text-align: left;"><font size="1"><font size="2">以 上的代码及配置文件可以达到目的：DWR与Spring结合，DWR直接使用Spring管理的Bean作为服务，当然，受Spring管理的Bean功 能一点都不减，复杂的如有着事务管理的Bean同样有用。 而且尽管是Spring管理的Bean，方法的参数中有Servlet相关的参数，DWR同样自动注入。</font></font></p>
<p style="text-align: left;"><font size="1"><font size="2">&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 现在去调试页面看看我们暴露的接口及效果如何！在调试页面找到store的服务，点击进去，可以见到暴露的方法有两个：</font></font></p>
<p class="separator" style="text-align: center; clear: both;"><a href="http://bbmyth.googlepages.com/store.JPG/store-full.jpg" style="border: 0pt none ; background-color: transparent; margin-left: 1em; margin-right: 1em;"><img src="http://bbmyth.googlepages.com/store.JPG/store-medium.jpg" alt="" /></a></p>
<p style="text-align: left;"><font size="1"><font size="2">&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 一个是SetAttribute(&quot;&quot;,&quot;&quot;,AUTO)，第三个参数表明自动注入，客户端只需要提供前两个参数即可。</font></font></p>
<p style="text-align: left;"><font size="1"><font size="2">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 一个是GetAttribute（&ldquo;&rdquo;，AUTO），第二个参数表明自动注入，客户只需要提供一个名字参数取值即可。</font></font></p>
<font size="1"><font size="2">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 在测试页面提供的输入框中测试两个函数，SetAttribute填入name,jeff提交成功，在GetAttribute函数中填入name获得一个返回值，正是jeff。至此，尝试成功！ </font></font><img src ="http://www.blogjava.net/aichan/aggbug/45321.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/aichan/" target="_blank">艾尘</a> 2006-05-10 01:18 <a href="http://www.blogjava.net/aichan/archive/2006/05/10/45321.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>