【发布时间】:2016-04-01 03:32:07
【问题描述】:
我正在使用 Spring Boot、Spring Security 和 Thymeleaf。目前,访问页面时身份验证和授权工作正常,但我无法使百里香授权工作。流程非常简单:我尝试访问需要具有“管理员”角色的 admin.html 页面。 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 元素,如下面的 sn-p:
<div sec:authorize="hasRole('ROLE_ADMIN1')">
This content is only shown to administrators.
</div>
但是,虽然它没有给出错误,但它显示了每个角色的秘密内容,甚至是不存在的角色。
我试图通过添加 SpringSecurityDialect 来定义 SpringTemplateEngine,但我收到一个错误,说我声明了两次(这是因为 Maven 依赖于我猜想的额外库)。
非常感谢您的帮助?
【问题讨论】:
标签: spring maven spring-security spring-boot thymeleaf