qq_38728720 2025-12-07 10:27 采纳率: 0%
浏览 4

CAS认证对接使用springboot

cas认证对接 使用springboot
当用户在网站A已经通过了身份验证,再访问网站B。

网站B验证浏览器的cookie,但是因为网站B没有登录过,所以没有网站B的cookie。
网站B将浏览器重定向到认证服务器,浏览器访问认证服务器时会带上cookie。
认证服务器发现cookie中有TGC,验证TGC与TGT的对应关系,TGC有效。
认证服务器将浏览器重定向到网站B,并返回一个ST。
浏览器使用ST请求网站B。
网站B使用这个ST去认证服务器进行认证。
如果认证成功,网站B为浏览器生成cookie,并返回用户请求的资源。

  • 写回答

2条回答 默认 最新

  • 檀越@新空间 2025-12-07 11:11
    关注

    上午好☀️☀️☀️️
    本答案参考通义千问

    你描述的是 CAS(Central Authentication Service) 的认证流程,这是一种基于 单点登录(SSO) 的认证机制。在 Spring Boot 项目中集成 CAS 认证,可以实现用户在多个系统间共享登录状态。


    问题分析

    你的场景是:

    • 用户已经通过 网站 A 登录;
    • 当访问 网站 B 时,因为没有登录过,所以 网站 B 会将浏览器重定向到 CAS 认证服务器
    • 在重定向过程中,浏览器会带上 TGC(Ticket Granting Cookie);
    • CAS 服务器 验证 TGC 是否有效;
    • 如果有效,则生成一个 Service Ticket(ST) 并将浏览器重定向回 网站 B
    • 网站 B 使用 ST 向 CAS 服务器验证身份;
    • 如果验证成功,网站 B 就为用户创建自己的 session 和 cookie,并返回资源。

    🛠️ 解决方案步骤

    以下是使用 Spring Boot 实现 CAS 认证对接的完整步骤:

    1. 引入依赖(pom.xml)

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.jasig.cas.client</groupId>
        <artifactId>cas-client-core</artifactId>
        <version>3.6.1</version>
    </dependency>
    

    注意: cas-client-core 是 CAS 客户端库,用于与 CAS 服务器通信。


    2. 配置 CAS 服务信息(application.yml)

    cas:
      server-url: https://your-cas-server.com/cas
      service-url: http://localhost:8080/login/cas
      logout-url: https://your-cas-server.com/cas/logout
    

    说明:

    • server-url: CAS 服务器地址。
    • service-url: 本应用的登录回调 URL(需注册到 CAS 服务器)。
    • logout-url: CAS 退出地址。

    3. 配置 Security 过滤器(SecurityConfig.java)

    @Configuration
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                .authorizeRequests()
                    .anyRequest().authenticated()
                    .and()
                .addFilter(casAuthenticationFilter())
                .exceptionHandling()
                    .authenticationEntryPoint(casAuthenticationEntryPoint());
        }
    
        @Bean
        public CasAuthenticationFilter casAuthenticationFilter() throws Exception {
            CasAuthenticationFilter filter = new CasAuthenticationFilter();
            filter.setCasServerLoginUrl("https://your-cas-server.com/cas/login");
            filter.setServiceProperties(serviceProperties());
            filter.setAuthenticationManager(authenticationManagerBean());
            return filter;
        }
    
        @Bean
        public CasAuthenticationEntryPoint casAuthenticationEntryPoint() {
            CasAuthenticationEntryPoint entryPoint = new CasAuthenticationEntryPoint();
            entryPoint.setLoginUrl("https://your-cas-server.com/cas/login");
            entryPoint.setCasServerUrlPrefix("https://your-cas-server.com/cas");
            return entryPoint;
        }
    
        @Bean
        public ServiceProperties serviceProperties() {
            ServiceProperties properties = new ServiceProperties();
            properties.setService("http://localhost:8080/login/cas");
            properties.setSendRenew(false);
            return properties;
        }
    }
    

    4. 创建 CAS 登录接口(Controller)

    @RestController
    @RequestMapping("/login")
    public class LoginController {
    
        @GetMapping("/cas")
        public String casLogin() {
            return "Redirect to CAS login page";
        }
    
        @GetMapping("/callback")
        public String callback(@RequestParam String ticket) {
            // 使用 ticket 验证用户身份
            // 通常通过 CAS 服务器验证 ST
            return "User authenticated via CAS";
        }
    }
    

    5. 配置 CAS 服务端(可选)

    如果你需要搭建 CAS 服务器,可以参考 Apereo CAS 官方文档进行部署。


    ⚙️ 关键组件说明

    | 名称 | 说明 | |--------------|----------------------------------------------------------------------| | TGC | Ticket Granting Cookie,用于标识用户已登录 CAS 服务器。 | | TGT | Ticket Granting Ticket,CAS 服务器生成的临时凭证,用于生成 ST。 | | ST | Service Ticket,用于访问特定服务的临时凭证。 | | Service URL | 应用的回调地址,必须在 CAS 服务器中注册。 |


    总结

    在 Spring Boot 中实现 CAS 认证的核心步骤如下:

    1. 引入 CAS 客户端依赖;
    2. 配置 CAS 服务信息;
    3. 配置 Security 过滤器和入口点;
    4. 创建登录和回调接口;
    5. 确保 CAS 服务端正确配置并注册服务 URL。

    📌 注意事项

    • 跨域问题:确保 CAS 服务器和应用之间无跨域限制;
    • 安全设置:建议使用 HTTPS 来保护通信;
    • Session 管理:在 CAS 成功验证后,应为用户创建本地 Session 或 JWT Token。

    如需进一步定制化(例如自定义登录页面、多租户支持等),可以继续扩展上述代码。欢迎继续提问!

    评论

报告相同问题?

问题事件

  • 修改了问题 12月7日
  • 创建了问题 12月7日