如何在MVC配置中允许匿名twitter连接
在tomcat 7上使用 – spring-social 1.1.4 – spring-social-twitter 1.1.2 – spring-mvc-4.2
下面是我的MVC应用程序的tomcat配置,包括twitter设置和usersConnectionRepository。
这对于登录的MVC用户来说都很好。
当我想允许客户端从非MVC认证用户可用的公共链接发送推文时,问题就出现了。 (#{request.userPrincipal.name}将为null)
我看了很多例子。 大多数示例使用旧的1.0.0 spring-social。 我注意到spring-social- twitter的新1.1.2版本没有零参数Twitter()构造函数,在某些示例中,它显示为没有connectionRepository的工作方式。
我正在寻找一种方法来配置或编码一个选项,以允许用户登录到我的应用程序和匿名用户使用我的网站上的一些选项。
在下面的配置中,重定向到connect / twitter的匿名用户将导致获取connetionRepository时出现问题。 usersConnectionRepository必须有一个“#{request.userPrincipal.name}”,因为我的connectController依赖于此。
我想也许我需要一个自定义连接控制器…必须有一个正确的方法。
欢迎任何建议。
我的web.xml
org.springframework.web.util.Log4jConfigListener Central Portal spring.profiles.active dev spring.profiles.default dev spring.liveBeansView.mbeanDomain dev mvc-dispatcher org.springframework.web.servlet.DispatcherServlet 1 mvc-dispatcher / 500 /WEB-INF//pages/error.jsp 404 /WEB-INF/pages/error.jsp /WEB-INF/pages/error.jsp org.springframework.web.context.ContextLoaderListener contextConfigLocation /WEB-INF/spring-security.xml, /WEB-INF/spring-database.xml hiddenHttpMethodFilter org.springframework.web.filter.HiddenHttpMethodFilter hiddenHttpMethodFilter /* springSecurityFilterChain org.springframework.web.filter.DelegatingFilterProxy springSecurityFilterChain /* org.springframework.security.web.session.HttpSessionEventPublisher
一点java配置
MVC-调度-servlet.xml中
/pages/ .jsp error <!-- -->
弹簧database.xml
<property name="password" value="" />
弹簧security.xml文件
<!-- enable csrf protection default is enabled from 4.0 spring -->
我看到你已经配置了twitterTemplate
bean,如果你能够在应用程序中使用它,你将不需要做任何其他事情。
使用TwitterTemplate
而不是Twitter
。 TwitterTemplate
实现了Twitter
界面,因此所有方法都可用。
例子:
//1. Search Twitter SearchResults results = twitterTemplate.searchOperations().search("#WinterIsComing"); List tweets = results.getTweets(); int i =0; for (Tweet tweet : tweets) { System.out.println(tweet.getUser().getName() + " Tweeted : "+tweet.getText() + " from " + tweet.getUser().getLocation() + " @ " + tweet.getCreatedAt() + tweet.getUser().getLocation() ); } //2. Search Place by GeoLocation RestTemplate restTemplate = twitterTemplate.getRestTemplate(); GeoTemplate geoTemplate = new GeoTemplate(restTemplate, true, true); List place = geoTemplate.search(37.423021, -122.083739); for (Place p : place) { System.out.println(p.getName() + " " + p.getCountry() + " "+p.getId()); } //3. Get Twitter UserProfile TwitterProfile userProfile = twitterTemplate.userOperations().getUserProfile(); System.out.println(userProfile.getName()+" has " +userProfile.getFriendsCount() + " friends");
使用TwitterTemplate
,用户无需登录。 希望这可以帮助!