java - Thymeleaf+Spring,isAuthenticated()/hasRole() 不起作用

标签 java spring maven

我的 isAuthenticated() 方法有问题。似乎无论用户是否登录,它总是返回 false。此外,调用此方法后,html 代码末尾不会显示任何内容。在此示例中:

aaa
<div sec:authorize="isAuthenticated()">
    This content is only shown to authenticated users.
</div>
bbb
ccc

仅显示aaa

Logged user: <span sec:authentication="name">Bob</span>
Roles: <span sec:authentication="principal.authorities">[ROLE_USER, ROLE_ADMIN]</span>

^这段代码工作得很好,所以 sec: 应该不会有问题(我猜)

我的 Thymeleaf 配置类:

public class ThymeleafConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Bean
    public ViewResolver viewResolver() {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        resolver.setCharacterEncoding("UTF-8");
        return resolver;
    }

    @Bean
    public TemplateEngine templateEngine() {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setEnableSpringELCompiler(true);
        engine.setTemplateResolver(templateResolver());
        engine.addDialect(new SpringSecurityDialect());
        return engine;
    }

    private ITemplateResolver templateResolver() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setApplicationContext(applicationContext);
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode(TemplateMode.HTML);
        return resolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(
                "/css/**")
                .addResourceLocations(
                        "classpath:/static/css/");

    }
}

和 Maven 依赖项:

<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring4</artifactId>
    <version>3.0.9.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity4</artifactId>
    <version>3.0.2.RELEASE</version>
</dependency>

什么可能导致此错误?

最佳答案

Change your file pom.xml, and add this these dependencies:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity5</artifactId>
    </dependency>

Now in your html file add:

<!DOCTYPE html>
<html lang="es" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">

that is all 😉


    <div sec:authorize="isAuthenticated()">
        Text visible only to authenticated users.
    </div>

关于java - Thymeleaf+Spring,isAuthenticated()/hasRole() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48138580/

相关文章:

java - Maven安装报错找不到符号addServlet

java - 如何使用@Scheduled注释多次安排同一任务

java - 如何将jtable保存到文本文件并将其读回jtable

java - 将@EJB注入(inject)到JBoss中的Spring Bean中

java - Maven + SLF4J : Version conflict when using two different dependencies that require two different SLF4J versions

java - 这种属性重写在 Maven3 中有效吗?

java - 带有java的Android IP地址

java - PHP CUrl 的 Java 替代品是什么

java - Spring使用java配置在 session 范围内定义一个bean

java - Maven : distribution management configuration in POM file or settings. xml