19 changed files with 369 additions and 1 deletions
			
			
		@ -0,0 +1,33 @@ | 
				
			|||
/** | 
				
			|||
 * Copyright 2018 人人开源 http://www.renren.io
 | 
				
			|||
 * <p> | 
				
			|||
 * Licensed 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 | 
				
			|||
 * <p> | 
				
			|||
 * http://www.apache.org/licenses/LICENSE-2.0
 | 
				
			|||
 * <p> | 
				
			|||
 * 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 com.epmet.commons.tools.annotation; | 
				
			|||
 | 
				
			|||
import com.epmet.commons.tools.enums.RequirePermissionEnum; | 
				
			|||
 | 
				
			|||
import java.lang.annotation.*; | 
				
			|||
 | 
				
			|||
/** | 
				
			|||
 * 运营端-权限注解 | 
				
			|||
 * @Author wxz | 
				
			|||
 * @Description | 
				
			|||
 * @Date 2022/09/27 16:17 | 
				
			|||
 **/ | 
				
			|||
@Target(ElementType.METHOD) | 
				
			|||
@Retention(RetentionPolicy.RUNTIME) | 
				
			|||
@Documented | 
				
			|||
public @interface OperRequiredPermission { | 
				
			|||
} | 
				
			|||
@ -0,0 +1,56 @@ | 
				
			|||
package com.epmet.commons.tools.aspect; | 
				
			|||
 | 
				
			|||
import com.epmet.commons.tools.dto.form.HasOperPermissionFormDTO; | 
				
			|||
import com.epmet.commons.tools.exception.EpmetErrorCode; | 
				
			|||
import com.epmet.commons.tools.exception.EpmetException; | 
				
			|||
import com.epmet.commons.tools.feign.CommonOperAccessOpenFeignClient; | 
				
			|||
import com.epmet.commons.tools.utils.Result; | 
				
			|||
import org.aspectj.lang.JoinPoint; | 
				
			|||
import org.aspectj.lang.annotation.Aspect; | 
				
			|||
import org.aspectj.lang.annotation.Before; | 
				
			|||
import org.aspectj.lang.reflect.MethodSignature; | 
				
			|||
import org.springframework.beans.factory.annotation.Autowired; | 
				
			|||
import org.springframework.core.annotation.Order; | 
				
			|||
import org.springframework.stereotype.Component; | 
				
			|||
import org.springframework.web.context.request.RequestAttributes; | 
				
			|||
import org.springframework.web.context.request.RequestContextHolder; | 
				
			|||
import org.springframework.web.context.request.ServletRequestAttributes; | 
				
			|||
 | 
				
			|||
import javax.servlet.http.HttpServletRequest; | 
				
			|||
 | 
				
			|||
@Aspect | 
				
			|||
@Component | 
				
			|||
@Order(30) | 
				
			|||
public class OperRequiredPermissionAspect { | 
				
			|||
 | 
				
			|||
    @Autowired | 
				
			|||
    private CommonOperAccessOpenFeignClient operAccessOpenFeignClient; | 
				
			|||
 | 
				
			|||
    @Before("@annotation(com.epmet.commons.tools.annotation.OperRequiredPermission)") | 
				
			|||
    public void proceed(JoinPoint pjp) throws Throwable { | 
				
			|||
//        MethodSignature signature = (MethodSignature) pjp.getSignature();
 | 
				
			|||
        HttpServletRequest request = getRequest(); | 
				
			|||
 | 
				
			|||
        String url = request.getRequestURI().toString(); | 
				
			|||
        String method = request.getMethod(); | 
				
			|||
 | 
				
			|||
        HasOperPermissionFormDTO form = new HasOperPermissionFormDTO(); | 
				
			|||
        form.setUri(url); | 
				
			|||
        form.setMethod(method); | 
				
			|||
        Result result = operAccessOpenFeignClient.hasOperPermission(form); | 
				
			|||
        if (result == null || !result.success()) { | 
				
			|||
            throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), "资源未授权", "资源未授权"); | 
				
			|||
        } | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * 获取Request对象 | 
				
			|||
     * | 
				
			|||
     * @return | 
				
			|||
     */ | 
				
			|||
    private HttpServletRequest getRequest() { | 
				
			|||
        RequestAttributes ra = RequestContextHolder.getRequestAttributes(); | 
				
			|||
        ServletRequestAttributes sra = (ServletRequestAttributes) ra; | 
				
			|||
        return sra.getRequest(); | 
				
			|||
    } | 
				
			|||
} | 
				
			|||
@ -0,0 +1,22 @@ | 
				
			|||
package com.epmet.commons.tools.dto.form; | 
				
			|||
 | 
				
			|||
import lombok.Data; | 
				
			|||
 | 
				
			|||
import javax.validation.constraints.NotBlank; | 
				
			|||
 | 
				
			|||
@Data | 
				
			|||
public class HasOperPermissionFormDTO { | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * uri | 
				
			|||
     */ | 
				
			|||
    @NotBlank(message = "uri不能为空") | 
				
			|||
    private String uri; | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * http方法 | 
				
			|||
     */ | 
				
			|||
    @NotBlank(message = "请求http方法不能为空") | 
				
			|||
    private String method; | 
				
			|||
 | 
				
			|||
} | 
				
			|||
@ -0,0 +1,13 @@ | 
				
			|||
package com.epmet.commons.tools.dto.result; | 
				
			|||
 | 
				
			|||
import lombok.Data; | 
				
			|||
 | 
				
			|||
@Data | 
				
			|||
public class OperResouce { | 
				
			|||
 | 
				
			|||
    private String userId; | 
				
			|||
    private String resourceUrl; | 
				
			|||
    private String ResourceMethod; | 
				
			|||
 | 
				
			|||
 | 
				
			|||
} | 
				
			|||
@ -0,0 +1,36 @@ | 
				
			|||
package com.epmet.commons.tools.feign; | 
				
			|||
 | 
				
			|||
import com.epmet.commons.tools.constant.ServiceConstant; | 
				
			|||
import com.epmet.commons.tools.dto.form.HasOperPermissionFormDTO; | 
				
			|||
import com.epmet.commons.tools.feign.fallback.CommonOperAccessOpenFeignClientFallbackFactory; | 
				
			|||
import com.epmet.commons.tools.utils.Result; | 
				
			|||
import org.springframework.cloud.openfeign.FeignClient; | 
				
			|||
import org.springframework.web.bind.annotation.GetMapping; | 
				
			|||
import org.springframework.web.bind.annotation.PostMapping; | 
				
			|||
import org.springframework.web.bind.annotation.RequestBody; | 
				
			|||
 | 
				
			|||
/** | 
				
			|||
 * @Description 运营端权限模块 | 
				
			|||
 * @Author yinzuomei | 
				
			|||
 * @Date 2020/5/21 15:17 本服务对外开放的API,其他服务通过引用此client调用该服务 | 
				
			|||
 */ | 
				
			|||
// , url = "http://localhost:8093"
 | 
				
			|||
@FeignClient(name = ServiceConstant.OPER_ACCESS_SERVER, fallbackFactory = CommonOperAccessOpenFeignClientFallbackFactory.class) | 
				
			|||
public interface CommonOperAccessOpenFeignClient { | 
				
			|||
    /** | 
				
			|||
     * @param | 
				
			|||
     * @return com.epmet.commons.tools.utils.Result | 
				
			|||
     * @Author yinzuomei | 
				
			|||
     * @Description 清空运营人员权限信息、菜单信息 | 
				
			|||
     * @Date 2020/5/21 17:08 | 
				
			|||
     **/ | 
				
			|||
    @GetMapping("/oper/access/menu/clearoperuseraccess") | 
				
			|||
    Result clearOperUserAccess(); | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * 是否有该接口的权限 | 
				
			|||
     * @return | 
				
			|||
     */ | 
				
			|||
    @PostMapping("/oper/access/menu/hasPermission") | 
				
			|||
    Result hasOperPermission(@RequestBody HasOperPermissionFormDTO form); | 
				
			|||
} | 
				
			|||
@ -0,0 +1,27 @@ | 
				
			|||
package com.epmet.commons.tools.feign.fallback; | 
				
			|||
 | 
				
			|||
import com.epmet.commons.tools.constant.ServiceConstant; | 
				
			|||
import com.epmet.commons.tools.dto.form.HasOperPermissionFormDTO; | 
				
			|||
import com.epmet.commons.tools.feign.CommonOperAccessOpenFeignClient; | 
				
			|||
import com.epmet.commons.tools.utils.ModuleUtils; | 
				
			|||
import com.epmet.commons.tools.utils.Result; | 
				
			|||
 | 
				
			|||
/** | 
				
			|||
 * @Description 运营端权限模块 | 
				
			|||
 * @Author yinzuomei | 
				
			|||
 * @Date 2020/5/21 15:47 | 
				
			|||
 */ | 
				
			|||
//@Component
 | 
				
			|||
public class CommonOperAccessOpenFeignClientFallback implements CommonOperAccessOpenFeignClient { | 
				
			|||
    @Override | 
				
			|||
    public Result clearOperUserAccess() { | 
				
			|||
        return ModuleUtils.feignConError(ServiceConstant.OPER_ACCESS_SERVER, "clearOperUserAccess"); | 
				
			|||
 | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    @Override | 
				
			|||
    public Result hasOperPermission(HasOperPermissionFormDTO form) { | 
				
			|||
        return ModuleUtils.feignConError(ServiceConstant.OPER_ACCESS_SERVER, "hasOperPermission"); | 
				
			|||
    } | 
				
			|||
} | 
				
			|||
 | 
				
			|||
@ -0,0 +1,19 @@ | 
				
			|||
package com.epmet.commons.tools.feign.fallback; | 
				
			|||
 | 
				
			|||
import com.epmet.commons.tools.exception.ExceptionUtils; | 
				
			|||
import com.epmet.commons.tools.feign.CommonOperAccessOpenFeignClient; | 
				
			|||
import feign.hystrix.FallbackFactory; | 
				
			|||
import lombok.extern.slf4j.Slf4j; | 
				
			|||
import org.springframework.stereotype.Component; | 
				
			|||
 | 
				
			|||
@Component | 
				
			|||
@Slf4j | 
				
			|||
public class CommonOperAccessOpenFeignClientFallbackFactory implements FallbackFactory<CommonOperAccessOpenFeignClient> { | 
				
			|||
    private CommonOperAccessOpenFeignClientFallback fallback = new CommonOperAccessOpenFeignClientFallback(); | 
				
			|||
 | 
				
			|||
    @Override | 
				
			|||
    public CommonOperAccessOpenFeignClient create(Throwable cause) { | 
				
			|||
        log.error(String.format("FeignClient调用发生异常,异常信息:%s", ExceptionUtils.getThrowableErrorStackTrace(cause))); | 
				
			|||
        return fallback; | 
				
			|||
    } | 
				
			|||
} | 
				
			|||
@ -0,0 +1,22 @@ | 
				
			|||
package com.epmet.dto.form; | 
				
			|||
 | 
				
			|||
import lombok.Data; | 
				
			|||
 | 
				
			|||
import javax.validation.constraints.NotBlank; | 
				
			|||
 | 
				
			|||
@Data | 
				
			|||
public class HasOperPermissionFormDTO { | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * uri | 
				
			|||
     */ | 
				
			|||
    @NotBlank(message = "uri不能为空") | 
				
			|||
    private String uri; | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * http方法 | 
				
			|||
     */ | 
				
			|||
    @NotBlank(message = "请求http方法不能为空") | 
				
			|||
    private String method; | 
				
			|||
 | 
				
			|||
} | 
				
			|||
@ -0,0 +1,13 @@ | 
				
			|||
package com.epmet.dto.result; | 
				
			|||
 | 
				
			|||
import lombok.Data; | 
				
			|||
 | 
				
			|||
@Data | 
				
			|||
public class OperResouce { | 
				
			|||
 | 
				
			|||
    private String userId; | 
				
			|||
    private String resourceUrl; | 
				
			|||
    private String ResourceMethod; | 
				
			|||
 | 
				
			|||
 | 
				
			|||
} | 
				
			|||
					Loading…
					
					
				
		Reference in new issue