三種SpringSecurity方法級別權限控制


JSR-250注解

1、在pom.xml添加
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
</dependency>

2、在spring-mvc.xml 開啟注解
<security:global-method-security jsr250-annotations="enabled"></security:global-method-security>

3、在權限控制的方法上面使用注解
@RolesAllowed("ADMIN")

這種也可以寫@RolesAllowed("ROLE_ADMIN"),上面是省略ROLE_前綴
@Secured注解

1、在spring-mvc.xml 開啟注解
<security:global-method-security secured-annotations="enabled"></security:global-method-security>

2、在權限控制的方法上面使用注解
@Secured("ROLE_ADMIN")
這里不能省略ROLE_前綴
 基於表達式操作

1、在spring-mvc.xml 開啟注解
<security:global-method-security pre-post-annotations="enabled"></security:global-method-security>

2、在權限控制的方法上面使用注解
@PreAuthorize("authentication.principal.username == 'peny'")
@PreAuthorize("hasRole('ROLE_ADMIN')")
這里可以省略ROLE_前綴;  @PreAuthorize("hasRole('ADMIN')")


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM