简单跳过登录认证的方法
启动类上
@SpringBootApplication
换成
@SpringBootApplication(exclude= {SecurityAutoConfiguration.class })
注意:上面方法是临时用 后续要正常使用登录认证方法需要改回来
[java.lang.IllegalStateException: This object has not been built]
原因是启用类上的@SpringBootApplication(exclude= {SecurityAutoConfiguration.class })
注解 导致配置类没加载, 改成原先的@SpringBootApplication
就行了
org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 : [no body]
解决方法:
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new DefaultResponseErrorHandler());
return restTemplate;
}
改成
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setOutputStreaming(false);
RestTemplate restTemplate = new RestTemplate(requestFactory);
restTemplate.setErrorHandler(new DefaultResponseErrorHandler());
return restTemplate;
}
就能看到具体的错误原因了
401 : “{“timestamp”:“2023-05-31T12:00:12.072+08:00”,“status”:401,“error”:“Unauthorized”,“message”:“Unauthorized”,“path”:”/oauth/check_token"}"
- postman 调用check_token出现401, 需要如下配置
public class AuthConfig extends AuthorizationServerConfigurerAdapter {
//.......
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) {
oauthServer.allowFormAuthenticationForClients();
// 放开check_token api, 允许不登陆访问
oauthServer.checkTokenAccess("permitAll()");
}
}
- postman 调用check_token 200, 但是带token访问api出现check_token 401问题
解决方法:确保颁布token的client id和配置里面的配置是一致的
即这两张图片上的client_id得是一致的
遇到莫名其妙问题 需要debug源码的
application.yml 文件里 配置debug模式
logging:
level:
org.springframework: DEBUG
这样可以看到完整的方法调用过程,方便找到问题所在