Http之转发与重定向

说明

redirect

透明式间接转发,两次请求,两次处理,两次响应 。

forward

隐身式直接转发,一次请求,两次处理,一次响应。

重定向示例

1
2
3
// URLDecoder.decode 解码url
// URLEncoder.encode 编码url
String reUrl = URLDecoder.decode(redirect_uri, "utf-8") + (redirect_uri.contains("?") ? "&" : "?") + "code=" + code + "&state=" + state;

示例一

1
return "redirect:" + reUrl;

示例二(推荐)

1
2
3
4
5
6
public Object red(){
	ModelAndView modelAndView = new ModelAndView("redirect:"+URLDecoder.decode(redirect_uri, "utf-8"));
	modelAndView.addObject("code", code);//追加参数(有效)
	modelAndView.addObject("state", state);
	return modelAndView;
}

示例三(推荐)

1
2
3
4
5
6
7
public Object red(){
	RedirectView redirectView = new RedirectView(URLDecoder.decode(redirect_uri, "utf-8"));		
	redirectView.addStaticAttribute("code", code);//追加参数(有效)
	redirectView.addStaticAttribute("state", state);
	redirectView.addStaticAttribute("type", state);
	return redirectView;
}

示例四

1
2
3
4
5
6
7
public Object red(HttpServletRequest request){
	HttpHeaders httpHeaders = new HttpHeaders();
	httpHeaders.setLocation(new URI(reUrl));
	request.setAttribute("code", code);//追加参数(无效)
	request.setAttribute("state", state);
	return new ResponseEntity<Object>(httpHeaders, HttpStatus.FOUND);
}

示例五

1
2
3
4
5
6
7
public Object red(RedirectAttributes redirectAttributes){
	HttpHeaders httpHeaders = new HttpHeaders();
	httpHeaders.setLocation(new URI(reUrl));
	redirectAttributes.addAttribute("code", code);//追加参数(无效)
	redirectAttributes.addAttribute("state", state);
	return new ResponseEntity<Object>(httpHeaders, HttpStatus.FOUND);
}

示例六

1
response.sendRedirect(reUrl);

示例七

1
2
3
4
5
public Object red(RedirectAttributes redirectAttributes){
	redirectAttributes.addAttribute("code", code);//追加参数(有效)
	redirectAttributes.addAttribute("state", state);
	return new ModelAndView("redirect:"+reUrl);
}

示例八

1
2
3
4
5
6
public Object red(Model model){
	model.addAttribute("code", code);//追加参数(无效)
	model.addAttribute("state", state);
	model.addAttribute("type", state);
	return "redirect:"+URLDecoder.decode(redirect_uri, "utf-8");
}

示例九(推荐)

1
2
3
4
5
6
7
8
9
10
11
12
public Object red(Model model){
	RedirectView redirectView = new RedirectView();
	redirectView.setUrl(URLDecoder.decode(codeUrl, "utf-8"));
	redirectView.setContentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE);
	redirectView.setStatusCode(HttpStatus.FOUND);
	redirectView.addStaticAttribute("client_id", clientId1);
	redirectView.addStaticAttribute("response_type", "code");
	redirectView.addStaticAttribute("redirect_uri", redirectUri);
	redirectView.addStaticAttribute("scope", scope);
	redirectView.addStaticAttribute("state", state);
	return redirectView;
}

示例十(推荐)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public Object red(Model model){
	Map<String, Object> clientMap = new HashMap<String, Object>();
	clientMap.put("client_id", clientId1);
	clientMap.put("response_type", "code");
	clientMap.put("redirect_uri", redirectUri);
	clientMap.put("scope", scope);
	clientMap.put("state", state);
	String reUrl = URLDecoder.decode(redirectUri, "utf-8");
	if (MapUtil.isNotEmpty(clientMap)) {
		for (Map.Entry<String, Object> entry : clientMap.entrySet()) {
			reUrl = reUrl + (reUrl.contains("?") ? "&" : "?") + entry.getKey() + "=" + entry.getValue();
		}
	}
	HttpHeaders httpHeaders = new HttpHeaders();
	httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
	httpHeaders.setLocation(new URI(reUrl));
	return new ResponseEntity<Object>(httpHeaders, HttpStatus.FOUND);
}

转发示例

1
String foUrl = "/oauth.html";

示例一

1
return "forward:"+foUrl;

示例二(推荐)

1
return new ModelAndView("forward:"+foUrl);

示例三

1
2
RequestDispatcher requestDispatcher = request.getRequestDispatcher(forUrl);
requestDispatcher.forward(request, response);

示例四

1
servletContext.getRequestDispatcher(foUrl).forward(request,response);