当前位置: 首页 > 知识库问答 >
问题:

thymeleaf中的身份验证对象为空或空

丌官嘉福
2023-03-14

我用的是Spring boot Spring Security和Thymeleaf。目前,身份验证和授权工作良好时访问页面,但我不能使thymeleaf授权工作。流程非常简单:我尝试访问admin.html页面,对于该页面,我需要一个“admin”角色。Spring Security正确地截取了请求,首先将我转发到登录页面,当以管理员身份登录时,它允许我继续。

现在我想根据角色“隐藏”管理页面上的一些内容。因此,在我的admin.html页面中,我有以下内容:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:include="header :: common-header">
    <title>Admin Page</title>
</head>

<body>

<div th:replace="navbar :: common-navbar"></div>

<div class="container">

    <div class="row">

        <div class="col-md-6 col-md-offset-3">

            <div class="well">

                <div th:if="${#authorization.expression('hasRole(''ROLE_ADMIN'')')}">
                    Secret Content
                </div>

                <div class="starter-template">
                    <h1>Hello Administrator!</h1>
                    <p class="lead">This page allows you to perform administrative tasks</p>
                    <form id="f" th:action="@{/logout}" method="post" role="form" class="form-group">
                        <input type="submit" value="Logout" class="btn btn-primary" />
                    </form>
                </div>

            </div>

        </div>

    </div>

</div><!-- /.container -->

<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<div th:include="header :: before-body-script">

</div>
</body>
</html>

我的pom.xml:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>


    <!-- Thymeleaf Dialect -->
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        <version>3.0.0.BETA01</version>
    </dependency>

我的安全配置类:

@Override
protected void configure(HttpSecurity http) throws Exception {

    // Allows for static content
    http.authorizeRequests().antMatchers("/webjars/**").permitAll();
    http.authorizeRequests().antMatchers("/css/**").permitAll();
    http.authorizeRequests().antMatchers("/js/**").permitAll();

    // Defines security for everything else
    http
            .authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/admin").hasRole("ADMIN")
                .anyRequest().authenticated()
                .and()
            .formLogin().loginPage("/login").failureUrl("/login?error").permitAll()
                .and()
            .logout()
                .permitAll();

}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth
            .inMemoryAuthentication()
            .withUser("user").password("password").roles("USER")
            .and()
            .withUser("admin").password("admin").roles("USER", "ADMIN");
}

我已经试了好几个小时来解决这个问题,但没有成功。当然,我可以避免使用Spring boot,可以使用JSP来代替thymeleaf,但我非常喜欢Spring boot+Spring Security+thymeleaf的组合,因为它更接近于干净的HTML。

我还尝试使用sec元素,如以下代码段所示:

  <div sec:authorize="hasRole('ROLE_ADMIN1')">
      This content is only shown to administrators.
  </div>

但是,虽然它没有给出错误,但它显示了每个角色的秘密内容,即使是不存在的角色。

我试图通过添加SpringSecurityDiscuale来定义SpringTemplateEngine,但是我得到了一个错误,说我声明了它两次(我想这是因为Maven依赖于extras库)。

我真的很感谢你的帮助?

共有1个答案

纪翰
2023-03-14

通过更改pom.xml中的以下依赖项修正:

 <!-- Thymeleaf Dialect -->
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        <version>2.1.2.RELEASE</version>
    </dependency>

而不是3.0.0.beta01。现在以下工作:

<div sec:authorize="hasRole('ROLE_ADMIN')">
                    This content is only shown to administrators.
                </div>
 类似资料:
  • 我目前正在使用Spring Security Oauth2 2.0.7版的一个相当普通的设置。与JdbcTokenStore一起发布,最近遇到了一个有趣的问题。 我知道,当访问令牌被创建和存储时,对象被序列化,并持久化到数据库中。我知道这是必要的,并且在以后在请求中使用访问令牌时使用。问题是,我的对象(Spring Security的用户详细信息接口的自定义iml)与我的应用程序域中的其他对象相关

  • 多因素身份验证的Spring OAuth2实现的完整代码已经上载到文件共享站点,您可以通过单击此链接下载该站点。下面的说明解释了如何使用链接在任何计算机上重新创建当前问题。提供500点的赏金。 当用户试图使用Spring Boot OAuth2应用程序中的双因素身份验证(来自上一段中的链接)进行身份验证时,将触发一个错误。当应用程序应该提供第二个页面来询问用户的pin码以确认用户的身份时,错误就会

  • 问题内容: 我正在尝试编写一个简单的脚本,该脚本从内部网络上的工具请求一些数据。这是代码: 现在的问题是,我该如何使用Kerberos票证进行身份验证,而不是以纯文本形式提供我的凭据? 问题答案: 在Paul Scheltema的答案中,您需要从操作系统的深度获取票证数据。您(或代表您的模块)必须使用GSS-API才能由Active Directory生成票证数据。 这种机制存在于Chrome中,

  • 我有一个使用spring security oauth2进行安全连接的项目。下面是我的spring配置文件。 Spring-安全.xml : 当我使用以下请求请求oauth访问令牌时,我将获得如下访问和刷新令牌。 请求是 回应是: 然后,当我使用下面的请求请求受保护的资源时,我收到“在SecurityContext中找不到身份验证对象”作为错误。 请求是 : 我使用“2.0.7.RELEASE”作

  • 身份验证 PDF版下载 企业应用中的URL链接可以通过OAuth2.0验证接口来获取员工的身份信息。 通过此接口获取员工身份会有一定的时间开销。对于频繁获取员工身份的场景,建议采用如下方案: 企业应用中的URL链接直接填写企业自己的页面地址; 员工跳转到企业页面时,企业校验是否有代表员工身份的cookie,此cookie由企业生成; 如果没有获取到cookie,重定向到OAuth验证链接,获取员工

  • 问题内容: 如何在Elasticsearch中定义安全性访问?我有elasticsearch-head插件,但是您的访问不需要任何安全性。 问题答案: 不再积极支持此答案中提到的插件。 elasticsearch中没有内置的访问控制。因此,您需要设置一个反向代理(这是一个博客文章,介绍如何设置nginx),使用第三方的Elasticsearch插件之一,例如https://github.com/A