Java 基于角色Springboot+;禁用/启用Html元素;百里香

Java 基于角色Springboot+;禁用/启用Html元素;百里香,java,spring,spring-security,thymeleaf,Java,Spring,Spring Security,Thymeleaf,我有一个具有多个角色(角色1、角色2)的安全springboot应用程序。一个用户同时拥有两个角色,而另一个用户只有一个角色。成功登录后,用户被发送到登录页,在那个里我想禁用元素,若用户只有一个角色 我已经尝试过thymeleaf-extras-springsecurity3,但没有成功。这是我的代码: Pom.xml ... <dependency> <groupId>org.thymeleaf.extras</groupId>

我有一个具有多个角色(角色1、角色2)的安全springboot应用程序。一个用户同时拥有两个角色,而另一个用户只有一个角色。成功登录后,用户被发送到登录页,在那个里我想禁用元素,若用户只有一个角色

我已经尝试过thymeleaf-extras-springsecurity3,但没有成功。这是我的代码:

Pom.xml

...
<dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity3</artifactId>
            <version>3.0.2.RELEASE</version>
        </dependency>
...
但是没有成功,我总是得到像这样的对象的空错误

org.springframework.expression.spel.SpelEvaluationException: EL1011E: Method call: Attempted to call method getPrincipal() on null context object
任何帮助都将不胜感激!谢谢

我认为方法是使用
xmlns:sec=”http://www.thymeleaf.org/extras/spring-security“
namespace以实现您想要的结果
sec:authorize=“hasRole('ROLE\u ADMIN')”

再看看类似的问题:

org.springframework.expression.spel.SpelEvaluationException: EL1011E: Method call: Attempted to call method getPrincipal() on null context object
<html xmlns:th="http://www.thymeleaf.org" 
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>...</head>
<body>
    <div sec:authorize="hasRole('ROLE_ADMIN')">...</div>

    // or use #authorization, but use escape quote 'hasRole(''ROLE_USER'')'
    <div th:if="${#authorization.expression('hasRole(''ROLE_USER'')')}">...</div>
</body>
 @Bean
public TemplateEngine templateEngine() {
    SpringTemplateEngine engine = new SpringTemplateEngine();
    engine.setTemplateResolver(templateResolver());
    engine.addDialect(securityDialect());
    return engine;
}

private IDialect securityDialect(){
    SpringSecurityDialect dialect = new SpringSecurityDialect();
    return dialect;
}