SpringSocial业务系统与社交网站的绑定与解绑

SpringSocial提供了了以下三个服务,我们要做的仅仅是调用它们的服务,但是SpringSocial仅仅只提供了数据,没有提供视图

⒈拿到所有社交网站与业务系统的绑定信息

  SpringSocial已经提供了相应的数据,但并没有提供视图,相关的代码写在ConnectController中,核心代码如下:

 1     @RequestMapping(
 2         method = {RequestMethod.GET}
 3     )
 4     public String connectionStatus(NativeWebRequest request, Model model) {
 5         this.setNoCache(request);
 6         this.processFlash(request, model);
 7         Map<String, List<Connection<?>>> connections = this.connectionRepository.findAllConnections();
 8         model.addAttribute("providerIds", this.connectionFactoryLocator.registeredProviderIds());
 9         model.addAttribute("connectionMap", connections);
10         return this.connectView();
11     }

  这个方法的返回值是一个View,路径是connect/status,我们只需要实现这个视图就可以了。

 1 package cn.coreqi.social;
 2 
 3 import com.fasterxml.jackson.databind.ObjectMapper;
 4 import org.apache.commons.collections4.CollectionUtils;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.social.connect.Connection;
 7 import org.springframework.stereotype.Component;
 8 import org.springframework.web.servlet.view.AbstractView;
 9 
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 
16 @Component("connect/status")
17 public class CoreqiConnectionStatusView extends AbstractView {
18 
19     @Autowired
20     private ObjectMapper objectMapper;
21     @Override
22     protected void renderMergedOutputModel(Map<String, Object> map, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
23         Map<String, List<Connection<?>>> connections = (Map<String, List<Connection<?>>>) map.get("connectionMap");
24         Map<String,Boolean> result = new HashMap<>();
25         for(String key:connections.keySet()){
26             result.put(key, CollectionUtils.isNotEmpty(connections.get(key)));
27         }
28         httpServletResponse.setContentType("application/json;charset=UTF-8");
29         httpServletResponse.getWriter().write(objectMapper.writeValueAsString(result));
30     }
31 }

 

⒉业务系统与社交网站绑定

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>绑定</title>
 6 </head>
 7 <body>
 8     <h2>标准绑定页面</h2>
 9     <!--form 的action地址是固定的格式,前面是connect,后面是OAuth的providerId-->
10     <form action="/connect/weixin" method="post">
11         <button type="submit">绑定微信</button>
12     </form>
13 </body>
14 </html> 

绑定成功后跳转到connect/{providerId}Connected这个视图,上面的例子将会跳转到connect/weixinConnected这个视图上。

 1 package cn.coreqi.social;
 2 
 3 import org.springframework.stereotype.Component;
 4 import org.springframework.web.servlet.view.AbstractView;
 5 import javax.servlet.http.HttpServletRequest;
 6 import javax.servlet.http.HttpServletResponse;
 7 import java.util.Map;
 8 
 9 @Component("connect/weixinConnect,connect/weixinConnected")   //如果当前绑定成功的视图需要重用,请注释@Component注解
10 //在配置类中以@Bean的形式注册当前类,并指定多个不同的name属性。
11 public class CoreqiConnectionView extends AbstractView {
12 
13     @Override
14     protected void renderMergedOutputModel(Map<String, Object> map, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
15         httpServletResponse.setContentType("text/html;charset=UTF-8");
16         if(map.get("connection") == null){
17             httpServletResponse.getWriter().write("<h3>解绑成功</h3>");
18         }else{
19             httpServletResponse.getWriter().write("<h3>绑定成功</h3>");
20         }
21     }
22 }

 

⒊业务系统与社交网站解绑

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>绑定</title>
 6 </head>
 7 <body>
 8     <h2>标准绑定页面</h2>
 9     <!--form 的action地址是固定的格式,前面是connect,后面是OAuth的providerId-->
10     <!--解除绑定只需将post请求改为delete请求,当然,form表单是无法发送delete请求的,我只是告诉你-->
11     <form action="/connect/weixin" method="delete">
12         <button type="submit">绑定微信</button>
13     </form>
14 </body>
15 </html>

解绑成功后跳转到connect/{providerId}Connect这个视图(相较于绑定,视图名去掉了ed而已),上面的例子将会跳转到connect/weixinConnect这个视图上。

 1 package cn.coreqi.social;
 2 
 3 import org.springframework.stereotype.Component;
 4 import org.springframework.web.servlet.view.AbstractView;
 5 import javax.servlet.http.HttpServletRequest;
 6 import javax.servlet.http.HttpServletResponse;
 7 import java.util.Map;
 8 
 9 @Component("connect/weixinConnect,connect/weixinConnected")   //如果当前绑定成功的视图需要重用,请注释@Component注解
10 //在配置类中以@Bean的形式注册当前类,并指定多个不同的name属性。
11 public class CoreqiConnectionView extends AbstractView {
12 
13     @Override
14     protected void renderMergedOutputModel(Map<String, Object> map, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
15         httpServletResponse.setContentType("text/html;charset=UTF-8");
16         if(map.get("connection") == null){
17             httpServletResponse.getWriter().write("<h3>解绑成功</h3>");
18         }else{
19             httpServletResponse.getWriter().write("<h3>绑定成功</h3>");
20         }
21     }
22 }

 

posted @ 2019-04-04 21:05  SpringCore  阅读(761)  评论(0编辑  收藏  举报