# springboot-springsecurity-jwt **Repository Path**: zhouhengCxy/springboot-springsecurity-jwt ## Basic Information - **Project Name**: springboot-springsecurity-jwt - **Description**: springboot-springsecurity-jwt - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2018-08-31 - **Last Updated**: 2021-12-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ##springboot-springSecurity-jwt ###本例实现了登录认证和权限认证的功能,可以灵活的实现用户-角色-菜单的权限认证 ####代码注解写得非常详细,直接启动项目,请求接口断点研究就行; #### 参考我的另一个项目,https://gitee.com/zhouhengCxy/spring-boot-activiti.git ####spring-boot-activiti基于该版本基础上有大量优化【建议使用该项目,删除activiti7部分少量内容即可】 ####1.token中只存入用户名,解析时用用户名获取角色及权限信息,避免修改角色或权限信息后没有即时生效 ####2.增加UserDetails的实现类SecurityUserInfo,更好的扩展和方便的获取权限 ####3.登录参数添加参数校验 ####4.filter注入HandlerExceptionResolver实现认证和权限的全局异常捕获 ####5.security集成swagger2 ####6.集成了activiti7 ####不好的一点就是删除了大量注释,对于新手学习来说,建议以本项目为主,实际开发中以spring-boot-activiti为准 ###快速启动 ####1.先要创建一个jwt_test库并生成/resources/sql/*.sql下的表 ####2.修改yml中自己的数据库配置信息 ####3.我使用的是mysql8,如果是其他版本连接不上修改pom mysql的驱动版本 ###业务说明 ####登录请求响应时,除出了请求头设置了token,其余没有任何东西; ####正常业务登录要获取菜单和字典等信息,登录了带上token再请求一次获取菜单等权限; ###使用postMan测试 ###1.获取验证码-http://localhost:8085/user/captcha ####执行顺序:1.JWTAuthenticationFilter.doFilterInternal ####虽然设置了排除了获取验证码路径,但是还是会进入1方法,检验无token后return,最后能够获取到验证码 ###2.登录请求-http://localhost:8085/login ####参数说明:输入用户名和密码以及验证码 ####正常执行顺序: ####1.JWTLoginFilter.attemptAuthentication ####2.CustomAuthenticationProvider.supports ####3.CustomAuthenticationProvider.authenticate ####4.JWTLoginFilter.successfulAuthentication ####验证码错误,用户和密码错误的异常会正常抛出 ####JWTLoginFilter.attemptAuthentication登录拦截后简单的校验验证码成功后,AuthenticationManager执行指定UsernamePasswordAuthenticationToken的鉴权方式, ####然后进入到AuthenticationProvider的自定义身份验证类CustomAuthenticationProvider,先执行supports方法判断是否为指定的相同方式UsernamePasswordAuthenticationToken, ####一样时再执行CustomAuthenticationProvider的authenticate方法,进行具体的用户名和密码校验,以及查询数据库返回所有权限Authentication(身份证明) ####最后又回到JWTLoginFilter类执行successfulAuthentication方法,把Authentication(身份证明)生成token,设置到请求头中返回 ###3.admin访问接口 ####参数说明:headers key:Authorization value:Bearer token ####正常执行顺序: ####1.JWTAuthenticationFilter.doFilterInternal:验token是否合法并转为Authentication ####2.请求action接口 ####3.接口上的功能注释与Authentication的具体功能权限(具体功能权限通过用户获取角色权限再获取具体功能权限,登录时就保存到了这里,再转为token返回给了前端)对比,有则通过,无则返回403 ####异常情况:不带token返回400;乱传token返回500; ####admin访问SysUserController.queryAll和SysRoleController.queryAll都可以 ####4.user访问接口 ####参数说明:headers key:Authorization value:Bearer token ####正常执行顺序:1.JWTAuthenticationFilter.doFilterInternal ####异常情况:(token异常忽略) ####user访问SysUserController.queryAll可以,访问SysRoleController.queryAll失败 ####5.zhangsan访问接口 ####参数说明:headers key:Authorization value:Bearer token ####正常执行顺序:1.JWTAuthenticationFilter.doFilterInternal ####异常情况:(token异常忽略) ####zhangsan访问SysUserController.queryAll和SysRoleController.queryAll 都失败 ####6.注册接口 ####@Secured可以作用于class上,这样所有的方法都会拥有该注解的功能 ####所以注册和验证码所在的controller,只能在每个方法上添加@Secured ####1.WebSecurityConfig排除了注册路径,但是方法上添加@Secured ####会报错403 ####2.WebSecurityConfig排除了注册路径,方法上去除@Secured ####能正常访问 ####3.WebSecurityConfig不排除注册路径,方法上添加@Secured ####这个肯定会报错,不用测试也知道,因为没有token ####4.WebSecurityConfig不排除注册路径,方法上去除@Secured ####会报错403 ####总结:不需要传token的就在WebSecurityConfig中排除路径,且方法也不要添加权限注解@Secured(例如:注册和登录) #### 还有一种情况,所有用户都能访问的,需要传token,就不要WebSecurityConfig排除路径,方法上也不要添加权限注解@Secured ####我们实现了UserDetailsService接口去做登录校验,感觉也是没有必要的,完全可以忽略