![](/img/trans.png)
[英]How to add Authorization header in redirect (Spring Security)
[英]Spring: Redirect with Authorization Header
我目前正在編寫一個按需發布 JWT 令牌的應用程序。 發出令牌后,應將用戶重定向到網頁。 這就像一個魅力 - 但我需要為該重定向設置一個授權標頭。
用戶在網頁 A 上輸入其憑據。網頁 A 向服務器 B 發送 POST 請求。服務器 B 檢查憑據並提供令牌。 現在用戶應該被重定向到網頁 C。
我嘗試了以下方法:
@RequestMapping(value = "/token", method = RequestMethod.POST, produces = "application/json", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity<Object> token(
@RequestParam("user") String _username,
@RequestParam("secret") String _secret
) throws Exception
{
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("user", _username);
map.add("secret", _secret);
HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<MultiValueMap<String, String>>(map, headers);
HttpStatus statusCode = HttpStatus.FOUND;
HttpHeaders httpHeaders = new HttpHeaders();
try {
ResponseEntity<String> request = restTemplate.exchange(_url, HttpMethod.POST, entity, String.class);
} catch (Exception ex) {
ex.printStackTrance();
}
String response = request.getBody();
JSONObject _tokenObject = new JSONObject(response);
String _token = _tokenObject.getString("access_token");
httpHeaders.add("Authorization", "Bearer: " + _token);
URI _redirectUri = new URI("http://foo.example.com/webpageC");
httpHeaders.setLocation(_redirectUri);
return new ResponseEntity<>(httpHeaders, HttpStatus.FOUND);
}
重定向有效,但只有/token
才能在重定向發生之前將授權標頭作為響應標頭。
如何實現將標頭發送到網頁 C?
謝謝。
更新
forward:
是不可能的,因為網頁 C 在另一個 URL 上而不是在同一個控制器中。
任何人都有一個想法如何解決?
通常,我們讓前端開發人員處理重定向。 如果你在后端工作,你可以提供一個寧靜的 API 來發布 JwtTokens。 前端會擔心如何在以下重定向的 Http 請求中攜帶 Authorization 標頭。 這是一個簡單的登錄控制器,使用mobile
和password
來換取 JwtToken。
@RequestMapping(value = "/login", method = RequestMethod.POST)
public Result login(@RequestBody Map<String, String> loginMap) {
User user = userService.findByMobile(mobile);
if(user == null || !user.getPassword().equals(password)) {
return new Result(ResultCode.MOBILEORPASSWORDERROR);
}else {
String token = jwtUtils.createJwt(user.getId(), user.getUsername(), map);
return new Result(ResultCode.SUCCESS,token);
}
}
如果您作為后端,無論如何都希望處理重定向,請將請求重定向到以令牌為參數的網頁,在這種情況下:
GET http://www.example.com/login/success?token=xxx&redirectUrl=%2Fxxx
相關的后端代碼將是:
protected String determineTargetUrl(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
Optional<String> redirectUri = CookieUtils.getCookie(request, REDIRECT_URI_PARAM_COOKIE_NAME)
.map(Cookie::getValue);
if(redirectUri.isPresent() && !isAuthorizedRedirectUri(redirectUri.get())) {
throw new BadRequestException();
}
String targetUrl = redirectUri.orElse(getDefaultTargetUrl());
String token = tokenProvider.createToken(authentication);
return UriComponentsBuilder.fromUriString(targetUrl)
.queryParam("token", token)
.build().toUriString();
}
同樣,讓前端將令牌作為授權標頭放入進一步的請求中。
請記住,您正在返回一個響應,以便您可以設置響應標頭。 您無法為前端設置下一個請求的請求標頭。
參考: https : //www.callicoder.com/spring-boot-security-oauth2-social-login-part-2/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.