学习Oauth2授权码授权时遇到访问 oauth/authorize 返回403。
查源码发现是在AuthorizationEndpoint类中的authorize的方法里抛出了异常:
User must be authenticated with Spring Security before authorization can be completed.
在这里插入图片描述

抛出异常的原因是principal == null,是没有配置登录方式导致的。
解决方法:修改继承了WebSecurityConfigurerAdapter的配置类中的protected void configure(HttpSecurity http)方法,配置登录方式

  1. 使用fromlogin登录
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin().and().authorizeRequests()
                .antMatchers("/**")
                .permitAll();
    }
  1. 使用基础的对话框登录
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.httpBasic().and().authorizeRequests()
                .antMatchers("/**")
                .permitAll();
    }

更多推荐