Hessian配置用户名和密码

朱雀 2022-08-20 02:23 420阅读 0赞

原文链接 http://www.zhaochao.net/index.php/2016/01/26/12/

配置原因

随着业务的发展项目会变的越来越多,项目需要进行模块化与服务化,服务化中常用的方法就是使用RPC技术,Hessian就是常用的一种RPC技术。之前用过Hessian,没有考虑太多,只是用用,最近想了想Hessina没有安全验证,将URL发部到网上后,只要别人知道你的URL,再知道你的方法,他就可以调用了,这样感觉不太安全,于是找了一下方法,给Hessian增加权限我所知道的有两种,一种是通过额外的代码去验证,另一种是Hessian自带的权限验证,主要是将用户名和密码增加到http 响应头在,部分源码如下所示:

  1. /**
  2. * Method that allows subclasses to add request headers such as cookies.
  3. * Default implementation is empty.
  4. */
  5. protected void addRequestHeaders(HessianConnection conn)
  6. {
  7. conn.addHeader("Content-Type", "x-application/hessian");
  8. conn.addHeader("Accept-Encoding", "deflate");
  9. String basicAuth = _factory.getBasicAuth();
  10. if (basicAuth != null)
  11. conn.addHeader("Authorization", basicAuth);
  12. }
  13. public String getBasicAuth()
  14. {
  15. if (_basicAuth != null)
  16. return _basicAuth;
  17. else if (_user != null && _password != null)
  18. return "Basic " + base64(_user + ":" + _password);
  19. else
  20. return null;
  21. }

在tomcat中配置服务url的访问用户名和密码,具体做法如下

  1. 在服务端的web.xml中为hessan的url增加权限验证代码如下:

    1. <security-constraint>
    2. <web-resource-collection>
    3. <web-resource-name>Hessian Security</web-resource-name>
    4. <url-pattern>/api/service/*</url-pattern>
    5. </web-resource-collection>
    6. <auth-constraint>
    7. <role-name>Hessian</role-name>
    8. </auth-constraint>
    9. </security-constraint>
    10. <!-- Define the Login Configuration for this Application -->
    11. <login-config>
    12. <auth-method>BASIC</auth-method>
    13. <realm-name>Hessian Security</realm-name>
    14. </login-config>
    15. <!-- Security roles referenced by this web application -->
    16. <security-role>
    17. <description></description>
    18. <role-name>Hessian</role-name>
    19. </security-role>
  2. 在tomcat 的tomcat-users.xml中增加 用户名和密码

  3. hessian客户端增加用户名和密码
    hessian自带客户端

    HVideoService videoClient = (HVideoService) factory.create(HVideoService.class, url);

    1. factory.setUser("zhaochao");
    2. factory.setPassword("zhaochao");

spring客户端

  1. <bean id="videoClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
  2. <property name="serviceUrl" value="${baseUrl}/api/service/videoExporter" />
  3. <property name="serviceInterface" value="com.***" />
  4. <property name="overloadEnabled" value="true" />
  5. <property name="username" value="zhaochao"></property>
  6. <property name="password" value="zhaochao"></property>
  7. </bean>

发表评论

表情:
评论列表 (有 0 条评论,420人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Linux 用户名密码遗忘

    菜鸟就是菜鸟,什么问题都能遇到啊。。刚想学习下linux 就遇到这种白痴问题,百度下 还是有结论的吗。呵呵   以下为参照的解决方案: 可以以单一使用者的模式登入,然后再