admin管理员组文章数量:1387338
in my Spring/Angular application I want ensure, whether I going to correct way in security.
Logic about generating tokens have already done, but is correct following logic ?
This POST create a cookie and set this cookie to the browser.Inside of this cookie is generated XSRF-TOKEN for secure CSRF attacks, there is aswell .httpOnly(true) for unpossible get this token by javascript. This cookie is set automatically by BE, so FE don't need get this token from body or header of response. In BE configuration have a @Bean , which mark me this cookie as 'Strict'.
At the end, return of this authenticate() method is JWT Authorization Bearer token in body, which will be manually stored into cookies via Angular. If user is not authenticated and doesn't exist throw error.
@PostMapping("/authenticate")
public ResponseEntity<AuthenticationResponse> authenticate(@RequestBody UserClient user, HttpServletResponse response, HttpServletRequest request){
CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class
.getName());
if (csrf != null) {
Cookie cookie = new Cookie("XSRF-TOKEN", csrf.getToken());
cookie.setPath("/");
cookie.setHttpOnly(true);
//cookie.setSecure(true); // if using https
response.addCookie(cookie);
}
return ResponseEntity.ok().body(loginService.authenticate(user));
}
Additional questions:
Is better idea separate these Tokens to 2 different endpoints ?
本文标签: javaGenerate JWT token and Cookie in one API callStack Overflow
版权声明:本文标题:java - Generate JWT token and Cookie in one API call - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744539703a2611534.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论