前言
spring-security权限管理
一、服务器端方法级权限控制
1.导入坐标
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>5.0.1.RELEASE</version>
</dependency>
利用注解对方法进行权限控制用到了AOP,所以需要引入aop相关的坐标
2.sping-security.xml配置
<security:global-method-security jsr250-annotations="enabled"/>
<security:global-method-security secured-annotations="enabled"/>
<security:global-method-security pre-post-annotations="enabled"/>
<!--识别表达式-->
<bean id="webexpressionHandler" class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler" />
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
3.注解使用
1.JSR-250注解
@RolesAllowed({“USER”, “ADMIN”})
2.@Secured注解
@Secured(“ROLE_TELLER”)
3.支持表达式的注解
@PreAuthorize(“hasAuthority(‘ADMIN’)”)
二、页面端标签控制权限
1.导入坐标
代码如下:
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>version</version>
</dependency>
2.页面使用
代码如下:
<%@taglib uri="http://www.springframework.org/security/tags" prefix="security"%>
<security:authentication property="principal.username" />
<security:authorize access="hasRole('ADMIN')">
{代码}
</security:authorize>
注意点
(1) 使用@Secured注解时需要将角色名补充完整
(2) 使用SPEL表达式需要配置DefaultWebSecurityExpressionHandler的bean对象