Spring-Social / Twitter – ConnectController不响应/连接?

好吧,所以我现在已经拉了很长时间了(至少看起来好像!)试图弄清楚我做错了什么:我有一个Java项目,我希望允许用户登录(通过普通的Spring) -Security JDBC enabled repository)将Twitter帐户的访问权限授予我的应用程序。 我已经在Twitter上注册了一个应用程序,并且有秘密和访问密钥以及测试所需的所有其他内容,但是,尽管所有docco的读取和所有配置都尝试过,即使我的spring配置创建了一个ConnectController ,每当我点击/connect/twitter我得到了404(未找到)虽然在Tomcat中出现的上下文中绝对没有生成错误,其他一切正常(即我的所有bean都被实例化,所有视图/控制器都工作等)。

根据我的理解 – 尽管我确实在努力使用Spring Social docco,但更多的例子显示只适用于特定版本! – 简单地实例化这个控制器应该照顾其余的 – 但也许我错了?

这是我的配置的样子 – 是的,它有点全面:

/WEB-INF/web.xml

   legototies  legototies org.springframework.web.servlet.DispatcherServlet 1   legototies /    default *.txt    default *.ico    default /static/*   org.springframework.web.context.ContextLoaderListener   contextConfigLocation  /WEB-INF/applicationContext.xml, /WEB-INF/spring-security.xml     springSecurityFilterChain  org.springframework.web.filter.DelegatingFilterProxy    springSecurityFilterChain /*   

/WEB-INF/legototies-servlet.xml

             /WEB-INF/tiles.xml     

/WEB-INF/applicationContext.xml的

       classpath*:config/*.properties                          classpath:hibernate.cfg.xml                                            

/WEB-INF/spring-security.xml

              

我没有在这里包含/WEB-INF/tiles.xml,因为我认为它不相关 – 它只是定义了一些基本模板。

最后,这是项目pom.xml – 请注意我正在使用最新的春季社交里程碑(M4):

  4.0.0 liviutudor legototies war 1.0.0-SNAPSHOT legototies 2013 This is "lego toties" http://legototies.com   Liviu Tudor liviut me at liviutudor.com      false  central Maven Repository Switchboard http://repo1.maven.org/maven2   spring-snapshots Spring Snapshots http://repo.spring.io/snapshot  true    spring-milestones Spring Milestones http://repo.spring.io/milestone  false     1.6 UTF-8 3.6.10.Final 1.7.2 4.10 3.2.3.RELEASE 1.1.0.M4 3.1.4.RELEASE 2.2.2    javax.servlet servlet-api 2.5 provided   javax.servlet jstl 1.2    org.springframework spring-core ${spring.version}   org.springframework spring-aop ${spring.version}   commons-logging commons-logging     org.springframework spring-jdbc ${spring.version}   org.springframework spring-orm ${spring.version}   org.springframework spring-web ${spring.version}   org.springframework spring-webmvc ${spring.version}   commons-logging commons-logging     org.springframework spring-context ${spring.version}    org.springframework.security spring-security-core ${spring.security.version}   org.springframework.security spring-security-web ${spring.security.version}   org.springframework.security spring-security-config ${spring.security.version}   org.springframework.security spring-security-taglibs ${spring.security.version}    org.springframework.security spring-security-crypto ${spring.security.version}    org.springframework.social spring-social-twitter ${spring.social.version}   org.springframework.social spring-social-web ${spring.social.version}   org.springframework.social spring-social-security ${spring.social.version}    org.hibernate hibernate-core ${hibernate.version}   org.hibernate hibernate-entitymanager ${hibernate.version}   org.hibernate.javax.persistence hibernate-jpa-2.0-api 1.0.0.Final    javassist javassist 3.12.0.GA   mysql mysql-connector-java 5.0.5   commons-dbcp commons-dbcp 1.4   org.apache.commons commons-lang3 3.1   org.jsoup jsoup ${jsoup.version}   org.apache.tiles tiles-extras ${tiles.version}    junit junit ${junit.version} test   org.springframework spring-test ${spring.version} test    legototies install   false src/main/java  **/*.properties **/*.xml    false src/main/resources  **/*.properties **/*.xml      maven-compiler-plugin  ${project.build.jdkVersion} ${project.build.jdkVersion}    org.apache.maven.plugins maven-resources-plugin  ${project.build.sourceEncoding}      

正如我所说的一切正常,但是,鉴于上述情况,即使定义了ConnectController/connect/twitter (或任何其他/connect/... URL)也会返回404 not found。 我错过了什么?

更新:关于连接控制器的记录段

 5148 [main] INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/connect/{providerId}],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.view.RedirectView org.springframework.social.connect.web.ConnectController.connect(java.lang.String,org.springframework.web.context.request.NativeWebRequest) 5148 [main] INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/connect/{providerId}],methods=[DELETE],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.view.RedirectView org.springframework.social.connect.web.ConnectController.removeConnections(java.lang.String,org.springframework.web.context.request.NativeWebRequest) 5148 [main] INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/connect/{providerId}/{providerUserId}],methods=[DELETE],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.view.RedirectView org.springframework.social.connect.web.ConnectController.removeConnection(java.lang.String,java.lang.String,org.springframework.web.context.request.NativeWebRequest) 5149 [main] INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/connect/{providerId}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String org.springframework.social.connect.web.ConnectController.connectionStatus(java.lang.String,org.springframework.web.context.request.NativeWebRequest,org.springframework.ui.Model) 5150 [main] INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/connect],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String org.springframework.social.connect.web.ConnectController.connectionStatus(org.springframework.web.context.request.NativeWebRequest,org.springframework.ui.Model) 5150 [main] INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/connect/{providerId}],methods=[GET],params=[oauth_token],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.view.RedirectView org.springframework.social.connect.web.ConnectController.oauth1Callback(java.lang.String,org.springframework.web.context.request.NativeWebRequest) 5151 [main] INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/connect/{providerId}],methods=[GET],params=[code],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.view.RedirectView org.springframework.social.connect.web.ConnectController.oauth2Callback(java.lang.String,org.springframework.web.context.request.NativeWebRequest) 5152 [main] INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/connect/{providerId}],methods=[GET],params=[error],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.view.RedirectView org.springframework.social.connect.web.ConnectController.oauth2ErrorCallback(java.lang.String,java.lang.String,java.lang.String,java.lang.String,org.springframework.web.context.request.NativeWebRequest) 

您的ConnectController位于错误的配置文件中。 您应该将其移动到legototies-servlet.xml

使用的HandlerMapping实现仅检测本地上下文中的(@)Controller bean(由DispatcherServlet加载的那个)不在父上下文中(由ContextLoaderListener加载)。 因此,您的ConnectController已配置但未执行任何操作,因为未检测到它。

您的配置还有另一个问题,即您要加载所有bean两次。 这是由于您配置组件扫描的方式。 在两个配置文件中都有一个 ,它基本上复制了所有bean。 通常,您应该将DispatcherServlet配置为仅加载@ControllersContextLoaderListener以加载所有内容但是@Controllers

    

以及DispatcherServlet

    

乍一看,您的数据库配置也有错误。 您正在使用hibernate但已配置了DataSourceTransactionManager而您应该配置HibernateTransactionManager 。 后者完全能够控制纯JDBC事务。

在这里,您需要覆盖ConnectController一些方法。 在这里,我给你一些可能对你有用的代码片段(注释)。 ConnectController的默认视图是/connect/{providerID}Connected – 一旦授权和连接& /connect/{providerID}Connect – 一旦断开连接,这是默认视图。 您收到404错误,因为您没有连接文件夹和twitterConnected.jsp。 只是为了测试你创建一个连接文件夹并将1.twitterConnected.jsp和2.twitterConnect.jsp放入其中并尝试在inheritanceConnectController之前连接它,如下所示。

 @Controller public class CustomConnectController extends ConnectController { @Inject public CustomConnectController( ConnectionFactoryLocator connectionFactoryLocator, ConnectionRepository connectionRepository) { super(connectionFactoryLocator, connectionRepository); } //This connectedView will be called after user authorize twitter app. So here you can redirect //users to the page you need. @Override protected String connectedView(String providerId){ return "redirect:/user/profile"; } //This connectView will be called if user disconnect from social media. Here you can redirect //them once they got disconnected. @Override protected String connectView(String providerId) { return "redirect:/connect"; } } 

试试吧,让我知道你的输出。

1)继续使用web.xml(您希望安全filter成为第一个获取请求的人)

   springSecurityFilterChain  org.springframework.web.filter.DelegatingFilterProxy    springSecurityFilterChain /*  

2)您正在使用磁贴,因此您不会使用jsp视图解析器。 改为

    

3)servlet名称必须与您的servlet配置文件名匹配。

编辑:也是加载上下文的正确方法

   appServlet org.springframework.web.servlet.DispatcherServlet   contextConfigLocation /WEB-INF/appServlet-context.xml  1  

将appServlet更改为您的servlet配置文件。

好的,所以我让它工作,虽然不知何故仍然不确定是什么问题 – 我认为它介于“快捷方式”弹簧标签的问题和错误配置的安全路径之间。 所以事实certificate我的spring-security允许在访问/connect/....时没有记录,此时#{request.userPrincipal.name}没有正确评估并抛出exception。 作为spring,没有登录,我从未发现它,一切都在悄然失败。 重新配置了整个Twitter /社交支持,如下所示:

                      

连接控制器也已配置:

            

并且在我的Spring安全性中进行了更改,实际上让用户登录以访问/connect... url,现在正在运行。 我没有像这里建议的那样使用自定义连接控制器,而是依赖于标准连接器。 这是我的spring-security.xml以供参考: