gpt4 book ai didi

java - Thymeleaf spring 安全方言根本不起作用

转载 作者:行者123 更新时间:2023-11-30 05:40:11 25 4
gpt4 key购买 nike

我想使用 spring security 方言,但是当我使用它的标签时,它根本不起作用。无论是否经过身份验证,它都会显示 sec:authorize 内容。

这是 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/");
}
}

最佳答案

我需要的只是这一行:

springTemplateEngine.addDialect(new SpringSecurityDialect()); 

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

关于java - Thymeleaf spring 安全方言根本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55810044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com