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

时间:2018-01-07 15:22:57

标签: 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>

可能导致此错误的原因是什么?

2 个答案:

答案 0 :(得分:1)

好的,我解决了这个问题。这来自:https://github.com/thymeleaf/thymeleaf-extras-springsecurity

Requirements (3.0.x) Spring Framework version 3.0.x to 4.3.x Spring Security version 3.0.x to 4.2.x

我使用Spring 5.0.0 - 使用Spring 4.3.13和Spring Security 4.2.3一切正常。

答案 1 :(得分:1)

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>