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);
    }
}
发布了81 篇原创文章 · 获赞 12 · 访问量 17万+

猜你喜欢

转载自blog.csdn.net/u012382791/article/details/105285117