feign授权调用

需要做的事

调用端携带Header信息
被调用端验证Header信息

调用端

feign配置类 FeignConfigure.java

@Configuration
public class FeignConfigure {
    @Bean
    public BasicAuthRequestInterceptor basicAuthRequestInterceptor(){
        return new BasicAuthRequestInterceptor("test"/*自定义用户名*/,"test"/*自定义密码*/);
    }
}

接口调用类

@FeignClient(contextId = "避免多个接口类重复注册需要指定不同的id 无实际意义", value = "调用服务名",configuration = FeignConfigure.class/*这里就是上面的配置类*/)
public interface IOpenData {
    @RequestMapping(value = "/open/last_data", method = RequestMethod.GET)
   String queryOpenData(@RequestParam("startDate") String startDate, @RequestParam("limit") Integer limit);
}

被调用端

拦截注解 FeignToken.java

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface FeignToken {
    boolean required() default true;
}

feign 拦截类

public class FeignAuthInterceptor implements HandlerInterceptor {

    /**
    * token生成在 BasicAuthRequestInterceptor 中
    *
    * this.headerValue = "Basic " + base64Encode((username + ":" + password).getBytes(charset));
    */

    private static final String CONST_FEIGN_TOKEN = "Basic dGVzdDp0ZXN0";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("authorization");
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }

        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
        if (method.isAnnotationPresent(FeignToken.class)) {
            FeignToken userToken = method.getAnnotation(FeignToken.class);
            if (userToken.required()) {
                // 执行认证
                if (token == null || !token.equals(CONST_FEIGN_TOKEN)) {
                    // TODO token错误
                }
                return true;
            } else {
                return true;
            }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
    }

}

启用拦截 InterceptorConfigure.java

@Configuration
public class InterceptorConfigure implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authenticationInterceptor())
                .addPathPatterns("/**");
    }

    @Bean
    public FeignAuthInterceptor authenticationInterceptor() {
        return new FeignAuthInterceptor();
    }
}

接口 OpenDataController.java

@RestController
@RequestMapping("open")
public class OpenDataController {

    @GetMapping("last_data")
    @FeignToken /*授权拦截 token*/
    public String lastData(String startDate,Integer limit){
        return "testController";
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值