Thymeleaf 报错 Exception evaluating SpringEL expression:

这个错误是多个原因引起的,表现是页面展示正常,后台 IDEA 窗口报错,因为在 HTML 注释掉的内容中包涵 spEL 表达式

如果您在使用`<div th:if="${#authorization.expression('hasRole(\'2\')')}">`时遇到了`Exception evaluating SpringEL expression`的错误,可能是因为您没有正确配置Spring Security。 在Spring Boot中,要使用Spring Security,您需要添加`spring-boot-starter-security`依赖,并在您的应用程序中添加`@EnableWebSecurity`注解。此外,如果您希望在Thymeleaf中使用表达式语言,请确保您的应用程序已经添加了`thymeleaf-extras-springsecurity5`依赖。 请按照以下步骤检查您的配置: 1. 检查您的依赖:请确保您的`pom.xml`文件中已经添加了`spring-boot-starter-security`和`thymeleaf-extras-springsecurity5`依赖。 2. 添加@EnableWebSecurity注解:请在您的应用程序的主类上添加`@EnableWebSecurity`注解。 3. 配置Spring Security:在您的应用程序中添加Spring Security配置类,例如: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/admin/**").hasRole("ADMIN") .anyRequest().authenticated() .and() .formLogin().permitAll() .and() .logout().permitAll(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("user").password("{noop}password").roles("USER") .and() .withUser("admin").password("{noop}password").roles("ADMIN"); } } ``` 在上面的示例中,我们配置了基于内存的身份验证,并为用户`user`和`admin`分配了`USER`和`ADMIN`角色。我们还配置了`/admin/**`路径需要`ADMIN`角色才能访问。 4. 使用表达式语言:在Thymeleaf模板中,您可以使用`<div th:if="${#authorization.expression('hasRole(\'2\')')}">`来检查当前用户是否具有`2`角色。 希望这些信息能够帮助您解决问题。如果您需要更多的帮助,请提供更多的细节和代码。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值