发布一个服务:
在spring配置文件中配置
<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName" value="ClientTaskService" />
<property name="service" ref="clientTaskService" />
<property name="serviceInterface" value="cn.com.harbor.rmiservice.task.service.GetClientTaskService" />
<property name="registryPort" value="1198" />
<property name="interceptors">
<list>
<ref bean="securityInterceptor" />
</list>
</property>
</bean>
<bean id="securityInterceptor" class="cn.com.harbor.rmiservice.interceptor.SecurityInterceptor">
<!-- 这里配置允许访问RMI的客户端IP地址 -->
<property name="allowed">
<set>
<value>127.0.0.1</value>
<value>192.168.8.58</value>
</set>
</property>
</bean>
拦截器代码:
public class SecurityInterceptor implements MethodInterceptor {
private Set allowed;
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
String clientHost = RemoteServer.getClientHost();
if (allowed != null && allowed.contains(clientHost)) {
return methodInvocation.proceed();
} else {
throw new SecurityException("非法访问。");
}
}
public void setAllowed(Set allowed) {
this.allowed = allowed;
}
}
客户端调用:
在spring配置文件中配置
<bean id="approval" class="org.springframework.remoting.rmi.RmiProxyFactoryBean" lazy-init="true">
<property name="serviceUrl" value="rmi://127.0.0.1:1198/ClientTaskService" />
<property name="serviceInterface" value="cn.com.harbor.rmiservice.task.service.GetClientTaskService" />
</bean>
客户端所有引用的类和接口必须和服务端的包路径一样!!!