3

Spring は html ビューを見つけることができず、開こうとしているときに 404 が表示されます/connect/linkedinその投稿を参照して、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());
    }
}

/linkedinマッピング ハンドラーを使用した私の HomeController

@Inject
private ConnectionRepository connectionRepository;

@Inject
private LinkedIn linkedIn;

@Inject
public HomeController(LinkedIn linkedIn) {
    this.linkedIn = linkedIn;
}

@RequestMapping(value = "/linkedin", method = RequestMethod.GET)
public String home(Principal currentUser, Model model) {
    Connection<LinkedIn> connection = connectionRepository.findPrimaryConnection(LinkedIn.class);
    if (connection == null) {
        return "redirect:/connect/linkedin";
    }
    model.addAttribute("profile", connection.getApi().profileOperations().getUserProfileFull());
    return "linkedin/profile";
}

そして、ビューを含む私の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

私はThymeleaf 3.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;
    }

Connection ControllerSpring Social からブレークポイントを設定しましたが、マップされたメソッド以外が呼び出されました。だから、アプリの設定がめちゃくちゃだと思います。

編集 私はSpringサーブレットの設定が間違っていました。Spring ソーシャル コントローラーをルート サーブレットとして登録しました。

4

3 に答える 3

0

私の推測ではspring.social.auto-connection-views=true、あなたの application.properties ファイルで必要になるかもしれません

于 2016-05-26T08:08:17.107 に答える
-1

長い時間が経ちましたが、この問題の解決策を見つけたと思います。

問題は、Spring 構成で「ConnectController」が Bean として検出されたが、コントローラーとしてマップされていないことです。また、ConnectController クラスのソースを確認しました。プレフィックスと providerId に基づいてビュー名を作成することが判明しました。

この回答に続いて: https://stackoverflow.com/a/19699607/4544269

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

   }
于 2016-08-02T15:26:40.770 に答える