ZPY博客

getForObject访问接口报错org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 null

用restTemplate.getForObject访问认证服务器的接口/check/token时报错

org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 null

原因是配置文件里没有将/check/token加入例外,导致需要认证所以报401

解决方法将/check/token加入白名单即可。设置如下:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.requestMatchers()
                .antMatchers("/login", "/check/token", "/oauth/authorize", "/oauth/token")
                .and()
                .authorizeRequests()
                .antMatchers("/check/token")
                .permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin().permitAll();
    }