SpringSocial提供了了以下三个服务,我们要做的仅仅是调用它们的服务,但是SpringSocial仅仅只提供了数据,没有提供视图
⒈拿到所有社交网站与业务系统的绑定信息
SpringSocial已经提供了相应的数据,但并没有提供视图,相关的代码写在ConnectController中,核心代码如下:
@RequestMapping(
method = {RequestMethod.GET}
)
public String connectionStatus(NativeWebRequest request, Model model) {
this.setNoCache(request);
this.processFlash(request, model);
Map<String, List<Connection<?>>> connections = this.connectionRepository.findAllConnections();
model.addAttribute("providerIds", this.connectionFactoryLocator.registeredProviderIds());
model.addAttribute("connectionMap", connections);
return this.connectView();
}
这个方法的返回值是一个View,路径是connect/status,我们只需要实现这个视图就可以了。
package cn.coreqi.social; import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.connect.Connection;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.AbstractView; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map; @Component("connect/status")
public class CoreqiConnectionStatusView extends AbstractView { @Autowired
private ObjectMapper objectMapper;
@Override
protected void renderMergedOutputModel(Map<String, Object> map, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
Map<String, List<Connection<?>>> connections = (Map<String, List<Connection<?>>>) map.get("connectionMap");
Map<String,Boolean> result = new HashMap<>();
for(String key:connections.keySet()){
result.put(key, CollectionUtils.isNotEmpty(connections.get(key)));
}
httpServletResponse.setContentType("application/json;charset=UTF-8");
httpServletResponse.getWriter().write(objectMapper.writeValueAsString(result));
}
}
⒉业务系统与社交网站绑定
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>绑定</title>
</head>
<body>
<h2>标准绑定页面</h2>
<!--form 的action地址是固定的格式,前面是connect,后面是OAuth的providerId-->
<form action="/connect/weixin" method="post">
<button type="submit">绑定微信</button>
</form>
</body>
</html>
绑定成功后跳转到connect/{providerId}Connected这个视图,上面的例子将会跳转到connect/weixinConnected这个视图上。
package cn.coreqi.social; import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.AbstractView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map; @Component("connect/weixinConnect,connect/weixinConnected") //如果当前绑定成功的视图需要重用,请注释@Component注解
//在配置类中以@Bean的形式注册当前类,并指定多个不同的name属性。
public class CoreqiConnectionView extends AbstractView { @Override
protected void renderMergedOutputModel(Map<String, Object> map, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
httpServletResponse.setContentType("text/html;charset=UTF-8");
if(map.get("connection") == null){
httpServletResponse.getWriter().write("<h3>解绑成功</h3>");
}else{
httpServletResponse.getWriter().write("<h3>绑定成功</h3>");
}
}
}
⒊业务系统与社交网站解绑
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>绑定</title>
</head>
<body>
<h2>标准绑定页面</h2>
<!--form 的action地址是固定的格式,前面是connect,后面是OAuth的providerId-->
<!--解除绑定只需将post请求改为delete请求,当然,form表单是无法发送delete请求的,我只是告诉你-->
<form action="/connect/weixin" method="delete">
<button type="submit">绑定微信</button>
</form>
</body>
</html>
解绑成功后跳转到connect/{providerId}Connect这个视图(相较于绑定,视图名去掉了ed而已),上面的例子将会跳转到connect/weixinConnect这个视图上。
package cn.coreqi.social; import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.AbstractView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map; @Component("connect/weixinConnect,connect/weixinConnected") //如果当前绑定成功的视图需要重用,请注释@Component注解
//在配置类中以@Bean的形式注册当前类,并指定多个不同的name属性。
public class CoreqiConnectionView extends AbstractView { @Override
protected void renderMergedOutputModel(Map<String, Object> map, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
httpServletResponse.setContentType("text/html;charset=UTF-8");
if(map.get("connection") == null){
httpServletResponse.getWriter().write("<h3>解绑成功</h3>");
}else{
httpServletResponse.getWriter().write("<h3>绑定成功</h3>");
}
}
}