回答

收藏

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

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

Springboot+Swagger+Oauth2 密码模式登录可以正常进入,但是请求接口的时候Header中没有token存在,接口返回401未授权。请问我应该怎么修改Swagger配置7 X  |" \+ Z/ [$ J! H
以下是我的Swagger配置类% C* l- M1 a+ L4 G" j" u

    ! {7 K) R. @2 X$ Q8 O
  • @Bean% T2 |6 x' N/ a( `, r. W& q% d
  •     public Docket api() {
    3 C  ]4 d4 W! V2 E4 Z
  •         return new Docket(DocumentationType.SWAGGER_2)
    - g3 F8 s3 a) }$ C
  •                 .enable(swaggerEnabled)
    ; _. g" ]$ p: m# C( s, ^; w' D0 f
  •                 .select()) z  _% w0 W1 `9 D
  •                 .apis(RequestHandlerSelectors.basePackage("com.polly.robot.web"))7 H/ T' r- @8 ]2 s: E. a- T! w
  •                 .paths(PathSelectors.any())
    $ M7 s' o9 A& N9 K4 L3 o
  •                 .build()3 y3 ~% \- m* O$ Q+ x- P: w. C
  •                 .apiInfo(apiInfo())6 |6 G9 H$ }: g6 q* {, h
  •                 .securitySchemes(Collections.singletonList(securitySchemes()))- g' O% t8 N+ s  F7 ~! U
  •                 .securityContexts(Collections.singletonList(securityContexts()));
    * v* g* h( V1 ]( |
  •     }0 {3 A$ d# D1 ^$ _3 m; Y6 a2 B8 n

  • - t' A  y! {5 e  y, ]$ {" ]
  •     /**
    ( e+ N# G5 a; h8 F1 L0 L
  •      * 这个方法主要是写一些文档的描述8 @; ]! f+ b! A3 g7 x
  •      */
    * Y# F  [  C/ e2 b) N( }
  •     private ApiInfo apiInfo() {) R. a9 M; j# s8 @/ |1 C
  •         return new ApiInfo(: a) ]) I5 E7 a) y( m9 h/ i
  •                 title,1 F% Y8 X$ `3 t9 E% V" U
  •                 "",
    % Z, K& d1 Y' L! g, H3 b6 b
  •                 "1.0.0",5 p! C2 r- M6 N2 a% G' ^0 h8 d
  •                 "",
    8 B- @/ f4 f1 V/ R# D/ k
  •                 new Contact("", contactUrl, email),
    # y; S  s) D  P( f8 C' _; C& A! J+ V
  •                 "", "", Collections.emptyList());, w% X: E7 p$ }( g7 {# _
  •     }2 J: t! H& j, [4 {  Q6 Q9 j0 \, X
  • 5 Z9 |7 q: ]9 z& l
  •     /**
    & ~; c/ M4 ^, ]. D; p  k, U
  •      * 认证方式使用密码模式* \0 b) [: ]) _) x$ a6 m
  •      */
    0 u( J  f2 ]- Y' ?  u: U5 s
  •     private SecurityScheme securitySchemes() {; @2 n$ p1 S% p' G5 Z
  •         GrantType grantType = new ResourceOwnerPasswordCredentialsGrant(authServer);- Y* l- _" J* C6 a, j# M
  • " c2 B3 T5 X( t* s+ P
  •         return new OAuthBuilder()
    4 b$ }9 y* M* P/ m) J8 h& I
  •                 .name("assess_token")
    0 R6 k3 W0 p" m
  •                 .grantTypes(Collections.singletonList(grantType))
    : \5 z- ]; n: |0 E/ e$ l
  •                 .scopes(Arrays.asList(scopes()))
    4 F; y$ _6 S; P
  •                 .build();
    6 a( Y% a* o* u
  •     }
    3 ?7 o# l& R- Q* Y9 O8 |
  • # w7 f1 \  \; l

  • . M' G1 m0 L4 m1 Z
  •     /**% r. ^+ {: V0 _- f
  •      * 设置 swagger2 认证的安全上下文
    , ?! F6 I9 Q/ Q4 _
  •      */
    . ~3 Z+ Q, J6 V' d+ o: N5 F
  •     private SecurityContext securityContexts() {* ]; c! A9 S7 y2 u6 N' e! e' b- D
  •         return SecurityContext.builder()
    0 u. ^: O( y4 m/ f
  •                 .securityReferences(Collections.singletonList(new SecurityReference("assess_token", scopes())))( ?2 _, f2 Q. K/ o/ \& {7 U
  •                 .forPaths(PathSelectors.any())) n( e3 e8 I  g, u4 }+ P
  •                 .build();  Y6 S9 ^  N2 `7 V2 L, ]; C  Z# D
  •     }
    " u# V& G& W0 T% T; P  W5 m
  • 4 H* E0 V/ O! ?7 i* c( e) J
  •     /**( ^8 _: E- V7 \
  •      * 允许认证的scope' e; I: s+ \6 b& e( r
  •      */' }! R6 y* E& _  d& ^* Z
  •     private AuthorizationScope[] scopes() {& K: S1 ^; K7 T
  •         AuthorizationScope authorizationScope = new AuthorizationScope("server", "接口测试");! j( {7 e2 G% h
  •         AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
    9 L' K" K" o# I0 b
  •         authorizationScopes[0] = authorizationScope;
    2 ]( L# D7 X) t. E% W' S
  •         return authorizationScopes;    }
    ' O8 e' b0 S% O4 R1 [: K: I
Swagger引用:
6 u- w% }& m1 I& R5 ?
    : ?! C# k- q: }" G/ S  b
  • <dependency>  X  u7 [6 v9 R! q, X
  •     <groupId>io.springfox</groupId>
    % t% s* f6 Q5 p$ T* Y( H7 b0 M8 \
  •     <artifactId>springfox-swagger2</artifactId>9 w) o3 C$ D: u( r$ e
  •     <version>2.9.2</version>
    ' O$ Y6 z1 K. v
  • </dependency>
    1 c7 H. A) h& e( Q& I
  • <dependency>
    8 {/ R8 L3 a: c" F$ i$ |
  •     <groupId>io.springfox</groupId>
    & m; c# }! X, f8 b
  •     <artifactId>springfox-swagger-ui</artifactId>( q4 |* V0 m6 [5 H8 C$ N$ `
  •     <version>2.9.2</version></dependency># f  R, r% P6 ]5 T8 c; W6 Y
0 J7 C3 p. a1 F0 I; w

8 Q/ q' N" y& }" P1 w7 E% Y8 V, `5 w: m! z
/ \" r) d6 E( H3 t, M  `( N
我知道答案 回答被采纳将会获得5 金钱 已有0人回答
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则