Hessian的一些基本简介已经在上一节已经全部介绍了,上一节还介绍了Hessian是把对象序列化为二进制流的形式在http信道中传输,那么对于安全性高的应用不应该采用hessian(比如网上支付等)、可以加一些权限验证,比如在服务器端加用户名,密码验证,然后在客户端提供用户名和密码,可如此一来用户名密码也会被捕获,毕竟用户名密码都在Http请求中,如果安全级别特别高的可以加Token,也就是加一层发送前的预备,如下图:
这样的话,既使请求被拦截,他们得到的也只不过是一个过期的Token,无法再一次发送到服务端,当然哪个程序都一样,安全级别一高就会添加很多的操作,就像开着防火墙网速会受一定的影响.下面我来介绍简单的权限认证,后续再结合实践优化程序.
修改服务端程序如下:
- public class YclHessianServiceExporter extends HessianServiceExporter {
- public static final String AUTH = "ycl";
- @Override
- public void handleRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String auth = request.getHeader("auth");
- if(auth == null || !auth.equalsIgnoreCase(AUTH)){
- //记录异常日志
- return ;
- }
- super.handleRequest(request, response);
- }
- }
public class YclHessianServiceExporter extends HessianServiceExporter {
public static final String AUTH = "ycl";
@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String auth = request.getHeader("auth");
if(auth == null || !auth.equalsIgnoreCase(AUTH)){
//记录异常日志
return ;
}
super.handleRequest(request, response);
}
}
服务器配置修改如下:
- <bean name="/PersonManager"
- class="org.springframework.remoting.caucho.YclHessianServiceExporter">
- <!-- 需要导出的目标bean-->
- <property name="service" >
- <ref bean="personManager" />
- </property>
- <!-- Hessian服务的接口-->
- <property name="serviceInterface" value="com.module.PersonManager"/>
- </bean>
<bean name="/PersonManager"
class="org.springframework.remoting.caucho.YclHessianServiceExporter">
<!-- 需要导出的目标bean-->
<property name="service" >
<ref bean="personManager" />
</property>
<!-- Hessian服务的接口-->
<property name="serviceInterface" value="com.module.PersonManager"/>
</bean>
修改客户端程序如下(这是一个代理工厂,每一次客户端通过代码连接服务器时都会先通过URL来得到服务器端的连接)
- public class YclHessianProxyFactory extends HessianProxyFactory{
- @Override
- protected URLConnection openConnection(URL url) throws IOException {
- URLConnection conn = super.openConnection(url);
- conn.setRequestProperty("AUTH", "ycl");
- return conn;
- }
- }
public class YclHessianProxyFactory extends HessianProxyFactory{
@Override
protected URLConnection openConnection(URL url) throws IOException {
URLConnection conn = super.openConnection(url);
conn.setRequestProperty("AUTH", "ycl");
return conn;
}
}
客户端配置如下:
- <!-- PersonManager服务 -->
- <bean id="personManagerClient"
- class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
- <property name="serviceUrl">
- <value>
- http://localhost/Hessian/remoting/PersonManager
- </value>
- </property>
- <property name="serviceInterface">
- <value>com.module.PersonManager</value>
- </property>
- <property name="proxyFactory">
- <bean class="com.caucho.hessian.client.YclHessianProxyFactory"/>
- </property>
- </bean>
<!-- PersonManager服务 -->
<bean id="personManagerClient"
class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl">
<value>
http://localhost/Hessian/remoting/PersonManager
</value>
</property>
<property name="serviceInterface">
<value>com.module.PersonManager</value>
</property>
<property name="proxyFactory">
<bean class="com.caucho.hessian.client.YclHessianProxyFactory"/>
</property>
</bean>
如此一来,在服务器启动的时候接收信息时,就会添加验证,如果Auth验证不通过,那么就不会再继续执行以下的程序.