springboot2.x中怎么实现oauth2授权码登陆,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

一 进行授权页
浏览器输入 http://localhost:8081/oauth/authorize?response_type=code&redirect_uri=http://localhost:8081/callback&client_id=android1&scop=all
二 使用资源站用户登陆
自动跨到资源登陆页,先登陆
三 授权资源类型
登陆成功后,去授权你的资源,这些资源是在AuthorizationServerConfig.configure方法里配置的
@Overridepublic void configure(ClientDetailsServiceConfigurer clients) throws Exception {clients.inMemory().withClient(ClientID).secret(passwordEncoder.encode(ClientSecret)).authorizedGrantTypes("authorization_code", "refresh_token","password", "implicit").scopes("read","write","del","userinfo").redirectUris(RedirectURLs);}
四 接到code
授权之后,系统会重定向到你的redirect_uri这个页面,并带上的code
五 获取access_token
我们拿着code就要再去授权服务器去获取token了,你可以在你的代码里写这个,也可以手动拿着code,去拼成一个url,再去拿token,就像这下面的实例。
注意向oauth/token发的是post请求,client_id和client_secret如果在url上传递,如果在AuthorizationServerConfig类的configure方法中开启allowFormAuthenticationForClients,代码如下
@Overridepublic void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()").allowFormAuthenticationForClients();//支持把secret和clientid写在url上,否则需要在头上}
然后请求后给有下面的响应
Authorization Ccode------RFRLFYaccess_token_url http://localhost:8081/oauth/token?client_id=android1&code=RFRLFY&grant_type=authorization_code&redirect_uri=http://localhost:8081/callback&client_secret=android1Access Token Response ---------{"access_token":"faadf3bf-6488-4036-bc3b-21b0a979602c","token_type":"bearer","refresh_token":"1b01f133-c5ab-419f-8125-088c85916ecb","expires_in":43187,"scope":"read"}
回调页面代码,主要实现了对code的获取,对access_token的组织,然后请求时把access_token带上,这个方法一般会做成公用的过滤器
@Controllerpublic class UserController { @RequestMapping(value = "/callback", method = RequestMethod.GET) public ResponseEntity
关于springboot2.x中怎么实现oauth2授权码登陆问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。
新闻标题:springboot2.x中怎么实现oauth2授权码登陆-创新互联
链接URL:http://www.jxjierui.cn/article/dhddss.html
其他资讯
- 甲鱼煮熟了可以放几天甲鱼煮熟了可以放多少天
- SaltStack之Job管理和Runner-创新互联
- c语言中是什么意思如果在写C语言时,代码没错,但是结果总错是什么原因?-创新互联
- 网站建设与管理介绍范文,企业宣传网站建设方案范文
- 如何用Python代码打造AI智能机器人
- c语言有哪些主函数,C语言的主函数
- 阿里云服务器提速插件 阿里云服务器提速插件是什么
- 清源正本,鉴往知来,Go lang1.18入门精炼教程,由白丁入鸿儒,Golang中引用类型是否进行引用传递EP18
- linux命令只显示一半 linux 只显示文件
- 文件扫描后怎样发邮件,什么是JPG格式?(发邮件怎么用jpeg文件发送)


咨询
建站咨询
