【问题标题】:Thymeleaf Security does not work with Spring Boot 1.3.5Thymeleaf 安全不适用于 Spring Boot 1.3.5
【发布时间】:2016-10-26 02:20:14
【问题描述】:

我正在尝试让我的 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;
}

(尽管我认为 Spring Boot 无论如何都会为我做到这一点)。 Wen 使用诸如 e 之类的表达方式。 G。

<li sec:authorize="hasRole('SITE_ADMIN') || hasRole('TENANT_ADMIN')">Admin 
</li>

不会渲染表达式,因为角色似乎为空,尽管我可以将自定义用户元素注入控制器:

@RequestMapping(value="/", method=RequestMethod.GET)
public String homePage(@AuthenticationPrincipal VZUser user, Model model){
    model.addAttribute("email", user.getEmail());
    return "home/index";
}

通过调试,我看到注入的 Principal 对象不为空,但模板似乎无法解析 sec: 对象。我删除了 xmlns 命名空间并重新插入它没有效果。坦率地说,有关此功能的文档非常令人震惊。有什么我想念的吗?

哦,是的。在 Spring Boot 1.3.2 中使用相同的代码(没有新的依赖项和 xhtml 模板中的命名空间声明)...

更新

我认为这与我使用自定义 UserDetailsS​​ervice 的事实有关。我对内存服务没有任何问题。但是,我的自定义用户实现只有以下内容:

@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。身份验证工作正常,只是似乎没有传递给视图。

【问题讨论】:

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


【解决方案1】:

确实,您不需要定义 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>

一切都按预期进行。

如果您有minimal repro project,请随时联系create an issue

【讨论】:

  • 谢谢!只要我使用内存中的UserDetailsS​​ervice,我就没有任何问题。但是,将我的帐户存储在 MongoDB 中后,我需要一个自定义服务(顺便说一句,它可以很好地用于身份验证)。
  • 那么问题可能出在该自定义服务上(不遵守委托人合同或不担任角色?),而不是 Thymeleaf 扩展这里...
  • 可能是。但是,我可以使用 ${#authentication.principal.authorities}。所以校长在。也许我会暂时解决这个问题。就像我说的。在 Spring Boot 1.3.5 之前,相同的代码没有问题。
【解决方案2】:

我找到了一种解决方法,据说这与 Spring Security 4 处理角色的方式有关。

sec:authorize="hasAuthority('SITE_ADMIN')"

效果很好

【讨论】:

    猜你喜欢
    • 2019-04-11
    • 2020-08-15
    • 1970-01-01
    • 2021-01-17
    • 2017-05-14
    • 2017-11-28
    • 2017-06-09
    • 2018-02-15
    • 1970-01-01
    相关资源
    最近更新 更多