3

I want to use spring security dialect but when i use it's tags it doesn't work at all. It shows sec:authorize content anyways weather it is authenticated or not.

here is pom:

 <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
        </dependency>
<dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity4</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>

here is one of my pages:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
    <meta charset="utf-8"/>
    <div th:replace="fragments/header :: header-css"></div>
</head>
<body>
<div th:replace="fragments/header :: header"/>
<div class="container">
    <header>
        <h1 align="center">
            Main Page
            <div class="logout" sec:authorize="hasAnyAuthority('ADMIN')">
                        <span id="currentUserLogin" sec:authentication="name" >
                            user temp
                        </span>
                <a href="/logout">
                    <i class="icon-off"></i>
                </a>
            </div>
        </h1>
    </header>
</div>
<div th:replace="fragments/footer :: footer"/>
<script src="/resources/jquery-1.8.1.min.js"></script>
</body>
</html>

it always shows logout with 'user temp' text no matter what. Can anyone help me understand why is it happening? I'm using cairo-sr7 for dependacy mnagment

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.spring.platform</groupId>
                <artifactId>platform-bom</artifactId>
                <version>Cairo-SR7</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

i think i need to register dialect through template engine but how should i do it? here is my webMvcConfig

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.rjproject"})
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private ApplicationContext applicationContext;

    @Bean
    public SpringResourceTemplateResolver templateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setApplicationContext(applicationContext);
        templateResolver.setPrefix("/WEB-INF/templates/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode(TemplateMode.HTML);
        templateResolver.setCacheable(true);
        return templateResolver;
    }

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

    @Bean
    public ViewResolver viewResolver() {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine());
        return viewResolver;
    }

    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }
}
5
  • Make sure you are using the proper version and have registered the dialect.
    – M. Deinum
    Commented Apr 23, 2019 at 11:39
  • what do you mean about 'register the dialect'?
    – Ars
    Commented Apr 23, 2019 at 11:43
  • Only adding the dependency will not do much, you have to register the dialect with the thymeleaf engine in your configuration. Unless you are using Spring Boot which will configure it automatically.
    – M. Deinum
    Commented Apr 23, 2019 at 12:00
  • i updated my post with my mvcConfig where i configure thymeleaf engine. Can you please provide an example how should i configure dialect?
    – Ars
    Commented Apr 23, 2019 at 12:31
  • why you are using 2.1.2.RELEASE and not inlining with current spring security dependency version you have? Seems dependency mismatch causing this issue.
    – ScanQR
    Commented Apr 23, 2019 at 13:30

2 Answers 2

4

All i needed is this line:

springTemplateEngine.addDialect(new SpringSecurityDialect()); 

inside thymeleaf template engine config. becouse dialects should be registered manualy.

3
  • Thanks for this post. Helped me Commented Sep 22, 2019 at 23:11
  • Helped me too !
    – springcorn
    Commented Dec 1, 2020 at 3:02
  • This took 3 days for me to figure out :D And i found this after solving it :) Commented Jan 6 at 18:58
2

I assume that you are using Spring Boot 2.1.X

Then you have to use the version 5:

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.