feat: 增加用户获取器
This commit is contained in:
parent
ac46f7e57b
commit
8cdc212d78
@ -1,5 +1,6 @@
|
|||||||
package dev.flyfish.boot.cas.config;
|
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.WebSessionDecorator;
|
||||||
import dev.flyfish.boot.cas.config.session.WebSessionListener;
|
import dev.flyfish.boot.cas.config.session.WebSessionListener;
|
||||||
import dev.flyfish.boot.cas.filter.CASFilter;
|
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.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
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.WebSession;
|
||||||
import org.springframework.web.server.session.DefaultWebSessionManager;
|
import org.springframework.web.server.session.DefaultWebSessionManager;
|
||||||
import org.springframework.web.server.session.InMemoryWebSessionStore;
|
import org.springframework.web.server.session.InMemoryWebSessionStore;
|
||||||
@ -25,7 +28,12 @@ import java.util.List;
|
|||||||
* @author wangyu
|
* @author wangyu
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
public class CASConfig {
|
public class CASConfig implements WebFluxConfigurer {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void configureArgumentResolvers(ArgumentResolverConfigurer configurer) {
|
||||||
|
configurer.addCustomResolver(new CASUserArgumentResolver());
|
||||||
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConfigurationProperties("cas.filter")
|
@ConfigurationProperties("cas.filter")
|
||||||
|
@ -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 {
|
||||||
|
}
|
@ -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<Object> resolveArgument(MethodParameter parameter, BindingContext bindingContext, ServerWebExchange exchange) {
|
||||||
|
return exchange.getSession().mapNotNull(session -> session.getAttribute(CASLoginFilter.CONST_CAS_USERNAME));
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
package dev.flyfish.boot.cas.controller;
|
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.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@ -14,7 +15,7 @@ public class IndexController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/hello")
|
@GetMapping("/hello")
|
||||||
public Object hello() {
|
public Object hello(@CASUser String user) {
|
||||||
return Map.of("message", "Hello World!");
|
return Map.of("message", "Hello World!", "user", user);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ import java.io.BufferedReader;
|
|||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.net.URI;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
@ -27,29 +28,17 @@ public class SecureURL {
|
|||||||
log.trace("entering retrieve(" + url + ")");
|
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 {
|
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
||||||
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) {
|
|
||||||
}
|
|
||||||
|
|
||||||
|
for (int chByte = in.read(); chByte != -1; chByte = in.read()) {
|
||||||
|
output.write(chByte);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return output.toString(StandardCharsets.UTF_8);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user