oauth2.0网页授权微信怎么用java获取code
1个回答
展开全部
@RequestMapping(value="oauth2/{type}/{appId}/{scope}", method=RequestMethod.GET)
public ModelAndView index(@PathVariable String type,@PathVariable String appId,@PathVariable String scope,String openId){
ModelAndView view=new ModelAndView();
if(StringUtils.isEmpty(appId)||mapWxAppInfo.get(appId)==null){
view.setViewName("fail");
return view;
}
//scope=snsapi_base 获取用户openId,无需授权
//scope=snsapi_userinfo 获取用户基本信息,需授权
String oauthUrl="https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect";
//处理授权回调地址
String callback="http://wx.lrlz.com/wx/callback/"+appId;
if(!StringUtils.isEmpty(openId)){
callback += "/"+openId;
}
oauthUrl=String.format(oauthUrl, appId,callback,scope,type);
log.info("微信授权认证地址="+oauthUrl);
view.setViewName("redirect:" + oauthUrl);
return view;
}
/**
* 处理网页授权回调
* */
@RequestMapping(value="callback/{appId}")
public ModelAndView callback(@PathVariable String appId,String code,String state,String redirectUri,HttpServletRequest req,HttpServletResponse response){
return getCallback(appId,code,state,null);
}
这样就行了
public ModelAndView index(@PathVariable String type,@PathVariable String appId,@PathVariable String scope,String openId){
ModelAndView view=new ModelAndView();
if(StringUtils.isEmpty(appId)||mapWxAppInfo.get(appId)==null){
view.setViewName("fail");
return view;
}
//scope=snsapi_base 获取用户openId,无需授权
//scope=snsapi_userinfo 获取用户基本信息,需授权
String oauthUrl="https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect";
//处理授权回调地址
String callback="http://wx.lrlz.com/wx/callback/"+appId;
if(!StringUtils.isEmpty(openId)){
callback += "/"+openId;
}
oauthUrl=String.format(oauthUrl, appId,callback,scope,type);
log.info("微信授权认证地址="+oauthUrl);
view.setViewName("redirect:" + oauthUrl);
return view;
}
/**
* 处理网页授权回调
* */
@RequestMapping(value="callback/{appId}")
public ModelAndView callback(@PathVariable String appId,String code,String state,String redirectUri,HttpServletRequest req,HttpServletResponse response){
return getCallback(appId,code,state,null);
}
这样就行了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询