JAVA authorization and authentication
Published:
https://github.com/GEORGE5961/markdown_photos/blob/master/jaas1.png?raw=true
Java RMI (remote mothed invocation) is a technique in Java like RPC (remote procedure call) in C.
A classic scene is that you submit a order in Amazon, this is to say, you send an HTTP request to Amazon’s backend server. Then Amazon’s backend server automatically informs the DHL of delivering goods. The communication between Amazon’s server and DHL’s server utilizes Java RMI.
You can find a demo here.
What is Java RMI?
Java RMI is an object-oriented equivalent of remote procedure calls (RPC), with support for direct transfer of serialized Java classes and distributed garbage-collection.
- Java RMI is employed for the communications of different Java VMs. Java VMs can be deployed in a single physical machine or mutiple physical machines.
- An Java object is able to call a method of an Java object in another Java VM. The called method should follow some standard.
- The propose of Java RMI is to make Java objects in different Java VMs seems a monolithic one.
Why Java RMI?
- Why don’t servers communicate with each other by HTTP proxy?
- HTTP cannot transport JAVA objects.
- Java RMI takes advantage of Java serializatio, encapsulating data encoding and decoding for developers.
How to implement Java RMI?
Register remote methods for lookup
How to get the reference of the registered remote methods?
- Format: rmi://host:port/name
- Host and port are optional
- If omit host, it will run on the localhost on default
- If omit port, the default port is 1099
JAVA RMI API:
- java.rmi: provide the client end with class, interface, and exception
- java.rmi.server: provide the server end with class, interface, and exception
- java.rmi.registry: create registry, lookup and name remote object
Demo
Server end
First, we build a service in server end.
In ServerInterface.java:
package com.shunfeng.demo.service;
import org.springframework.stereotype.Service;
@Service
public interface ServerInterface {
public void testRMI();
}
In ServerImpl.java:
package com.shunfeng.demo.service;
import org.springframework.stereotype.Service;
@Service
public class ServerImpl implements ServerInterface {
public void testRMI() {
System.out.println("welcome use RMI!");
}
}
Second, register the service for client to lookup.
package com.shunfeng.demo;
import com.shunfeng.demo.service.ServerImpl;
import com.shunfeng.demo.service.ServerInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.rmi.RmiServiceExporter;
@Configuration
public class registry {
@Autowired
private ServerImpl serverImpl;
@Bean
public RmiServiceExporter rmiServiceExporter() {
RmiServiceExporter exporter = new RmiServiceExporter();
exporter.setServiceInterface(ServerInterface.class);
exporter.setServiceName("rmiService");
exporter.setService(serverImpl);
exporter.setRegistryPort(5555);
return exporter;
}
}
Client end
First, you should declare the same method interface as that in server end.
In ServerInterface.java:
package com.rmi_client.demo.service;
import org.springframework.stereotype.Service;
@Service
public interface ServerInterface {
public void testRMI();
}
Second, find the romote method.
In registry.java:
package com.shunfeng.demo;
import com.shunfeng.demo.service.ServerImpl;
import com.shunfeng.demo.service.ServerInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.rmi.RmiServiceExporter;
@Configuration
public class registry {
@Autowired
private ServerImpl serverImpl;
@Bean
public RmiServiceExporter rmiServiceExporter() {
RmiServiceExporter exporter = new RmiServiceExporter();
exporter.setServiceInterface(ServerInterface.class);
exporter.setServiceName("rmiService");
exporter.setService(serverImpl);
exporter.setRegistryPort(5555);
return exporter;
}
}
Finally, we test whether RMI works.
In test.java:
package com.rmi_client.demo.action;
import com.rmi_client.demo.service.ServerInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.remoting.rmi.RmiProxyFactoryBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class Test {
@Autowired
private RmiProxyFactoryBean factoryBean;
@ResponseBody
@RequestMapping("/testRMI")
public String testRMI() {
ServerInterface rmiTestProvider = (ServerInterface) factoryBean.getObject();
rmiTestProvider.testRMI();
System.out.println("welcome use fuck RMI!");
return "RMI调用成功.";
}
}
Now we succeed in calling a method in a java object in another Java VM.
Reference
1.https://my.oschina.net/langwanghuangshifu/blog/2223042
It’s worth reading.