如何在Quarkus的REST接口中创建与读取Cookie?
一、在getDateLogicielIncrementedOneDay方法中创建Cookie
Quarkus基于Jakarta EE的JAX-RS规范,可通过jakarta.ws.rs.core.NewCookie类创建Cookie,并在返回的Response中附加该Cookie。
修改后的完整代码示例:
... import jakarta.enterprise.context.ApplicationScoped; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.NewCookie; // 新增导入语句 ... @Path("/settings") @ApplicationScoped public class SettingResource { ... @Path("/get-date-logiciel-incremented-one-day") @GET @Produces(MediaType.APPLICATION_JSON) public Response getDateLogicielIncrementedOneDay() { TMmcParametrage setting = service.getSettingByKey("DATE_LOGICIELLE"); LocalDate dateLogicielle = LocalDate.parse(setting.getValeur()); LocalDate futureDateLogicielle = dateLogicielle.plusDays(1); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); String dateLogicielFuturStr = futureDateLogicielle.format(formatter); HashMap<String,String> ret = new HashMap<String,String>(); ret.put("data", dateLogicielFuturStr); // 构建Cookie:参数依次为名称、值、路径、域名、版本、注释、过期时间(秒)、是否安全、是否HttpOnly NewCookie dateCookie = new NewCookie( "logiciel_date", dateLogicielFuturStr, "/", null, "Quarkus generated cookie", 86400, // 过期时间设为1天(86400秒) false, true // 开启HttpOnly,防止前端JS读取,提升安全性 ); // 在响应中添加Cookie并返回 return Response.ok(ret, MediaType.APPLICATION_JSON) .cookie(dateCookie) .build(); } }
参数说明:
- 域名、注释等参数可根据业务需求传
null省略 HttpOnly=true是推荐配置,可避免XSS攻击窃取Cookie- 过期时间单位为秒,设为0表示会话结束后失效
二、在Quarkus其他方法中读取Cookie
方式1:通过@CookieParam直接注入(推荐)
在JAX-RS资源方法中,用@CookieParam注解直接获取指定名称的Cookie值:
@Path("/check-date-cookie") @GET public Response readCookieValue(@CookieParam("logiciel_date") String dateCookieValue) { // Cookie不存在时dateCookieValue为null return Response.ok("Cookie值:" + dateCookieValue).build(); }
若需要获取Cookie的完整属性(如过期时间、路径),可注入Cookie对象:
@Path("/check-full-cookie") @GET public Response readFullCookie(@CookieParam("logiciel_date") Cookie dateCookie) { if (dateCookie != null) { String value = dateCookie.getValue(); int maxAge = dateCookie.getMaxAge(); return Response.ok("Cookie值:" + value + ",过期时间(秒):" + maxAge).build(); } return Response.ok("未找到目标Cookie").build(); }
方式2:通过HttpHeaders获取所有Cookie
若需要一次性获取请求中的所有Cookie,可注入jakarta.ws.rs.core.HttpHeaders:
import jakarta.ws.rs.core.HttpHeaders; ... @Path("/list-all-cookies") @GET public Response readAllCookies(@Context HttpHeaders headers) { Map<String, Cookie> cookies = headers.getCookies(); Cookie dateCookie = cookies.get("logiciel_date"); return dateCookie != null ? Response.ok("目标Cookie值:" + dateCookie.getValue()).build() : Response.ok("未找到目标Cookie").build(); }
方式3:通过HttpServletRequest读取(非JAX-RS场景适用)
如果代码不在JAX-RS资源类中,可注入jakarta.servlet.http.HttpServletRequest来读取Cookie:
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.Cookie; ... @ApplicationScoped public class CookieService { @Inject HttpServletRequest request; public String getLogicielDateCookie() { Cookie[] cookies = request.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { if ("logiciel_date".equals(cookie.getName())) { return cookie.getValue(); } } } return null; } }
内容的提问来源于stack exchange,提问作者pheromix
相关产品推荐
相关产品推荐

