|
|
@@ -0,0 +1,220 @@
|
|
|
+package com.webflux.launchadmin.config;
|
|
|
+
|
|
|
+
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.webflux.launchadmin.context.UserContext;
|
|
|
+import com.webflux.launchadmin.context.XxlSsoUser;
|
|
|
+import com.webflux.launchcommon.utils.RL;
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.core.io.buffer.DataBuffer;
|
|
|
+import org.springframework.data.redis.core.ReactiveRedisTemplate;
|
|
|
+import org.springframework.http.HttpCookie;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.http.server.RequestPath;
|
|
|
+import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
|
+import org.springframework.http.server.reactive.ServerHttpResponse;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
+import org.springframework.web.server.ServerWebExchange;
|
|
|
+import org.springframework.web.server.WebFilter;
|
|
|
+import org.springframework.web.server.WebFilterChain;
|
|
|
+import reactor.core.publisher.Mono;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.Optional;
|
|
|
+import java.util.stream.Stream;
|
|
|
+
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class LoginWebFiter implements WebFilter {
|
|
|
+// @Autowired
|
|
|
+// private ReactiveRedisTemplate<String,Object> reactiveRedisTemplate;
|
|
|
+ @Value("${xxl.sso.excluded.paths}")
|
|
|
+ private String xxlSsoExcludedPaths;
|
|
|
+ @Value("${xxl.sso.excluded.start}")
|
|
|
+ private String xxlSsoExcludedstart;
|
|
|
+ @Value("${xxl.sso.server}")
|
|
|
+ private String loginPageUrl;
|
|
|
+ @SneakyThrows
|
|
|
+ @Override
|
|
|
+ public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
|
|
|
+ ServerHttpRequest request = exchange.getRequest();
|
|
|
+ RequestPath path = request.getPath();
|
|
|
+ String string = path.toString();
|
|
|
+ String[] split = xxlSsoExcludedPaths.split(",");
|
|
|
+ String[] split1 = xxlSsoExcludedstart.split(",");
|
|
|
+ Optional<String> first = Stream.of(split).filter(string::equals).findFirst();
|
|
|
+ Optional<String> first1 = Stream.of(split1).filter(string::contains).findFirst();
|
|
|
+ if (first.isEmpty() && first1.isEmpty()) {
|
|
|
+ //验证权限
|
|
|
+ try {
|
|
|
+ String sessionid = getSessionid(exchange);
|
|
|
+ BaseContextHandler.setSessionId(sessionid);
|
|
|
+ Boolean user = getUser(getSessionid(exchange));
|
|
|
+
|
|
|
+
|
|
|
+ if (!user) {
|
|
|
+ return notLogin(exchange);
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ //日志
|
|
|
+ log.error("LoginWebFiter getUserInfo E:"+ e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ System.out.println("...abcd");
|
|
|
+ Mono<Void> filter = chain.filter(exchange); //放行
|
|
|
+ //流一旦经过某个操作就会变成新流
|
|
|
+ Mono<Void> voidMono = filter.doOnError(err -> {
|
|
|
+ System.out.println("目标方法异常以后...");
|
|
|
+ }) // 目标方法发生异常后做事
|
|
|
+ .doFinally(signalType -> {
|
|
|
+ System.out.println("目标方法执行以后...");
|
|
|
+ });// 目标方法执行之后
|
|
|
+ //上面执行不花时间。
|
|
|
+ return voidMono; //看清楚返回的是谁!!!
|
|
|
+ }
|
|
|
+
|
|
|
+ private Mono<Void> notLogin(ServerWebExchange exchange) {
|
|
|
+ ServerHttpResponse response = exchange.getResponse();
|
|
|
+ response.setStatusCode( org.springframework.http.HttpStatus.NOT_IMPLEMENTED);
|
|
|
+ response.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
|
|
|
+ RL result =new RL();
|
|
|
+ result.setCode(501);
|
|
|
+ result.setLoginPageUrl(loginPageUrl);
|
|
|
+ result.setMsg("sso not login.");
|
|
|
+ DataBuffer dataBuffer = response.bufferFactory().wrap(JSONUtil.toJsonStr(result).getBytes());
|
|
|
+ return response.writeWith(Mono.just(dataBuffer));
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getSessionid(ServerWebExchange exchange) {
|
|
|
+ MultiValueMap<String, HttpCookie> cookies = exchange.getRequest().getCookies();
|
|
|
+ if (!cookies.containsKey("xxl_sso_sessionid")) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String xxl_sso_sessionid = cookies.get("xxl_sso_sessionid").toString();
|
|
|
+ if (Objects.isNull(xxl_sso_sessionid)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return xxl_sso_sessionid.replace("[","").replace("]","");
|
|
|
+ }
|
|
|
+
|
|
|
+ public Boolean getUser(String cookie) throws IOException {
|
|
|
+ if (Objects.isNull(cookie)) {
|
|
|
+ log.error("111111111");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if(UserContext.cMap.containsKey(cookie)){
|
|
|
+ XxlSsoUser xxlSsoUser = UserContext.cMap.get(cookie);
|
|
|
+ BaseContextHandler.setUserId(xxlSsoUser.getUserid());
|
|
|
+ BaseContextHandler.setUserName(xxlSsoUser.getUsername());
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
|
+ //https://kuaitou.mokamrp.com/api/system/qwlb/getAdminInfo
|
|
|
+ HttpGet httpget = new HttpGet("http://test.ad-frontend.mokamrp.com/api/system/qwlb/getAdminInfo");
|
|
|
+ httpget.setHeader("Cookie",cookie);
|
|
|
+ CloseableHttpResponse response = httpclient.execute(httpget);
|
|
|
+ try {
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ String result = EntityUtils.toString(entity);
|
|
|
+ EntityUtils.consume(entity);
|
|
|
+ log.error("3");
|
|
|
+ if (Objects.nonNull(result)) {
|
|
|
+ log.error("4");
|
|
|
+ JSONObject jsonObject = JSONUtil.parseObj(result);
|
|
|
+ if (jsonObject.containsKey("code") && jsonObject.get("code", Integer.class) == 200) {
|
|
|
+ log.error("5");
|
|
|
+ if(jsonObject.containsKey("data")){
|
|
|
+ log.error("7");
|
|
|
+ XxlSsoUser data = jsonObject.get("data", XxlSsoUser.class);
|
|
|
+ BaseContextHandler.setUserId(data.getUserid());
|
|
|
+ BaseContextHandler.setUserName(data.getUsername());
|
|
|
+ UserContext.cMap.put(cookie,data);
|
|
|
+ System.out.println(data);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }else if(jsonObject.containsKey("code") && jsonObject.get("code", Integer.class) == 501){
|
|
|
+ return false;
|
|
|
+ }else {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.error("8");
|
|
|
+ } catch (Exception e) {
|
|
|
+ return true;
|
|
|
+ } finally {
|
|
|
+ response.close();
|
|
|
+ }
|
|
|
+ log.error("2222222222222");
|
|
|
+ return true;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// public static String parseStoreKey(String sessionId) {
|
|
|
+// if (sessionId != null && sessionId.indexOf("_") > -1) {
|
|
|
+// String[] sessionIdArr = sessionId.split("_");
|
|
|
+// if (sessionIdArr.length == 2 && sessionIdArr[0] != null && sessionIdArr[0].trim().length() > 0) {
|
|
|
+// String userId = sessionIdArr[0].trim();
|
|
|
+// return userId;
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// return null;
|
|
|
+// }
|
|
|
+// private static String xxl0(ServerWebExchange exchange) {
|
|
|
+//
|
|
|
+// MultiValueMap<String, HttpCookie> cookies = exchange.getRequest().getCookies();
|
|
|
+// if (!cookies.containsKey("xxl_sso_sessionid")) {
|
|
|
+// return null;
|
|
|
+// }
|
|
|
+// String xxl_sso_sessionid = cookies.get("xxl_sso_sessionid").toString();
|
|
|
+// if (Objects.isNull(xxl_sso_sessionid)) {
|
|
|
+// return null;
|
|
|
+// }
|
|
|
+// String xxl_sso_sessionid1 = xxl_sso_sessionid.replace("[","");
|
|
|
+// String xxl_sso_sessionid2 =xxl_sso_sessionid1.replace("]","");
|
|
|
+//
|
|
|
+// String[] split = xxl_sso_sessionid2.split("=");
|
|
|
+// if(Objects.isNull(split[1])){
|
|
|
+// return null;
|
|
|
+// }
|
|
|
+// String value = split[1];
|
|
|
+// String s = parseStoreKey(value);
|
|
|
+// return redisKey(s);
|
|
|
+// }
|
|
|
+// private static String redisKey(String sessionId) {
|
|
|
+// return "xxl_sso_sessionid".concat("#").concat(sessionId);
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+//
|
|
|
+// public Boolean xxl1(ServerWebExchange exchange){
|
|
|
+// String key = xxl0(exchange);
|
|
|
+// if (Objects.isNull(key)) {
|
|
|
+// return false;
|
|
|
+// }
|
|
|
+// Mono<Object> objectMono = reactiveRedisTemplate.opsForValue().get(key);
|
|
|
+// // Optional<Object> o = objectMono.blockOptional();
|
|
|
+// return false;
|
|
|
+//
|
|
|
+// }
|
|
|
+//
|
|
|
+// private Mono<Void> unauthorizedResponse(ServerWebExchange exchange, String msg) {
|
|
|
+// return ServletUtils.webFluxResponseWriter(exchange.getResponse(), msg, HttpStatus.UNAUTHORIZED);
|
|
|
+// }
|
|
|
+}
|