亲宝软件园·资讯

展开

Spring-boot oauth2 后台自动登录 Spring-boot oauth2使用RestTemplate进行后台自动登录的实现

Guoye 人气:1

内容不限于登录业务,主要简单介绍RestTemplate的用法,包括

登录流程

主要代码

  // 构造 post的body内容(要post的内容,按需定义)
  MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
  paramsMap.set("grant_type", "password");
  paramsMap.set("username", "yourname");
  paramsMap.set("password", "yourpassword");

  // 构造头部信息(若有需要)
  HttpHeaders headers = new HttpHeaders();
  headers.add("Authorization", "Basic xxxxxx你的认证密钥");
  // 设置类型 "application/json;charset=UTF-8"
  headers.setContentType(MediaType.APPLICATION_JSON); 
  
  // 构造请求的实体。包含body和headers的内容
  HttpEntity<MultiValueMap<String, String>> request = new HttpEntity(paramsMap, headers);
  
  // 声明 restTemplateAuth(用作请求)
  RestTemplate restTemplateAuth = new RestTemplate(); 
  // 进行请求,并返回数据 
  String authInfo = restTemplateAuth.postForObject("http://localhost:8089/oauth/token", request, String.class);

使用josn请求的示例代码

Posting JSON with postForObject

  JSONObject personJsonObject = new JSONObject();
  personJsonObject.put("id", 1);
  personJsonObject.put("name", "John");

  HttpEntity<String> request = new HttpEntity<String>(personJsonObject.toString(), headers);
  String personResultAsJsonStr = restTemplate.postForObject("url", request, String.class);

 到此这篇关于Spring-boot oauth2使用RestTemplate进行后台自动登录的实现的文章就介绍到这了,更多相关Spring-boot oauth2 后台自动登录内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

加载全部内容

相关教程
猜你喜欢
用户评论