记录一次spring发布的oauth2服务器demo学习——第一步,跑demo

背景

几周前,spring就在YouTube上发布了最新的Spring Authorization Server项目使用demo。刚好我过去写个类似的oauth2认证服务器demo,虽说基础的授权码模式认证实现了,但是代码逻辑不是很标准。
故这次就看看spring发布的demo。

Spring Authorization Server项目地址:https://github.com/spring-projects/spring-authorization-server
YouTube视频地址:https://www.youtube.com/watch?v=ZIjqDIdFyBw&t=2512s

拉下代码,用intellij idea打开

我们可以看到sample下有四个案例,由上至下分别是authorization、authorization-custom-consent-page、client、resource。

查看README文件


IMPORTANT: Make sure to modify your /etc/hosts file to avoid problems with session cookie overwrites between client and authorizationserver. Simply add the entry 127.0.0.1 auth-server
重点:修改hosts文件,为了授权,资源服务器与client服务器的host不一样,避免session cookie串号,配置auth-server域名未127.0.0.1 。
在这里有个坑,即使windows的hosts改了以后还是会有代理问题。在linux系统上没有这个问题。

windwos
image

cnetos

所以如果是windows系统建议直接将client或者授权服务器的ip设置为除了本地网卡的另一个网卡的ip(就是你本机除了127.0.0.1以外,别的局域网ip)
我选择的是直接全局替换auth-server172.18.128.1
image

启动项目

先启动authorization服务再启动resource服务。最后启动client。
直接访问client首页(http://127.0.0.1:8080),浏览器显示oauth2认证地址,详情下文说。
image

输入账号密码登录,浏览器显示client首页,详情下文说。
image

至此demo成功跑起来

错误记录

  • org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration':
    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration': Unsatisfied dependency expressed through method 'setFilterChains' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityFilterChain' defined in class path resource [sample/config/ResourceServerConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.web.SecurityFilterChain]: Factory method 'securityFilterChain' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jwtDecoderByIssuerUri' defined in class path resource [org/springframework/boot/autoconfigure/security/oauth2/resource/servlet/OAuth2ResourceServerJwtConfiguration$JwtDecoderConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.oauth2.jwt.JwtDecoder]: Factory method 'jwtDecoderByIssuerUri' threw exception; nested exception is java.lang.IllegalArgumentException: Unable to resolve the Configuration with the provided Issuer of "http://172.18.128.1:9000"
    需要先构建下项目,因为认证服务依赖于 oauth2-authorization-server 项目,而这个项目我们是本地源代码,需要先构建一遍才行。指令构建项目或者idea点击该项目的build按钮
posted @ 2021-10-15 12:46  临渊不羡渔  阅读(715)  评论(0编辑  收藏  举报