Spring Security权限控制

Spring Security权限控制

服务器端方法级别权限控制

在服务器端,我们可以通过Spring security提供的注解对方法来进行权限控制. Spring security在方法的权限控制上支持三种类型的注解, JSR-250注解 / @Secured注解 / 支持表达式的注解, 者三种注解默认都是没有启用的, 需要单独通过global-method-security元素的对应属性进行启用

JSR-250使用

pom.xml导入依赖坐标

<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>jsr250-api</artifactId>
    <version>1.0</version>
</dependency>

Spring Security配置文件配置开启注解

<security:global-method-security jsr250-annotations="enabled"/>

@RoleAllowed注解

在指定方法上使用, 表示访问对应方法时所应该具有的角色, 其参数为一个String的数组

@RolesAllowed("ADMIN")
@RequestMapping("/findAll.do")
public void findAll(){
 ...   
}

以上代码表示只有ADMIN角色能够访问该方法


@PermitAll注解

表示允许所有的角色进行访问, 也就是不进行权限控制


@DenyAll注解

@PermitAll相反, 表示无论什么角色都不能访问



@Secured使用

Spring securtiy配置文件开启 secured注解

<security:global-method-security secured-annotations="enabled"/>

@Secured注解标注的方法进行权限控制的支持, 其默认为disabled


在指定方法上使用, 表示访问对应方法时所应该具有的角色, 其参数为一个String的数组

在使用@Secured注解时, 需要添加角色的前缀ROLE_才能正常使用

@Secured("ROLE_ADMIN")
@RequestMapping("/findAll.do")
public ModelAndView findAll()  throws Exception {
    ...
}

以上代码表示只有ROLE_ADMIN角色能够访问该方法



支持表达式的注解

spring securtiy配置文件中开启注解表达式支持

<security:global-method-security pre-post-annotations="enabled" jsr250-annotations="enabled" secured-annotations="enabled"/>

Spring EL表达式

spring security运行我们在定义URL访问或方法访问所应用的权限时使用Spring EL表达式, 在定义所需的访问权限时, 如果对应的表达式返回结果为true, 则表示拥有对应的权限, 反之则无. Spring Security可用表达式对象的基类时SecurityExpressionRoot, 其为我们提供了如下在使用Spring EL 表达式对URL或方法进行权限控制时的内置表达式

表达式描述
hasRole([role])当前用户是否拥有角色
hasAnyRole([role1, role2])多个角色是一个以逗号进行分隔的字符串。如果当前用户拥有指定角色中的任意一个则返回true
hasAuthority([auth])等同于hasRole()
hasAnyAuthority([auth1, auth2])等同于hasAntRole()
Principle代表当前用户的principle对象
authentication直接从SecurityContext获取的当前Authentication对象
permitAll总是返回true, 表示允许所有
denyAll总是返回false, 表示拒绝所有
isAnonymous()当前用户是否是一个匿名用户
isRememberMe()表示当前用户是否通过Remember-Me自动登录的
isAuthenticated表示当前用户是否已经登录认证成功了
isFullyAuthenticated()如果当前用户既不是一个匿名用户, 同时又不是通过Remember-Me自动登录的, 则返回true

@PerAuthorize注解

在方法调用之前, 基于表达式的计算结果来对限制对方法的访问


@PostAuthorize注解

允许方法调用, 但是如果表达式计算结果为false, 将抛出一个安全异常


@PostFilter注解

允许方法调用, 但必须按照表达式来过滤方法的结果


@PreFilter注解

运行方法调用, 但必须在进入方法之前过滤输入值



页面端权限控制

  • 第一步: 导入依赖坐标

    <!-- 页面端权限控制 -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-taglibs</artifactId>
        <version>${spring.security.version}</version>
    </dependency>
    
  • 第二部: 在jsp页面导入指令

    <%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %>
    
  • 开启表达式解析

    spring-security.xml中配置 use-expressionstrue

    <security:http auto-config="true" use-expressions="true">
    

    配置拦截权限时也需要使用表达式

    <!-- 配置具体的拦截的规则, 使用表达式配置access -->
    <security:intercept-url pattern="/**" access="hasAnyRole('ROLE_USER','ROLE_ADMIN')"/>
    

常用标签

jsp中我们可以使用以下三种标签, 其中authentication代表的是当前认证对象, 可以获取当前认证对象的信息, 如; 用户名. 其他两个标签我们可以用于权限控制


authentication标签

可以获取当前正在操作的用户信息

<!-- 显示当前用户用户名 -->
<security:authentication property="principal.username"/>

authorize标签

用户控制页面上某些标签是否可以显示

<!-- 只有拥有ROLE_ADMIN角色 才能看见h1标签 -->
<security:authorize access="hasRole('ROLE_ADMIN')">
	<h1>Hello Wrold!</h1>
</security:authorize>
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值