【Spring Security Oauth2】构建资源服务器(二):授权管理(Web授权,注解授权:securedEnabled, prePostEnabled, jsr250Enabled)

一、环境准备

【Spring Security Oauth2】构建资源服务器(一):构建服务

二、授权管理相关配置

1、web授权,web授权指的就是通过ResourceServerConfig资源配置类的configure(HttpSecurity http)方法进行授权配置。

方法介绍:

方法作用
permitAll()所有用户可访问
denyAll()所有用户不可访问
authenticated()登录用户可访问
anonymous()匿名用户可访问
rememberMe()通过 remember me 登录的用户可访问
fullyAuthenticated()非 remember me 登录的用户可访问
hasIpAddress(String ipaddressExpression)指定 IP 表达式的用户可访问
hasRole(String role)拥有指定角色的用户可访问
hasAnyRole(String… roles)拥有指定任一角色的用户可访问
hasAuthority(String authority)拥有指定权限(authority)的用户可访问
hasAnyAuthority(String… authorities)拥有指定任一权限(authority)的用户可访问
access(String attribute)当 Spring EL 表达式的执行结果为 true 时,可以访问

使用:

/**
     * 安全控制
     *
     * @param http
     * @throws Exception
     */
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/sel").permitAll()
                .antMatchers("/sel").hasAnyAuthority("")
                .antMatchers("/sel").hasAuthority("")
                .antMatchers("/sel").hasAnyRole("")
                .antMatchers("/sel").hasRole("")
                .antMatchers("/del").hasIpAddress("")
                .antMatchers("/sel").denyAll()
                .antMatchers("/sel").access("hasAnyAuthority('') or hasRole('')")
                .antMatchers("/sel").anonymous()
                .antMatchers("/**").access("#oauth2.hasScope('sys_manage')")
                .and().csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }

2、注解授权,通过在请求方法上添加对应注解进行授权配合。

在使用这些注解时,需要先开启该配置。每个配置管理不同的注解。
在这里插入图片描述

@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, jsr250Enabled = true)

注解介绍:

注解说明需开启配置
@PreAuthorize方法调用前进行权限检查,当 Spring EL 表达式的执行结果为 true 时,可以访问prePostEnabled = true
@PostAuthorize调用后进行权限检查,当 Spring EL 表达式的执行结果为 true 时,可以访问prePostEnabled = true
@PreFilter可以用来对集合类型的参数进行过滤prePostEnabled = true
@PostFilter可以用来对返回值进行过滤prePostEnabled = true
@RolesAllowed配置允许的角色或权限,角色可以添加ROLE_前缀,也可以不添加ROLE_前缀jsr250-annotations = true
@PermitAll所有用户可访问jsr250-annotations = true
@DenyAll所有用户不可访问jsr250-annotations = true
@Secure配置允许的角色或权限,角色必须有ROLE_前缀securedEnabled = true

使用:

package com.cyun.sys.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PreFilter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import java.util.List;

/**
 * 测试 controller
 *
 * @author He PanFu
 * @date 2022-03-12 17:53:02
 */
@RestController
@RequiredArgsConstructor
public class TestController {

    @GetMapping("/add")
    @RolesAllowed({"ADMIN"})
    public String add() {
        return "新增接口";
    }

    @GetMapping("/up")
    @Secured({"ADMIN", "ROLE_ADMIN"})
    public String update() {
        return "修改接口";
    }

    @GetMapping("/del")
    @PreAuthorize("hasAuthority('DELETE') and hasRole('DBA') or #oauth2.hasScope('sys')")
    public String delete() {
        return "删除接口";
    }

    @PreFilter(filterTarget = "ids", value = "filterObject%2==0")
    public void delete(List<Integer> ids, List<String> usernames) {

    }

    @GetMapping("/sel")
    @PermitAll
    public String select() {
        return "查询接口";
    }
}

推荐文章:
@EnableGlobalMethodSecurity三方法详解
@PreAuthorize、@PostAuthorize、@PreFilter、@PostFilter的区别与用法

三、问题总结

1、通过@PermitAll注解配置的请求,需要在请求头中传入参数Authorization。而通过“http.antMatchers("/sel").permitAll()”Web授权,则不要传入参数Authorization也能访问。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值