Spring Boot整合Security系列步骤及问题排查(十三)—— 绑定解绑

SpringSocial对于绑定状态提供了数据支持,需要完善视图功能:
数据访问地址:/connect
即访问org.springframework.social.connect.web.ConnectController中
connectionStatus(NativeWebRequest request, Model model)方法

新建数据视图处理工具类DemoConnectionStatusView:

/**
 * 绑定状态视图实现
 *
 * @author zhaohaibin
 */
@Component("connect/status")
public class DemoConnectionStatusView extends AbstractView {

    @Autowired
    private ObjectMapper objectMapper;

    /**
     * org.springframework.social.connect.web.ConnectController
     * connectionStatus(NativeWebRequest request, Model model)
     * @param map
     * @param request
     * @param response
     * @throws Exception
     */
    @Override
    protected void renderMergedOutputModel(Map<String, Object> map, HttpServletRequest request, HttpServletResponse response) 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)));
        }

        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().write(objectMapper.writeValueAsString(result));

    }
}

新建绑定结果视图工具类DemoConnectView:

/**
 * 第三方通用绑定结果处理
 *
 * @author zhaohaibin
 */
public class DemoConnectView extends AbstractView {
    @Override
    protected void renderMergedOutputModel(Map<String, Object> map, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

        httpServletResponse.setContentType("text/html;charset=UTF-8");
        if (null == map.get("connection")) {
            httpServletResponse.getWriter().write("<h3>绑定成功</h3>");
        } else {
            httpServletResponse.getWriter().write("<h3>解绑成功</h3>");
        }


    }
}

配置:
更新WeixinAutoConfiguration:

/**
 * 绑定结果配置
 * 绑定:weixinConnect,解绑:weixinConnected
 * @return
 */
@Bean({"connect/weixinConnect", "connect/weixinConnected"})
@ConditionalOnMissingBean(name = "weixinConnectedView")
public View weixinConnectedView() {
    return new DemoConnectView();
}

新建绑定页面:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>第三方绑定</title>
</head>
<body>
<h2>标准绑定页面</h2>
<!--绑定请求是post,解绑请求是delete,需要依赖工具测试解绑-->
<form action="connect/weixin" method="post">
    <input type="submit" value="绑定微信">
</form>
</body>
</html>

问题排查:

// 访问/connect始终404,怀疑对应ConnectController未生效,查资料后增加如下实现:
/**
 * 问题:应用扫不到@Controller而无@ComponentScan注解的
 * org.springframework.social.connect.web.ConnectController
 *
 * 因为:@SpringBootApplication注解等价于@Configuration, @EnableAutoConfiguration and @ComponentScan
 *
 * 所以:通过继承定义组件使应用加载
 *
 * @author zhaohaibin
 */
@Component
public class DefaultConnectController extends ConnectController {
    public DefaultConnectController(ConnectionFactoryLocator connectionFactoryLocator, ConnectionRepository connectionRepository) {
        super(connectionFactoryLocator, connectionRepository);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值