【问题标题】:Spring boot 2.0.0.M7 and Oauth2 not triggering initial redirectSpring boot 2.0.0.M7 和 Oauth2 未触发初始重定向
【发布时间】:2018-06-06 22:24:02
【问题描述】:

我使用的是 Spring boot 2.0.0.M7,但无法让 OAuth2 工作。我让它在 M4 下为 facebook 和 github 工作。 (M5改了,当时没试过升级)。

我似乎无法触发从我的应用到 oauth 提供程序的初始重定向。使用提供者 1 (facebook),以前 /login/facebook 过滤器将重定向到外部 OAuth2。 现在...我不知道触发令牌/重定向逻辑需要点击的 URL(可能在文档中丢失?)。

https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-security-oauth2 提到 /login/oauth2/code/* - 但这似乎是针对提供商的响应,并且不会触发重定向。

我当前的配置如下(在 M4 中工作的钩子已作为 cmets 保留)

@Configuration
@EnableWebSecurity
@EnableOAuth2Client
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
// ...
@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .exceptionHandling()
            .authenticationEntryPoint(new AuthenticationEntryPoint() {
                @Override
                public void commence(HttpServletRequest request, HttpServletResponse response,
                        AuthenticationException authException) throws IOException, ServletException {
                    response.sendRedirect("/login");
                }
            })
            .and()
        .authorizeRequests()
            .antMatchers("/", "/favicon.ico", "/static/**", "/login", "/login/**", "/logout", "/at/**").permitAll()
            .anyRequest().authenticated()
        .and()
//      .addFilterBefore(new OAuth2ClientContextFilter(), BasicAuthenticationFilter.class)
//      .addFilterBefore(oauthFilter("facebook"), BasicAuthenticationFilter.class)
//      .addFilterBefore(githubFilter(), BasicAuthenticationFilter.class)
        .csrf()
            .disable()
        .rememberMe()
            .alwaysRemember(true)
        ;

        http.httpBasic().disable();
        http.formLogin().disable();
    }
// private Filter facebookFilter() {
//  @Bean @ConfigurationProperties("facebook.client") public AuthorizationCodeResourceDetails facebook() {
// ...
}

登录页面包含指向 /login/facebook 和 /login/github 的链接。

我的 application.yml 是:

spring.security.oauth2.client.registration:
  # developer.facebook.com
  facebook:
    client-id: redacted
    client-secret: redacted
  #https://github.com/settings/applications/redacted
  github:
    client-id: redacted
    client-secret: redacted

因为它可能会有所帮助,所以从 build.gradle 中选择了一些依赖项:

compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.security.oauth:spring-security-oauth2:2.2.1.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-security'
compile 'org.springframework:spring-context:5.0.2.RELEASE'
compile 'org.springframework.security:spring-security-web:5.0.0.RELEASE'
compile 'org.springframework.security:spring-security-oauth2-client:5.0.0.RELEASE'

我花了很长时间在这个圈子里。欢迎任何帮助:) 谢谢

【问题讨论】:

    标签: spring-mvc spring-boot oauth-2.0 spring-security-oauth2


    【解决方案1】:

    所以

    compile 'org.springframework.security.oauth:spring-security-oauth2:2.2.1.RELEASE'
    

    compile 'org.springframework.security:spring-security-oauth2-client:5.0.0.RELEASE'
    

    完全不兼容。 几乎没有关于新的做事方式的文档。缺少信息和过时的指南。

    因为我在这里:

            //the spring security 5 way
            //OAuth2AuthorizationRequestRedirectFilter based config...
            http.addFilterAfter(new OAuth2AuthorizationRequestRedirectFilter(clientRegistrationRepository), BasicAuthenticationFilter.class);
            http.addFilterAfter(oAuth2LoginAuthenticationFilter(), OAuth2AuthorizationRequestRedirectFilter.class);
    

    连同 application.yml 配置:

    spring.security.oauth2.client.registration:
    

    解决了。当示例和文档赶上 Spring 5 时会很好

    【讨论】:

      猜你喜欢
      • 2018-08-13
      • 2020-04-15
      • 2018-06-17
      • 1970-01-01
      • 2018-06-20
      • 1970-01-01
      • 2018-06-29
      • 2018-05-17
      • 2015-10-28
      相关资源
      最近更新 更多