|
| 1 | +# 示例说明 |
| 2 | +所有示例的授权模式都为授权码模式(authorization code) |
| 3 | + |
| 4 | +## JDBC模式的token存储 |
| 5 | +先执行sql语句建表 `\oauth2-server\src\main\resources\oauth2.sql`,该结构为Spring官方提供,也可以自己进行改造. |
| 6 | +注册客户端SQL |
| 7 | +INSERT INTO `oauth2`.`oauth_client_details` |
| 8 | +(`client_id`, |
| 9 | +`resource_ids`, |
| 10 | +`client_secret`, |
| 11 | +`scope`, |
| 12 | +`authorized_grant_types`, |
| 13 | +`web_server_redirect_uri`, |
| 14 | +`authorities`, |
| 15 | +`access_token_validity`, |
| 16 | +`refresh_token_validity`, |
| 17 | +`additional_information`, |
| 18 | +`autoapprove`) |
| 19 | +VALUES |
| 20 | +( |
| 21 | +'myClient', -- 客户端id |
| 22 | +'oauth2-resource', -- 资源id,可随意填 |
| 23 | +'$2a$10$cNkm8sp0MojCcxcblqEeJORPD5PFgkT9KYZAq5hv1uSyT5ifoKfLm', -- 客户端密码 |
| 24 | +'read,write,trust', -- scope授权范围,这里可以自定义字符串,多个以逗号隔开 |
| 25 | +'password,authorization_code,refresh_token,implicit,client_credentials', -- 授权模式: 密码模式,授权码模式,刷新token,简化模式,客户端模式 |
| 26 | +'', |
| 27 | +'ROLE_CLIENT,ROLE_TRUSTED_CLIENT', -- 角色,随便填 |
| 28 | +'3600', -- access_token有效时间,单位:秒 |
| 29 | +NULL, |
| 30 | +'{}', |
| 31 | +''); |
| 32 | + |
| 33 | +注意: 客户端密码是经过了BCryptPasswordEncoder()编码,所以先将密码编码完再INSERT |
| 34 | +`String encodePassword = new BCryptPasswordEncoder().encode("你真实密码")` |
| 35 | + |
| 36 | +注册用户SQL |
| 37 | +INSERT INTO `oauth2`.`users` (`username`, `password`, `enabled`) VALUES ('dave', 'secret', '1'); |
| 38 | + |
| 39 | +`oauth2-server`: 认证server |
| 40 | +`oauth2-resource`: 资源server (一般与认证server在一起,为演示单独独立出来) |
| 41 | +`oauth2-client`: 客户端,通过 `RestTemplate` 访问 `oauth2-server` 和 `oauth2-resource` |
| 42 | + |
| 43 | +### 启动与演示 |
| 44 | +启动server和client,访问 `http://localhost:9081/client/get` |
| 45 | +此时页面跳转至 `http://localhost:9080/login`,输入注册的用户名和密码,通过认证后即可访问到认证服务器上的资源. |
| 46 | + |
| 47 | +## 内存模式的token存储 |
| 48 | +无需建表和配置(适合开发环境) |
| 49 | +`demo-oauth2-server`: 认证server,资源server |
| 50 | +`demo-oauth2-client`: 客户端,加入权限认证,`WebSecurityConfiguation`中配置的受权限 |
| 51 | +控制的url被访问时,先跳转到认证服务器登录页面,登陆后,拿到授权码,再利用授权码拿到access_token,再利用 |
| 52 | +access_token访问资源地址 |
| 53 | + |
| 54 | +### 启动与演示 |
| 55 | +启动server和client,访问 `http://localhost:8082/ui/` |
| 56 | +此时页面跳转至 `http://localhost:8081/auth/login`,输入注册的用户名:john和密码:123,通过认证后返回用户信息. |
| 57 | + |
| 58 | +## GITHUB客户端 |
| 59 | +`demo-oauth2-githubclient` github oauth2 客户端 |
| 60 | +首先需要有github账号,没有的可以花2分钟去注册一个,登录github, 在 `https://github.com/settings/developers` 界面注册 OAuth App |
| 61 | +配置Homepage URL : http://localhost:8090 |
| 62 | +配置Authorization callback URL: http://localhost:8090/login/github |
| 63 | +注意此处配置的/login/github需要在filter中进行配置 |
| 64 | +```java |
| 65 | +private Filter sso() { |
| 66 | + OAuth2ClientAuthenticationProcessingFilter githubFilter = new OAuth2ClientAuthenticationProcessingFilter("/login/github"); |
| 67 | + OAuth2RestTemplate githubTemplate = new OAuth2RestTemplate(github(), oauth2ClientContext); |
| 68 | + githubFilter.setRestTemplate(githubTemplate); |
| 69 | + githubFilter.setTokenServices(new UserInfoTokenServices(githubResource().getUserInfoUri(), github().getClientId())); |
| 70 | + return githubFilter; |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +### 启动与演示 |
| 75 | +启动项目,访问 `http://localhost:8090/user/info`,此时页面跳转至 `http://localhost:8090/login` |
| 76 | +点击最下面的 `以GITHUB账户登录` 转至GITHUB授权登录页面,输入GITHUB用户密码,认证成功后重定向至 `http://localhost:8090/user/info` |
| 77 | +同时拿到了GITHUB的用户名称等信息 |
0 commit comments