9

好的,所以我现在一直在拉头发(至少看起来是这样!)试图找出我做错了什么:我有一个 Java 项目,我想允许用户登录(通过普通 Spring - 启用 JDBC 的安全存储库)以向我的应用程序授予对其 Twitter 帐户的访问权限。我已经在 Twitter 等上注册了一个应用程序,并且拥有秘密和访问密钥以及测试所需的所有其他内容,但是,尽管阅读了所有 docco 并尝试了所有配置,即使我的 spring 配置创建了一个ConnectController,每当我点击时,/connect/twitter我都会得到一个 404 (未找到)尽管在 Tomcat 中出现的上下文期间绝对没有产生错误,并且其他一切都正常(即我的所有 bean 都被实例化并且所有视图/控制器都工作等)。

据我了解——尽管我确实在使用 Spring Social docco 时遇到了困难,但更重要的是,其中一些示例仅适用于特定版本!-- 简单地实例化这个控制器应该会处理剩下的 -- 但也许我错了???

这是我的配置的样子——是的,它有点到处都是:

/WEB-INF/web.xml

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
    <display-name>legototies</display-name>
    <servlet>
        <servlet-name>legototies</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>legototies</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- allow robots.txt -->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.txt</url-pattern>
    </servlet-mapping>

    <!-- allow favicon.ico -->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.ico</url-pattern>
    </servlet-mapping>

    <!-- allow everything under /static/ -->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/static/*</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/applicationContext.xml,
            /WEB-INF/spring-security.xml
        </param-value>
    </context-param>

    <!-- Spring Security -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>
            org.springframework.web.filter.DelegatingFilterProxy
        </filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

/WEB-INF/legototies-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.lt" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:order="1">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <bean id="tilesviewResolver" class="org.springframework.web.servlet.view.tiles2.TilesViewResolver" p:order="0"/>

    <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles.xml</value>
            </list>
        </property>
    </bean>

</beans>

/WEB-INF/applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
    xmlns:twitter="http://www.springframework.org/schema/social/twitter"
    xmlns:social="http://www.springframework.org/schema/social"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
       http://www.springframework.org/schema/social/twitter http://www.springframework.org/schema/social/spring-social-twitter.xsd
       http://www.springframework.org/schema/social http://www.springframework.org/schema/social/spring-social-1.1.xsd">

    <!-- **** BEGIN: Config files **** -->
    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath*:config/*.properties</value>
            </list>
        </property>
        <property name="ignoreResourceNotFound" value="true" />
    </bean>
    <!-- **** END: Config files **** -->

    <context:component-scan base-package="com.lt" />


    <!-- **** BEGIN: Database **** -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${database.driverClassName}" />
        <property name="url" value="${database.url}" />
        <property name="username" value="${database.username}" />
        <property name="password" value="${database.password}" />
        <property name="initialSize" value="${database.initial.size}" />
        <property name="maxActive" value="${database.max.active}" />
    </bean>
    <bean id="localJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg>
            <ref bean="dataSource" />
        </constructor-arg>
    </bean>

    <bean id="txManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
    </bean>
    <!-- **** END: Database **** -->


    <bean id="messageAssembler" class="com.lt.message.MessageAssembler" />


    <!-- **** BEGIN: Scheduler **** -->
    <!-- Tasks -->
    <bean id="createTweetsScheduler" class="com.lt.scheduller.CreateTweetsScheduler">
        <constructor-arg index="0" ref="sessionFactory" />
        <constructor-arg index="1" ref="execSendTweet" />
        <constructor-arg index="2" ref="messageAssembler" />
    </bean>
    <bean id="bookScrapingNeededTask" class="com.lt.scheduller.BooksScrapingNeededCheckTask">
        <constructor-arg index="0" ref="sessionFactory" />
        <constructor-arg index="1" ref="execPageScraping" />
        <constructor-arg index="2" value="${shefari.htmlpath}" />
    </bean>

    <!-- Schedulers -->
    <task:executor id="execPageScraping" pool-size="${page.scrape.threadpool.size}" />
    <task:executor id="execSendTweet" pool-size="${broadcastTweets.threadpool.size}" />

    <task:scheduler id="mainScheduler" pool-size="${mainScheduler.size}" />
    <task:scheduled-tasks scheduler="mainScheduler">
        <task:scheduled ref="createTweetsScheduler" method="run"
            fixed-rate="${selectTweets.period.ms}" initial-delay="${selectTweets.initial.delay.ms}" />
        <task:scheduled ref="bookScrapingNeededTask" method="run"
            fixed-rate="${page.scrape.check.period.ms}" initial-delay="${page.scrape.check.initial.delay.ms}" />
    </task:scheduled-tasks>
    <!-- **** END: Scheduler **** -->


    <bean id="textEncryptor" class="org.springframework.security.crypto.encrypt.Encryptors"
        factory-method="noOpText" />
    <bean id="passwordEncoder"
        class="org.springframework.security.crypto.password.NoOpPasswordEncoder"
        factory-method="getInstance" />


    <!-- **** BEGIN: twitter/social **** -->
    <social:jdbc-connection-repository />
    <twitter:config app-id="${twitter.app.consumer.key}"
        app-secret="${twitter.app.consumer.secret}" />
    <bean id="userIdSource"
        class="org.springframework.social.security.AuthenticationNameUserIdSource" />
    <bean id="connectController"
        class="org.springframework.social.connect.web.ConnectController">
        <property name="connectInterceptors">
            <list>
                <bean class="com.lt.utils.TweetAfterConnectInterceptor">
                    <constructor-arg index="0" value="${twitter.app.connect.msg}" />
                </bean>
            </list>
        </property>
        <property name="applicationUrl" value="http://localhost:8080/" />
    </bean>
    <!-- **** END: twitter/social **** -->
</beans>

/WEB-INF/spring-security.xml

<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">

    <global-method-security secured-annotations="enabled" />

    <http auto-config="true" use-expressions="true">
        <intercept-url pattern="/static/**" access="permitAll" />
        <intercept-url pattern="/home" access="permitAll" />
        <intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
    </http>

    <authentication-manager>
        <authentication-provider>
            <jdbc-user-service data-source-ref="dataSource"

           users-by-username-query="select username,password, enabled from users where username=?" 

           authorities-by-username-query="select u.username, ur.authority from users u, user_roles ur 
              where u.id = ur.user_id and u.username =?  " 

            />
        </authentication-provider>
    </authentication-manager>
</beans:beans>

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

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

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>liviutudor</groupId>
    <artifactId>legototies</artifactId>
    <packaging>war</packaging>
    <version>1.0.0-SNAPSHOT</version>
    <name>legototies</name>
    <inceptionYear>2013</inceptionYear>
    <description>This is "lego toties"</description>
    <url>http://legototies.com</url>
    <developers>
        <developer>
            <name>Liviu Tudor</name>
            <id>liviut</id>
            <email>me at liviutudor.com</email>
        </developer>
    </developers>
    <repositories>
        <repository>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <id>central</id>
            <name>Maven Repository Switchboard</name>
            <url>http://repo1.maven.org/maven2</url>
        </repository>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>http://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>http://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <properties>
        <project.build.jdkVersion>1.6</project.build.jdkVersion>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <hibernate.version>3.6.10.Final</hibernate.version>
        <jsoup.version>1.7.2</jsoup.version>
        <junit.version>4.10</junit.version>
        <spring.version>3.2.3.RELEASE</spring.version>
        <spring.social.version>1.1.0.M4</spring.social.version>
        <spring.security.version>3.1.4.RELEASE</spring.security.version>
        <tiles.version>2.2.2</tiles.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Spring security -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency> <!-- needed by spring social twitter -->
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-crypto</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <!-- Spring Social -->
        <dependency>
            <groupId>org.springframework.social</groupId>
            <artifactId>spring-social-twitter</artifactId>
            <version>${spring.social.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.social</groupId>
            <artifactId>spring-social-web</artifactId>
            <version>${spring.social.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.social</groupId>
            <artifactId>spring-social-security</artifactId>
            <version>${spring.social.version}</version>
        </dependency>

        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <!-- this is needed for hibernate -->
        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.0.GA</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.0.5</version>
        </dependency>

        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.1</version>
        </dependency>

        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>${jsoup.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-extras</artifactId>
            <version>${tiles.version}</version>
        </dependency>

        <!-- Test dependencies -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>legototies</finalName>
        <defaultGoal>install</defaultGoal>
        <resources>
            <resource>
                <filtering>false</filtering>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <filtering>false</filtering>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${project.build.jdkVersion}</source>
                    <target>${project.build.jdkVersion}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

正如我所说的一切都很好,但是,鉴于上述情况,即使有ConnectController定义,/connect/twitter(或任何其他/connect/...URL)返回 404 未找到。我错过了什么?

更新:有关连接控制器的日志记录段

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)
4

4 回答 4

4

ConnectController在错误的配置文件中。你应该把它移到legototies-servlet.xml.

使用的HandlerMapping实现只检测本地上下文(由 加载的)而不是父上下文(由 加载)中的(@)Controllerbean 。因此,您已配置但未执行任何操作,因为未检测到它。DispatcherServletContextLoaderListenerConnectController

您的配置还有另一个问题,您要加载所有 bean 两次。这是由于您配置组件扫描的方式。在这两个配置文件中都有一个<context:component-scan base-package="com.lt" />基本上复制了所有 bean。一般来说,您应该配置DispatcherServletto load only@ControllersContextLoaderListenerto load everything BUT @Controllers

<context:component-scan base-package="com.lt">
    <context:exclude-filter type="annotation" value="org.springframework.stereotype.Controller" />
</context:component-scan>

并且对于DispatcherServlet

<context:component-scan base-package="com.lt" use-default-filters="false">
    <context:include-filter type="annotation" value="org.springframework.stereotype.Controller" />
</context:component-scan>

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

于 2013-10-30T12:27:00.623 回答
3

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

@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";
    }
}

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

于 2013-10-31T06:16:31.497 回答
1

1) 位于 web.xml 之上(您希望安全过滤器成为第一个获得请求的人)

 <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>
            org.springframework.web.filter.DelegatingFilterProxy
        </filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

2)你正在使用瓷砖,所以你不会使用 jsp 视图解析器。将其更改为

<bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.tiles2.TilesView" />
    </bean>

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

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

  <servlet>
            <servlet-name>appServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/appServlet-context.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>

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

于 2013-11-01T18:18:36.273 回答
1

好的,所以我让它工作了,尽管不知何故仍然不确定问题是什么——我认为它介于“快捷方式”弹簧标签的问题和错误配置的安全路径之间。所以事实证明,我spring-security允许未登录的访问/connect/....在这一点上#{request.userPrincipal.name}没有正确评估并引发异常。春天,没有打开日志,我从来没有发现它,一切都在悄悄地失败。重新配置了整个 Twitter/社交支持,如下所示:

    <bean id="connectionFactoryLocator"
        class="org.springframework.social.connect.support.ConnectionFactoryRegistry">
        <property name="connectionFactories">
            <list>
                <ref bean="twitterConnectFactory" />
            </list>
        </property>
    </bean>
    <bean id="twitterConnectFactory" class="org.springframework.social.twitter.connect.TwitterConnectionFactory">
        <constructor-arg value="${twitter.app.consumer.key}" />
        <constructor-arg value="${twitter.app.consumer.secret}" />
    </bean> 

    <bean id="usersConnectionRepository"
        class="org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository">
        <constructor-arg ref="dataSource" />
        <constructor-arg ref="connectionFactoryLocator" />
        <constructor-arg ref="textEncryptor" />
    </bean>

    <bean id="connectionRepository" factory-method="createConnectionRepository"
        factory-bean="usersConnectionRepository" scope="request">
        <constructor-arg value="#{request.userPrincipal.name}" />
        <aop:scoped-proxy proxy-target-class="false" />
    </bean>

还配置了连接控制器:

<bean id="connectController"
    class="org.springframework.social.connect.web.ConnectController">
    <constructor-arg ref="connectionFactoryLocator" />
    <constructor-arg ref="connectionRepository" />
    <property name="connectInterceptors">
        <list>
            <bean class="com.lt.utils.TweetAfterConnectInterceptor">
                <constructor-arg index="0" value="${twitter.app.connect.msg}" />
            </bean>
        </list>
    </property>
</bean>

并在我的 spring 安全性中进行了更改,以实际让用户登录以访问/connect...url,这现在可以正常工作。我也没有按照这里的建议使用自定义连接控制器,而是依赖标准连接控制器。这也是我spring-security.xml的参考:

<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">

    <global-method-security secured-annotations="enabled" />

    <http auto-config="true" use-expressions="true">
        <intercept-url pattern="/in/**" access="hasRole('ROLE_USER')" />
        <intercept-url pattern="/connect" access="hasRole('ROLE_USER')" />
        <intercept-url pattern="/connect/**" access="hasRole('ROLE_USER')" />
        <intercept-url pattern="/*" access="permitAll" />
        <intercept-url pattern="/static/**" access="permitAll" />
        <intercept-url pattern="/login" access="permitAll" />
        <intercept-url pattern="/loginfailed" access="permitAll" />


        <form-login login-page="/login" default-target-url="/" authentication-failure-url="/loginfailed" />
        <logout logout-success-url="/logout" />
    </http>

    <authentication-manager>
        <authentication-provider>
            <jdbc-user-service data-source-ref="dataSource"
                users-by-username-query="select username,password, enabled from users where username=?"
                authorities-by-username-query="select u.username, ur.authority from users u, user_roles ur 
              where u.id = ur.user_id and u.username =?" />
        </authentication-provider>
    </authentication-manager>
</beans:beans>
于 2013-11-16T02:35:30.750 回答