[英]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.