* * [功能简介](#功能简介) * [sso对接](#sso对接) * [服务端配置](#服务端配置) * [客户端配置](#客户端配置) * [单点登录测试用例](#单点登录测试用例) * [锁定应用](#锁定应用) * [正常应用](#正常应用) * [问题总结](#问题总结) * [报错二:](#报错二) * [测试用例](#测试用例) ## 一、功能简介 auth-sso是一个单点功能,这个系统是`auth-sever`的一个客户端服务。 ## 二、sso对接 auth-sso应与auth-server的对接。 ### [](#服务端配置)服务端配置 ![image](https://s3.ax1x.com/2020/12/15/rK3R7F.png?ynotemdtimestamp=1608128738913) ### [](#客户端配置)客户端配置 > 在客户端应有一个客户端秘钥的配置: auth-sso中`application.yml`的配置内容为: ~~~ security: ignored: /,/favicon.ico,/home.html,/dashboard.html,/js/**,/css/**,/webjars/** sessions: ALWAYS user: password: 123456 oauth2: sso: login-path: /dashboard/login client: client-id: owen client-secret: owen user-authorization-uri: http://127.0.0.1:9200/api-auth/oauth/authorize #直接配置认证中心端口(http://127.0.0.1:9200/oauth/authorize),也可以配置网关端口 access-token-uri: http://127.0.0.1:9200/api-auth/oauth/token #直接配置认证中心端口(http://127.0.0.1:9200/oauth/authorize),也可以配置网关端口 resource: # user-info-uri: http://127.0.0.1:8000/auth/users #返回认证服务器检查 # prefer-token-info: false token-info-uri: http://127.0.0.1:9200/api-auth/oauth/check_token #直接配置认证中心端口(http://127.0.0.1:9200/oauth/authorize),也可以配置网关端口 prefer-token-info: true ~~~ ## 三、单点登录测试用例 访问地址:[http://127.0.0.1:9997/dashboard/](http://127.0.0.1:9997/dashboard/) ### [](#锁定应用)锁定应用 锁定应用“owen”。 访问sso主页,auth-server返回: ~~~ [auth-server:169.254.80.80:8000] [f55fce2e23409855,f55fce2e23409855] 2020-12-15 11:20:53.685 ERROR 15308 [http-nio-8000-exec-8] com.open.capacity.uaa.server.service.RedisClientDetailsService clientId:owen,owen [auth-server:169.254.80.80:8000] [f55fce2e23409855,f55fce2e23409855] 2020-12-15 11:20:53.685 ERROR 15308 [http-nio-8000-exec-8] com.open.capacity.uaa.server.service.RedisClientDetailsService clientId:owen,owen [auth-server:169.254.80.80:8000] [f55fce2e23409855,f55fce2e23409855] 2020-12-15 11:20:53.686 INFO 15308 [http-nio-8000-exec-8] org.springframework.security.oauth2.provider.endpoint.AuthorizationEndpoint Handling OAuth2 error: error="invalid_client", error_description="应用获取失败" ~~~ 浏览器返回: > OAuth Error error="unsupported\_response\_type", error\_description="??????", code="400", msg="??????" 该日志信息是由org.springframework.security.oauth2.common.exceptions.OAuth2Exception返回的。 ### [](#正常应用)正常应用 ## 四、问题总结 client端无法登录,auth-server报错: ~~~ [auth-server:169.254.80.80:8000] [7af92e2ba3037cf2,7af92e2ba3037cf2] 2020-12-15 14:03:59.606 WARN 18032 [http-nio-8000-exec-9] org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder Empty encoded password ~~~ 原因分析: > 刚好看到一篇博文:[《Spring Security 中的 BCryptPasswordEncoder加密、验证策略》](https://blog.csdn.net/u013019701/article/details/110249239) 应用owen的原始加密秘钥为:`$2a$10$a1ZEXiZQr604LN.wVxet.etPm6RvDs.HIaXP48J2HKRaEnZORTVwe`。 在系统中进行保存后,生成新的秘钥为:`$2a$10$.t/BRbvOwUsK5RS4.Jfnnu1BO6M3tthLGdM4o9KqPX3/SKjRy6Xoy`。很明显二者的加密算法是不一致的,作者真是神坑啊。 源码分析: ~~~ matches:112, BCryptPasswordEncoder (org.springframework.security.crypto.bcrypt) matches:592, WebSecurityConfigurerAdapter$LazyPasswordEncoder (org.springframework.security.config.annotation.web.configuration) additionalAuthenticationChecks:90, DaoAuthenticationProvider (org.springframework.security.authentication.dao) authenticate:166, AbstractUserDetailsAuthenticationProvider (org.springframework.security.authentication.dao) authenticate:175, ProviderManager (org.springframework.security.authentication) ~~~ ### [](#报错二)报错二: ~~~ { "msg": "Failed to handle request [GET http://127.0.0.1:9200/api-auth/oauth/authorize?client_id=owen&redirect_uri=http://127.0.0.1:9997/dashboard/login&response_type=code&state=lhzdiY]: 503 SERVICE_UNAVAILABLE \"Unable to find instance for auth-server\"", "code": 404 } ~~~ 【出错原因】以上报错是auth-server和auth-sso刚刚重启后会出现,原因是这两个服务重新启动后,需要在nacos中完成服务注册,nacos需要一点点时间确认服务可用才会向外暴露。 OAuth2客户端登录: 获取token:org.springframework.security.oauth2.client.OAuth2RestOperations#getAccessToken ## [](#测试用例)测试用例 > 场景:在已登录过的前提下,再次点击login;调试到该位置会报错。 > 报错位置:`/org/springframework/security/oauth2/client/token/grant/code/AuthorizationCodeAccessTokenProvider.java:204` 错误描述:request的size显示为0,但是resource中是有数据的。如图: 1-源码调试:![](https://i0.hdslb.com/bfs/album/408a4ed694d8bca56966b7efea42df2ded019912.png@1e_1c.webp?ynotemdtimestamp=1608128738913) 继续调试,但是在抛出的异常中能看到`stateKey`:![](https://i0.hdslb.com/bfs/album/85c2fb6f825004a1db96a1f8922a24ef8c0255c3.png@1e_1c.webp?ynotemdtimestamp=1608128738913)