oauth2及时从一个项目A申请另外一个项目B的访问的时候,不用在项目A输入项目B的用户名和密码,我的理解先跳转到项目B,利用项目B的用户名和密码获得一个code之类的,这里有点像openID,不过不是的,这是用户某次受权的,不是用户惟一标识一致能够受权,作了2个关于oauth2的demo,这里记录一下git
用户从A访问B的地址,例如:localhost:8080/oauth/authorize?client_id=client&response_type=code&redirect_uri=http://www.baidu.com,而后须要输入spring security的用户名和密码,选择Approve,这里有点像,王者荣耀确认获取微信信息的界面github
利用B的用户名和密码获得code,而后访问 http://client:secret@localhost:8080/oauth/token?grant_type=authorization_code&code=Li4NZo&redirect_uri=http://www.baidu.com获得access_token,有了access_token了就能够访问项目B了,到时候把access_token做为参数带过去了就行了就能够访问了,这样咱们就算不知道项目B的用户名和密码也能够访问服务B的资源了spring
这里也能够直接访问 http://client:secret@localhost:8080/oauth/token的地址直接获得access_token,例如:http://localhost:8080/oauth/token?grant_type=password&username=user&password=123&scope=settingw,不过这样就须要项目A有项目B的用户名和密码了,这现实吗,确定不现实微信
最后咱们能够指定是否要refsh_token,access_token,过时时间expires_in等等spring-boot
github源码地址:https://github.com/waterlufei/spring-boot.git,下面的Oauth2SpringCloud和TestOauth2都是些的demo,用的jar包不同一个用了spring boot的,一个用了spring cloud的code