问题描述:
项目扫描的时候出一个漏洞,Cookie未配置HttpOnly标志。那HttpOnly是什么呢?Httponly是微软对cookie做的扩展。这个主要是解决用户的cookie可能被盗用的问题。在web应用中、JSESSIONID (Cookie)没有设置Httponly属性可能会窃取或操纵客户会话和 cookie,它们可能用于模仿合法用户,从而使黑客能够以该用户身份查看或变更用户记录以及执行事务。
解决办法:
这里我是写一个拦截器,实现GlobalFilter,我们可以在拦截器中向Cookie中添加HttpOnly属性。
@Component
@Slf4j
public class CookieFilter implements GlobalFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpResponse response = exchange.getResponse();
ServerHttpRequest request = exchange.getRequest();
MultiValueMap<String, HttpCookie> cookies = request.getCookies();
for (Map.Entry<String, List<HttpCookie>> stringListEntry : cookies.entrySet()) {
List<HttpCookie> cookieList = stringListEntry.getValue();
// 所有的Cookie都要添加
for (HttpCookie httpCookie : cookieList) {
ResponseCookie httpOnlyCookie = ResponseCookie.from(httpCookie.getName(), httpCookie.getValue())
.httpOnly(true)
.sameSite("Lax")
.build();
// 添加到响应头
response.getHeaders().add(HttpHeaders.SET_COOKIE, httpOnlyCookie.toString());
}
}
return chain.filter(exchange);
}
}