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

Rya*_*yan 9 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);
}
Run Code Online (Sandbox Code Playgroud)

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

我可以通过添加以下内容来实现它:

        .and().formLogin().loginPage("/")
Run Code Online (Sandbox Code Playgroud)

...但在继续前进之前,我真的很想了解这是教程中的问题还是我对它的实现或其他问题.Oauth安全系统决定"登录"页面的机制是什么?

Rya*_*yan 8

解决方案是将以下内容添加到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)
Run Code Online (Sandbox Code Playgroud)

其中将身份验证流重定向到特定URL(在这种情况下,我将其发送到"/ login",但它也适用于"/"或我选择的任何其他内容).我不知道教程应该如何在没有明确添加此行的情况下进行重定向.