Как перенаправить после входа в конкретный URI в весенней загрузке auth2 facebook - PullRequest
0 голосов
/ 02 марта 2019

Я следую этому уроку: 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

Я попытался добавить ниже метод configure , но он только создает больше проблем с отсутствующей зависимостью, а затем спропущенный бин и т. д.

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

Может кто-нибудь посоветовать?

1 Ответ

0 голосов
/ 03 марта 2019

Кажется, что нет никаких свойств в автоконфигурации Spring Security, поэтому вам нужно самостоятельно запустить фильтр и установить в нем обработчик успеха, вот ссылка в 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);
    }


}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...