中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁(yè) > news >正文

seo營(yíng)銷(xiāo)網(wǎng)站的設(shè)計(jì)標(biāo)準(zhǔn)百度快照優(yōu)化

seo營(yíng)銷(xiāo)網(wǎng)站的設(shè)計(jì)標(biāo)準(zhǔn),百度快照優(yōu)化,wordpress網(wǎng)站關(guān)鍵詞,長(zhǎng)寧廣州網(wǎng)站建設(shè)GraphQL Directive(指令)是GraphQL中的一種特殊類(lèi)型,它允許開(kāi)發(fā)者在GraphQL schema中添加元數(shù)據(jù),以控制查詢和解析操作的行為 Directive的詳細(xì)說(shuō)明及使用可見(jiàn)GraphQL(五)指令[Directive]詳解 本文將介紹通過(guò)…

GraphQL Directive(指令)是GraphQL中的一種特殊類(lèi)型,它允許開(kāi)發(fā)者在GraphQL schema中添加元數(shù)據(jù),以控制查詢和解析操作的行為

Directive的詳細(xì)說(shuō)明及使用可見(jiàn)GraphQL(五)指令[Directive]詳解

本文將介紹通過(guò)自定義Directive實(shí)現(xiàn)的GraphQL登錄態(tài)校驗(yàn),步驟依次為:

  1. Schema中定義directive
  2. 實(shí)現(xiàn)DgsReactiveCustomContextBuilderWithRequest接口,構(gòu)建請(qǐng)求內(nèi)全局使用的上下文對(duì)象
  3. 實(shí)現(xiàn)SchemaDirectiveWiring,對(duì)Field進(jìn)行攔截校驗(yàn)
  4. Directive注入

Schema Directive定義

"必須要登錄"
directive @needLogin on FIELD_DEFINITIONtype Employee {"雇員名稱"employees(month: Date : [String] @needLogin
}

LoginContextBuilder

實(shí)現(xiàn)DgsReactiveCustomContextBuilderWithRequest接口,可以使用當(dāng)前的請(qǐng)求信息,例如 HTTP 請(qǐng)求頭、請(qǐng)求參數(shù)等構(gòu)建自定義的上下文對(duì)象

在查詢執(zhí)行過(guò)程中,GraphQL 會(huì)將該上下文對(duì)象傳遞給所有的數(shù)據(jù)解析器(DataFetcher),使得數(shù)據(jù)解析器能夠訪問(wèn)和修改上下文對(duì)象中的數(shù)據(jù)

使用DgsReactiveCustomContextBuilderWithRequest接口可以實(shí)現(xiàn)許多有用的功能,例如:

  • 存儲(chǔ)用戶身份驗(yàn)證信息,以便在數(shù)據(jù)解析器中進(jìn)行鑒權(quán)
  • 將請(qǐng)求相關(guān)的信息(例如請(qǐng)求參數(shù)、請(qǐng)求頭等)傳遞給數(shù)據(jù)解析器,以便數(shù)據(jù)解析器根據(jù)這些信息返回正確的數(shù)據(jù)
  • 存儲(chǔ)請(qǐng)求相關(guān)的上下文信息,例如請(qǐng)求開(kāi)始時(shí)間、請(qǐng)求結(jié)束時(shí)間等,以便進(jìn)行性能分析和監(jiān)控
@Component
public class LoginContextBuilder implements DgsReactiveCustomContextBuilderWithRequest<YyContext> {private static final Logger LOGGER = LoggerFactory.getLogger(LoginContextBuilder.class);@Autowiredprivate ReactiveLoginService reactiveLoginService;@Autowiredprivate HttpHeaderAuthorization httpHeaderAuthorization;@NotNull@Overridepublic Mono<LoginContext> build(@Nullable Map<String, ?> map, @Nullable HttpHeaders httpHeaders, @Nullable ServerRequest serverRequest) {boolean interiorAuth = httpHeaderAuthorization.auth(serverRequest);if(interiorAuth){LoginContext loginContext = new LoginContext(true, IpUtil.getClientIpAddress(serverRequest)).setInteriorAuth(true);LOGGER.info("interior request loginContext:{}",loginContext);return Mono.just(loginContext);}else{return reactiveLoginService.getUid(serverRequest).doOnSuccess(user-> LOGGER.info("login user:{}",user)).onErrorResume(e-> Mono.just(LoginUser.NOT_LOGIN_USER)).map(user -> new LoginContext(user, IpUtil.getClientIpAddress(serverRequest)));}}
}

ReactiveLoginService

subscribeOn(Schedulers.boundedElastic())將該Mono訂閱到一個(gè)boundedElastic調(diào)度器的線程中,這樣Mono中的操作就會(huì)在該線程上執(zhí)行,而不會(huì)阻塞當(dāng)前的線程

Schedulers.boundedElastic()調(diào)度器是一個(gè)彈性線程池,它根據(jù)需要?jiǎng)討B(tài)地創(chuàng)建和銷(xiāo)毀線程,以適應(yīng)不同的負(fù)載情況

public class ReactiveLoginService {private LoginService loginService;public ReactiveLoginService(LoginService loginService){this.loginService = loginService;}/*** 登錄校驗(yàn)* @param httpServerRequest* @return*/public Mono<LoginUser> getUid(ServerRequest httpServerRequest){return Mono.fromCallable(()->{long uid = loginService.login(new WebFluxHttpServletRequest(httpServerRequest),null);return uid > 0 ? new LoginUser(uid) : LoginUser.NOT_LOGIN_USER;}).subscribeOn(Schedulers.boundedElastic());}public void close(){loginService.close();}
}

Directive 實(shí)現(xiàn)

@Component
public class NeedLoginDirective implements SchemaDirectiveWiring {private static final Logger LOGGER = LoggerFactory.getLogger(NeedLoginDirective.class);public static final String NEED_LOGIN_DIRECTIVE = "needLogin";public NeedLoginDirective() {}@Overridepublic GraphQLFieldDefinition onField(SchemaDirectiveWiringEnvironment<GraphQLFieldDefinition> environment) {GraphQLFieldDefinition field = environment.getElement();GraphQLFieldsContainer parentType = environment.getFieldsContainer();// 原始DataFetcher,無(wú)需修改參數(shù)值時(shí),最后需返回原始DataFetcher的值DataFetcher originalDataFetcher = environment.getCodeRegistry().getDataFetcher(parentType, field);if (field.getDirective(NEED_LOGIN_DIRECTIVE) == null) {return field;}LOGGER.info("onField field:{} needLogin.", field);DataFetcher needLoginDataFetcher = dfe -> {LoginContext loginContext = DgsContext.getCustomContext(dfe);if (loginContext.getLoginUser().hasLogin()) {return originalDataFetcher.get(dfe);} else {LOGGER.info("not login return null");throw new NeedLoginRuntimeException();}};environment.getCodeRegistry().dataFetcher(parentType, field, needLoginDataFetcher);return field;}
}

Directive 注入

GraphQLSchemaConfiguration

@Configuration
public class GraphQLSchemaConfiguration {@DgsComponentpublic class SecuredDirectiveRegistration {private NeedLoginDirective needLoginDirective;public SecuredDirectiveRegistration(NeedLoginDirective needLoginDirective) {this.needLoginDirective = needLoginDirective;}@DgsRuntimeWiringpublic RuntimeWiring.Builder addSecuredDirective(RuntimeWiring.Builder builder) {return builder.directive(NeedLoginDirective.NEED_LOGIN_DIRECTIVE,needLoginDirective);}}
}

參考資料:

  1. GraphQL(五)指令[Directive]詳解
  2. Derectives 原理
http://www.risenshineclean.com/news/64900.html

相關(guān)文章:

  • 有什么專(zhuān)門(mén)搜試卷做的網(wǎng)站app推廣平臺(tái)排行榜
  • 重慶裝修貸廣州seo服務(wù)外包
  • 廣州天河區(qū)網(wǎng)站建設(shè)搜索引擎推廣有哪些
  • 移動(dòng)端是指手機(jī)還是電腦優(yōu)化大師如何刪掉多余的學(xué)生
  • 安卓市場(chǎng)2022最新版下載河南網(wǎng)站關(guān)鍵詞優(yōu)化
  • 蘇州網(wǎng)站建設(shè)選蘇州夢(mèng)易行百度網(wǎng)游排行榜
  • 余姚網(wǎng)站制作軟文營(yíng)銷(xiāo)是什么意思
  • 俄文網(wǎng)站策劃搜索引擎都有哪些
  • 燕郊做網(wǎng)站的外貿(mào)網(wǎng)站建設(shè) google
  • 淘客軟件自動(dòng)做網(wǎng)站百度網(wǎng)址大全舊版
  • 網(wǎng)站界面用什么軟件做百度云電腦版網(wǎng)站入口
  • 適合做外鏈的網(wǎng)站互聯(lián)網(wǎng)平臺(tái)
  • 社區(qū)問(wèn)答網(wǎng)站開(kāi)發(fā)谷歌推廣開(kāi)戶
  • 杭州網(wǎng)站建設(shè)杭州磁力引擎
  • 百度網(wǎng)站快速排名公司重慶seo網(wǎng)絡(luò)推廣
  • 佛山市城市建設(shè)檔案館網(wǎng)站競(jìng)猜世界杯
  • 深圳網(wǎng)站建設(shè)html5惠州seo怎么做
  • 做外貿(mào)收費(fèi)的網(wǎng)站seo交流論壇
  • 買(mǎi)公司的網(wǎng)站建設(shè)北京seo顧問(wèn)外包
  • 盤(pán)古建站模板seo研究中心論壇
  • 河南官網(wǎng)網(wǎng)站建設(shè)廣告語(yǔ)
  • 互動(dòng)網(wǎng)站設(shè)計(jì)與制作提供seo顧問(wèn)服務(wù)適合的對(duì)象是
  • 上海裝修公司做網(wǎng)站seo日常工作
  • 小網(wǎng)站建設(shè)360搜索引擎
  • 西藏做網(wǎng)站找誰(shuí)網(wǎng)址關(guān)鍵詞查詢網(wǎng)站
  • 一諾建站廣東省人大常委會(huì)
  • 自貢做網(wǎng)站的公司百度快速收錄賬號(hào)購(gòu)買(mǎi)
  • ftp網(wǎng)站目錄深圳關(guān)鍵詞優(yōu)化公司哪家好
  • 嘉興南湖區(qū)優(yōu)秀營(yíng)銷(xiāo)型網(wǎng)站建設(shè)關(guān)鍵詞優(yōu)化計(jì)劃
  • 政府網(wǎng)站建設(shè)依據(jù)怎么做網(wǎng)站宣傳