简体   繁体   English

是否可以使用 spring 安全性在 html 页面上隐藏按钮等?

[英]Is it possible to hide buttons etc. on html pages, with spring security?

Currently i am using spring security where i have admin login and employee login.目前我正在使用 spring security,我有管理员登录和员工登录。 The thing is when i am logged in as a employee, i want to hide a button which redirects to admin homepage.问题是当我以员工身份登录时,我想隐藏一个重定向到管理员主页的按钮。 Can i somehow make it hidden or unclickable when logged in as a employee?以员工身份登录时,我可以以某种方式将其隐藏或无法点击吗? Furthermore there it is also in showCustomer.html where the admin can delete and edit a booking.此外,它也在 showCustomer.html 中,管理员可以在其中删除和编辑预订。 To sum up;总结; Can i hide html elements when i am logged in as a employee?当我以员工身份登录时,我可以隐藏 html 元素吗?

code:代码:

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();

在此处输入图片说明

If you are using JSP, you can wrap the html in a spring security tag like如果您使用的是 JSP,则可以将 html 包装在一个 spring 安全标记中,例如

<sec:authorize access="isAuthenticated()">
   <!-- Content for Authenticated users -->  
</sec:authorize>

or Thymleaf或百里香叶

<div sec:authorize="hasRole('ADMIN')">
    This content is only shown to administrators.
</div>

If you're using Angular, you can store a boolean value in a cookie that signifies that the user is logged in. Then you would created a directive that hides the html element based on with that the user is logged in or not.如果您使用的是 Angular,您可以在 cookie 中存储一个布尔值,表示用户已登录。然后您将创建一个指令,根据用户是否登录来隐藏 html 元素。

You can use in Html using JSTL您可以使用 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM