![](/img/trans.png)
[英]Thymeleaf 3.0 Spring Boot + Security integration does not work
[英]Thymeleaf Security does not work with Spring Boot 1.3.5
我正在尝试使我的Web应用程序再次与新的Spring Boot版本1.3.5一起使用,但是百里香方言似乎不再起作用。
我加了
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
到我的.pom甚至在我的安全配置中注册了一个bean:
@Bean
public SpringSecurityDialect springSecurityDialect(){
SpringSecurityDialect dialect = new SpringSecurityDialect();
return dialect;
}
(尽管我认为弹簧靴无论如何都会帮我做到这一点)。 温使用诸如
<li sec:authorize="hasRole('SITE_ADMIN') || hasRole('TENANT_ADMIN')">Admin
</li>
尽管我可以将自定义User元素注入控制器,但是该角色将显示为空,因此该角色将不会呈现:
@RequestMapping(value="/", method=RequestMethod.GET)
public String homePage(@AuthenticationPrincipal VZUser user, Model model){
model.addAttribute("email", user.getEmail());
return "home/index";
}
从调试中,我看到注入的Principal对象不是null,但是模板似乎无法解析sec:对象。 我删除了xmlns名称空间,然后重新插入它而没有任何效果。 坦率地说,有关此功能的文档令人震惊。 我想念什么吗?
哦耶。 在Spring Boot 1.3.2中可以使用相同的代码(没有新的依赖项并且在xhtml模板中具有名称空间声明)...
更新
我认为这与我使用自定义UserDetailsService的事实有关。 我的内存服务没有问题。 但是,我的自定义用户实现仅具有以下内容:
@Override
public Collection<? extends GrantedAuthority> getAuthorities(){
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
for(VZUserRoles role: roles){
authorities.add(new SimpleGrantedAuthority(role.name()));
}
return authorities;
}
我定义了一个基本的UserDetails服务,并将其放入AuthenticationMAnagerBuilder中。 身份验证工作正常,但似乎并没有传递给视图。
确实,您不需要定义SpringSecurityDialect
bean,它是通过Spring Boot自动配置为您完成的。
我在这里的示例中没有看到任何错误,并且我尝试重现该问题,但未成功。
具有以下依赖性:
<dependencies>
<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.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
一个简单的控制器:
@Controller
public class HomeController {
@RequestMapping("/")
public String home() {
return "index";
}
}
定制安全性配置:
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("brian").password("password").roles("USER");
}
}
index.html模板:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
</head>
<body>
<h2>Thymleaf example</h2>
<p sec:authorize="hasRole('ROLE_USER')">
Authenticated
</p>
<p th:text="${#authentication.principal.username}">the_username</p>
</body>
</html>
一切正常。
一旦有了最小的repro项目,就可以随意创建问题 。
我找到了一种解决方法,该方法可能与Spring Security 4处理角色的方式有关。
sec:authorize="hasAuthority('SITE_ADMIN')"
效果很好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.