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

时间:2016-03-13 22:23:55

标签: spring spring-security spring-security-oauth2 spring-oauth2

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

3 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:3)

请按this回答。 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

答案 2 :(得分:-1)

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

相关问题