当前位置: 首页 > 后端技术 > Java

SpringBoot快速实现认证系统,基于LoopAuth

时间:2023-04-01 20:25:16 Java

LoopAuth是一个低侵入、精简、轻量级、细粒度的JavaWeb权限管理框架,目前包括以下功能:注解认证码认证登录功能有/无状态登录Redis登录业务存储分离后续扩展计划(已安排)按开发顺序):ABAC权限扩展微服务支持账户风险监控官方地址:GiteeGitHub官方文档尝试添加依赖com.sobercodingLoopAuth-spring-boot-starter1.0.2配置文件快速体验无需配置yml文件,完成其他配置启动直接登录.rules和持久层的配置需要开启token-persistence配置项access-modes是请求获取token的位置。同时,成功登录或续签操作会主动将token返回给HEADER或COOKIEloop-auth:time-out:5#tokenvalidtime(inseconds)默认24小时token-persistence:true#tokenpersistenceconfigurationdefaultfalsetoken-name:token#默认也使用token名称LoopAuthmutualism:true#token共生defaultfalse开启,账号可以同时在线exclusion:true#互斥登录,默认false,如果开启,多人操作同一台设备会相互排挤(此配置仅在mutualism=true时有效)max-login-count:3#同一账号最大登录次数默认为1-1表示无限制续订:false#自动续订默认为true。每执行一次isLogin操作,token的有效期会自动刷新。即如果在COOKIE中认证成功,则不会去HEADER中获取-HEADER-COOKIEsecret-key:secret#默认LoopAuthToken生成密钥token-persistence-prefix:tokenPrefix#默认LoopAuthToken令牌持久层前缀login-id-persistence-prefix:loginIdPrefix#默认LoopAuthLoginIdLoginId持久层存储前缀cookie-config:#cookie配置记住:true#是否长期有效,默认为false,开启cookie的有效时间超时,网页关闭后cookie会丢失domain:localhost#domaindefaultserverdomainpath:/test#默认'/'路径http-only:true#默认false是否允许js运行secure:true#默认false是否只在https安全协议中传输#安全级别Strict(完全禁止第三方cookie,跨站,任何情况下都不会发送Cookie)#Lax不发送第三方cookie,除了导航到目标URL的Get请求#None不限制默认参数same-site:严格简单使用newControllerclass@RestControllerpublicclassDemoController{@GetMapping("/login")publicStringregister(){//登录方法LoopAuthFaceImpl.login("1");return"登录成功";}@GetMapping("/islogin")publicStringisLogin(){//验证你是否登录LoopAuthFaceImpl.isLogin();返回“已经Logodin";}@GetMapping("/out")publicStringloginOut(){//验证是否登录LoopAuthFaceImpl.isLogin();//注销再登录LoopAuthFaceImpl.logout();return"注销成功";}}Authentication或者登录验证实现PermissionInterface接口实现角色/权限码认证,需要获取当前登录账号的角色列表和权限码列表,需要手动实现PermissionInterface接口,注入public类PermissionInterfaceImplimplementsPermissionInterface{@OverridepublicSetgetPermissionSet(StringuserId,StringloginType){//这里只是为了演示,硬编码根据业务查询数据库或其他操作returnnewHashSet(){{add("user-*");}};}@OverridepublicSetgetRoleSet(StringuserId,StringloginType){//这只是为了演示,所以硬编码查询根据业务进行数据库或其他操作returnnewHashSet(){{add("user");}};}}自动注入可以在PermissionInterface的实现类上加上@Component注解@ComponentpublicclassPermissionInterfaceImplimplementsPermissionInterface{...}手动注入保证在项目启动时可以执行下面的语句。LoopAuthStrategy.setPermissionInterface(newPermissionInterfaceImpl());LoopAuthVerifyModeLoopAuthVerifyMode是一个枚举类,里面包含OR,AND,NOOR代表或者AND代表和NON代表不是所有的方法都需要填写LoopAuthVerifyMode,如果不填写,默认AND码认证所有需要登录的方法都会在内部调用LoopAuthFaceImpl.isLogin();默认情况下,即使用checkByRole等方法时不需要手动调用isLogin//判断是否登录LoopAuthFaceImpl.isLogin();//判断用户是否拥有用户角色LoopAuthFaceImpl.checkByRole("user")//判断用户是否有user-**或order-get中的权限码LoopAuthFaceImpl.checkByPermission(LoopAuthVerifyMode.OR,"user-**","order-get")注解认证所有需要登录的方法都会执行@LoopAutoCheckLogin默认情况下,即@LoopAuthPermission不需要使用@LoopAutoCheckLogin注解,可以在类中加入注解认证,需要依赖拦截器注入Interceptor@ComponentpublicclassLoopAuthMvcConfigureimplementsWebMvcConfigurer{/***RegisterLoopAuth的拦截器,开启注解认证功能*/@OverridepublicvoidaddInterceptors(InterceptorRegistryregistry){//注册注解拦截器registry.addInterceptor(newLoopAuthAnnotation拦截器()).addPathPatterns("/**");}}带注解的拦截//验证登录@LoopAutoCheckLogin//判断用户是否有user-**或order-get中的权限码@LoopAuthPermission(value={"user-**","order-get"},mode=LoopAuthVerifyMode.OR)@GetMapping("/testPermission")publicStringtestPermission(){return"Detectedsuccessfully";}//验证登录@LoopAutoCheckLogin//判断用户是否拥有用户角色@LoopAuthRole(value="user")@GetMapping("/testRole")publicStringtestRole(){return"检测成功";}更多功能请参考官方文档