【发布时间】:2017-09-20 11:37:55
【问题描述】:
我在我的 Spring Boot 应用程序中使用 Spring Security,Thymeleaf 授权似乎无法正常工作。
我有带有以下代码的 Thymeleaf 模板:
<div class="container">
<div class="row" sec:authorize="isAuthenticated()">
<h2 style="color:green">User is Logged In</h2>
<p sec:authentication="principal.username">username</p>
</div>
<div class="row" sec:authorize="!isAuthenticated()">
<h2 style="color:red">User is Logged Out</h2>
</div>
<div class="row" sec:authorize="hasRole('ROLE_SUPERUSER')">
<h2>This will only be displayed if authenticated user has role ROLE_SUPERUSER.</h2>
</div>
<div class="row" sec:authorize="hasRole('ROLE_ADMIN')">
<h2>This will only be displayed if authenticated user has role ROLE_ADMIN.</h2>
</div>
<div class="row" sec:authorize="hasRole('ROLE_USER')">
<h2>This will only be displayed if authenticated user has role ROLE_USER.</h2>
</div>
<div th:if="${#authorization.expression('hasRole(''ROLE_ADMIN'')')}">
This will only be displayed if authenticated user has role ROLE_ADMIN.
</div>
<div th:if="${#authorization.expr('hasRole(''ROLE_ADMIN'')')}">
This will only be displayed if authenticated user has role ROLE_ADMIN.
</div>
</div>
示例来自:https://github.com/thymeleaf/thymeleaf-extras-springsecurity
但是显示的唯一内容是sec:authorize="isAuthenticated()" 和sec:authorize="!isAuthenticated()",并且无论用户的角色如何,授权总是被忽略。
我的百里香配置是:
@Configuration
public class ThymeleafConfig {
@Bean
public TemplateResolver defaultTemplateResolver() {
TemplateResolver resolver = new TemplateResolver();
resolver.setResourceResolver(thymeleafResourceResolver());
resolver.setPrefix("classpath:/templates/");
resolver.setSuffix(".html");
resolver.setTemplateMode("HTML5");
resolver.setCharacterEncoding("UTF-8");
resolver.setCacheable(true);
return resolver;
}
@Bean
public SpringResourceResourceResolver thymeleafResourceResolver() {
return new SpringResourceResourceResolver();
}
@Bean
public SpringTemplateEngine templateEngine(TemplateResolver templateResolver) {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(templateResolver);
engine.addDialect(new SpringSecurityDialect());
engine.addDialect(new LayoutDialect());
return engine;
}
@Bean
public ThymeleafViewResolver thymeleafViewResolver(SpringTemplateEngine templateEngine) {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine);
resolver.setCharacterEncoding("UTF-8");
resolver.setContentType("text/html");
resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 5);
return resolver;
}
}
我对@987654322@ 使用以下依赖项:
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
版本 3.0.2.RELEASE 根本不起作用,sec 命名空间总是被 Thymeleaf 忽略。
我的 Spring Boot 版本是1.5.2.RELEASE。
可能是什么原因?
更新。SecurityConfig 中的 configure(HttpSecurity http) 方法如下所示:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().ignoringAntMatchers("/h2-console").disable()
.authorizeRequests()
.antMatchers("/webjars/**", "/static/**", "/images/**", "/**/favicon.ico").permitAll()
.antMatchers("/heat/**", "/power/**", "/water/**").permitAll()
// start allowing h2-console
.antMatchers("/h2-console/**").permitAll();
http.csrf().disable();
http.headers().frameOptions().disable()
// end allowing h2-console
.and().authorizeRequests().antMatchers("/info").permitAll()
.and().authorizeRequests().antMatchers("/users/**").authenticated()
.and().authorizeRequests().antMatchers("/users/**").hasAnyAuthority("ADMIN", "SUPERUSER")
.and().formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll()
.deleteCookies("remove")
.logoutUrl("/logout")
.logoutSuccessUrl("/")
.invalidateHttpSession(true)
.and().exceptionHandling().accessDeniedPage("/access_denied");
}
IndexController 的映射非常简单,它只返回 login 模板:
@RequestMapping("/login")
public String loginForm() {
return "login";
}
【问题讨论】:
-
据我所知,角色只是一个带有特殊
ROLE_前缀的权限。我的角色没有这个前缀,它们只是USER、ADMIN等等:i.stack.imgur.com/jpBA1.png -
当我使用
hasRole时,我会在我的角色前面加上ROLE_,但表中的角色名称本身没有这个前缀。 -
@DimaSan:这就是重点。如果使用
hasRole,则必须在数据库中使用前缀。但是您可以配置不同的前缀或使用hasAuthority(如您的回答)。 -
是的,您是对的,现在我可以确认它可以按照您的描述工作。那么,让角色名称带有或不带有
ROLE_前缀的最佳做法是什么? -
我只使用带前缀的角色名,因为我不喜欢太多的自定义配置。我尽量使用默认值。但是 Spring 开发人员会说,两者皆有可能是你的决定。
标签: java spring-boot spring-security thymeleaf