11

我一直在研究 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 安全系统决定什么是“登录”页面的机制是什么?

4

3 回答 3

10

解决方案是将以下内容添加到 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”,但它也适用于“/”或我选择的任何其他内容)。我不知道本教程应该如何在不明确添加此行的情况下进行重定向。

于 2016-03-22T18:06:45.760 回答
3

请按照这个 答案。 1.您必须在 Apache 代理上设置标头:

<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 Boot 应用程序使用这些标头。所以在你的 application.properties (或 Spring Boots 理解属性的任何其他地方)中放入以下行:

server.use-forward-headers=true
于 2016-12-09T07:04:12.837 回答
-1

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

于 2016-03-14T00:48:47.220 回答