SpringBoot整合SpringSecurity系列(1

一、基于注解访问控制

  1. Spring Security 中提供了一些访问控制的注解,这些注解默认不可用,需要通过 @EnableGlobalMethodSecurity 进行开启后使用,如果设置的条件允许则程序正常执行,反之不允许会报 500(AccessDeniedException异常)
    • org.springframework.security.access.AccessDeniedException
1
2
3
4
5
java复制代码@Configuration
@EnableGlobalMethodSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
// ...
}
  1. @EnableGlobalMethodSecurity开启之后,外有三大注解可以写到 Service 接口或方法上,也可以写到 controller或 controller 的方法上,通常情况下都是写在控制器方法上,控制接口URL是否允许被访问
  2. 新建annotation.html,用于注解控制
1
2
3
4
5
6
7
8
9
10
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_ 开头,由于只能判断角色,所以实际中使用并不多
1
2
3
4
5
6
7
8
9
10
11
12
13
java复制代码@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)
1
2
3
4
5
java复制代码@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
// ...
}
  1. 在控制器方法上添加@Secured注解,要求具备 ROLE_ADMIN 权限才能访问
1
2
3
4
5
6
7
8
java复制代码/**
* 注解权限访问
*/
@RequestMapping("/toAnnotation")
@Secured("ROLE_ADMIN")
public String annotation() {
return "redirect:/annotation.html";
}
  1. 在SecurityConfig中配置对应的权限
    • successForwardUrl指定到对应的处理器
1
2
3
4
5
6
7
java复制代码http.formLogin()
// 指定登录页面,/不能舍弃
.loginPage("/login.html")
// 表单提交路径,和登录表单配置一样
.loginProcessingUrl("/login")
// 注解访问控制
.successForwardUrl("/toAnnotation");
  1. 启动项目,然后测试有权限和无权限
    • 有权限

在这里插入图片描述

  • 无权限

在这里插入图片描述

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

image.png

2.2 @PreAuthorize

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

2.3 @PostAuthorize

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

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%