fix:正则引发的错误
This commit is contained in:
parent
761e923f89
commit
10a890a3cb
@ -21,7 +21,7 @@ import java.lang.annotation.*;
|
|||||||
* @author jacky
|
* @author jacky
|
||||||
* 用于标记匿名访问方法
|
* 用于标记匿名访问方法
|
||||||
*/
|
*/
|
||||||
//@Inherited
|
@Inherited
|
||||||
@Documented
|
@Documented
|
||||||
@Target({ElementType.METHOD,ElementType.ANNOTATION_TYPE})
|
@Target({ElementType.METHOD,ElementType.ANNOTATION_TYPE})
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@ -17,6 +17,7 @@ package com.aircraft.config.webConfig;
|
|||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import com.aircraft.utils.AnonTagUtils;
|
import com.aircraft.utils.AnonTagUtils;
|
||||||
|
import org.apache.commons.lang3.RegExUtils;
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||||
@ -43,6 +44,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -66,6 +68,10 @@ public class SwaggerConfig {
|
|||||||
|
|
||||||
private final ApplicationContext applicationContext;
|
private final ApplicationContext applicationContext;
|
||||||
|
|
||||||
|
private static final Pattern PATTERN = Pattern.compile("\\{(.*?)\\}");
|
||||||
|
|
||||||
|
public String ASTERISK = "*";
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Docket createRestApi() {
|
public Docket createRestApi() {
|
||||||
return new Docket(DocumentationType.SWAGGER_2)
|
return new Docket(DocumentationType.SWAGGER_2)
|
||||||
@ -106,7 +112,12 @@ public class SwaggerConfig {
|
|||||||
|
|
||||||
private SecurityContext getContextByPath() {
|
private SecurityContext getContextByPath() {
|
||||||
Set<String> urls = AnonTagUtils.getAllAnonymousUrl(applicationContext);
|
Set<String> urls = AnonTagUtils.getAllAnonymousUrl(applicationContext);
|
||||||
urls = urls.stream().filter(url -> !url.equals("/")).collect(Collectors.toSet());
|
urls = urls.stream().filter(url -> !url.equals("/"))
|
||||||
|
.map(url -> {
|
||||||
|
//替代path variable 为 *
|
||||||
|
return RegExUtils.replaceAll(url, PATTERN, ASTERISK);
|
||||||
|
})
|
||||||
|
.collect(Collectors.toSet());
|
||||||
String regExp = "^(?!" + apiPath + String.join("|" + apiPath, urls) + ").*$";
|
String regExp = "^(?!" + apiPath + String.join("|" + apiPath, urls) + ").*$";
|
||||||
return SecurityContext.builder()
|
return SecurityContext.builder()
|
||||||
.securityReferences(defaultAuth())
|
.securityReferences(defaultAuth())
|
||||||
|
@ -105,9 +105,9 @@ public class CpTextController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// @AnonymousAccess
|
@AnonymousAccess
|
||||||
@ApiOperation(value = "查询单个文本内容")
|
@ApiOperation(value = "查询单个文本内容")
|
||||||
@RequestMapping(value = "{id}", method = {RequestMethod.GET})
|
@GetMapping(value = "/{id}")
|
||||||
@ApiImplicitParam(name = "id", value = "文本内容ID", required = true, paramType = "path")
|
@ApiImplicitParam(name = "id", value = "文本内容ID", required = true, paramType = "path")
|
||||||
public ResponseEntity<CpText> one(@PathVariable final Integer id) {
|
public ResponseEntity<CpText> one(@PathVariable final Integer id) {
|
||||||
try {
|
try {
|
||||||
|
@ -66,14 +66,6 @@ public class SpringSecurityConfig {
|
|||||||
protected SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
|
protected SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
|
||||||
// 获取匿名标记
|
// 获取匿名标记
|
||||||
Map<String, Set<String>> anonymousUrls = AnonTagUtils.getAnonymousUrl(applicationContext);
|
Map<String, Set<String>> anonymousUrls = AnonTagUtils.getAnonymousUrl(applicationContext);
|
||||||
// 手动添加需要匿名访问的接口路径(核心修改)
|
|
||||||
// 1. 分页查询接口:GET /cpText
|
|
||||||
anonymousUrls.computeIfAbsent(RequestMethodEnum.GET.getType(), k -> new HashSet<>())
|
|
||||||
.add("/cpText");
|
|
||||||
// 2. 通过 ID 查询接口:GET /cpText/{id}(支持任意 ID)
|
|
||||||
anonymousUrls.computeIfAbsent(RequestMethodEnum.GET.getType(), k -> new HashSet<>())
|
|
||||||
.add("/cpText/**");
|
|
||||||
|
|
||||||
return httpSecurity
|
return httpSecurity
|
||||||
// 禁用 CSRF
|
// 禁用 CSRF
|
||||||
.csrf().disable()
|
.csrf().disable()
|
||||||
|
Loading…
Reference in New Issue
Block a user