thymeleaf获取cookie及当cookie不存在时显示异常的解决方法

一般用法:

<input id="login-Act" type="text" class="form-control" placeholder="请输入账号"
                       th:each="cookie :${#httpServletRequest.getCookies()}"
                       th:if="${cookie.getName().equals('accountNum')}"
                       th:value="${cookie.getValue()}">

这时如果浏览器不存在该Cookie值,会发现整个input框消失了;我看了一下控制台,发现当找不到Cookie时,整个input标签会被移除掉,这就是input框不见的原因。
所以,如果想动态填充值进输入框,可将遍历的过程放到另一个标签里即可,这样如果Cookie不存在,删除的也只是无伤大雅的标签罢了。

<input id="loginAct" type="text" class="form-control" placeholder="请输入账号">
                <span id="initAct" th:each="cookie :${#httpServletRequest.getCookies()}"
                      th:if="${cookie.getName().equals('accountNum')}"
                      th:acc="${cookie.getValue()}"></span>

这里用了span标签,acc属性是我们自定义的,用来保存Cookie值。
ps:不建议用id/class保存。

之后在js里获取span的acc属性值,将其填到input标签里即可:

$(function () {
		var initAct = $("#initAct").attr("acc");
        $("#loginAct").val(initAct);
})
			
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Thymeleaf is a popular Java-based templating engine used for server-side rendering in web applications. It works well with Spring and Spring Boot frameworks. CSRF (Cross-Site Request Forgery) is a security vulnerability that allows attackers to perform unwanted actions on behalf of authenticated users. To prevent CSRF attacks in Thymeleaf, you can use Thymeleaf's built-in support for CSRF protection. Thymeleaf provides a CSRF token mechanism that you can use to protect your forms. To include a CSRF token in your Thymeleaf forms, you can use the `th:action` and `th:object` attributes. Here's an example: ```html <form th:action="@{/submit}" th:object="${form}" method="post"> <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" /> <!-- other form fields --> <button type="submit">Submit</button> </form> ``` In this example, `@{/submit}` is the form action URL, `${form}` is the form object, and `${_csrf.parameterName}` and `${_csrf.token}` are Thymeleaf expressions for the CSRF token name and value, respectively. The CSRF token is added as a hidden input field in the form. On the server-side, you need to configure CSRF protection in your Spring Security configuration. You can enable CSRF protection by adding the `@EnableWebSecurity` annotation to your configuration class and configuring it to use Thymeleaf's CSRF support. Here's an example: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .csrf() .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()); } } ``` In this example, `CookieCsrfTokenRepository.withHttpOnlyFalse()` is used as the CSRF token repository. This configuration allows the CSRF token to be stored in a cookie and sent as a request header. By following these steps, you can integrate Thymeleaf's CSRF protection in your web application to prevent CSRF attacks.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值