|
Springboot+Swagger+Oauth2 密码模式登录可以正常进入,但是请求接口的时候Header中没有token存在,接口返回401未授权。请问我应该怎么修改Swagger配置6 s: u3 @. T& ?6 X+ o5 n
以下是我的Swagger配置类/ ~: U1 k( R5 O$ h! \5 B M
# U6 Y5 ~+ h' I& J) q- @Bean' u4 P# D7 X+ W
- public Docket api() {
, Y: \& _5 a. e) l - return new Docket(DocumentationType.SWAGGER_2)
5 A" ^& d7 q5 I* f% V3 c! j - .enable(swaggerEnabled)
; x/ p7 I3 J6 g+ b - .select()
8 D2 J/ u' i2 J% _ - .apis(RequestHandlerSelectors.basePackage("com.polly.robot.web"))3 b0 R8 A- o7 g
- .paths(PathSelectors.any())
, O( H; I& }) f) ~' K - .build()
6 }2 E" d) C- B - .apiInfo(apiInfo())9 N- O* U4 U" G7 i! j3 y
- .securitySchemes(Collections.singletonList(securitySchemes()))3 B0 p2 r( Q( b8 Q! \
- .securityContexts(Collections.singletonList(securityContexts()));
, D) E. K! x1 O - }
7 k' |( g2 t5 d1 j' Z, O$ u6 ? - # O+ Y. H- W- Z+ X
- /**
2 Q" O/ {2 @) g4 O - * 这个方法主要是写一些文档的描述) L7 y. N% W9 y/ [& e
- */& T3 H' {7 e; n$ Q/ ~7 _" |0 Y
- private ApiInfo apiInfo() {1 f/ o- B$ T' @$ M* Z- N
- return new ApiInfo(7 r3 X1 \$ @8 U) L$ s
- title,
. |7 B5 }: _; Z& W1 g7 f" ~ - "",
: d7 Q1 Z' j) e) D$ v: l - "1.0.0",( h! H1 @* z9 ^( ` n: c8 E" t
- "",6 y& P' X* X! A8 O' t
- new Contact("", contactUrl, email),5 V! n i% W: t1 g& c
- "", "", Collections.emptyList());; k; X( S/ M" }
- }
; y* V: [6 J# _$ S7 S# ] - # j0 F% ^8 j& n3 H' {% V
- /**
8 e8 X; ^! D2 I7 `4 h5 J - * 认证方式使用密码模式
% S M' X3 J- {( P - */9 o- O9 d) |3 a
- private SecurityScheme securitySchemes() {6 r6 a% i5 A ?; x. ?9 C( g
- GrantType grantType = new ResourceOwnerPasswordCredentialsGrant(authServer);& d5 V1 x% U, D4 i: r9 D) `
0 K7 y+ H' Y1 s( L" i9 v \- return new OAuthBuilder()
! a( m( @, h! ?! Z - .name("assess_token"): k7 s9 E# n7 Y+ _- d5 P! `0 L3 O
- .grantTypes(Collections.singletonList(grantType))/ g0 i. \, t2 \
- .scopes(Arrays.asList(scopes()))$ }' D3 s) I6 ]& N
- .build();
8 }6 a, A+ A2 ~! O) S) L+ h; U - }' k; x5 i/ P0 Y5 A W
- " z. b. s9 [ n
- 1 l" N) |% l$ C
- /**
* b+ o$ C' D1 X* K - * 设置 swagger2 认证的安全上下文" c* e9 k6 Z7 K y; k% {( u1 c2 [; H& `
- */1 |" k" e1 [4 _/ E( `) `# U
- private SecurityContext securityContexts() {
9 Z) c. V/ T% S - return SecurityContext.builder()
6 X$ u/ [8 u' E* o6 X' G' ~! _ - .securityReferences(Collections.singletonList(new SecurityReference("assess_token", scopes())))
, g B9 L) E1 R" H - .forPaths(PathSelectors.any())
/ \/ ]4 O9 \" p" L* y - .build();# X; f D5 G" q( r" j
- }5 T: @6 H) m0 d" F
+ O6 T3 @6 W! @* T. p- R5 e1 }- /**" ^( v4 V- `2 r N7 G
- * 允许认证的scope5 {2 H0 `. [* R2 f7 e
- */, Y$ c: i; P7 G, c! E$ q
- private AuthorizationScope[] scopes() {' R+ q; s1 `, q. ~, W4 T) b. w
- AuthorizationScope authorizationScope = new AuthorizationScope("server", "接口测试");- l$ Q8 L& e" L# N
- AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
, g& c: E( H# l! y% }" D+ V+ i - authorizationScopes[0] = authorizationScope;+ ~2 `1 V6 r O8 a3 ~
- return authorizationScopes; }
- B1 N5 L6 m/ N Swagger引用:! Q" p2 B* l/ Y \) u W
" b: t7 n5 l1 b' y5 \/ m+ F8 W
- <dependency>: E4 s. r/ X6 o) M5 H0 S
- <groupId>io.springfox</groupId>$ n6 d0 z0 Z6 W) \3 L9 a6 K9 Z
- <artifactId>springfox-swagger2</artifactId>9 b$ N4 ~# J7 X( B) h0 y2 e' T
- <version>2.9.2</version>" ~/ W2 X7 X, K& t" b$ m
- </dependency>5 G0 a* x' @3 @0 q* A- M
- <dependency>
/ H% K: U$ j4 V4 }- J' m - <groupId>io.springfox</groupId>$ W- y. Q/ x8 Q9 v
- <artifactId>springfox-swagger-ui</artifactId>
0 a, j& G, b* C: I - <version>2.9.2</version></dependency>" i: J4 y6 H8 h% |! M
1 I8 @( K% c) Q' g z
8 ^4 }: E4 s* |2 V G8 D# ^/ l. z- {8 a$ W5 F) c
; W- u1 Y T l( I$ I; _+ }
我知道答案 回答被采纳将会获得5 金钱 已有0人回答 |
|