SpringBoot整合SpringSecurity系列(10)-注解访问控制

一、基于注解访问控制

  1. Spring Security 中提供了一些访问控制的注解,这些注解默认不可用,需要通过 @EnableGlobalMethodSecurity 进行开启后使用,如果设置的条件允许则程序正常执行,反之不允许会报 500(AccessDeniedException异常)
    • org.springframework.security.access.AccessDeniedException
@Configuration
@EnableGlobalMethodSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
	// ...
}
  1. @EnableGlobalMethodSecurity开启之后,外有三大注解可以写到 Service 接口或方法上,也可以写到 controller或 controller 的方法上,通常情况下都是写在控制器方法上,控制接口URL是否允许被访问
  2. 新建annotation.html,用于注解控制
<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <title>SpringBoot Security</title>
</head>
<body>
  <h3>欢迎登录SpringBoot Security 注解控制首页</h3>
</body>
</html>
  1. 在WebSecurityConfigurerAdapter配置类上 @EnableGlobalMethodSecurity 开启注解即可开启全局方法注解功能

二、三大注解控制方法

2.1 @Secured

  1. @Secured专门用于判断是否具有指定角色,可写在方法或类上,注意参数以 ROLE_ 开头,由于只能判断角色,所以实际中使用并不多
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Secured {

	/**
	 * Returns the list of security configuration attributes (e.g.&nbsp;ROLE_USER,
	 * ROLE_ADMIN).
	 * @return String[] The secure method attributes
	 */
	String[] value();
}
  1. @EnableGlobalMethodSecurity 注解功能类上通过指定参数 securedEnabled = true 开启 @Secured 功能
    • @EnableGlobalMethodSecurity(securedEnabled = true)
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    // ...
}
  1. 在控制器方法上添加@Secured注解,要求具备 ROLE_ADMIN 权限才能访问
/**
 * 注解权限访问
 */
@RequestMapping("/toAnnotation")
@Secured("ROLE_ADMIN")
public String annotation() {
	return "redirect:/annotation.html";
}
  1. 在SecurityConfig中配置对应的权限
    • successForwardUrl指定到对应的处理器
http.formLogin()
	// 指定登录页面,/不能舍弃
	.loginPage("/login.html")
	// 表单提交路径,和登录表单配置一样
	.loginProcessingUrl("/login")
	// 注解访问控制
	.successForwardUrl("/toAnnotation");
  1. 启动项目,然后测试有权限和无权限
    • 有权限

在这里插入图片描述

  • 无权限

在这里插入图片描述

  • 并且控制台抛出对应异常

image.png

2.2 @PreAuthorize

  1. @PreAuthorize是方法或类级别注解,表示访问方法或类在执行之前先判断权限,大多情况下都是使用这个注解,注解的参数和access()方法参数取值相同,都是权限表达式
  2. 使用时需要在 @EnableGlobalMethodSecurity 注解功能类上通过指定参数 prePostEnabled= true 开启**@PreAuthorize** 功能
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    // ...
}
  1. 在控制器方法上添加@PreAuthorize,参数可以是任何 access() 支持的表达式
@RequestMapping("/preAuthorize")
@PreAuthorize("hasRole('ADMIN')")
public String preAuthorize() {
    return "redirect:/annotation.html";
}
  1. 访问接口,分别使用admin和root访问,结果和@Secured一致
    • http://localhost:8888/preAuthorize

2.3 @PostAuthorize

  1. @PostAuthorize是方法或类级别注解,表示方法或类执行结束后判断权限,此注解很少被使用到
  2. 使用时需要在 @EnableGlobalMethodSecurity 注解功能类上通过指定参数 prePostEnabled= true 开启 @PostAuthorize功能
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    // ...
}
  1. 在控制器方法上添加@PreAuthorize,参数可以是任何access()支持的表达式
@RequestMapping("/postAuthorize")
@PostAuthorize("hasRole('ADMIN')")
public String postAuthorize() {
    return "redirect:/annotation.html";
}
  1. 访问接口,分别使用admin和root访问,结果和@Secured一致
    • http://localhost:8888/postAuthorize
  2. 由于是方法和类执行结束后才判断的权限,而实际中通常是在执行前做判断,不然权限拦截就没有实际意义
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值