以下是简单的hessian服务代码:

 

1.导包

<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.38</version>
</dependency>

2.编写HessianServerProxyExporter类,实现验证

**
* 自定义hessian服务发布,可用于自定义验证服务
*
* @author smiledada
*
*/
public class HessianServerProxyExporter extends HessianServiceExporter {
@Value("${server.userName}")
private String userName;
@Value("${server.passWord}")
private String passWord;

private static final Logger log = LoggerFactory.getLogger(HessianServerProxyExporter.class);

@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String authorization = request.getHeader("Authorization");
HessianContext.setRequest(request); //保存Request到Hessian线程上下文
if (StringUtils.isEmpty(authorization)) {
throw new NestedServletException("Auth Is Empty!");
}
String[] authArr = authorization.trim().split(" ");
String auth = authArr[1];
auth = new String(Base64.getDecoder().decode(auth));
String[] namePwdArr = auth.split(":");
String pwd = namePwdArr[1];
String name = namePwdArr[0];
// 验证IP地址
String ipAddress=NetUtils.getIpAddress(request);
log.info(ipAddress+"进入到本系统。。。。。。。。。。");

if (ipAddress.equals("127.0.0.1")){
// 验证密码
if (!passWord.equals(pwd)) {
throw new NestedServletException("密码错误");
}
if (!userName.equals(name)) {
throw new NestedServletException("用户名错误");
}
super.handleRequest(request, response);
}else {
                System.out.println("ip验证失败");
            }
}

}

3.编写Hessian服务api

public interface TestApi {
/**
* 测试
*
* @return
*/
public Map<String, String> testt();

}

4.发布Hessian服务

//发布服务
@Bean(name = "/testApi")
public HessianServiceExporter accountService() {
HessianServerProxyExporter exporter = new HessianServerProxyExporter();
exporter.setService(testApi);
exporter.setServiceInterface(TestApi.class);
return exporter;
}