【问题标题】:Is it possible to hide buttons etc. on html pages, with spring security?是否可以使用弹簧安全性在 html 页面上隐藏按钮等?
【发布时间】:2019-12-20 18:13:16
【问题描述】:

目前我正在使用 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();

【问题讨论】:

  • Spring Security 是否提供该 html 页面?

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


【解决方案1】:

如果您使用的是 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 元素.

【讨论】:

    【解决方案2】:

    您可以使用 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>
    

    【讨论】:

      猜你喜欢
      • 2012-01-18
      • 2013-09-09
      • 2021-05-26
      • 1970-01-01
      • 2019-03-03
      • 2012-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多