我是关于Spring Social API的新手,我试图通过Facebook登录我的应用程序,但它没有将我重定向到我的域名网址。
我的应用程序在两台服务器上运行,这两台服务器基于提交了ajax表单的restcontroller。一个是apache web服务器,另一个是tomcat服务器。
但我的facebook身份验证不是基于restcontroller,如果用户需要通过facebook ID登录,那么他只需点击登录facebook按钮并且页面成功重定向到facebook login pate,但不能重定向回所请求的URL
我的代码如下:
用Facebook登录@RequestMapping(value =" / fbauth",method = RequestMethod.GET) public void fbredirect(@RequestParam Map requestParams)throws UnsupportedEncodingException { String accessToken = null; // String accessToken = //" CAAFv5tfCxvsBAMkGz7Mz63ESCLEK3jgZBEZAZBqlj3CApIyqmvA5hOrtl9Ln05O3BRTcx95TjT9IxZBfRz3fKZBjNJ74qko1SFjjzAXmdDnzVePmGXswOM8NHEGTZA6aub7bnZBYBIMO2ZBXX2gpo6ky7Dkqu2xZBUymAalAgg3ixaRQMe3e3w1ruUtVeyQrtMb3x0pehHOIaTQZDZD&#34 ;; RestTemplate restTemplate = new RestTemplate(); Map result = null; if(StringUtils.isEmpty(accessToken)){ for(String key:requestParams.keySet()){ 对象值= requestParams.get(key); logger.info("密钥:" +密钥+"值:" +值); }
String code = requestParams.get("code");
if (StringUtils.isNotEmpty(code)) {
String authUrl = "http://" + request.getServerName() + "/realtybud/fbauth";
StringBuilder stringBuilder = new StringBuilder();
String fbAuthApi = "https://graph.facebook.com/v2.3/oauth/access_token?client_id=";
stringBuilder.append(fbAuthApi);
stringBuilder.append(selectAppId());
stringBuilder.append("&redirect_uri=");
stringBuilder.append(authUrl);
stringBuilder.append("&client_secret=");
stringBuilder.append(selectAppSecret());
stringBuilder.append("&code=");
stringBuilder.append(code);
String fbGraphUrl = stringBuilder.toString();
result = restTemplate.getForObject(fbGraphUrl, Map.class);
accessToken = result.get("access_token");
logger.info("Access Token: " + accessToken);
}
}
StringBuilder stringBuilder = new StringBuilder();
String fbGraphApi = "https://graph.facebook.com/v2.3/me?fields=id,name,email&access_token=";
stringBuilder.append(fbGraphApi);
stringBuilder.append(accessToken);
String fbProfileInfo = stringBuilder.toString();
// String info = restTemplate.getForObject(fbProfileInfo, String.class);
// logger.info("Info: " + info);
Map<String, String> info = restTemplate.getForObject(fbProfileInfo, Map.class);
logger.info("Id: " + info.get("id") + " name: " + info.get("name") + " email: " + info.get("email"));
String email = info.get("email");
if (StringUtils.isNotEmpty(email)) {
User user = userService.findUserByEmail(email);
if (user == null) {
user = new User();
user.setEmail(email);
user.setFacebookAccessToken(accessToken);
user.setFacebookName(info.get("name"));
userService.userSave(user);
Role userRole = new Role();
userRole.setRoleName("ROLE_USER");
userRole.setUser(user);
userService.roleSave(userRole);
List<Role> roles = new ArrayList<Role>();
roles.add(userRole);
user.setRoles(roles);
} else {
user.setFacebookAccessToken(accessToken);
user.setFacebookName(info.get("name"));
userService.userSave(user);
}
SecurityUser secUser = new SecurityUser(user);
Authentication authentication = new UsernamePasswordAuthenticationToken(secUser, secUser.getPassword(),
secUser.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authentication);
} else {
// error
}
// String fbAuthSuccessUrl = "/index?fbAuth=success";
//return "redirect:/";
}
请帮助我如何使用登录凭据重定向到Apache Web服务器。
提前致谢。 Waqas
答案 0 :(得分:0)
我通过更改我的网络服务器中的重定向网址解决了我的问题(在我的情况下是nginx),我不知道这是最好的方式(甚至是适当的)还是没有!