springboot2.x实现oauth2授权码登陆

2019-08-08| 发布者: admin| 查看: |

回调页面代码,主要实现了对code的获取,对access_凯发娱乐指定官方入口token的组织,然后请求时把access_token带上,这个方法一般会做成公用的过滤器

@controller
public class usercontroller {
 @requestmapping
 public responseentity string callback string code) throws jsonprocessingexception, ioexception {
 responseentity string response = null;
 system.out.println;
 resttemplate resttemplate = new resttemplate;
 string access_token_url = "http://localhost:8081/oauth/token";
 access_token_url += "?client_id=android1 code=" + code;
 access_token_url += " grant_type=authorization_code";
 access_token_url += " redirect_uri=http://localhost:8081/callback";
 access_token_url += " client_secret=android1";
 system.out.println;
 response = resttemplate.exchange;
 objectmapper mapper = new objectmapper;
 jsonnode node = mapper.readtree);
 string token = node.path.astext; system.out.println;
 string url = "http://localhost:8081/index"; httpheaders headers1 = new httpheaders; headers1.add; httpentity string entity = new httpentity ; responseentity string result = resttemplate.exchange; return result; }