浏览器解析ajax发起跨域请求.程序虽然能够正确的调用,可是浏览器能够监控用户的全部的参数及返回值.在一些特定的条件下该操做不安全.(例如:支付宝支付操做)
通常使用跨域的请求都是用来获取其余服务器的数据(查询操做),若是遇到了POST须要提交的参数应该使用更加安全的请求方式实现.html
HTTP 协议多是如今 Internet 上使用得最多、最重要的协议了,愈来愈多的 Java 应用程序须要直接经过 HTTP 协议来访问网络资源。虽然在 JDK 的 java net包中已经提供了访问 HTTP 协议的基本功能,可是对于大部分应用程序来讲,JDK 库自己提供的功能还不够丰富和灵活。HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,而且它支持 HTTP 协议最新的版本和建议。HttpClient 已经应用在不少的项目中,好比 Apache Jakarta 上很著名的另外两个开源项目 Cactus 和 HTMLUnit 都使用了 HttpClient。如今HttpClient最新版本为 HttpClient 4.5 .6(2015-09-11)前端
以上是百度的概念java
总结来讲就是:HttpClient是用来提供高效的、最新的、功能丰富的支持HTTP协议的客户端编程工具包web
<!--添加httpClient jar包 --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </dependency>
public class TestHttpClient { /** * 步骤: * 1.实例化httpClient工具API * 2.定义请求url地址 任意网络地址.... * 3.定义请求的类型 get/post/put/delete * 4.发起请求,获取响应的结果 * 5.判断响应的状态码信息. 200 404 500 406 400.... * 6.动态解析返回值执行后续操做. */ @Test public void test01(){ HttpClient httpClient = HttpClients.createDefault(); String url = "https://www.baidu.com/"; HttpGet get = new HttpGet(url); try { HttpResponse httpResponse = httpClient.execute(get); //判断状态码是否正确 int statusCode = httpResponse.getStatusLine().getStatusCode(); if(statusCode == 200){ //表示请求正确 HttpEntity httpEntity = httpResponse.getEntity(); //获取服务器的所有响应信息(json/html/xml/xxxx) String result = EntityUtils.toString(httpEntity,"UTF-8"); //获取以后能够执行业务处理...... System.out.println(result); } } catch (IOException e) { e.printStackTrace(); } } }
需求:根据userId查询用户的信息.
1.用户的url地址: http://www.xx.com/findUserById/7
;
2.须要在xx-web的Controller中动态的接收数据.将请求转给sso单点登陆系统;
url:http://sso.xx.com/findUserById/7
3.在jt-sso中的Controller根据userId查询用户信息.ajax
前端controller接收客户端http请求apache
/** * 为了测试httpClient 实现业务功能查询用户信息 * url地址:http://www.jt.com/user/findUserById/7 * 参数: 7用户ID * 返回值: user对象 */ @RequestMapping("/findUserById/{userId}") @ResponseBody public User findUserById(@PathVariable Long userId){ return userService.findUserById(userId); }
在前端service中经过入门案例中的六步,进行远程跨域调用后端编程
@Service public class UserServiceImpl implements UserService{ /** * 由xx-web向xx-sso进行数据的请求.以后获取数据. * @param userId * @return */ @Override public User findUserById(Long userId) { //1.定义url地址 String url = "http://sso.xx.com/user/findUserById/"+userId; HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); try { HttpResponse httpResponse = httpClient.execute(httpGet); if(httpResponse.getStatusLine().getStatusCode() == 200){ HttpEntity httpEntity = httpResponse.getEntity(); //json格式 String result = EntityUtils.toString(httpEntity, "UTF-8"); return ObjectMapperUtil.toObject(result, User.class); }else{ throw new RuntimeException("请求失败,请校验地址信息"); } } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } } }
后端controller接收httpclient发送的远程调用请求json
/** * 测试httpClient调用方式 * url地址: http://sso.jt.com/user/findUserById/"+userId * 返回: user对象的JSON数据 */ @RequestMapping("/findUserById/{userId}") public User findUserById(@PathVariable Long userId){ return userService.findUserById(userId); }
实现后端 service查询业务实现后端
@Override public User findUserById(Long userId) { return userMapper.selectById(userId); }