一、环境准备
【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的区别与用法