如何在春季启动oauth2 facebook登录到特定的uri后重定向

问题描述 投票:0回答:1

我正在学习本教程:https://spring.io/guides/tutorials/spring-boot-oauth2/

成功登录后,我无法弄清楚如何重定向到我们说的html页面。

这是我的出发点:

@SpringBootApplication
@EnableOAuth2Sso
public class SimpleApplication extends WebSecurityConfigurerAdapter {

    public static void main(String[] args) {
        SpringApplication.run(SimpleApplication.class, args);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .antMatcher("/**")
            .authorizeRequests()
            .antMatchers("/", "/login**", "/webjars/**", "/error**")
            .permitAll()
            .anyRequest()
            .authenticated();
    }
}

这是我的application.yml

security:
  oauth2:
    client:
      clientId: id
      clientSecret: secret
      accessTokenUri: https://graph.facebook.com/oauth/access_token
      userAuthorizationUri: https://www.facebook.com/dialog/oauth
      tokenName: oauth_token
      authenticationScheme: query
      clientAuthenticationScheme: form
    resource:
      userInfoUri: https://graph.facebook.com/me

我尝试在下面添加配置方法但它只会产生更多缺少依赖关系的问题,然后缺少bean等等

.and()
.oauth2Login().defaultSuccessUrl("/after");

有人可以建议吗?

spring spring-boot spring-mvc spring-security spring-security-oauth2
1个回答
1
投票

似乎spring安全自动配置中没有属性,所以你需要自己初始化过滤器,并在其中设置成功处理程序,这里是github中的链接

@SpringBootApplication
@Slf4j
@EnableOAuth2Sso
public class StackOverflowApplication extends WebSecurityConfigurerAdapter {



    private AuthenticationSuccessHandler successHandler() {
        return new SimpleUrlAuthenticationSuccessHandler("/after");
    }


    private OAuth2ClientAuthenticationProcessingFilter oAuth2ClientAuthenticationProcessingFilter() {
        OAuth2SsoProperties sso = (OAuth2SsoProperties)this.getApplicationContext().getBean(OAuth2SsoProperties.class);
        OAuth2RestOperations restTemplate = ((UserInfoRestTemplateFactory)this.getApplicationContext().getBean(UserInfoRestTemplateFactory.class)).getUserInfoRestTemplate();
        ResourceServerTokenServices tokenServices = (ResourceServerTokenServices)this.getApplicationContext().getBean(ResourceServerTokenServices.class);
        OAuth2ClientAuthenticationProcessingFilter filter = new OAuth2ClientAuthenticationProcessingFilter(sso.getLoginPath());
        filter.setRestTemplate(restTemplate);
        filter.setTokenServices(tokenServices);
        filter.setApplicationEventPublisher(this.getApplicationContext());
        filter.setAuthenticationSuccessHandler(successHandler());
        return filter;
    }

    public static void main(String[] args) {
        SpringApplication.run(StackOverflowApplication.class, args);
    }

    protected void configure(HttpSecurity http) throws Exception {
        http
                .antMatcher("/**")
                .authorizeRequests()
                .antMatchers("/login**", "/webjars/**", "/error**")
                .permitAll()
                .anyRequest()
                .authenticated()
                ;
        http.addFilterAfter(oAuth2ClientAuthenticationProcessingFilter(), LogoutFilter.class);
    }


}
© www.soinside.com 2019 - 2024. All rights reserved.