首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在登录到spring boot auth2 facebook中的特定uri后重定向

如何在登录到spring boot auth2 facebook中的特定uri后重定向
EN

Stack Overflow用户
提问于 2019-03-02 20:05:25
回答 2查看 667关注 0票数 0

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

我不知道如何在成功登录后重定向到我的html页面。

这是我的起点:

代码语言:javascript
复制
@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

代码语言:javascript
复制
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方法中,但这只会带来更多的问题,比如缺少依赖项、缺少bean等等

代码语言:javascript
复制
.and()
.oauth2Login().defaultSuccessUrl("/after");

有人能给点建议吗?

EN

回答 2

Stack Overflow用户

发布于 2019-03-03 23:43:43

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

代码语言:javascript
复制
@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);
    }


}
票数 1
EN

Stack Overflow用户

发布于 2020-01-06 23:04:17

还可以检索预先配置的弹簧过滤器并更改其属性:

代码语言:javascript
复制
@EventListener
public void retrieveAuthenticationFilter(ApplicationStartedEvent event){
    FilterChainProxy filterChain = (FilterChainProxy) event.getApplicationContext().getBean(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME);
    filterChain.getFilterChains().stream()
            .map(f -> f.getFilters())
            .flatMap(List::stream)
            .filter(f -> f.getClass().isAssignableFrom(OAuth2ClientAuthenticationProcessingFilter.class))
            .map(f -> (OAuth2ClientAuthenticationProcessingFilter)f)
            .findFirst()
            .ifPresent(this::configureAuthenticationFilter);
}

private void configureAuthenticationFilter(OAuth2ClientAuthenticationProcessingFilter authenticationFilter){
    SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
    successHandler.setUseReferer(true);
    authenticationFilter.setAuthenticationSuccessHandler(successHandler);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54958335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档