spring security oauth 应用中的问题

简单跳过登录认证的方法

启动类上

@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"}"

  1. postman 调用check_token出现401, 需要如下配置
public class AuthConfig extends AuthorizationServerConfigurerAdapter {
    //.......
    @Override
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) {
        oauthServer.allowFormAuthenticationForClients();
        // 放开check_token api, 允许不登陆访问
        oauthServer.checkTokenAccess("permitAll()");
    }
}
  1. postman 调用check_token 200, 但是带token访问api出现check_token 401问题
    解决方法:确保颁布token的client id和配置里面的配置是一致的

即这两张图片上的client_id得是一致的在这里插入图片描述
在这里插入图片描述

遇到莫名其妙问题 需要debug源码的

application.yml 文件里 配置debug模式

logging:
  level:
    org.springframework: DEBUG

这样可以看到完整的方法调用过程,方便找到问题所在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值