小修改
This commit is contained in:
@@ -1,19 +0,0 @@
|
||||
package com.macro.mall.demo.config;
|
||||
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* RestTemplate的配置
|
||||
* Created by macro on 2018/9/18.
|
||||
*/
|
||||
@Configuration
|
||||
public class RestTemplateConfig {
|
||||
@Bean
|
||||
@LoadBalanced
|
||||
public RestTemplate restTemplate(){
|
||||
return new RestTemplate();
|
||||
}
|
||||
}
|
||||
@@ -1,109 +0,0 @@
|
||||
package com.macro.mall.demo.controller;
|
||||
|
||||
import com.macro.mall.common.api.CommonResult;
|
||||
import com.macro.mall.model.PmsBrand;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.util.UriComponents;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* RestTemplate示例Controller
|
||||
* Created by macro on 2018/9/17.
|
||||
*/
|
||||
@Api(tags = "RestTemplateDemoController", description = "RestTemplate示例")
|
||||
@Controller
|
||||
@RequestMapping("/template")
|
||||
public class RestTemplateDemoController {
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
@Value("${host.mall.admin}")
|
||||
private String HOST_MALL_ADMIN;
|
||||
|
||||
@ApiOperation("getForEntity url")
|
||||
@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public Object getForEntity(@PathVariable Long id) {
|
||||
String url = HOST_MALL_ADMIN + "/brand/{id}";
|
||||
ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(url, CommonResult.class, id);
|
||||
return responseEntity.getBody();
|
||||
}
|
||||
|
||||
@ApiOperation("getForEntity params")
|
||||
@RequestMapping(value = "/get2/{id}", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public Object getForEntity2(@PathVariable Long id) {
|
||||
String url = HOST_MALL_ADMIN + "/brand/{id}";
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("id", String.valueOf(id));
|
||||
ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(url, CommonResult.class, params);
|
||||
return responseEntity.getBody();
|
||||
}
|
||||
|
||||
@ApiOperation("getForEntity Uri")
|
||||
@RequestMapping(value = "/get3/{id}", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public Object getForEntity3(@PathVariable Long id) {
|
||||
String url = HOST_MALL_ADMIN + "/brand/{id}";
|
||||
UriComponents uriComponents = UriComponentsBuilder.fromUriString(url).build().expand(id).encode();
|
||||
ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(uriComponents.toUri(), CommonResult.class);
|
||||
return responseEntity.getBody();
|
||||
}
|
||||
|
||||
@ApiOperation("getForObject url")
|
||||
@RequestMapping(value = "/get4/{id}", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public Object getForObject(@PathVariable Long id) {
|
||||
String url = HOST_MALL_ADMIN + "/brand/{id}";
|
||||
CommonResult commonResult = restTemplate.getForObject(url, CommonResult.class, id);
|
||||
return commonResult;
|
||||
}
|
||||
|
||||
@ApiOperation("postForEntity jsonBody")
|
||||
@RequestMapping(value = "/post", method = RequestMethod.POST)
|
||||
@ResponseBody
|
||||
public Object postForEntity(@RequestBody PmsBrand brand) {
|
||||
String url = HOST_MALL_ADMIN + "/brand/create";
|
||||
ResponseEntity<CommonResult> responseEntity = restTemplate.postForEntity(url, brand, CommonResult.class);
|
||||
return responseEntity.getBody();
|
||||
}
|
||||
|
||||
@ApiOperation("postForEntity jsonBody")
|
||||
@RequestMapping(value = "/post2", method = RequestMethod.POST)
|
||||
@ResponseBody
|
||||
public Object postForObject(@RequestBody PmsBrand brand) {
|
||||
String url = HOST_MALL_ADMIN + "/brand/create";
|
||||
CommonResult commonResult = restTemplate.postForObject(url, brand, CommonResult.class);
|
||||
return commonResult;
|
||||
}
|
||||
|
||||
@ApiOperation("postForEntity form")
|
||||
@RequestMapping(value = "/post3", method = RequestMethod.POST)
|
||||
@ResponseBody
|
||||
public Object postForEntity3(@RequestParam String name) {
|
||||
String url = HOST_MALL_ADMIN + "/productAttribute/category/create";
|
||||
//设置头信息
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
||||
//构造表单参数
|
||||
MultiValueMap<String, String> params= new LinkedMultiValueMap<>();
|
||||
params.add("name", name);
|
||||
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
|
||||
ResponseEntity<CommonResult> responseEntity = restTemplate.postForEntity(url, requestEntity, CommonResult.class);
|
||||
return responseEntity.getBody();
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,3 @@ management: #开启SpringBoot Admin的监控
|
||||
endpoint:
|
||||
health:
|
||||
show-details: always
|
||||
host:
|
||||
mall:
|
||||
admin: http://mall-admin
|
||||
|
||||
@@ -7,7 +7,6 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
@@ -24,7 +23,6 @@ import java.util.Map;
|
||||
* HMACSHA512(base64UrlEncode(header) + "." +base64UrlEncode(payload),secret)
|
||||
* Created by macro on 2018/4/26.
|
||||
*/
|
||||
//@Component
|
||||
public class JwtTokenUtil {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(JwtTokenUtil.class);
|
||||
private static final String CLAIM_KEY_USERNAME = "sub";
|
||||
@@ -58,7 +56,7 @@ public class JwtTokenUtil {
|
||||
.parseClaimsJws(token)
|
||||
.getBody();
|
||||
} catch (Exception e) {
|
||||
LOGGER.info("JWT格式验证失败:{}",token);
|
||||
LOGGER.info("JWT格式验证失败:{}", token);
|
||||
}
|
||||
return claims;
|
||||
}
|
||||
@@ -77,7 +75,7 @@ public class JwtTokenUtil {
|
||||
String username;
|
||||
try {
|
||||
Claims claims = getClaimsFromToken(token);
|
||||
username = claims.getSubject();
|
||||
username = claims.getSubject();
|
||||
} catch (Exception e) {
|
||||
username = null;
|
||||
}
|
||||
@@ -124,13 +122,14 @@ public class JwtTokenUtil {
|
||||
/**
|
||||
* 判断token是否可以被刷新
|
||||
*/
|
||||
public boolean canRefresh(String token) {
|
||||
private boolean canRefresh(String token) {
|
||||
return !isTokenExpired(token);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 当原来的token没过期是可以刷新
|
||||
*
|
||||
* @param oldToken 带tokenHead的token
|
||||
*/
|
||||
public String refreshHeadToken(String oldToken) {
|
||||
|
||||
Reference in New Issue
Block a user