[英]Is it possible to hide buttons etc. on html pages, with spring security?
目前我正在使用 spring security,我有管理员登录和员工登录。 问题是当我以员工身份登录时,我想隐藏一个重定向到管理员主页的按钮。 以员工身份登录时,我可以以某种方式将其隐藏或无法点击吗? 此外,它也在 showCustomer.html 中,管理员可以在其中删除和编辑预订。 总结; 当我以员工身份登录时,我可以隐藏 html 元素吗?
代码:
protected void configure (HttpSecurity httpSecurity) throws Exception {
httpSecurity.csrf().disable();
httpSecurity.authorizeRequests()
.antMatchers("/admin", "/opretBooking", "/showCustomer", "/editCustomer", "/sletBooking", "/medarbejder").hasRole("ADMIN")
.antMatchers("/user", "/showCustomer", "/medarbejder").hasRole("USER")
.antMatchers("/**").permitAll()
.and().formLogin().loginPage("/login").and().logout().permitAll();
如果您使用的是 JSP,则可以将 html 包装在一个 spring 安全标记中,例如
<sec:authorize access="isAuthenticated()">
<!-- Content for Authenticated users -->
</sec:authorize>
或百里香叶
<div sec:authorize="hasRole('ADMIN')">
This content is only shown to administrators.
</div>
如果您使用的是 Angular,您可以在 cookie 中存储一个布尔值,表示用户已登录。然后您将创建一个指令,根据用户是否登录来隐藏 html 元素。
您可以使用 JSTL 在 Html 中使用
<sec:authorize access="isAnonymous()">
<c:redirect url="/login"/>
</sec:authorize>
<sec:authorize access="hasAnyAuthority('ADMIN')">
<button class="btn btn-info" type="submit">Submit</button>
</sec:authorize>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.