目录
1. Spring Security详细介绍
2. Spring Security详细使用
3. Spring Security实现JWT token验证
4. JWT(JSON Web Token,JSON令牌)
5. Spring Security安全注解
Spring Security安全注解(三种)
Spring Security默认是禁用注解的,要想开启注解,需要在继承WebSecurityConfigurerAdapter的类上加@EnableGlobalMethodSecurity注解,并在该类中将AuthenticationManager定义为Bean
如果要启用基于注解的方法安全性,要在配置类上使用**@EnableGlobalMethodSecurity**
①:
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration;@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true,jsr250Enabled = true,prePostEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
}
②:
@Configuration
@EnableWebSecurity // 启用Spring Security
@EnableGlobalMethodSecurity(securedEnabled = true, jsr250Enabled = true, prePostEnabled = true) // 拦截注解
public class SecurityConfig extends WebSecurityConfigurerAdapter {
}
设置了securedEnabled = true,此时Spring将会创建一个切点,并将带有@Secured注解的方法防入切面中。同理,jsr250Enabled = true 与 prePostEnabled = true,分别表示启用@RolesAllowed与表达式驱动的注解
Controller中使用
@GetMapping(value = "/admin")
@Secured("ROLE_ADMIN")
public String admin(){return "admin";
}
(1)Spring Security自带注解:@Secured
当@EnableGlobalMethodSecurity(securedEnabled=true)的时候,@Secured可以使用:
功能:支持单一角色或者多个角色之间的任何一个角色,不支持spring EL表达式
@GetMapping("/helloUser")
// 拥有normal或者admin角色的用户都可以方法helloUser()方法
@Secured({"ROLE_normal","ROLE_admin"})
public String helloUser() {return "hello,user";
}
注意:匹配的字符串需要添加前缀“ROLE_”,如果要求,只有同时拥有admin & noremal的用户才能方法helloUser()方法,这时候@Secured就无能为力了
(2)JSR-250注解
如果选择使用@RolesAllowed的话,需要将@EnableGlobalMethodSecurity的jsr250Enabled属性设置为true,以开启此功能
注解 | 描述 |
---|---|
@DenyAll | 拒绝所有访问 |
@PermitAll | 允许所有访问 |
@RolesAllowed | @RolesAllowed注解和@Secured注解在各个方面基本上都是一致的。唯一显著的区别在于@RolesAllowed是JSR-250定义的Java标准注解 |
@RequestMapping("/test")
@RolesAllowed("ROLE_ADMIN")
public String test1(){
return "test";
}
注解案例 | 功能说明 |
---|---|
@RolesAllowed({“USER”, “ADMIN”}) | 该方法只要具有"USER", "ADMIN"任意一种权限就可以访问。这里可以省略前缀ROLE_,实际的权限可能是ROLE_ADMIN |
(3)表达式驱动注解
基于表达式的注解,并可以自定义扩展,只需继承GlobalMethodSecurityConfiguration类就可以实现。当然,在该扩展类上添加注解:@EnableGlobalMethodSecurity(prePostEnabled = true)来启动这种注解支持。如果没有访问方法的权限,会抛出AccessDeniedException
当**@EnableGlobalMethodSecurity(prePostEnabled=true)**的时候,@PreAuthorize、@PostAuthorize、@PostFilter、@PreFilter可以使用
注解 | 描述 |
---|---|
@PreAuthorize | 在方法调用之前,基于表达式的计算结果来限制对方法的访问 |
@PostAuthorize | 允许方法调用,但是如果表达式计算结果为false,将抛出一个安全性异常 |
@PostFilter | 允许方法调用,但必须按照表达式来过滤方法的结果 |
@PreFilter | 允许方法调用,但必须在进入方法之前过滤输入值 |
Spring的@PreAuthorize/@PostAuthorize注解更适合方法级的安全,也支持Spring表达式语言,提供了基于表达式的访问控制
@PreAuthorize(最常用):方法进入之前
注解适合进入方法前的权限验证,@PreAuthorize可以将登录用户的roles/permissions参数传到方法中
注解案例 | 功能说明 |
---|---|
@PreAuthorize(“hasRole(‘ADMIN’)”) | 拥有ADMIN角色权限才能访问 |
@PreAuthorize(“hasRole(‘ADMIN’) AND hasRole(‘DBA’)”) | 拥有ADMIN角色和DBA角色权限才能访问 |
@PreAuthorize(“hasAnyRole(‘ADMIN’,‘DBA’)”) | 拥有ADMIN或者DBA角色均可访问 |
@GetMapping("/helloUser")
// 拥有normal或者admin角色的用户都可以方法helloUser()方法
@PreAuthorize("hasAnyRole('normal','admin')")
public String helloUser() {return "hello,user";
}
此时如果要求用户必须同时拥有normal和admin的话,那么可以这么编码:
@GetMapping("/helloUser")
@PreAuthorize("hasRole('normal') AND hasRole('admin')")
public String helloUser() {return "hello,user";
}
此时如果使用user/123登录的话,就无法访问helloUser()的方法了
@PostAuthorize
@PostAuthorize注解使用并不多,在方法执行后再进行权限验证,适合验证带有返回值的权限,Spring EL提供返回对象能够在表达式语言中获取返回的对象returnObject
@GetMapping("/helloUser")
@PreAuthorize("returnObject!=null && returnObject.username == authentication.name")
public User helloUser() {Object pricipal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();User user;if("anonymousUser".equals(pricipal)) {user = null;} else {user = (User) pricipal;}return user;
}
@PostFilter
在方法执行之后执行,而且这里可以调用方法的返回值,然后对返回值进行过滤或处理或修改并返回。EL变量return Object表示返回的对象。只有方法返回的是集合或数组类型的才可以使用。(与分页技术不兼容)
用于指定方法筛选表达式的注释,该表达式将在调用方法后进行计算
@PostFilter通过将规则应用于列表中的每个元素,定义了用于过滤方法返回列表的规则。如果评估值为true,则该项目将保留在列表中。否则,该项目将被删除
@PreFilter
在方法执行之前执行,而且这里可以调用方法的参数,然后对参数值进行过滤或处理或修改,EL变量filterObject表示参数,如有多个参数,使用filterTarget注解参数。只有方法参数是集合或数组才行。(很少会用到,与分页技术不兼容)
@PreFilter的工作方式非常相似,但是,过滤将应用于作为输入参数传递给带注释方法的列表