【发布时间】:2021-06-20 16:56:42
【问题描述】:
sec:authorize 不起作用... 这是我的 index.html 代码。
<!DOCTYPE html>
<html lang="ko"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="https://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<!--xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5"> -->
<head>
<meta http-equiv="Content-type" content="text/html;" charset="UTF-8" />
<title>title</title>
</head>
<body>
<div sec:authorize="isAuthenticated()">
<a href="/logout">Logout</a>
</div>
<div sec:authorize="!isAuthenticated()">
<a href="/user/login">Login</a>
</div>
</body>
</html>
我尝试使用 springsecurity5 命名空间运行,但也失败了。 我不知道为什么这不起作用。
这是我的 build.gradle 配置,仅关于依赖项。
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'
implementation 'org.modelmapper:modelmapper:2.3.0'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'mysql:mysql-connector-java'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
这是我的 Spring Security 配置代码。
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/user/login", "/user/signIn", "/", "/incomeMap").permitAll()
.anyRequest().authenticated()
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/user/login")
.and()
.formLogin()
.loginPage("/user/login")
.loginProcessingUrl("/login");
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("admin").password("{noop}admin").roles("ADMIN")
.and()
.withUser("user").password("{noop}user").roles("USER");
}
}
和<div sec:authorize> 没有处理这些配置和代码。
【问题讨论】:
-
index.html 是否在“src/main/resources/templates”文件夹中?
-
是的,这就是它不起作用的原因。
标签: html spring-boot gradle spring-security thymeleaf