Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improve] Shiro session cannot expire bug fixed #3939

Merged
merged 11 commits into from
Aug 4, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ class CURLBuilder(val url: String) {
headers.keySet.foreach(h => cURL.append(String.format("-H \'%s: %s\' \\\n", h, headers.get(h))))
formData.foreach(k =>
cURL.append(String.format("--data-urlencode \'%s=%s\' \\\n", k._1, k._2)))
cURL.append("-i")
cURL.toString
cURL.toString.trim.dropRight(1)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,36 +40,6 @@ public final class WebUtils {
private WebUtils() {
}

/**
* token encrypt
*
* @param token token
* @return encrypt token
*/
public static String encryptToken(String token) {
try {
return EncryptUtils.encrypt(token);
} catch (Exception e) {
log.info("token encrypt failed: ", e);
return null;
}
}

/**
* token decrypt
*
* @param encryptToken encryptToken
* @return decrypt token
*/
public static String decryptToken(String encryptToken) {
try {
return EncryptUtils.decrypt(encryptToken);
} catch (Exception e) {
log.info("token decrypt failed: ", e);
return null;
}
}

/**
* camel to underscore
*
Expand Down Expand Up @@ -113,15 +83,8 @@ public static File getAppLibDir() {
return getAppDir(LIB);
}

public static File getAppPluginsDir() {
return getAppDir(PLUGINS);
}

public static File getAppClientDir() {
return getAppDir(CLIENT);
}

public static File getAppConfDir() {
return getAppDir(CONF);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.apache.streampark.console.core.annotation;

import org.apache.streampark.console.core.aspect.StreamParkAspect;
import org.apache.streampark.console.core.aspect.AppChangeEventAspect;
import org.apache.streampark.console.core.watcher.FlinkAppHttpWatcher;

import org.aspectj.lang.ProceedingJoinPoint;
Expand All @@ -31,12 +31,12 @@
* In the controller({@link org.apache.streampark.console.core.controller}), If some method causes
* application state update, need to add this annotation, This annotation marks which methods will
* cause the application to be updated, Will work together with {@link
* StreamParkAspect#appUpdated(ProceedingJoinPoint)}, The final purpose will be refresh {@link
* AppChangeEventAspect#appChangeEvent(ProceedingJoinPoint)}, The final purpose will be refresh {@link
* FlinkAppHttpWatcher#WATCHING_APPS}, Make the state of the job consistent with the database
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AppUpdated {
public @interface AppChangeEvent {

boolean value() default true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
String defaultValue() default "";

String bindFor() default "";

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.streampark.console.core.aspect;

import org.apache.streampark.console.core.watcher.FlinkAppHttpWatcher;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Aspect
public class AppChangeEventAspect {

@Autowired
private FlinkAppHttpWatcher flinkAppHttpWatcher;

@Pointcut("@annotation(org.apache.streampark.console.core.annotation.AppChangeEvent)")
public void appChangeEventPointcut() {
}

@Around("appChangeEventPointcut()")
public Object appChangeEvent(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
log.debug("appUpdated aspect, method:{}", methodSignature.getName());
Object target = joinPoint.proceed();
flinkAppHttpWatcher.init();
return target;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.streampark.console.core.aspect;

import org.apache.streampark.common.util.DateUtils;
import org.apache.streampark.common.util.ReflectUtils;
import org.apache.streampark.console.base.domain.RestResponse;
import org.apache.streampark.console.base.exception.ApiAlertException;
import org.apache.streampark.console.core.annotation.OpenAPI;
import org.apache.streampark.console.system.entity.AccessToken;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.TimeZone;

@Slf4j
@Component
@Aspect
public class OpenAPIAspect {

@Pointcut("execution(public"
+ " org.apache.streampark.console.base.domain.RestResponse"
+ " org.apache.streampark.console.core.controller.*.*(..))")
public void openAPIPointcut() {
}

@SuppressWarnings("checkstyle:SimplifyBooleanExpression")
@Around(value = "openAPIPointcut()")
public RestResponse openAPI(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
log.debug("restResponse aspect, method:{}", methodSignature.getName());
Boolean isApi = (Boolean) SecurityUtils.getSubject().getSession().getAttribute(AccessToken.IS_API_TOKEN);
if (isApi != null && isApi) {
HttpServletRequest request =
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
OpenAPI openAPI = methodSignature.getMethod().getAnnotation(OpenAPI.class);
if (openAPI == null) {
String url = request.getRequestURI();
throw new ApiAlertException("openapi unsupported: " + url);
} else {
Object[] objects = joinPoint.getArgs();
for (OpenAPI.Param param : openAPI.param()) {
String bingFor = param.bindFor();
if (StringUtils.isNotBlank(bingFor)) {
String name = param.name();
for (Object args : objects) {
Field bindForField = ReflectUtils.getField(args.getClass(), bingFor);
if (bindForField != null) {
Object value = request.getParameter(name);
bindForField.setAccessible(true);
if (value != null) {
if (param.type().equals(String.class)) {
bindForField.set(args, value.toString());
} else if (param.type().equals(Boolean.class)
|| param.type().equals(boolean.class)) {
bindForField.set(args, Boolean.parseBoolean(value.toString()));
} else if (param.type().equals(Integer.class) || param.type().equals(int.class)) {
bindForField.set(args, Integer.parseInt(value.toString()));
} else if (param.type().equals(Long.class) || param.type().equals(long.class)) {
bindForField.set(args, Long.parseLong(value.toString()));
} else if (param.type().equals(Date.class)) {
bindForField.set(args, DateUtils.parse(value.toString(), DateUtils.fullFormat(),
TimeZone.getDefault()));
}
}
}
}
}
}
}
}
return (RestResponse) joinPoint.proceed();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,16 @@

import org.apache.streampark.console.base.domain.RestResponse;
import org.apache.streampark.console.base.exception.ApiAlertException;
import org.apache.streampark.console.core.annotation.OpenAPI;
import org.apache.streampark.console.core.annotation.Permission;
import org.apache.streampark.console.core.entity.Application;
import org.apache.streampark.console.core.enums.UserTypeEnum;
import org.apache.streampark.console.core.service.application.ApplicationManageService;
import org.apache.streampark.console.core.util.ServiceHelper;
import org.apache.streampark.console.core.watcher.FlinkAppHttpWatcher;
import org.apache.streampark.console.system.entity.AccessToken;
import org.apache.streampark.console.system.entity.Member;
import org.apache.streampark.console.system.entity.User;
import org.apache.streampark.console.system.service.MemberService;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
Expand All @@ -47,67 +43,23 @@
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Component
@Aspect
public class StreamParkAspect {

@Autowired
private FlinkAppHttpWatcher flinkAppHttpWatcher;
public class PermissionAspect {

@Autowired
private MemberService memberService;

@Autowired
private ApplicationManageService applicationManageService;

@Pointcut("execution(public"
+ " org.apache.streampark.console.base.domain.RestResponse"
+ " org.apache.streampark.console.core.controller.*.*(..))")
public void openAPI() {
}

@SuppressWarnings("checkstyle:SimplifyBooleanExpression")
@Around(value = "openAPI()")
public RestResponse openAPI(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
log.debug("restResponse aspect, method:{}", methodSignature.getName());
Boolean isApi = (Boolean) SecurityUtils.getSubject().getSession().getAttribute(AccessToken.IS_API_TOKEN);
if (isApi != null && isApi) {
OpenAPI openAPI = methodSignature.getMethod().getAnnotation(OpenAPI.class);
if (openAPI == null) {
HttpServletRequest request =
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String url = request.getRequestURI();
throw new ApiAlertException("openapi unsupported: " + url);
}
}
return (RestResponse) joinPoint.proceed();
}

@Pointcut("@annotation(org.apache.streampark.console.core.annotation.AppUpdated)")
public void appUpdated() {
}

@Around("appUpdated()")
public Object appUpdated(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
log.debug("appUpdated aspect, method:{}", methodSignature.getName());
Object target = joinPoint.proceed();
flinkAppHttpWatcher.init();
return target;
}

@Pointcut("@annotation(org.apache.streampark.console.core.annotation.Permission)")
public void permissionAction() {
public void permissionPointcut() {
}

@Around("permissionAction()")
@Around("permissionPointcut()")
public RestResponse permissionAction(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Permission permission = methodSignature.getMethod().getAnnotation(Permission.class);
Expand Down
Loading
Loading