如何根据用户显示不同的视图/HTML 页面?

jav*_*999 -1 oracle spring administrator spring-mvc thymeleaf

我正在Web application使用Spring MVC构建一个简单的。

我想显示不同views的:

  • 管理员

  • 标准用户

我怎样才能做到这一点?我要求作为标准用户Administrators能够执行不同的功能并看到额外的等。buttons

管理员将被授予额外的特权/权限,可能使用数据库flag中的来标识oracle

作为参考,我还在应用程序中使用Spring DataThyme leaf 。

Sha*_*aan 5

Thymeleaf 已与 Spring Security 集成,为我们提供了满足您要求的要求的一致基础。

单击此处获取教程和指南。如果您有兴趣阅读,本指南还提供了 Spring Security 文档的链接。

1) Spring security - 在使用 userId 验证用户表后将决定合格的用户角色。例如,如果用户符合“ROLE_ADMIN”、“ROLE_USER”角色,则在登录后可以看到以下 2 个按钮。

2) 设置授权角色后的 Thymeleaf 代码示例。

<div sec:authorize="hasRole('ROLE_ADMIN')">
  //show the save button  
</div>
<div sec:authorize="hasRole('ROLE_USER')">
  //show the view button
</div>
Run Code Online (Sandbox Code Playgroud)