mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4
546 words
2 minutes
单点登录(SSO)学习笔记
2024-09-21

单点登录(SSO)学习笔记#

1. 概念#

SSO(Single Sign-On,单点登录) 是一种认证机制,用户只需登录一次,就可以访问多个相互信任的系统,无需再次输入用户名和密码。

优点#

  • 用户体验好:一次登录即可访问多个系统。
  • 降低密码管理成本,提高安全性。
  • 集中式管理:统一管理认证和权限。

缺点#

  • 单点故障风险:SSO 服务宕机可能导致所有应用无法访问。
  • 实现复杂:涉及跨域、协议、安全等问题。
  • 安全风险集中:SSO 安全漏洞会影响所有相关系统。

2. 核心原理#

SSO 的核心是 认证和信任传递

用户 -> 系统A -> SSO登录 -> Token/Cookie -> 系统A验证 -> 访问成功
用户 -> 系统B -> 验证SSO Token -> 访问成功

3. 常见 SSO 流程#

1. 用户访问系统 A,检测未登录 -> 重定向到 SSO 登录中心
2. 用户输入用户名密码 -> SSO 验证通过
3. SSO 设置认证 Cookie
4. 重定向回系统 A -> 系统 A 读取 Cookie -> 登录成功
5. 用户访问系统 B -> 带 Cookie -> 验证通过 -> 登录成功

3.2 Token(JWT)方式(示例)#

// 前端请求系统A
GET /systemA/resource
// 系统A重定向到SSO
GET /sso/login?redirectUrl=/systemA
// 用户登录SSO
POST /sso/authenticate {username,password}
// SSO 返回JWT Token
HTTP 302 Redirect to /systemA?token=eyJhbGciOiJIUzI1NiIsInR...
// 系统A验证Token
Claims claims = Jwts.parser()
.setSigningKey(secretKey)
.parseClaimsJws(token)
.getBody();
System.out.println("User: " + claims.getSubject());

4. 常见协议#

协议说明
CAS (Central Authentication Service)经典 SSO 协议,支持 Ticket 验证
OAuth2授权框架,主要用于第三方应用授权登录
OpenID Connect (OIDC)基于 OAuth2 的身份认证协议
SAML基于 XML 的企业级单点登录协议

5. 示例实现#

5.1 Spring Security + OAuth2#

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/login","/sso/**").permitAll()
.anyRequest().authenticated()
.and()
.oauth2Login()
.loginPage("/login")
.defaultSuccessUrl("/home", true);
}
}

5.2 CAS 客户端 Java 示例#

@Configuration
public class CasSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.logout().logoutSuccessUrl("/sso/logout")
.and()
.cas()
.loginUrl("https://sso.example.com/login")
.serviceProperties(serviceProperties());
}
@Bean
public ServiceProperties serviceProperties() {
ServiceProperties sp = new ServiceProperties();
sp.setService("https://app.example.com/login/cas");
sp.setSendRenew(false);
return sp;
}
}

6. 注意事项#

  1. 跨域问题:不同域下 Cookie 无法共享,需要使用 Token 或配置 CORS。
  2. 安全性:Token/Cookie 必须加密和签名。
  3. 注销机制:设计全局退出,确保用户在所有系统登出。
  4. 过期与刷新:Token/Cookie 需要设置有效期,并支持刷新。

7. 实战建议#

  • 小型系统:Cookie + Session 足够。
  • 多域/跨平台:JWT + OAuth2 或 OIDC。
  • 企业级系统:SAML 或 Keycloak。

8. 流程图示例#

+-------+ +------------+ +---------+
| User | -----> | System A | ----> | SSO |
+-------+ +------------+ +---------+
| | |
| | <--- Redirect ---|
| | |
| <--- Token ---- | |
| | |
| | ------> verify ->|
| | |
| <---- success --| |

9. 学习总结#

  • SSO 本质是“一次登录,多系统共享认证”。
  • 常用实现方式:Cookie/Session、JWT、OAuth2/OIDC、CAS、SAML。
  • 核心难点:跨域、全局登出、安全性、协议选择。
  • 推荐实践:
    • 小系统用 Cookie + Session
    • 跨域系统用 OAuth2/JWT
    • 企业应用用 Keycloak / SAML
Share

If this article helped you, please share it with others!

单点登录(SSO)学习笔记
https://mizuki.mysqil.com/posts/sso-learning/
Author
梦幻晨风
Published at
2024-09-21
License
CC BY-NC-SA 4.0

Some information may be outdated

Table of Contents