目前我正在使用 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 Security 标记中,例如
<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 中存储一个 boolean 值,表示用户已登录。然后您将创建一个指令,根据用户是否登录来隐藏 html 元素。
关于java - 是否可以使用 Spring Security 在 html 页面上隐藏按钮等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59417691/