|
Springboot+Swagger+Oauth2 密码模式登录可以正常进入,但是请求接口的时候Header中没有token存在,接口返回401未授权。请问我应该怎么修改Swagger配置
' m; {& b' ^, |( V以下是我的Swagger配置类, A' r) |; P* t, w7 B+ K6 l
6 V( N) K5 P, i1 J2 F; c. t. I- @Bean
c8 j3 `" B8 h1 A - public Docket api() {+ x8 ~$ A* g: v1 T4 E7 V! B+ G
- return new Docket(DocumentationType.SWAGGER_2)6 ]# v5 I; n* ^- P5 b& k- J
- .enable(swaggerEnabled)3 R" f% O- ]; G& M
- .select()3 g \! z, h: Q) H3 a. c0 R! k
- .apis(RequestHandlerSelectors.basePackage("com.polly.robot.web"))3 h( }+ \. F0 n' f: Z: X
- .paths(PathSelectors.any())8 B8 r+ M# N7 S+ T
- .build()
: f$ P4 {" d1 h% A3 `- s& Y# P: b - .apiInfo(apiInfo()) l2 I1 t! ^& K; V U, u. B
- .securitySchemes(Collections.singletonList(securitySchemes()))5 k' C2 M* v: S. I1 k; e
- .securityContexts(Collections.singletonList(securityContexts()));; G9 W0 t# X" W8 w0 ?
- }- U8 Q9 O! Q5 h& u% u9 C* e
! N3 b. o* t7 e( d$ \- /**: b; T3 `) }0 ^' c G4 q7 `# L
- * 这个方法主要是写一些文档的描述
. ?: e: a. z* Y - */, [4 Y$ B; q) u [
- private ApiInfo apiInfo() {1 {5 g1 l! _, H' t9 e: Q0 a& i
- return new ApiInfo(
1 D1 i& J7 s x. }8 M - title,
2 u4 F/ @. B4 L* l; z/ T - "",
/ H( M8 F* B$ ^0 b- r Z/ g- J - "1.0.0",
8 m$ ^! |' \& J) v2 o, O& X( V! c - "",! x( ]2 m }" \' e" s% L7 D* t) Z
- new Contact("", contactUrl, email),9 X. l& _* n8 R, W
- "", "", Collections.emptyList());* f2 n6 @; b0 D( ?
- }3 i3 R& `9 [7 y7 D% F6 R! |
. h6 K/ d3 @0 a' g+ T7 p- /**. m5 ? i! L1 l$ \
- * 认证方式使用密码模式
8 h; K7 {, \" t5 E0 t5 L - */8 z- b3 i" L& {& g
- private SecurityScheme securitySchemes() {
* p- ?( r4 H8 Z+ Z0 d, n# s - GrantType grantType = new ResourceOwnerPasswordCredentialsGrant(authServer);
+ m4 a) t. [6 y- U
! @6 c+ \- }- u2 ]% F! X; m- return new OAuthBuilder()
! b- a* z; o8 i9 Q4 }/ Q8 t* N; C - .name("assess_token")% t& @/ G, J0 C% n D9 B
- .grantTypes(Collections.singletonList(grantType))
* w5 p" } b0 ]/ ?& G - .scopes(Arrays.asList(scopes()))
7 V6 D1 Z' X$ Z7 v9 S - .build();; F- ]3 Y4 ?2 H1 X
- }
, b' ]1 ~5 p: x' _6 P j' N" n3 B
9 x4 a: _7 V9 z; J% n
0 S! ^; K2 @3 n- c* l; W- /**
( o' q( Y2 ?+ @ r& E- [9 H8 q - * 设置 swagger2 认证的安全上下文
+ m, X! x+ }) m% W - */
9 i1 {: H7 {- A( j - private SecurityContext securityContexts() {8 \! \6 E k& S+ n; _7 e9 c, V" H
- return SecurityContext.builder()
7 A1 O, f8 W% y0 q" Q2 W) k - .securityReferences(Collections.singletonList(new SecurityReference("assess_token", scopes())))
2 d: V _+ Q+ Z4 I+ u/ \ - .forPaths(PathSelectors.any())0 Y& M+ @3 w" ?
- .build();
! p2 k1 [0 i. B2 g8 [ - }
3 g3 ]/ {) b- f
8 J3 C" r) M* X7 e9 o% `5 \) }$ l1 q- /**/ K; s' S, y5 K2 N7 M
- * 允许认证的scope( D( c. L' B+ d) `: Z+ W
- */
0 @' Q; f6 q6 V! O+ Q! L% k - private AuthorizationScope[] scopes() {; [0 r$ z K) o' t6 e
- AuthorizationScope authorizationScope = new AuthorizationScope("server", "接口测试");
3 k9 U; Y. Z4 y. P - AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
2 `# m- \5 h( r7 B* J - authorizationScopes[0] = authorizationScope;: W$ M4 {3 o4 N7 M- S( N8 o
- return authorizationScopes; }
2 c' P; I- h; v+ a q Swagger引用:0 y7 n/ F8 F& k/ M0 h+ F! ^( U
2 C; M. x3 L) Q5 [* R
- <dependency>
; T0 A$ B) @, M - <groupId>io.springfox</groupId>
2 e0 h" Y5 `, c3 K4 {. b - <artifactId>springfox-swagger2</artifactId>
' S3 [: L) r' ~ - <version>2.9.2</version>
$ G1 R! E8 l& s5 M6 `0 s - </dependency>' H7 W, _; R; L' V9 _9 J$ [
- <dependency>
3 m! l3 j0 \$ \$ _+ @4 m - <groupId>io.springfox</groupId>9 m+ Q& b) G6 }
- <artifactId>springfox-swagger-ui</artifactId>3 k1 J8 |0 y% a0 [# Z
- <version>2.9.2</version></dependency>0 D0 A( v C1 a/ H
) J4 t0 r; U" m0 u0 V; ^& C9 {; @: c/ k$ S
2 {9 ^3 ?3 ]* `& P! C
( g$ g; d6 t( i# R j K% f
我知道答案 回答被采纳将会获得5 金钱 已有0人回答 |
|