You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.adoc 1.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. = spring-boot-demo-oauth-resource-server
  2. Doc Writer <lzy@echocow.cn>
  3. v1.0, 2019-01-09
  4. :toc:
  5. spring boot oauth2 资源服务器,同 授权服务器 一起使用。
  6. > 使用 `spring security oauth`
  7. - JWT 解密,远程公钥获取
  8. - 基于角色访问控制
  9. - 基于应用授权域访问控制
  10. == jwt 解密
  11. 要先获取 jwt 公钥
  12. [source,java]
  13. .OauthResourceTokenConfig
  14. ----
  15. public class OauthResourceTokenConfig {
  16. // ......
  17. private String getPubKey() {
  18. // 如果本地没有密钥,就从授权服务器中获取
  19. return StringUtils.isEmpty(resourceServerProperties.getJwt().getKeyValue())
  20. ? getKeyFromAuthorizationServer()
  21. : resourceServerProperties.getJwt().getKeyValue();
  22. }
  23. // ......
  24. }
  25. ----
  26. 然后配置进去
  27. [source, java]
  28. .OauthResourceServerConfig
  29. ----
  30. public class OauthResourceServerConfig extends ResourceServerConfigurerAdapter {
  31. @Override
  32. public void configure(ResourceServerSecurityConfigurer resources) {
  33. resources
  34. .tokenStore(tokenStore)
  35. .resourceId(resourceServerProperties.getResourceId());
  36. }
  37. }
  38. ----
  39. == 访问控制
  40. 通过 `@EnableGlobalMethodSecurity(prePostEnabled = true)` 注解开启 `spring security` 的全局方法安全控制
  41. - `@PreAuthorize("hasRole('ADMIN')")` 校验角色
  42. - `@PreAuthorize("#oauth2.hasScope('READ')")` 校验令牌授权域
  43. == 测试
  44. 测试用例: `com.xkcoding.oauth.controller.TestControllerTest`
  45. 先获取 `token`,携带 `token` 去访问资源即可。

一个用来深度学习并实战 spring boot 的项目,目前总共包含 66 个集成demo,已经完成 55 个。