Thymeleaf Spring安全方言根本不起作用

时间:2019-04-23 11:19:04

标签: java spring spring-mvc spring-security thymeleaf

我想使用Spring Security方言,但是当我使用它的标签时,它根本无法工作。它显示sec:无论是否经过身份验证都对内容进行授权。

这是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>

这是我的页面之一:

<!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>

无论如何,它总是显示带有“用户临时”文本的注销。谁能帮助我了解为什么会这样? 我正在使用cairo-sr7进行依赖性管理

<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>

我认为我需要通过模板引擎注册方言,但是我该怎么办? 这是我的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/");
    }
}

2 个答案:

答案 0 :(得分:0)

我假设您正在使用Spring Boot 2.1.X

然后您必须使用版本5:

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

答案 1 :(得分:0)

我所需要的只是这一行:

// on click call this function
    (click)="HyperLink()"
// on ts file 
    HyperLink(){
       window.open('/locations-list');  
   }

内部thymeleaf模板引擎配置。 因为方言应该手动注册。