Spring Social 解绑社交时报 302错误

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35170213/article/details/84792377

首先说一下解绑的接口
org.springframework.social.connect.web.ConnectController#removeConnections
方法如下:

@RequestMapping(value="/{providerId}", method=RequestMethod.DELETE)
	public RedirectView removeConnections(@PathVariable String providerId, NativeWebRequest request) {
		//我们直接看他跳转的地址
		HttpServletRequest servletRequest = request.getNativeRequest(HttpServletRequest.class);
		String path = "/connect/" + providerId + getPathExtension(servletRequest);
		if (prependServletPath(servletRequest)) {
			path = servletRequest.getServletPath() + path;
		}
		return new RedirectView(path, true);
	}

其实他最终的跳转地址是 /connect/你的providerId

实际上这个地址还是ConnectController类中connectionStatus方法,如下:

	@RequestMapping(value="/{providerId}", method=RequestMethod.GET)
	public String connectionStatus(@PathVariable String providerId, NativeWebRequest request, Model model) {
		setNoCache(request);
		processFlash(request, model);
		List<Connection<?>> connections = connectionRepository.findConnections(providerId);
		setNoCache(request);
		// 这个方法是解绑和绑定公用的,都会来调用这个方法,当我们解绑之后跳过来
		// 这个connections 是没有值的,如果是解绑则会查询出来值
		if (connections.isEmpty()) {
			// 拼接后地址为  connect/你得提供者IdConnect
			// 例如你的提供者为  wx 则 
			return connectView(providerId); 
		} else {
			model.addAttribute("connections", connections);
			return connectedView(providerId);			
		}
	}
	 
	protected String connectView(String providerId) {
		return getViewPath() + providerId + "Connect";		
	}	 
	protected String connectedView(String providerId) {
		return getViewPath() + providerId + "Connected";		
	}

但这个视图不存在,需要你建立一个AbstractView的子类,并他把加入到容器中,就像如下

public class MyConnectView extends AbstractView {
    @Override
    protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setContentType("text/html;charset=utf-8");
        if(model.get("connections") != null){
            response.getWriter().print("<h2>绑定成功</h2>");
        }else{
            response.getWriter().print("<h2>解绑成功</h2>");
        }
    }
}
	// 我这里解绑和绑定用的一个对象,以model中有没有connections来区分解绑还是绑定
    @Bean({"connect/wxConnected", "connect/wxConnect"})
    public MyConnectView connectView(){
        return new MyConnectView();
    }
    

讲过上面的配置浏览器from表单中使用是没问题了!
示例代码:

    <form action="/connect/wx" method="post">
        <input type="hidden" name="_method" value="delete">
        <button>解绑微信</button>
    </form>

但是你发ajax时就会出现302响应码!不知道是Ajax请求后台不能重定向还是其他原因,但是你可以写一个自己的接口来处理Ajax的解绑请求!


/**
 * @author qiaolin
 * @version 2018/11/23
 **/

@RestController
public class BrowserSecurityController {
	@Autowired
    private ConnectionFactoryLocator connectionFactoryLocator;

    @Autowired
    private ConnectionRepository connectionRepository;
   
    @RequestMapping(value="/connect/ajax/{providerId}", method= RequestMethod.DELETE)
    public SimpleResponse removeConnections(@PathVariable String providerId, NativeWebRequest request) {
        ConnectionFactory<?> connectionFactory = connectionFactoryLocator.getConnectionFactory(providerId);
        connectionRepository.removeConnections(providerId);
        HttpServletRequest servletRequest = request.getNativeRequest(HttpServletRequest.class);
        String path = "/connect/" + providerId + getPathExtension(servletRequest);
        return new SimpleResponse("解除成功");
    }
}

这样浏览器用浏览器的接口,Ajax请求用这个就好了!

猜你喜欢

转载自blog.csdn.net/qq_35170213/article/details/84792377
今日推荐