【问题标题】:Spring Security Expressions in ThymeleafThymeleaf 中的 Spring 安全表达式
【发布时间】:2017-08-11 20:59:06
【问题描述】:

使用 Spring Boot 1.5.2 和 Thymeleaf 2.1,我试图在 HTML 页面上添加一些代码来识别用户的角色。

但是,所有这些语句的计算结果都是正确的,这是不正确的:

<div sec:authorize="hasAuthority('ADMIN')" > Has Authority ADMIN </div> 
<div sec:authorize="hasAuthority('USER')" > Has Authority USER </div> 
<div sec:authorize="hasRole('ROLE_ADMIN')">Has Role ROLE_ADMIN</div>
<div sec:authorize="hasRole('ROLE_USER')">Has Role ROLE_USER</div>
<div sec:authorize="hasRole('ADMIN')">Has Role ADMIN</div>
<div sec:authorize="hasRole('USER')">Has Role USER</div>

User.java

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles;

Role.java

@Entity
@Table(name = "role")
public class Role {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private int id;

    @Column(name = "role")
    private String role;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

}

【问题讨论】:

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


    【解决方案1】:

    我解决了这个问题。我错过了三个项目:

    thymeleaf-extras-springsecurity4

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

    html 模板中的 xmlns:sec

    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
    

    正确的输出

    现在,当具有 ROLE=USER 的用户登录时,渲染模板时会显示“具有权限 USER”

    <div sec:authorize="hasAuthority('USER')" > Has Authority USER </div> 
    

    【讨论】:

      【解决方案2】:

      检查模板中的定义xmlns:sec - html

      【讨论】:

        【解决方案3】:

        您是否在页面的 html 部分中定义了 sec。像这样:

        <html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
        

        【讨论】:

          【解决方案4】:

          你需要在porm中导入

          <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-thymeleaf</artifactId>
                </dependency>
          
          <dependency>
                      <groupId>org.thymeleaf.extras</groupId>
                      <artifactId>thymeleaf-extras-springsecurity4</artifactId>
                  </dependency>
          

          这是给你的网站 gui https://www.thymeleaf.org/doc/articles/springsecurity.html

          【讨论】:

            【解决方案5】:

            听起来很奇怪..

            你确定你的项目中包含了 Thymeleaf 吗?

            如果您的项目/文件中未包含 Thymeleaf,则 html 页面将呈现为常规 html 页面。因此,您可能看起来拥有所有角色,但实际上,该网站只是呈现为常规 html。

            你可以用这个检查你的 Thymeleaf 是否正确渲染:

            <p th:text="Hello World!" />
            

            【讨论】:

              猜你喜欢
              • 2015-12-31
              • 2017-04-06
              • 2021-11-29
              • 2018-02-15
              • 2018-04-21
              • 2018-06-09
              • 1970-01-01
              • 2019-04-19
              • 2017-05-16
              相关资源
              最近更新 更多