【问题标题】:sec:authorize doesn't work on spring securitysec:authorize 对 Spring Security 不起作用
【发布时间】: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 命名空间运行,但也失败了。 我不知道为什么这不起作用。

index.html 输出

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

&lt;div sec:authorize&gt; 没有处理这些配置和代码。

【问题讨论】:

  • index.html 是否在“src/main/resources/templates”文件夹中?
  • 是的,这就是它不起作用的原因。

标签: html spring-boot gradle spring-security thymeleaf


【解决方案1】:

你只能像下面这样使用百里香:

<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
...
<body>
        ...
        <a th:if="${NOT #authorization.expression('hasRole(''ROLE_USER'')')}"
           th:href="@{/login}"  class="btn btn-primary"> Login </a>
        <a th:if="${#authorization.expression('hasRole(''ROLE_USER'')')}"
           th:href="@{/logout}" class="btn btn-primary"> Logout </a>
        ...
<body>

如果不起作用,请尝试将ROLE_USER 替换为USER

【讨论】:

    猜你喜欢
    • 2018-10-31
    • 2016-08-03
    • 2014-05-26
    • 2020-08-05
    • 2014-12-12
    • 2021-02-23
    • 2015-04-20
    • 2014-06-14
    • 2015-11-26
    相关资源
    最近更新 更多