服务内部调用api鉴权忽略token(动态放权)

通常情况下微服务之间调用或开放接口的时候,我们会在资源服务器的security配置当中配置路径,觉得比较麻烦,参考了网上一些案例,写了下面的应用,感觉比较实用的是可以不用谢那么多重复的代码permitAll()方法。灵活配置动态添加删除,只需要一个注解就可以配置接口是否完全开放,是否内部开放,是否外部开放。希望能帮助到有需要的人。

创建一个注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface IgnoreToken {
    /**
     * 是否AOP统一处理
     */
    boolean value() default true;
}

注解配置一个切面

@Aspect
@Component
@Slf4j
public class IgnoreTokenAspect implements Ordered {
    @Autowired
    private HttpServletRequest request;

    @Around("@annotation(ignoreToken)")
    public Object around(ProceedingJoinPoint point, IgnoreToken ignoreToken) throws Throwable {
        String header = request.getHeader(SecurityConstants.FROM);
        if (ignoreToken.value() && !StringUtils.equals(SecurityConstants.FROM_IN, header)){
            log.warn("访问接口 {} 没有权限", point.getSignature().getName());
            throw new AccessDeniedException("Access is denied");
        }
        return point.proceed();
    }

    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE + 1;
    }
}

常量配置

public final class SecurityConstants {
    public static final String FROM = "from";
    public static final String FROM_IN = "in";
}

初始化忽略token的链接

@Configuration
public class PermitAllUrlProperties implements InitializingBean {

    private static final Pattern PATTERN = Pattern.compile("\\\\{(.*?)\\\\}");

    @Autowired
    private ApplicationContext applicationContext;

    private List<String> urls = new ArrayList<>();

    public static final String ASTERISK = "*";

    @Override
    public void afterPropertiesSet() {
        RequestMappingHandlerMapping mapping = applicationContext.getBean("requestMappingHandlerMapping",RequestMappingHandlerMapping.class);
        Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();

        map.keySet().forEach(info -> {
            HandlerMethod handlerMethod = map.get(info);

            // 获取@IgnoreToke注解的替代path variable 为 *
            IgnoreToken method = AnnotationUtils.findAnnotation(handlerMethod.getMethod(), IgnoreToken.class);
            Optional.ofNullable(method).ifPresent(inner -> info.getPatternsCondition().getPatterns()
                    .forEach(url -> urls.add(ReUtil.replaceAll(url, PATTERN, ASTERISK))));

            // 获取@IgnoreToke注解的访问路径替代path variable 为 *
            IgnoreToken controller = AnnotationUtils.findAnnotation(handlerMethod.getBeanType(), IgnoreToken.class);
            Optional.ofNullable(controller).ifPresent(inner -> info.getPatternsCondition().getPatterns()
                    .forEach(url -> urls.add(ReUtil.replaceAll(url, PATTERN, ASTERISK))));
        });
    }

    public List<String> getUrls() {
        return urls;
    }

    public void setUrls(List<String> urls) {
        this.urls = urls;
    }
}

资源服务器配置

@Configuration
@EnableResourceServer
public class EcmContentResourceServerConfigure extends ResourceServerConfigurerAdapter {
    @Autowired
    PermitAllUrlProperties permitAllUrlProperties;

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.headers().frameOptions().disable();
        http.csrf().disable()
                .requestMatchers().antMatchers("/**")
                .and()
                .headers().frameOptions().disable()
                .and()
                .authorizeRequests()
                //添加注解动态添加放权
                .antMatchers(permitAllUrlProperties.getUrls().stream().distinct().toArray(String[]::new)).permitAll()
                .antMatchers("/**").authenticated();
    }
}

注解使用,直接添加到controller 方法上value = false表示可外部网关访问true表示内部接口外部无法访问。


    @GetMapping(value = "/getSite/{siteId}")
    @IgnoreToken(value = false)
    public EcmResponse getSite(@ApiParam(value = "siteid", required = true) @PathVariable("siteId") String siteId) {

        return siteServiceI.getSiteById(siteId);
    }

内部feign调佣需要请求头添加from参数

@GetMapping(value = "/public/lvanecm/v1/site/getSite/{siteId}") 
EcmResponse getSiteBySiteId(@PathVariable(value="siteId")String siteId,@RequestHeader(SecurityConstants.FROM) String from);

保证内部接口安全,网关拦截器全局处理,外部请求删除请求头中from参数

// 清洗请求头中from 参数
request = exchange.getRequest().mutate().headers(httpHeaders -> httpHeaders.remove(SecurityConstants.FROM)).build();
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值