oauth2及时从一个项目A申请另一个项目B的访问的时候,不用在项目A输入项目B的用户名和密码,个人理解先跳转到项目B,利用项目B的用户名和密码得到一个code之类的,这里有点像openID,不过不是的,这是用户某次授权的,不是用户唯一标识一致可以授权,做了2个关于oauth2的demo,这里记录一下

    用户从A访问B的地址,例如:localhost:8080/oauth/authorize?client_id=client&response_type=code&redirect_uri=http://www.baidu.com,然后需要输入spring security的用户名和密码,选择Approve,这里有点像,王者荣耀确认获取微信信息的界面

  利用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的资源了

    这里也可以直接访问 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等等

github源码地址:https://github.com/waterlufei/spring-boot.git,下面的Oauth2SpringCloud和TestOauth2都是些的demo,用的jar包不一样一个用了spring boot的,一个用了spring cloud的

posted on 2017-08-24 18:59  请叫我小路飞  阅读(5244)  评论(1编辑  收藏  举报