SpringSecurity是Spring家族中的一个安全管理框架。相比与另外一个安全框架Shiro,它提供了更丰富的功能,社区资源也比Shiro丰富。
一般来说中大型的项目都是使用SpringSecurity来做安全框架。小项目有Shiro的比较多,因为相比与SpringSecurity,Shiro的上手更加的简单。
认证:验证当前访问系统的是不是本系统的用户,并且要确认具体是哪个用户
我们先要搭建一个简单的SpringBoot工程
①设置父工程添加依赖
xml
java
@SpringBootApplicationpublicclassSecurityApplication{publicstaticvoidmain(String[]args){SpringApplication.run(SecurityApplication.class,args);}}③创建Controller
importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;@RestControllerpublicclassHelloController{@RequestMapping("/hello")publicStringhello(){return"hello";}}1.2引入SpringSecurity在SpringBoot项目中使用SpringSecurity我们只需要引入依赖即可实现入门案例。
xml:
必须登陆之后才能对接口进行访问。
想要知道如何实现自己的登陆流程就必须要先知道入门案例中SpringSecurity的流程。
SpringSecurity的原理其实就是一个过滤器链,内部包含了提供各种功能的过滤器。这里我们可以看看入门案例中的过滤器。
图中只展示了核心过滤器,其它的非核心过滤器并没有在图中展示。
UsernamePasswordAuthenticationFilter:负责处理我们在登陆页面填写了用户名密码后的登陆请求。入门案例的认证工作主要有它负责。
ExceptionTranslationFilter:处理过滤器链中抛出的任何AccessDeniedException和AuthenticationException。
FilterSecurityInterceptor:负责权限校验的过滤器。
我们可以通过Debug查看当前系统中SpringSecurity过滤器链中有哪些过滤器及它们的顺序。
概念速查:
AuthenticationManager接口:定义了认证Authentication的方法
UserDetailsService接口:加载用户特定数据的核心接口。里面定义了一个根据用户名查询用户信息的方法。
UserDetails接口:提供核心用户信息。通过UserDetailsService根据用户名获取处理的用户信息要封装成UserDetails对象返回。然后将这些信息封装到Authentication对象中。
调用ProviderManager的方法进行认证如果认证通过生成jwt
把用户信息存入redis中
②自定义UserDetailsService
在这个实现类中去查询数据库
校验:
①定义Jwt认证过滤器
获取token
解析token获取其中的userid
从redis中获取用户信息
存入SecurityContextHolder
①添加依赖
我们先创建一个用户表,建表语句如下:
mysql
yml
spring:datasource:url:jdbc:mysql://localhost:3306/sg_securitycharacterEncoding=utf-8&serverTimezone=UTCusername:rootpassword:rootdriver-class-name:com.mysql.cj.jdbc.Driver定义Mapper接口
publicinterfaceUserMapperextendsBaseMapper
类名上加@TableName(value="sys_user"),id字段上加@TableId配置Mapper扫描
@SpringBootApplication@MapperScan("com.sangeng.mapper")publicclassSimpleSecurityApplication{publicstaticvoidmain(String[]args){ConfigurableApplicationContextrun=SpringApplication.run(SimpleSecurityApplication.class);System.out.println(run);}}添加junit依赖
这样登陆的时候就可以用sg作为用户名,1234作为密码来登陆了。
实际项目中我们不会把密码明文存储在数据库中。
默认使用的PasswordEncoder要求数据库中的密码格式为:{id}password。它会根据id去判断密码的加密方式。但是我们一般不会采用这种方式。所以就需要替换PasswordEncoder。
我们一般使用SpringSecurity为我们提供的BCryptPasswordEncoder。
我们只需要使用把BCryptPasswordEncoder对象注入Spring容器中,SpringSecurity就会使用该PasswordEncoder来进行密码校验。
我们可以定义一个SpringSecurity的配置类,SpringSecurity要求这个配置类要继承WebSecurityConfigurerAdapter。
在接口中我们通过AuthenticationManager的authenticate方法来进行用户认证,所以需要在SecurityConfig中配置把AuthenticationManager注入容器。
认证成功的话要生成一个jwt,放入响应中返回。并且为了让用户下回请求时能通过jwt识别出具体的是哪个用户,我们需要把用户信息存入redis,可以把用户id作为key。
@RestControllerpublicclassLoginController{@AutowiredprivateLoginServcieloginServcie;@PostMapping("/user/login")publicResponseResultlogin(@RequestBodyUseruser){returnloginServcie.login(user);}}java
@ServicepublicclassLoginServiceImplimplementsLoginServcie{@AutowiredprivateAuthenticationManagerauthenticationManager;@AutowiredprivateRedisCacheredisCache;@OverridepublicResponseResultlogin(Useruser){UsernamePasswordAuthenticationTokenauthenticationToken=newUsernamePasswordAuthenticationToken(user.getUserName(),user.getPassword());Authenticationauthenticate=authenticationManager.authenticate(authenticationToken);if(Objects.isNull(authenticate)){thrownewRuntimeException("用户名或密码错误");}//使用userid生成tokenLoginUserloginUser=(LoginUser)authenticate.getPrincipal();StringuserId=loginUser.getUser().getId().toString();Stringjwt=JwtUtil.createJWT(userId);//authenticate存入redisredisCache.setCacheObject("login:"+userId,loginUser);//把token响应给前端HashMap
使用userid去redis中获取对应的LoginUser对象。
然后封装Authentication对象存入SecurityContextHolder
总结起来就是不同的用户可以使用不同的功能。这就是权限系统要去实现的效果。
所以我们还需要在后台进行用户权限的判断,判断当前用户是否有相应的权限,必须具有所需权限才能进行相应的操作。
在SpringSecurity中,会使用默认的FilterSecurityInterceptor来进行权限校验。在FilterSecurityInterceptor中会从SecurityContextHolder获取其中的Authentication,然后获取其中的权限信息。当前用户是否拥有访问当前资源所需的权限。
然后设置我们的资源所需要的权限即可。
SpringSecurity为我们提供了基于注解的权限控制方案,这也是我们项目中主要采用的方式。我们可以使用注解去指定访问对应的资源所需的权限。
@EnableGlobalMethodSecurity(prePostEnabled=true)然后就可以使用对应的注解。@PreAuthorize
@RestControllerpublicclassHelloController{@RequestMapping("/hello")@PreAuthorize("hasAuthority('test')")publicStringhello(){return"hello";}}3.2.2封装权限信息我们前面在写UserDetailsServiceImpl的时候说过,在查询出用户后还要获取对应的权限信息,封装到UserDetails中返回。
我们先直接把权限信息写死封装到UserDetails中进行测试。
我们之前定义了UserDetails的实现类LoginUser,想要让其能封装权限信息就要对其进行修改。
sql
SELECT DISTINCTm.`perms`FROM sys_user_roleur LEFTJOIN`sys_role`rONur.`role_id`=r.`id` LEFTJOIN`sys_role_menu`rmONur.`role_id`=rm.`role_id` LEFTJOIN`sys_menu`mONm.`id`=rm.`menu_id`WHERE user_id=2 ANDr.`status`=0 ANDm.`status`=0java
所以我们可以先定义个mapper,其中提供一个方法可以根据userid查询权限信息。
yaml
spring:datasource:url:jdbc:mysql://localhost:3306/sg_securitycharacterEncoding=utf-8&serverTimezone=UTCusername:rootpassword:rootdriver-class-name:com.mysql.cj.jdbc.Driverredis:host:localhostport:6379mybatis-plus:mapper-locations:classpath*:/mapper/**/*.xml然后我们可以在UserDetailsServiceImpl中去调用该mapper的方法查询权限信息封装到LoginUser对象中即可。
如果是认证过程中出现的异常会被封装成AuthenticationException然后调用AuthenticationEntryPoint对象的方法去进行异常处理。
所以如果我们需要自定义异常处理,我们只需要自定义AuthenticationEntryPoint和AccessDeniedHandler然后配置给SpringSecurity即可。
①自定义实现类
@ComponentpublicclassAccessDeniedHandlerImplimplementsAccessDeniedHandler{@Overridepublicvoidhandle(HttpServletRequestrequest,HttpServletResponseresponse,AccessDeniedExceptionaccessDeniedException)throwsIOException,ServletException{ResponseResultresult=newResponseResult(HttpStatus.FORBIDDEN.value(),"权限不足");Stringjson=JSON.toJSONString(result);WebUtils.renderString(response,json);}}java
先注入对应的处理器
@AutowiredprivateAuthenticationEntryPointauthenticationEntryPoint;@AutowiredprivateAccessDeniedHandleraccessDeniedHandler;然后我们可以使用HttpSecurity对象的方法去配置。
前后端分离项目,前端项目和后端项目一般都不是同源的,所以肯定会存在跨域请求的问题。
所以我们就要处理一下,让前端能进行跨域请求。
①先对SpringBoot配置,运行跨域请求
由于我们的资源都会收到SpringSecurity的保护,所以想要跨域访问还要让SpringSecurity运行跨域访问。
这里我们先不急着去介绍这些方法,我们先去理解hasAuthority的原理,然后再去学习其他方法你就更容易理解,而不是死记硬背区别。并且我们也可以选择定义校验方法,实现我们自己的校验逻辑。
hasAuthority方法实际是执行到了SecurityExpressionRoot的hasAuthority,大家只要断点调试既可知道它内部的校验原理。
它内部其实是调用authentication的getAuthorities方法获取用户的权限列表。然后判断我们存入的方法参数数据在权限列表中。
hasAnyAuthority方法可以传入多个权限,只有用户有其中任意一个权限都可以访问对应资源。
@PreAuthorize("hasAnyAuthority('admin','test','system:dept:list')")publicStringhello(){return"hello";}hasRole要求有对应的角色才可以访问,但是它内部会把我们传入的参数拼接上ROLE_后再去比较。所以这种情况下要用用户对应的权限也要有ROLE_这个前缀才可以。
@PreAuthorize("hasRole('system:dept:list')")publicStringhello(){return"hello";}hasAnyRole有任意的角色就可以访问。它内部也会把我们传入的参数拼接上ROLE_后再去比较。所以这种情况下要用用户对应的权限也要有ROLE_这个前缀才可以。
@PreAuthorize("hasAnyRole('admin','system:dept:list')")publicStringhello(){return"hello";}自定义权限校验方法我们也可以定义自己的权限校验方法,在@PreAuthorize注解中使用我们的方法。
@Component("ex")publicclassSGExpressionRoot{publicbooleanhasAuthority(Stringauthority){//获取当前用户的权限Authenticationauthentication=SecurityContextHolder.getContext().getAuthentication();LoginUserloginUser=(LoginUser)authentication.getPrincipal();List
@RequestMapping("/hello")@PreAuthorize("@ex.hasAuthority('system:dept:list')")publicStringhello(){return"hello";}基于配置的权限控制我们也可以在配置类中使用使用配置的方式对资源进行权限控制。
SpringSecurity去防止CSRF攻击的方式就是通过csrf_token。后端会生成一个csrf_token,前端发起请求的时候需要携带这个csrf_token,后端会有过滤器进行校验,如果没有携带或者是伪造的就不允许访问。
我们可以发现CSRF攻击依靠的是cookie中所携带的认证信息。但是在前后端分离的项目中我们的认证信息其实是token,而token并不是存储中cookie中,并且需要前端代码去把token设置到请求头中才可以,所以CSRF攻击也就不用担心了。
我们也可以自己去自定义成功处理器进行成功后的相应处理。
@ComponentpublicclassSGSuccessHandlerimplementsAuthenticationSuccessHandler{@OverridepublicvoidonAuthenticationSuccess(HttpServletRequestrequest,HttpServletResponseresponse,Authenticationauthentication)throwsIOException,ServletException{System.out.println("认证成功了");}}java
我们也可以自己去自定义失败处理器进行失败后的相应处理。
@ComponentpublicclassSGFailureHandlerimplementsAuthenticationFailureHandler{@OverridepublicvoidonAuthenticationFailure(HttpServletRequestrequest,HttpServletResponseresponse,AuthenticationExceptionexception)throwsIOException,ServletException{System.out.println("认证失败了");}}java
@ComponentpublicclassSGLogoutSuccessHandlerimplementsLogoutSuccessHandler{@OverridepublicvoidonLogoutSuccess(HttpServletRequestrequest,HttpServletResponseresponse,Authenticationauthentication)throwsIOException,ServletException{System.out.println("注销成功");}}java
宗旨就是需要三个数据:请求所需的权限,能获取到该请求的Object,以及已认证对象所拥有的权限。(其实就是投票器执行方法decide的三个参数)