经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring Boot » 查看文章
我开源了团队内部基于SpringBoot Web快速开发的API脚手架v1.6.0更新
来源:cnblogs  作者:程序员三时  时间:2023/7/26 9:23:25  对本文有异议

什么是 rest-api-spring-boot-starter

rest-api-spring-boot-starter 适用于SpringBoot Web API 快速构建让开发人员快速构建统一规范的业务RestFull API 不在去关心一些繁琐。重复工作,而是把重点聚焦到业务。

动机

每次Web API常用功能都需要重新写一遍。或者复制之前的项目代码。于是我封装了这么一个stater

抽出SpringBoot Web API 每个项目必备需要重复写的模块,和必备功能。
并且扩展了我工作中用到的 所有工具库。 解放双手提高开发效率

推荐版本

  • SpringBoot
  1. SpringBoot 2.7.x

版本更新

目前最新版本1.6.2 支持功能如下:

  • 支持一键配置自定义RestFull API 统一格式返回

  • 支持RestFull API 错误国际化

  • 支持全局异常处理,全局参数验证处理

  • 业务错误断言工具封装,遵循错误优先返回原则

  • 封装Redis key,value 操作工具类。统一key管理 spring cache缓存实现

  • RestTemplate 封装 POST,GET 请求工具

  • 日志集成。自定义日志路径,按照日志等级分类,支持压缩和文件大小分割。按时间显示

  • 工具库集成 集成了lombok,hutool,commons-lang3,guava。不需要自己单个引入

  • 集成mybatisPlus一键代码生成

  • 日志记录,服务监控,支持日志链路查询。自定义数据源

  • OpenApi3文档集成支持一键配置。支持多种文档和自动配置

  • 生成JWT标准Token和权限认证

  • 接口限流,Ip城市回显

  • HttpUserAgent请求设备工具封装

  • RequestUtil参数解析封装工具

  • GitHub 地址

  • gitee 地址

Web JWT Token 权限支持

JWT Web Token

可以轻松自定义生成自己JWT Web Token.和基于JWT 的userJwtToken

通过userJwtToken你可以轻松生成基于用户登录认证的Token

  1. @Autowired
  2. private UserJwtToken userJwtToken;
  3. @GetMapping("/login")
  4. public Result login() {
  5. UserEntry userEntry = new UserEntry();
  6. userEntry.setUserId("2");
  7. userEntry.setUsername("billy");
  8. userEntry.setHobby("eat");
  9. userJwtToken.rememberMe=true;
  10. String token = userJwtToken.createdToken(userEntry.getUserId(), userEntry.getUsername(), userEntry);
  11. return Result.buildSuccess(token);
  12. }

解析token获取用户信息

  1. @GetMapping("/user")
  2. public Result getUser() {
  3. String token = "eyJhbGciOiJIUzI1NiIsInppcCI6IkRFRiJ9.eNqqViouTVKyUkrKzMmpVNJRyiwuBvKMgKyskkwoK7WiQMnK0MzC0tTUwsDEWEeptDi1SMmqGkx7pkBVgTh5ibmpSIZl5CclVQL5qYklSrW1AAAAAP__.8nWRs40LbRTIQBhJ8jVaANPcvsmX0zoLR66R-b2Uc4M";
  4. String userName=userJwtToken.getUserName(token);
  5. String userId= userJwtToken.getUserId(token);
  6. UserEntry userEntry=userJwtToken.parseUserToken(token,UserEntry.class);
  7. return Result.buildSuccess(userId);
  8. }

自定义Token秘钥和签名配置

  1. jwt:
  2. secret: 123456 # 秘钥 建议加密后秘钥如md5 不要使用明文长度大于6位
  3. expiration: 86400 # token 过期时间(单位秒 1天后过期)
  4. token-header: Token #header token 名称
  5. remember-me-expiration: 604800 #记住我 token过期时间(单位秒 7天后过期)
  6. user-sign: true # 是否自定义签名。为true需要实现加密接口。和 配置 jwtCfg注入对应bean

自定义签名认证和动态秘钥授权需要实现UserSign接口配置UserJwtConfig配置类注入自定义签名bean

  1. package cn.soboys.superaide.config;
  2. import cn.soboys.restapispringbootstarter.authorization.UserSign;
  3. import io.jsonwebtoken.SignatureAlgorithm;
  4. /**
  5. * @author 公众号 程序员三时
  6. * @version 1.0
  7. * @date 2023/7/16 00:20
  8. * @webSite https://github.com/coder-amiao
  9. */
  10. public class MyUserSign implements UserSign {
  11. @Override
  12. public SignatureAlgorithm sign() {
  13. return SignatureAlgorithm.HS256;
  14. }
  15. @Override
  16. public String AuthKey() {
  17. return null;
  18. }
  19. }

AuthKey返回null时候会使用你在属性文件配置的秘钥。没有会使用默认的

  1. package cn.soboys.superaide.config;
  2. import cn.soboys.restapispringbootstarter.authorization.*;
  3. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.context.annotation.Primary;
  7. /**
  8. * @author 公众号 程序员三时
  9. * @version 1.0
  10. * @date 2023/7/15 09:49
  11. * @webSite https://github.com/coder-amiao
  12. * 用户jwt token生成配置
  13. */
  14. @Configuration
  15. public class UserJwtConfig {
  16. @Bean
  17. public UserSign MyUserSign() {
  18. return new MyUserSign();
  19. }
  20. @Bean
  21. public UserJwtToken userJwtToken(UserSign MyUserSign) {
  22. UserJwtToken userJwtToken = new UserJwtToken();
  23. userJwtToken.setUserSign(MyUserSign);
  24. return userJwtToken;
  25. }
  26. }

权限认证

基于JWT Web Token 也帮你封装了权限登录认证。 你只需要在属性文件配置开启即可。

  1. jwt:
  2. authorization:
  3. has-authorization: true
  4. includes-url: /user # 需要认证请求 多个用逗号隔开
  5. excludes-url: /login,/register/** # 配置无需认证的

全局帮你自动处理Token过期异常。和错误异常你只需要在heard中配置你自己的Token就行

  1. {
  2. "success": false,
  3. "code": "401",
  4. "msg": "未授权 ",
  5. "requestId": "9a3ytEtOX0UuojSaA2LD",
  6. "timestamp": "2023-07-17 17:08:05",
  7. "data": null
  8. }

如果需要自定义自己认证授权逻辑,实现LoginAuthorization接口即可
并且在UserJwtConfig配置类中注入对应LoginAuthorization bean

如:

  1. package cn.soboys.superaide.config;
  2. import cn.soboys.restapispringbootstarter.Assert;
  3. import cn.soboys.restapispringbootstarter.HttpStatus;
  4. import cn.soboys.restapispringbootstarter.authorization.LoginAuthorization;
  5. import cn.soboys.restapispringbootstarter.authorization.UserJwtToken;
  6. import org.dromara.hutool.core.text.StrUtil;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Component;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. /**
  12. * @author 公众号 程序员三时
  13. * @version 1.0
  14. * @date 2023/7/16 11:00
  15. * @webSite https://github.com/coder-amiao
  16. */
  17. @Component
  18. public class MyLoginAuthorization implements LoginAuthorization {
  19. @Autowired
  20. private UserJwtToken userJwtToken;
  21. @Override
  22. public Boolean authorization(HttpServletRequest request, HttpServletResponse response, Object handler) {
  23. String token = request.getHeader("Token");
  24. Assert.isFalse(StrUtil.isEmpty(token),HttpStatus.UNAUTHORIZED);
  25. String userId = userJwtToken.getUserId(token); //验证token有效合法性。
  26. //其他数据库 或者业务操作
  27. return true;
  28. }
  29. }

在配置类中注入bean

  1. package cn.soboys.superaide.config;
  2. import cn.soboys.restapispringbootstarter.authorization.*;
  3. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.context.annotation.Primary;
  7. /**
  8. * @author 公众号 程序员三时
  9. * @version 1.0
  10. * @date 2023/7/15 09:49
  11. * @webSite https://github.com/coder-amiao
  12. * 用户jwt token生成配置
  13. */
  14. @Configuration
  15. public class UserJwtConfig {
  16. @Bean
  17. public UserSign MyUserSign() {
  18. return new MyUserSign();
  19. }
  20. @Bean
  21. @Primary
  22. public LoginAuthorization loginAuthorizationSubject() {
  23. return new MyLoginAuthorization();
  24. }
  25. @Bean
  26. public UserJwtToken userJwtToken(UserSign MyUserSign) {
  27. UserJwtToken userJwtToken = new UserJwtToken();
  28. userJwtToken.setUserSign(MyUserSign);
  29. return userJwtToken;
  30. }
  31. }

三方权限认证框架

基于JWT Web Token也可以很轻松集成Shiro 或者是。Spring Security等其他第三权限框架

当然后续版本我会把权限认证独立出来一个完整轻量级权限框架项目。如:
通过注解@hasPerm,@hasRole,@hasAnyPerm,@hasAnyRoles
轻松实现相对复杂的权限认证。

后续更新

通用业务

在我们聚焦项目开发时候,总是会有一些相对公共独立的第三方业务模块。
如:三方登录,三方支付,消息推送,资源上传
后续我会持续集成。通用业务生态。 实现真的解放生产力。自由组合。

有任何编程问题。

关注公众号,程序员三时 持续输出优质内容 希望给你带来一点启发和帮助

原文链接:https://www.cnblogs.com/kenx/p/17581560.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号