当前位置: 首页 > 知识库问答 >
问题:

Spring Security OAuth2舞蹈并获取参数

范豪
2023-03-14

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

在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%26级别%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的解决方案--在重定向到登录时保留URL参数),但它不起作用(AuthenticationProcessingFilterEntryPoint.Commitence 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());
        }
    }

会有什么不对的?

共有1个答案

李和昶
2023-03-14

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

    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);
        }

    }

请纠正我是否有更好的方法

 类似资料:
  • 前面的文字介绍了舞蹈链,这里就不详细描述什么是舞蹈链了,舞蹈链(Dancing links)是一种数据结构,可以用来实现X算法,以解决精确覆盖问题。本篇的内容主要把舞蹈链Dancing links应用于实际问题,通过实践才能帮助大家更好的理解,片面的了解理论知识,最终不能落到实处,也就没有意义了。 当然,把Dancing links应用于实际问题时,只有一个难点,就是如何把具体的问题转换为可以精确

  • 问题 集合 s = { x_1,x_2, cdots ,x_n } 拥有 n 个成员,现在集合 s 有 m 个子集 { sub_1,sub_2, cdots ,sub_m } 。在 m 个子集中选择一些组成集合 t = { sub_1,sub_2, cdots } ,使 t 中包含的成员可以覆盖集合 s ,即 s 中所有成员都属于 t 中的某个或某些子集。 重复覆盖:集合 s 中的任意成员 for

  • 舞蹈链(Dancing links)实际上是一种数据结构,可以用来实现 X算法,以解决精确覆盖问题。 什么是精确覆盖(Exact Cover)问题呢?维基百科上对精确覆盖的定义如下:在一个全集X中若干子集的集合为S。S* 是 S的一个子集,当且仅当X中的每一个元素在S*中恰好出现一次时,S*称之为一个精确覆盖。在计算机科学中,精确覆盖问题指找出这样的一种覆盖,或证明其不存在。这是一个NP-完全问题

  • 上一章节,主要介绍了一下如何使用不同 location 进行协作,对 location 进行糅合,往往都是要需要参数的二次调整。如何正确获取传递参数、设置参数,就是你的必修课了。本章目的是给出在 OpenResty 的世界中,我们如何正确获取、设置 uri 参数。 获取请求 uri 参数 首先看一下官方 API 文档,获取一个 uri 有两个方法:ngx.req.get_uri_args、ngx.

  • 我们经常需要获取用户传递的数据,包括 Get、POST 等方式的请求,beego 里面会自动解析这些数据,你可以通过如下方式获取数据: GetString(key string) string GetStrings(key string) []string GetInt(key string) (int64, error) GetBool(key string) (bool, error) Get

  • 问题内容: 我已经为两个窗口/阶段编写了一个控制器。在MainClass中打开第一个窗口。如果用户单击按钮,则控制器中的第二个按钮。如何在applyFor()方法中从second.fxml获取TextFields? 谢谢。 问题答案: 在fxml之间共享控制器不是很好,除非它们具有相同的用途。在这里,这两个fxml似乎有不同的用途(帐户管理,登录或其中一个类似,并为另一个创建新帐户)。更糟糕的是,