springboot 2.7.12 在整合 knife4j 3.0.3 的时候遇到了空指针异常

springboot 2.7.12 在整合 knife4j 3.0.3 的时候遇到了空指针异常

报错截图:

在这里插入图片描述

从网上一搜发现是Spring Boot 2.6以上引入的新PathPatternParser,需要进行配置,不然整合起来会报错

解决方案:

需要在 application.yaml 配置文件中配置

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

从SpringBoot 2.6.0开始,请求路径与Spring MVC处理映射匹配的默认策略采用PathPatternParser,而不是AntPathMatcher,如果不进行配置的话就会报错

api解释说明:

AntPathMatcher:spring路径匹配器

参考文章:https://blog.csdn.net/kkorkk/article/details/123774484

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,我来为你简单介绍一下Spring Boot 2.7.12集成JWT的方式。 JWT(JSON Web Token)是一种基于 JSON 的开放标准(RFC 7519),用于在网络上安全地传输声明(Claims)。Spring Security 5.x 中内置了对 JWT 的支持,可以方便地实现身份认证和授权功能。 下面是集成JWT的步骤: 1. 添加依赖 在 pom.xml 文件中添加以下依赖: ``` <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifactId> <version>0.9.1</version> </dependency> ``` 2. 配置JWT 在 Spring Boot 的配置文件中添加以下配置: ``` jwt: secret: mySecret expiration: 604800 # 过期时间为一周 ``` 这里的 `secret` 是用于生成和校验 JWT 的密钥,`expiration` 是 JWT 的过期时间,单位为秒。 3. 创建JWT工具类 创建一个 JWT 工具类,用于生成和解析 JWT: ``` @Component public class JwtUtils { @Value("${jwt.secret}") private String secret; @Value("${jwt.expiration}") private Long expiration; public String generateToken(String username) { Date now = new Date(); Date expireDate = new Date(now.getTime() + expiration * 1000); return Jwts.builder() .setSubject(username) .setIssuedAt(now) .setExpiration(expireDate) .signWith(SignatureAlgorithm.HS512, secret) .compact(); } public String getUsernameFromToken(String token) { Claims claims = Jwts.parser() .setSigningKey(secret) .parseClaimsJws(token) .getBody(); return claims.getSubject(); } public boolean validateToken(String token) { try { Jwts.parser().setSigningKey(secret).parseClaimsJws(token); return true; } catch (SignatureException ex) { log.error("JWT signature is invalid"); } catch (MalformedJwtException ex) { log.error("JWT token is invalid"); } catch (ExpiredJwtException ex) { log.error("JWT token has expired"); } catch (UnsupportedJwtException ex) { log.error("JWT token is unsupported"); } catch (IllegalArgumentException ex) { log.error("JWT claims string is empty"); } return false; } } ``` 这个工具类中包含了生成和解析 JWT 的方法,以及校验 JWT 的方法。 4. 配置Spring Security 在 Spring Security 的配置类中添加以下配置: ``` @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private JwtUtils jwtUtils; @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable() .authorizeRequests() .antMatchers("/api/auth/**").permitAll() .anyRequest().authenticated() .and() .addFilterBefore(new JwtAuthenticationFilter(jwtUtils), UsernamePasswordAuthenticationFilter.class); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { // 配置用户认证的方式,比如基于数据库的认证,LDAP认证等 } } ``` 这里的 `JwtAuthenticationFilter` 是一个自定义的过滤器,用于校验 JWT 并设置用户的身份信息。你需要自己实现这个过滤器。 5. 实现登录接口 实现一个登录接口,用于验证用户的身份,并生成 JWT: ``` @RestController @RequestMapping("/api/auth") public class AuthController { @Autowired private JwtUtils jwtUtils; @PostMapping("/login") public ResponseEntity<?> login(@RequestBody LoginDto loginDto) { // 调用用户认证的方式验证用户的身份 // 如果验证通过,生成 JWT 并返回给客户端 String token = jwtUtils.generateToken(loginDto.getUsername()); return ResponseEntity.ok(new AuthResponse(token)); } } ``` 这里的 `AuthResponse` 是一个简单的响应类,用于返回 JWT。 以上就是Spring Boot 2.7.12集成JWT的基本步骤。当然,实际情况可能会更加复杂,你需要根据自己的业务需求进行适当的调整。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

诗雨卿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值