1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| @AutoConfiguration @AutoConfigureOrder(-1) @EnableMethodSecurity(securedEnabled = true) public class CustomWebSecurityConfigurerAdapter{
@Resource private ApplicationContext applicationContext;
@Bean protected SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception{ httpSecurity .cors(Customizer.withDefaults()) .csrf(AbstractHttpConfigurer::disable); Multimap<HttpMethod, String> permitAllUrls = getPermitAllUrlsFromAnnotations();
httpSecurity .authorizeHttpRequests(c ->c .requestMatchers(HttpMethod.GET, "/*.html", "/*.html", "/*.css", "/*.js").permitAll() .requestMatchers(HttpMethod.GET, permitAllUrls.get(HttpMethod.GET).toArray(new String[0])).permitAll() .requestMatchers(HttpMethod.POST, permitAllUrls.get(HttpMethod.POST).toArray(new String[0])).permitAll() .requestMatchers(HttpMethod.PUT, permitAllUrls.get(HttpMethod.PUT).toArray(new String[0])).permitAll() .requestMatchers(HttpMethod.DELETE, permitAllUrls.get(HttpMethod.DELETE).toArray(new String[0])).permitAll() .requestMatchers(HttpMethod.HEAD, permitAllUrls.get(HttpMethod.HEAD).toArray(new String[0])).permitAll() .requestMatchers(HttpMethod.PATCH, permitAllUrls.get(HttpMethod.PATCH).toArray(new String[0])).permitAll() .requestMatchers( "/swagger-ui/**", "/swagger-resources/**", "/v3/api-docs/**", "/webjars/**", "/doc.html" ).permitAll()) .authorizeHttpRequests(c -> c.anyRequest().authenticated()); return httpSecurity.build(); }
private Multimap<HttpMethod,String> getPermitAllUrlsFromAnnotations(){ Multimap<HttpMethod, String> result = HashMultimap.create(); RequestMappingHandlerMapping handlerMapping = applicationContext.getBean(RequestMappingHandlerMapping.class); Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods(); for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethods.entrySet()) { RequestMappingInfo requestMappingInfo = entry.getKey(); HandlerMethod handlerMethod = entry.getValue(); if (!handlerMethod.hasMethodAnnotation(PermitAll.class)) continue;
Set<String> urls = new HashSet<>(); if (Objects.nonNull(requestMappingInfo.getPatternsCondition())) urls.addAll(entry.getKey().getPatternsCondition().getPatterns());
if (Objects.nonNull(requestMappingInfo.getPathPatternsCondition())) urls.addAll(convertList(requestMappingInfo.getPathPatternsCondition().getPatterns(), PathPattern::getPatternString));
if (urls.isEmpty())continue;
requestMappingInfo.getMethodsCondition().getMethods().forEach(requestMethod -> { switch (requestMethod) { case GET: result.putAll(HttpMethod.GET, urls); break; case POST: result.putAll(HttpMethod.POST, urls); break; case PUT: result.putAll(HttpMethod.PUT, urls); break; case DELETE: result.putAll(HttpMethod.DELETE, urls); break; case HEAD: result.putAll(HttpMethod.HEAD, urls); break; case PATCH: result.putAll(HttpMethod.PATCH, urls); break; } });
} return result; } public static <T, U> List<U> convertList(Collection<T> from, Function<T, U> func) { if (CollUtil.isEmpty(from)) { return new ArrayList<>(); } return from.stream().map(func).filter(Objects::nonNull).collect(Collectors.toList()); } }
|