【问题标题】:Spring Security OAuth2 dance and get parametersSpring Security OAuth2 跳舞并获取参数
【发布时间】:2016-04-14 01:11:41
【问题描述】:

在我的 Java Spring 应用程序中,我通过外部 OAuth2 提供程序实现了 OAuth2 用户授权。

在我的本地主机上,为了通过这个外部 OAuth2 提供程序对用户进行身份验证,我需要通过以下 url:https://127.0.0.1:8443/login/ok 并且在 OAuth2 舞蹈之后,我可以让这个用户通过身份验证。到目前为止一切正常。

但是当我的登录 url 中有一些请求参数时,例如 uid 和 level:

https://127.0.0.1:8443/login/ok?uid=45134132&level=3

在 OAuth2 跳舞之后,我被重定向到 https://127.0.0.1:8443/ 并丢失了这些参数。

在我的 Chrome 网络面板中,我可以看到以下一组调用:

  1. https://127.0.0.1:8443/login/ok?uid=45134132&level=3
  2. https://connect.ok.ru/oauth/authorize?redirect_uri=https://127.0.0.1:8443/login/ok?uid%3D45134132%26level%3D3&response_type=code&state=AKakq....
  3. https://127.0.0.1:8443/login/ok?uid=45134132&level=3&code=....
  4. https://127.0.0.1:8443/

所以我在第 3 步之后丢失了这些参数。

是否可以配置 Spring Security + OAuth2 以将这些参数也传递给步骤#4?

这是我的配置(这是基于此答案 Spring Security - Retaining URL parameters on redirect to login 的解决方案)但它不起作用(AuthenticationProcessingFilterEntryPoint .commence method 未被调用):

    @Override
    public void configure(HttpSecurity http) throws Exception {
        // @formatter:off   
        http
        .headers().frameOptions().disable()
        .and().logout()
        .and().antMatcher("/**").authorizeRequests()
            .antMatchers("/", "/login**", "/index.html", "/home.html").permitAll()
            .anyRequest().authenticated()
        .and().exceptionHandling().authenticationEntryPoint(new AuthenticationProcessingFilterEntryPoint("/"))
        .and().logout().logoutSuccessUrl("/").permitAll()
        .and().csrf().csrfTokenRepository(csrfTokenRepository())
        .and().addFilterAfter(csrfHeaderFilter(), CsrfFilter.class)
        .addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);
        // @formatter:on
    }

    public class AuthenticationProcessingFilterEntryPoint extends LoginUrlAuthenticationEntryPoint {
        public AuthenticationProcessingFilterEntryPoint(String loginFormUrl) {
            super(loginFormUrl);
        }

        @Override
        public void commence(HttpServletRequest request, HttpServletResponse response,
                AuthenticationException authException) throws IOException, ServletException {
            RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
            redirectStrategy.sendRedirect(request, response, getLoginFormUrl() + "?" + request.getQueryString());
        }
    }

有什么问题?

【问题讨论】:

  • when I have a GET parameters in my login url ...我不太清楚。你能详细说明一下吗?您是在谈论 http GET 方法还是其他?
  • 我说的是这个 url - https://127.0.0.1:8443/login/ok?uid=45134132&level=3 在 GET 参数下我的意思是这部分 url - uid=45134132&level=3
  • 第 4 步是可疑的。 IMO 它不应该在那里。我猜当/login/ok?level=...&uid=... 被执行时,你的应用程序出了点问题。您的代码的某些部分可能会引发异常,并且 ExceptionHandler 会将您重定向到 / 。总结一下我的印象:问题可能与您的安全配置没有直接关系。
  • @ben75,感谢您的回答。请看下面的代码。它现在可以工作了。但我不确定我是否以正确的方式实现它。

标签: java spring spring-security spring-boot spring-security-oauth2


【解决方案1】:

我通过以下方式实现了这一点:

    private Filter ssoFilter(ClientResources client, String path) {
        OAuth2ClientAuthenticationProcessingFilter clientFilter = new OAuth2ClientAuthenticationProcessingFilter(path);
        .......
        clientFilter.setAuthenticationSuccessHandler(new UrlParameterAuthenticationHandler());
        return clientFilter;
    }

    public class UrlParameterAuthenticationHandler extends SimpleUrlAuthenticationSuccessHandler {

        @Override
        protected void handle(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
                throws IOException, ServletException {
            String targetUrl = determineTargetUrl(request, response);

            if (response.isCommitted()) {
                logger.debug("Response has already been committed. Unable to redirect to " + targetUrl);
                return;
            }

            String queryString = HttpUtils.removeParams(request.getQueryString(), "state", "code");
            targetUrl = !StringUtils.isEmpty(queryString) ? targetUrl + "?" + queryString : targetUrl;
            getRedirectStrategy().sendRedirect(request, response, targetUrl);
        }

    }

如果有更好的方法请指正

【讨论】:

    猜你喜欢
    • 2013-04-17
    • 2018-07-04
    • 2019-09-25
    • 2017-12-22
    • 2021-03-21
    • 2019-04-29
    • 2017-09-11
    • 2019-04-28
    • 2015-11-14
    相关资源
    最近更新 更多