有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaSpringBootSecurity不会重定向对configure(httpsecurityhttp)方法的oauth/authorize调用

我正在设置一个新的服务器,因此我必须将服务器配置为允许一些URL在不进行身份验证的情况下访问,并且我遇到了一些问题。 除了/oauth/authorize之外,其他都可以正常工作,因为在最后一个操作中,当输入凭据时,页面没有重定向,而是停留在同一个页面上

我尝试的代码如下所示,它允许我在没有任何身份验证的情况下访问URL,但是/oauth/authorize重定向不起作用

@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {

      http.authorizeRequests()
        .antMatchers("/oauth/token", "/oauth/authorize**", "/login**", "/user/recover_password", "/user/register").permitAll()
        .anyRequest().authenticated()
        .and()
        .formLogin()
        .and()
        .logout().logoutUrl("/oauth/logout")
        .and()
        .csrf().disable();
    }
}

所以,正如我所说的,我需要在不进行身份验证的情况下访问这个URL,并且能够在/oauth/authorize上重定向,以便能够使用一些外部资源登录。但我得到的是,当我输入我的凭证时,页面保持不变


共 (1) 个答案

  1. # 1 楼答案

    好的,我终于找到了修复方法,现在它开始工作了,添加了会话管理,尽管我不知道确切的原因

    我添加了以下代码:

            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
            .and()
    

    因此,完整的方法应该如下所示:

    public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
    
        @Override
        public void configure(HttpSecurity http) throws Exception {
    
          http.
          .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
            .and()
            .authorizeRequests()
            .antMatchers("/oauth/token", "/oauth/authorize**", "/login**", "/user/recover_password", "/user/register").permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .and()
            .logout().logoutUrl("/oauth/logout")
            .and()
            .csrf().disable();
        }
    }
    

    现在,/oauth/authorize正在按预期工作,我的公共URL也可以在无需身份验证的情况下访问

    所以,现在,如果有人能解释会话管理的作用,那就太好了