java - 是否可以使用 Spring Security 在 html 页面上隐藏按钮等?

标签 java spring spring-boot spring-mvc 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();

enter image description here

最佳答案

如果您使用 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/

相关文章:

java - 当我尝试运行 Web 服务客户端时,Eclipse 与 tomcat : java. lang.reflect.InitationTargetException

java - 部分有序比较器到全序比较器

java - Eclipse + GWT 类路径错误

java - Wiremock stub 错误 : "Unrecognized field\"timestamp\"(class com.github.tomakehurst.wiremock.common.Errors), not marked as ignorable"

java - Javers 审核自定义删除

java - 为什么我的java应用程序创建多个名称为 "JMX client heartbeat"的线程,这些线程消耗大量CPU?

Spring Data Solr - 由 OR 分隔的多个 FilterQueries

java - Spring boot的数据库配置需要在web中才能正常工作

java - 在多个项目/模块中使用多个属性文件(通过 PropertyPlaceholderConfigurer)

java - Spring Boot 中是否可以更改基于时间的日志级别