使用ngb-cookie库生成安全的HttpOnly cookie,同时在Spring Boot中启用HTTPS。
代码示例:
Angular端:
// 安装ngb-cookie库 npm install --save ngb-cookie
// 在需要使用cookies的组件中导入CookieService import { CookieService } from 'ngb-cookie';
constructor(private cookieService: CookieService) { }
// 设置cookie this.cookieService.put('cookieName', 'cookieValue', { secure: true, sameSite: 'None', httpOnly: true });
// 获取cookie this.cookieService.get('cookieName');
Spring Boot端:
// 在Spring Boot配置文件中开启HTTPS server: port: 8443 ssl: enabled: true key-store: classpath:keystore.jks key-store-password: password key-store-type: JKS
// 在Controller中使用@CookieValue注解获取cookie @GetMapping("/endpoint") public ResponseEntity> endpointMethod(@CookieValue(value = "cookieName") String cookieValue) { // Do something with cookieValue }