回答

收藏

Springboot+Swagger+Oauth2 密码模式登录后,请求中无token5金钱.anslist{b

技术问答 技术问答 632 人阅读 | 0 人回复 | 2023-09-11

Springboot+Swagger+Oauth2 密码模式登录可以正常进入,但是请求接口的时候Header中没有token存在,接口返回401未授权。请问我应该怎么修改Swagger配置- X; P% L3 v6 ~5 X5 s
以下是我的Swagger配置类6 p( m, ^5 _: Z/ Q; }& `: p! |

    6 L  ~: d8 K, m, Q; K
  • @Bean1 ]" f8 g6 T' h+ H* w8 A! r
  •     public Docket api() {* j" ^& }& H( O$ t
  •         return new Docket(DocumentationType.SWAGGER_2)2 z( [2 \( q- N# Z* O
  •                 .enable(swaggerEnabled)6 T3 p- x- t+ }" Y1 H" @
  •                 .select()0 t9 T, c7 |% y  k+ r
  •                 .apis(RequestHandlerSelectors.basePackage("com.polly.robot.web"))( ~5 Y+ o' V& v
  •                 .paths(PathSelectors.any())
    / I, ^$ |! e; a5 F7 y& O
  •                 .build()! \9 D" z: }0 Z
  •                 .apiInfo(apiInfo()). g- r! ^& S2 H  O
  •                 .securitySchemes(Collections.singletonList(securitySchemes()))9 K8 G! o% G9 K
  •                 .securityContexts(Collections.singletonList(securityContexts()));
    : K( k' _7 X& L( C
  •     }
    ' j+ H: N3 N3 ~7 f2 X+ [8 B" Z& o, N
  • ; B) q6 m2 k) M5 D
  •     /**: Z6 r) a. D+ q
  •      * 这个方法主要是写一些文档的描述, u, ?* F; h& N1 D7 X' g; j
  •      */
    - i  ~& z3 i0 A* O
  •     private ApiInfo apiInfo() {
    0 `% _1 J' l; Z) d
  •         return new ApiInfo(
    ( y7 y- I5 S9 R+ O5 O1 ~
  •                 title,
    8 m, h* y: ~5 N4 I
  •                 "",
    0 u3 l6 e! _# q
  •                 "1.0.0",
    ) W- l) ]0 S3 c% k' _; J; B! V
  •                 "",8 a4 x; ?- ]/ @& z, X# L
  •                 new Contact("", contactUrl, email),
    ' n1 l5 L9 G% i
  •                 "", "", Collections.emptyList());/ X. T) s0 J: I& ?/ I/ P
  •     }* j/ D3 v5 Y0 H2 a# v3 u
  •   W1 B! W) Q0 n. E3 X! k. D8 O
  •     /**( c- ^& A: F; M# T' Q. t/ i- q
  •      * 认证方式使用密码模式2 w6 p" i# l- z$ L' o6 e; @& B. n5 F
  •      */
    9 B& q6 A, W. v) Z
  •     private SecurityScheme securitySchemes() {4 d5 R: |2 f+ x# V) R2 p5 S: _, L
  •         GrantType grantType = new ResourceOwnerPasswordCredentialsGrant(authServer);& A! Z/ x6 i* Y

  • , z4 _+ U$ }1 u
  •         return new OAuthBuilder()
    4 g9 C- _% y& b6 [+ n
  •                 .name("assess_token"). x7 ^! s+ C" \, h! F- f
  •                 .grantTypes(Collections.singletonList(grantType))
      r- s9 d4 c2 t  z
  •                 .scopes(Arrays.asList(scopes()))/ ]" O  j" T6 F/ b
  •                 .build();: g% e7 Y* l2 V, ]) x( ~
  •     }# h# D$ }( B- m" w3 E3 N+ ^9 g/ y4 _

  • : g7 |" t- D* e! g! L
  • * L0 D; s. D. w, c- u
  •     /**
    3 h- U  e2 ~8 T6 ^$ v
  •      * 设置 swagger2 认证的安全上下文5 m7 p: U5 j9 r4 c
  •      */+ Z" a, a- {+ \' F1 K
  •     private SecurityContext securityContexts() {2 D; I! j. _2 Q$ ?% n' \
  •         return SecurityContext.builder()
    : w0 T1 i" c) Q7 X- v% q% x
  •                 .securityReferences(Collections.singletonList(new SecurityReference("assess_token", scopes())))
      L# d" f  g8 V, J* D
  •                 .forPaths(PathSelectors.any())
    , `! q  @! v; j; [
  •                 .build();3 n0 [4 b- G# t# [& j+ n
  •     }) C) V) u1 u) v- w. K8 ?) J1 D! L

  • . x3 v+ b7 i2 c* Z* |/ A
  •     /**
      r5 t* K" O5 p- l% i
  •      * 允许认证的scope
    / a3 w9 B! W5 I# ]6 ~
  •      */
    7 u+ E+ _- P( ]9 N4 b
  •     private AuthorizationScope[] scopes() {1 F$ v1 w+ q: A( y# a, K; E& \
  •         AuthorizationScope authorizationScope = new AuthorizationScope("server", "接口测试");  Y. p; ^: c( C% z
  •         AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
    + `; Q- m  u: z7 s" X- j
  •         authorizationScopes[0] = authorizationScope;
    + k- Y0 r- z- }3 J* b! `
  •         return authorizationScopes;    }. F) E5 o2 D( L  r4 M) J- h" ]" f/ x
Swagger引用:
- M4 J. w4 `; K7 n7 ^0 ^
    + |, ?/ A! D, B+ a' O
  • <dependency>
    " z* }# a6 m( K, l8 c
  •     <groupId>io.springfox</groupId>: c: v0 e# _* p* U* j
  •     <artifactId>springfox-swagger2</artifactId>, _6 \# g6 Z1 {
  •     <version>2.9.2</version># U  s( C+ {* t8 {$ N2 n
  • </dependency>, A- g* V: ^% w2 G. X
  • <dependency>
    / N# C) f# J) C- g# L
  •     <groupId>io.springfox</groupId>8 M7 i1 Y0 q+ w% Y* Y7 a. B
  •     <artifactId>springfox-swagger-ui</artifactId>
    ' E& ]- |3 d' j; _4 p2 G. _
  •     <version>2.9.2</version></dependency>3 m& s( u7 A, p2 K' |/ N
* \2 i  G. H# ^4 K2 t1 ^

: ~8 M) A; p, U" O# N/ T
7 ]! U+ I0 s: @- u! [
# i: [4 U/ Q' c9 z9 I! p
我知道答案 回答被采纳将会获得5 金钱 已有0人回答
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则