feat(新增): 🚀 返回图片验证码接口

This commit is contained in:
Bunny 2024-07-27 23:36:23 +08:00
parent e202c10336
commit 0d218cd7ba
5 changed files with 69 additions and 3 deletions

View File

@ -30,8 +30,11 @@ public class WebMvcConfiguration implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
String[] excludeList = {"/api/checkCode", "/api/sendEmailCode", "/api/register", "/api/login", "/api/article/loadArticle/**"};
log.info("WebMvcConfiguration===>开始注册自定义拦截器...");
String[] excludeList = {"/api/checkCode", "/api/sendEmailCode", "/api/register", "/api/login",};
registry.addInterceptor(userTokenInterceptor).excludePathPatterns(excludeList);
// TODO 如果想使用普通JWT可以使用这个不使用 SpringSecurity6
// registry.addInterceptor(userTokenInterceptor).addPathPatterns("/api/**").excludePathPatterns(excludeList);
}

View File

@ -88,8 +88,9 @@ public class WebSecurityConfig {
// 排出鉴定路径
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
String[] annotations = {"/", "/test/**", "/diagram-viewer/**", "/editor-app/**", "/*.html",
"/*/*/noAuth/**", "/*/noAuth/**", "/favicon.ico", "/swagger-resources/**", "/webjars/**", "/v3/**", "/swagger-ui.html/**", "/doc.html"};
String[] annotations = {"/", "/test/**", "/*.html", "/*/*/noAuth/**", "/*/noAuth/**", "/favicon.ico", "/swagger-resources/**", "/swagger-ui.html/**",
"/api/**"
};
return web -> web.ignoring().requestMatchers(annotations);
}
}

View File

@ -0,0 +1,31 @@
package cn.bunny.service.controller;
import cn.bunny.service.service.LoginService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Tag(name = "web相关接口")
@RestController
@RequestMapping("/api")
public class WebController {
@Autowired
private LoginService loginService;
@Operation(summary = "生成验证码", description = "生成验证码")
@GetMapping("checkCode")
public ResponseEntity<byte[]> checkCode() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_JPEG);
byte[] image = loginService.checkCode();
return new ResponseEntity<byte[]>(image, headers, HttpStatus.OK);
}
}

View File

@ -0,0 +1,10 @@
package cn.bunny.service.service;
public interface LoginService {
/**
* * 生成验证码
*
* @return 验证码图片数组
*/
byte[] checkCode();
}

View File

@ -0,0 +1,21 @@
package cn.bunny.service.service.impl;
import cn.bunny.service.service.LoginService;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import org.springframework.stereotype.Service;
@Service
public class LoginServiceImpl implements LoginService {
/**
* * 生成验证码
*
* @return 验证码图片数组
*/
@Override
public byte[] checkCode() {
// 生成验证码
CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(150, 48, 4, 2);
return captcha.getImageBytes();
}
}