Spring社交/连接返回HTTP 404
Spring找不到html视图,当我试图打开Spring社交/连接返回HTTP 404,spring,spring-mvc,spring-social,spring-social-linkedin,Spring,Spring Mvc,Spring Social,Spring Social Linkedin,Spring找不到html视图,当我试图打开/connect/linkedin时,我得到了404。在referece to中,我仔细检查了webapp文件夹中的路径。这是我的SpringSocialConfig @Configuration @EnableSocial public class SocialConfig implements SocialConfigurer { @Inject private DataSource dataSource; @Overr
/connect/linkedin
时,我得到了404。在referece to中,我仔细检查了webapp文件夹中的路径。这是我的SpringSocialConfig
@Configuration
@EnableSocial
public class SocialConfig implements SocialConfigurer {
@Inject
private DataSource dataSource;
@Override
public void addConnectionFactories(ConnectionFactoryConfigurer connectionFactoryConfigurer, Environment environment) {
connectionFactoryConfigurer.addConnectionFactory(new LinkedInConnectionFactory("xxxxxx", "xxxxxx"));
}
@Override
public UserIdSource getUserIdSource() {
return new UserIdSource() {
@Override
public String getUserId() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
throw new IllegalStateException("Unable to get a ConnectionRepository: no user signed in");
}
return authentication.getName();
}
};
}
@Bean
public ConnectController connectController(ConnectionFactoryLocator connectionFactoryLocator, ConnectionRepository connectionRepository) {
return new ConnectController(connectionFactoryLocator, connectionRepository);
}
@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public LinkedIn linkedin(ConnectionRepository repository) {
Connection<LinkedIn> connection = repository.findPrimaryConnection(LinkedIn.class);
return connection != null ? connection.getApi() : null;
}
@Override
public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
return new JdbcUsersConnectionRepository(dataSource, connectionFactoryLocator, Encryptors.noOpText());
}
}
和我的WEBAPP文件夹中的视图
正如我在日志中看到的,发生了一些事情,发现了/connect/*
映射
[localhost-startStop-1] INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/connect/{providerId}],methods=[POST]}" onto public org.springframework.web.servlet.view.RedirectView org.springframework.social.connect.web.ConnectController.connect(java.lang.String,org.springframework.web.context.request.NativeWebRequest)
[localhost-startStop-1] INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/connect/{providerId}],methods=[GET]}" 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)
[localhost-startStop-1] INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/connect],methods=[GET]}" onto public java.lang.String org.springframework.social.connect.web.ConnectController.connectionStatus(org.springframework.web.context.request.NativeWebRequest,org.springframework.ui.Model)
[localhost-startStop-1] INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/connect/{providerId}],methods=[GET],params=[oauth_token]}" onto public org.springframework.web.servlet.view.RedirectView org.springframework.social.connect.web.ConnectController.oauth1Callback(java.lang.String,org.springframework.web.context.request.NativeWebRequest)
[localhost-startStop-1] INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/connect/{providerId}],methods=[GET],params=[code]}" onto public org.springframework.web.servlet.view.RedirectView org.springframework.social.connect.web.ConnectController.oauth2Callback(java.lang.String,org.springframework.web.context.request.NativeWebRequest)
[localhost-startStop-1] INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/connect/{providerId}],methods=[GET],params=[error]}" 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)
[localhost-startStop-1] INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/connect/{providerId}],methods=[DELETE]}" onto public org.springframework.web.servlet.view.RedirectView org.springframework.social.connect.web.ConnectController.removeConnections(java.lang.String,org.springframework.web.context.request.NativeWebRequest)
[localhost-startStop-1] INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/connect/{providerId}/{providerUserId}],methods=[DELETE]}" 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)
[local
我正在使用Thymeleaf3.0
@Bean
public ViewResolver viewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
resolver.setCharacterEncoding("UTF-8");
resolver.setContentType("text/html; charset=UTF-8");
return resolver;
}
@Bean
public TemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setEnableSpringELCompiler(true);
engine.setTemplateResolver(templateResolver());
engine.addDialect(new SpringSecurityDialect());
return engine;
}
private ITemplateResolver templateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(applicationContext);
resolver.setPrefix("/views/");
resolver.setSuffix(".html");
resolver.setTemplateMode(TemplateMode.HTML);
resolver.setCharacterEncoding("UTF-8");
resolver.setCacheable(Boolean.parseBoolean(THYMELEAF_CACHE));
return resolver;
}
调度程序配置
public class WebAppInitializer implements WebApplicationInitializer {
private final String APP_SERVLET_NAME = "x";
private final String DISPLAY_NAME = "App";
@Override
public void onStartup(ServletContext container) {
container.addListener(new ContextLoaderListener(getContext()));
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("utf-8");
characterEncodingFilter.setForceEncoding(true);
container.addFilter("charEncodingFilter", characterEncodingFilter).addMappingForUrlPatterns(null, false, "/*");
container.addFilter("securityFilter", new DelegatingFilterProxy("springSecurityFilterChain")).addMappingForUrlPatterns(null, false, "/*");
container.addFilter("apiFilter", new DelegatingFilterProxy("apiExceptionHandler"));
container.addFilter("hidden", new HiddenHttpMethodFilter());
AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext();
dispatcherServlet.register(ServletConfig.class);
ServletRegistration.Dynamic dispatcher = container.addServlet(APP_SERVLET_NAME, new DispatcherServlet(dispatcherServlet));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
private AnnotationConfigWebApplicationContext getContext() {
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(JPARepositoryConfig.class,
ThymeleafConfig.class,
WebSecurityConfig.class,
SocialConfig.class,
MailConfig.class,
MongoRepositoryConfig.class,
ServiceConfig.class,
CacheConfig.class);
rootContext.setDisplayName(DISPLAY_NAME);
return rootContext;
}
我在Spring的连接控制器中设置断点,并调用了非映射方法。所以我认为应用程序配置是一团糟
编辑
我有错误的SpringServlet配置。我将spring社交控制器注册为根servlet 我猜您可能需要在application.properties文件中使用spring.social.auto connection views=true
我认为问题在于视图的位置
它位于src/main/webapp
下,spring正在src/main/resources
下搜索视图
将所有html文件移动到src/main/resources/views
或更改配置,以便spring在src/main/webapp/views
下搜索
检查此项配置与您拥有的配置相似,但视图位于src/main/resources
文件夹下。我知道已经很久了,但我想我找到了解决此问题的方法
问题是,您的spring配置发现“ConnectController”是bean,但它没有映射为控制器。此外,我还检查了ConnectController类的源代码。事实证明,它基于前缀和providerId构建视图名称
以下是答案:
我继承了ConnectController,并重写了“connectView”和“connectedView”,现在指向WEB-INF文件夹中的Freemarkers模板
@Controller
public class CustomConnectController extends ConnectController{
@Autowired
public CustomConnectController(
ConnectionFactoryLocator connectionFactoryLocator,
ConnectionRepository connectionRepository) {
super(connectionFactoryLocator, connectionRepository);
}
@Override
protected String connectedView(String providerId){
return "connect/facebookConnected";
}
@Override
protected String connectView(String providerId) {
return "connect/facebookConnect";
}
}
我根本没有application.properties文件。我使用的是Spring MVC,而不是Boot.well“redirect:/connect/linkedin”如果设置为true,通常会重定向到这些视图,如果您没有设置此标志,我想您需要提供自己的视图来建立连接,或者以某种方式将此标志设置为true(我不是Spring MVC用户,因此无法帮助)非常确定,您只需要提供自己的视图就可以重定向到您的社交媒体进行登录,因为这样的重定向不应该起作用(它应该只在带有标志的引导中起作用,该标志提供了一些隐藏的视图和配置)。正如您所见,我提供了自定义视图(webapp/views/connect/*
)也许我应该把这些文件移到其他地方?你重定向到“connect/linkedin”,但在(webapp/views/connect/*)中你没有“linkedin”视图,这就是为什么你得到404 imo。嘿@Jakub,你能试着为spring controller
设置调试日志级别吗?log4j.logger.controller=DEBUG,consoleCoull您可以在GitHub上创建一个简单的项目来复制此错误吗?帮助你会更容易。
@Controller
public class CustomConnectController extends ConnectController{
@Autowired
public CustomConnectController(
ConnectionFactoryLocator connectionFactoryLocator,
ConnectionRepository connectionRepository) {
super(connectionFactoryLocator, connectionRepository);
}
@Override
protected String connectedView(String providerId){
return "connect/facebookConnected";
}
@Override
protected String connectView(String providerId) {
return "connect/facebookConnect";
}
}