Spring Oauth2 登录重定向如何工作?

How does Spring Oauth2 login redirect work?

我一直在研究 Spring Boot Oauth2 教程,但我似乎无法让一个非常关键的元素起作用:

https://spring.io/guides/tutorials/spring-boot-oauth2/

我想运行作为授权服务器。我已尽可能严格地按照说明进行操作,但是当我转到 /oauth/authorize 端点时,我得到的只是 403 Forbidden 响应。鉴于本教程设置的 HttpSecurity 配置,这对我来说实际上很有意义:

protected void configure(HttpSecurity http) throws Exception {
    http
      .antMatcher("/**")
      .authorizeRequests()
        .antMatchers("/", "/login**", "/webjars/**")
        .permitAll()
      .anyRequest()
        .authenticated()
        .and().logout().logoutSuccessUrl("/").permitAll()
        .and().csrf().csrfTokenRepository(csrfTokenRepository())
        .and().addFilterAfter(csrfHeaderFilter(), CsrfFilter.class)
        .addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);
}

本教程的登录页面实际上是主要索引,我绝对没有在教程中看到任何指示 Oauth 系统将登录流程重定向到那里的内容。

我可以通过添加这个来让它工作:

        .and().formLogin().loginPage("/")

...但在继续之前,我真的很想了解这是否是教程或我的实施问题或其他问题。 Oauth 安全系统决定什么是 "login" 页面的机制是什么?

您不需要直接转到 /oauth/authorize 端点。这是在幕后使用的,index.html 页面是本教程演示的页面。

解决方案是将以下内容添加到 SecurityConfig.configure 调用中:

@Override
protected void configure(HttpSecurity http) throws Exception {
    AuthenticationEntryPoint aep = new AuthenticationEntryPoint() {

        @Override
        public void commence(HttpServletRequest request,
                HttpServletResponse response,
                AuthenticationException authException) throws IOException,
                ServletException {
            response.sendRedirect("/login");
        }
    };

    http.exceptionHandling()
            .authenticationEntryPoint(aep)

它将身份验证流程重定向到特定的 URL(在本例中,我将其发送到“/login”,但它也适用于“/”或我选择的任何其他内容)。我不知道教程应该如何在不显式添加此行的情况下进行重定向。

请按照this回答。 1. 您必须在 Apache 代理上设置 headers:

<VirtualHost *:443>
    ServerName www.myapp.org
    ProxyPass / http://127.0.0.1:8080/
    RequestHeader set X-Forwarded-Proto https
    RequestHeader set X-Forwarded-Port 443
    ProxyPreserveHost On
    ... (SSL directives omitted for readability)
</VirtualHost>

2. 你必须告诉你的 Spring 引导应用程序使用这些 headers。因此,将以下行放入 application.properties(或 Spring Boots 理解属性的任何其他位置):

server.use-forward-headers=true