From 8cdc212d7880c774c2a4863fc1a7540d3739b880 Mon Sep 17 00:00:00 2001 From: wangyu <727842003@qq.com> Date: Mon, 14 Oct 2024 11:16:38 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flyfish/boot/cas/config/CASConfig.java | 10 +++++- .../boot/cas/config/annotation/CASUser.java | 9 ++++++ .../resolver/CASUserArgumentResolver.java | 27 ++++++++++++++++ .../boot/cas/controller/IndexController.java | 5 +-- .../flyfish/boot/cas/validator/SecureURL.java | 31 ++++++------------- 5 files changed, 58 insertions(+), 24 deletions(-) create mode 100644 src/main/java/dev/flyfish/boot/cas/config/annotation/CASUser.java create mode 100644 src/main/java/dev/flyfish/boot/cas/config/resolver/CASUserArgumentResolver.java diff --git a/src/main/java/dev/flyfish/boot/cas/config/CASConfig.java b/src/main/java/dev/flyfish/boot/cas/config/CASConfig.java index 543263f..f3b5664 100644 --- a/src/main/java/dev/flyfish/boot/cas/config/CASConfig.java +++ b/src/main/java/dev/flyfish/boot/cas/config/CASConfig.java @@ -1,5 +1,6 @@ package dev.flyfish.boot.cas.config; +import dev.flyfish.boot.cas.config.resolver.CASUserArgumentResolver; import dev.flyfish.boot.cas.config.session.WebSessionDecorator; import dev.flyfish.boot.cas.config.session.WebSessionListener; import dev.flyfish.boot.cas.filter.CASFilter; @@ -9,6 +10,8 @@ import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.web.reactive.config.WebFluxConfigurer; +import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer; import org.springframework.web.server.WebSession; import org.springframework.web.server.session.DefaultWebSessionManager; import org.springframework.web.server.session.InMemoryWebSessionStore; @@ -25,7 +28,12 @@ import java.util.List; * @author wangyu */ @Configuration -public class CASConfig { +public class CASConfig implements WebFluxConfigurer { + + @Override + public void configureArgumentResolvers(ArgumentResolverConfigurer configurer) { + configurer.addCustomResolver(new CASUserArgumentResolver()); + } @Bean @ConfigurationProperties("cas.filter") diff --git a/src/main/java/dev/flyfish/boot/cas/config/annotation/CASUser.java b/src/main/java/dev/flyfish/boot/cas/config/annotation/CASUser.java new file mode 100644 index 0000000..69be797 --- /dev/null +++ b/src/main/java/dev/flyfish/boot/cas/config/annotation/CASUser.java @@ -0,0 +1,9 @@ +package dev.flyfish.boot.cas.config.annotation; + +import java.lang.annotation.*; + +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.PARAMETER) +public @interface CASUser { +} diff --git a/src/main/java/dev/flyfish/boot/cas/config/resolver/CASUserArgumentResolver.java b/src/main/java/dev/flyfish/boot/cas/config/resolver/CASUserArgumentResolver.java new file mode 100644 index 0000000..519ff51 --- /dev/null +++ b/src/main/java/dev/flyfish/boot/cas/config/resolver/CASUserArgumentResolver.java @@ -0,0 +1,27 @@ +package dev.flyfish.boot.cas.config.resolver; + +import dev.flyfish.boot.cas.config.annotation.CASUser; +import dev.flyfish.boot.cas.filter.CASLoginFilter; +import org.springframework.core.MethodParameter; +import org.springframework.core.ReactiveAdapterRegistry; +import org.springframework.web.reactive.BindingContext; +import org.springframework.web.reactive.result.method.HandlerMethodArgumentResolverSupport; +import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Mono; + +public class CASUserArgumentResolver extends HandlerMethodArgumentResolverSupport { + + public CASUserArgumentResolver() { + super(ReactiveAdapterRegistry.getSharedInstance()); + } + + @Override + public boolean supportsParameter(MethodParameter parameter) { + return checkAnnotatedParamNoReactiveWrapper(parameter, CASUser.class, (anno, type) -> true); + } + + @Override + public Mono resolveArgument(MethodParameter parameter, BindingContext bindingContext, ServerWebExchange exchange) { + return exchange.getSession().mapNotNull(session -> session.getAttribute(CASLoginFilter.CONST_CAS_USERNAME)); + } +} diff --git a/src/main/java/dev/flyfish/boot/cas/controller/IndexController.java b/src/main/java/dev/flyfish/boot/cas/controller/IndexController.java index 2041f56..5392ee0 100644 --- a/src/main/java/dev/flyfish/boot/cas/controller/IndexController.java +++ b/src/main/java/dev/flyfish/boot/cas/controller/IndexController.java @@ -1,5 +1,6 @@ package dev.flyfish.boot.cas.controller; +import dev.flyfish.boot.cas.config.annotation.CASUser; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @@ -14,7 +15,7 @@ public class IndexController { } @GetMapping("/hello") - public Object hello() { - return Map.of("message", "Hello World!"); + public Object hello(@CASUser String user) { + return Map.of("message", "Hello World!", "user", user); } } diff --git a/src/main/java/dev/flyfish/boot/cas/validator/SecureURL.java b/src/main/java/dev/flyfish/boot/cas/validator/SecureURL.java index a7ba238..b9fa633 100644 --- a/src/main/java/dev/flyfish/boot/cas/validator/SecureURL.java +++ b/src/main/java/dev/flyfish/boot/cas/validator/SecureURL.java @@ -7,6 +7,7 @@ import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.net.URI; import java.net.URL; import java.net.URLConnection; import java.nio.charset.StandardCharsets; @@ -27,29 +28,17 @@ public class SecureURL { log.trace("entering retrieve(" + url + ")"); } - BufferedReader r = null; + URL u = URI.create(url).toURL(); + URLConnection uc = u.openConnection(); + uc.setRequestProperty("Connection", "close"); + InputStream in = uc.getInputStream(); - try { - URL u = new URL(url); - URLConnection uc = u.openConnection(); - uc.setRequestProperty("Connection", "close"); - InputStream in = uc.getInputStream(); - - ByteArrayOutputStream output = new ByteArrayOutputStream(); - - for (int chByte = in.read(); chByte != -1; chByte = in.read()) { - output.write(chByte); - } - - return output.toString(StandardCharsets.UTF_8); - } finally { - try { - if (r != null) { - r.close(); - } - } catch (IOException var14) { - } + ByteArrayOutputStream output = new ByteArrayOutputStream(); + for (int chByte = in.read(); chByte != -1; chByte = in.read()) { + output.write(chByte); } + + return output.toString(StandardCharsets.UTF_8); } }