Hessian 设置访问用户名和密码
1: web.xml文件增加如下代码
<!-- 定义hessian认证 -->
<security-constraint>
<web-resource-collection>
<web-resource-name>Hessian Security</web-resource-name>
<url-pattern>/esb/hessian/TransferService</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>Hessian</role-name>
</auth-constraint>
</security-constraint>
<!-- Define the Login Configuration for this Application -->
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Hessian Security</realm-name>
</login-config>
<!-- Security roles referenced by this web application -->
<security-role>
<description></description>
<role-name>Hessian</role-name>
</security-role>
2:设置tomcat 配置文件下tomcat-users.xml
<role rolename="tomcat"/>
<role rolename="role1"/>
<role rolename="Hessian"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user username="both" password="tomcat" roles="tomcat,role1"/>
<user username="role1" password="tomcat" roles="role1"/>
<user username="Hessianuser" password="password" roles="Hessian"/>
3:增加客户端代码
String url = "http://192.168.0.236:8080/test/esb/hessian/TransferService";
HessianProxyFactory factory = new HessianProxyFactory();
factory.setUser("aig");
factory.setPassword("password");
这样就可以给hessian 增加权限了.
如果密码不正确会出现以下错误:
Exception in thread "main" com.caucho.hessian.client.HessianRuntimeException: com.caucho.hessian.io.HessianProtocolException: 401: java.io.IOException: Server returned HTTP response code: 401 for URL: http://192.168.0.236:8080/test/esb/hessian/TransferService
at com.caucho.hessian.client.HessianProxy.invoke(HessianProxy.java:213)
at $Proxy0.transferInfoAig(Unknown Source)
at test.TransferInofTest.main(TransferInofTest.java:87)
Caused by: com.caucho.hessian.io.HessianProtocolException: 401: java.io.IOException: Server returned HTTP response code: 401 for URL: http://192.168.0.236:8080/test/esb/hessian/sapTransferService
at com.caucho.hessian.client.HessianProxy.invoke(HessianProxy.java:185)
... 2 more
Caused by: java.io.IOException: Server returned HTTP response code: 401 for URL: http://192.168.0.236:8080/test/esb/hessian/TransferService
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1360)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1354)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1008)
at com.caucho.hessian.client.HessianProxy.invoke(HessianProxy.java:167)
... 2 more
Caused by: java.io.IOException: Server returned HTTP response code: 401 for URL: http://192.168.0.236:8080/CHJ_G4/esb/hessian/TransferService
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1305)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:373)
at com.caucho.hessian.client.HessianProxy.invoke(HessianProxy.java:158)
... 2 more
Hessian 设置访问用户名和密码
1: web.xml文件增加如下代码
<!-- 定义hessian认证 -->
<security-constraint>
<web-resource-collection>
<web-resource-name>Hessian Security</web-resource-name>
<url-pattern>/esb/hessian/TransferService</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>Hessian</role-name>
</auth-constraint>
</security-constraint>
<!-- Define the Login Configuration for this Application -->
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Hessian Security</realm-name>
</login-config>
<!-- Security roles referenced by this web application -->
<security-role>
<description></description>
<role-name>Hessian</role-name>
</security-role>
2:设置tomcat 配置文件下tomcat-users.xml
<role rolename="tomcat"/>
<role rolename="role1"/>
<role rolename="Hessian"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user username="both" password="tomcat" roles="tomcat,role1"/>
<user username="role1" password="tomcat" roles="role1"/>
<user username="Hessianuser" password="password" roles="Hessian"/>
3:增加客户端代码
String url = "http://192.168.0.236:8080/test/esb/hessian/TransferService";
HessianProxyFactory factory = new HessianProxyFactory();
factory.setUser("aig");
factory.setPassword("password");
这样就可以给hessian 增加权限了.
如果密码不正确会出现以下错误:
Exception in thread "main" com.caucho.hessian.client.HessianRuntimeException: com.caucho.hessian.io.HessianProtocolException: 401: java.io.IOException: Server returned HTTP response code: 401 for URL: http://192.168.0.236:8080/test/esb/hessian/TransferService
at com.caucho.hessian.client.HessianProxy.invoke(HessianProxy.java:213)
at $Proxy0.transferInfoAig(Unknown Source)
at test.TransferInofTest.main(TransferInofTest.java:87)
Caused by: com.caucho.hessian.io.HessianProtocolException: 401: java.io.IOException: Server returned HTTP response code: 401 for URL: http://192.168.0.236:8080/test/esb/hessian/sapTransferService
at com.caucho.hessian.client.HessianProxy.invoke(HessianProxy.java:185)
... 2 more
Caused by: java.io.IOException: Server returned HTTP response code: 401 for URL: http://192.168.0.236:8080/test/esb/hessian/TransferService
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1360)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1354)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1008)
at com.caucho.hessian.client.HessianProxy.invoke(HessianProxy.java:167)
... 2 more
Caused by: java.io.IOException: Server returned HTTP response code: 401 for URL: http://192.168.0.236:8080/CHJ_G4/esb/hessian/TransferService
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1305)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:373)
at com.caucho.hessian.client.HessianProxy.invoke(HessianProxy.java:158)
... 2 more