SpringBoot+SpringSecurity OAuth2 认证服务搭建实战 (二)

上一篇文章的代码git地址:Demo_01

1.查看授权服务的Meta信息

先启动下项目

2024-06-19T16:36:49.679+08:00  INFO 24640 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2024-06-19T16:36:49.679+08:00  INFO 24640 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2024-06-19T16:36:49.679+08:00  INFO 24640 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 0 ms
2024-06-19T16:43:48.528+08:00  INFO 24640 --- [   File Watcher] rtingClassPathChangeChangedEventListener : Restarting due to 1 class path change (0 additions, 0 deletions, 1 modification)
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v3.3.0)

2024-06-19T16:43:48.588+08:00  INFO 24640 --- [  restartedMain] c.o.s.demo.OauthServerDemoApplication    : Starting OauthServerDemoApplication using Java 17.0.10 with PID 24640 (C:\Users\CN-zhangyongtaoOD\eclipse-workspace\oauth_server_demo\target\classes started by CN-zhangyongtaoOD in C:\Users\CN-zhangyongtaoOD\eclipse-workspace\oauth_server_demo)
2024-06-19T16:43:48.588+08:00  INFO 24640 --- [  restartedMain] c.o.s.demo.OauthServerDemoApplication    : No active profile set, falling back to 1 default profile: "default"
2024-06-19T16:43:48.693+08:00  INFO 24640 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)
2024-06-19T16:43:48.693+08:00  INFO 24640 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2024-06-19T16:43:48.694+08:00  INFO 24640 --- [  restartedMain] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.24]
2024-06-19T16:43:48.703+08:00  INFO 24640 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2024-06-19T16:43:48.703+08:00  INFO 24640 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 114 ms
2024-06-19T16:43:48.718+08:00  INFO 24640 --- [  restartedMain] r$InitializeUserDetailsManagerConfigurer : Global AuthenticationManager configured with UserDetailsService bean with name userDetailsService
2024-06-19T16:43:48.985+08:00  INFO 24640 --- [  restartedMain] o.s.s.web.DefaultSecurityFilterChain     : Will secure org.springframework.security.oauth2.server.authorization.config.annotation.web.configurers.OAuth2AuthorizationServerConfigurer$$Lambda$675/0x0000000800474f78@2d30f4c0 with [org.springframework.security.web.session.DisableEncodeUrlFilter@4e756719, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@39dcd08b, org.springframework.security.web.context.SecurityContextHolderFilter@2d7defbc, org.springframework.security.oauth2.server.authorization.config.annotation.web.configurers.AuthorizationServerContextFilter@41e4484f, org.springframework.security.web.header.HeaderWriterFilter@2bb1110e, org.springframework.web.filter.CorsFilter@7917fb62, org.springframework.security.web.csrf.CsrfFilter@5baf21c4, org.springframework.security.web.authentication.logout.LogoutFilter@60f6a211, org.springframework.security.oauth2.server.authorization.web.OAuth2AuthorizationServerMetadataEndpointFilter@45bd6178, org.springframework.security.oauth2.server.authorization.web.OAuth2AuthorizationEndpointFilter@56c64ee5, org.springframework.security.oauth2.server.authorization.web.OAuth2DeviceVerificationEndpointFilter@13bbc655, org.springframework.security.oauth2.server.authorization.web.NimbusJwkSetEndpointFilter@60f41fe5, org.springframework.security.oauth2.server.authorization.web.OAuth2ClientAuthenticationFilter@370ce510, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@f7e4ad6, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@12ef1ae, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@532b9674, org.springframework.security.web.access.ExceptionTranslationFilter@613dec63, org.springframework.security.web.access.intercept.AuthorizationFilter@4446e580, org.springframework.security.oauth2.server.authorization.web.OAuth2TokenEndpointFilter@39d952c6, org.springframework.security.oauth2.server.authorization.web.OAuth2TokenIntrospectionEndpointFilter@56e1cc3a, org.springframework.security.oauth2.server.authorization.web.OAuth2TokenRevocationEndpointFilter@1c7b92e5, org.springframework.security.oauth2.server.authorization.web.OAuth2DeviceAuthorizationEndpointFilter@53979a78]
2024-06-19T16:43:48.988+08:00  INFO 24640 --- [  restartedMain] o.s.s.web.DefaultSecurityFilterChain     : Will secure any request with [org.springframework.security.web.session.DisableEncodeUrlFilter@1fbdd86b, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@18c66049, org.springframework.security.web.context.SecurityContextHolderFilter@326e99ca, org.springframework.security.web.header.HeaderWriterFilter@751bc7e6, org.springframework.web.filter.CorsFilter@47790593, org.springframework.security.web.csrf.CsrfFilter@31c991a9, org.springframework.security.web.authentication.logout.LogoutFilter@76f7b301, org.springframework.security.oauth2.server.resource.web.authentication.BearerTokenAuthenticationFilter@54990642, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@4fd702f7, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@1b9e1c62, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@5b4eb028, org.springframework.security.web.access.ExceptionTranslationFilter@50a32f5, org.springframework.security.web.access.intercept.AuthorizationFilter@6ad6b6d4]
2024-06-19T16:43:48.996+08:00  INFO 24640 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2024-06-19T16:43:48.999+08:00  INFO 24640 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path '/'
2024-06-19T16:43:49.001+08:00  INFO 24640 --- [  restartedMain] c.o.s.demo.OauthServerDemoApplication    : Started OauthServerDemoApplication in 0.421 seconds (process running for 1731.901)
2024-06-19T16:43:49.001+08:00  INFO 24640 --- [  restartedMain] .ConditionEvaluationDeltaLoggingListener : Condition evaluation unchanged
2024-06-19T17:30:04.628+08:00  INFO 24640 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2024-06-19T17:30:04.635+08:00  INFO 24640 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2024-06-19T17:30:04.650+08:00  INFO 24640 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 15 ms

可以看到没什么特别的信息。

只有两个DefaultSecurityFilterChain 对象被创建了。这两个对象,一个是提供授权服务的,一个是资源服务器的。

授权服务器会对外发布一些对接信息。因为咱们这边没有使用oidc,即Open ID Connect 1.0 。

所以授权服务对外发布的地址是:http://localhost:8080/.well-known/oauth-authorization-server

 2.通过Jemter,采用客户端凭证模式 获取token

     目前咱们没有建客户端服务项目,所以就把jemter当做客户端来用,给授权服务发请求。

hello的请求,我就不上图了。源码中有的。

这个Jemter可以到apache上下载,下载地址:Apache Jemter 。下载apache-jmeter-5.6.3.zip

然后,还有上面的jemter 脚本下载地址:Demo_01 OAuth2 Jemter 脚本 

三.总结 

OAuth2 整体分三总分:

OAuth授权服务(Authorization),

OAuth资源服务(Authentication),

客户端服务(Client Service 其实就是一个web服务,它是来用OAuth的)

Spring Security 轻松的就搞定了所有配置。Nice

当然想玩转Spring Security OAuth 还有很乐子在里面。

下一篇文章再深入讲一下,其它玩儿法。

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

觉自性本然

您的鼓励与支持是我最大的动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值