# 默认的方式集成cas

# 1、前置条件

jdk1.8

springboot

需要启动cas-sever

# 2、集成方式及说明

参考项目 client-springboot,cas常规引入方式。

# 2.1 application.yml

# cas客户端配置
cas:
  # CAS-server 域名+contentPath
  server-url-prefix: http://127.0.0.1:8000/cas
  # CAS-server 登陆地址
  server-login-url: http://127.0.0.1:8000/cas/login
  # 我当前应用访问地址
  # 必须处理成域名形式,否则会报错:cert.CertificateException: No subject alternative names present
  client-host-url: http://127.0.0.1:8100

  validation-type: CAS3
  use-session: true

  # ---- cas 权限部分配置 ----
  # 认证url,确保已登录。默认 /*
  authentication-url-patterns:
    - /home/*
    - /home
  # 校验url,确保用户未超时。默认 /*
  validation-url-patterns:
    - /home/*
    - /home
  # 对url 进行包装,之后就可在request中获取到用户信息,默认/*
  request-wrapper-url-patterns:
    - /home/*
    - /home
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

# 2.2 开启cas注解

//开启CAS支持
@EnableCasClient
@SpringBootApplication
public class ClientSpringbootApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClientSpringbootApplication.class, args);
    }
}
1
2
3
4
5
6
7
8

# 2.3 获取用户信息

@RequestMapping(value = {"/index", "/", ""})
@ResponseBody
public ModelAndView index(HttpServletRequest request) {
    System.out.println("index");

    ModelAndView mv = new ModelAndView();
    HttpSession session = request.getSession();
    if (session != null) {
        //方法1 通过session获取
        //Assertion assertion = (Assertion)session.getAttribute("_const_cas_assertion_");
        //assertion.getPrincipal();
        //mv.addObject("isValid", assertion.isValid());

        //方法2 基于tomcat-embed-core-9.x  通过 request.getUserPrincipal() 获取
        AttributePrincipal userPrincipal = (AttributePrincipal) request.getUserPrincipal();
        if (userPrincipal != null) {
            mv.addObject("getName", userPrincipal.getName());
            mv.addObject("getAttributes", userPrincipal.getAttributes());
            mv.addObject("userName", userPrincipal.getAttributes().get("userName"));
            mv.addObject("email", userPrincipal.getAttributes().get("email"));
            mv.addObject("userType", userPrincipal.getAttributes().get("userType"));
        }
    }
    //跳转到index.html页面
    mv.setViewName("/home");
    return mv;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

# 2.4 登出代码

//cas服务端url
@Value("${cas.server-url-prefix}")
String CAS_SERVER_URI;
//本地服务url
@Value("${cas.client-host-url}")
String CLIENT_HOST_URI;

/**
 * 登出
 */
@RequestMapping(value = {"/logout"})
public String logout(HttpServletRequest request, HttpServletResponse response) {
    System.out.println("logout");

    //1 清除本地cookie
    // 获取Cookies数组
    Cookie[] cookies = request.getCookies();
    // 迭代查找并清除Cookie
    for (Cookie cookie: cookies) {
        //将cookie.setMaxAge(0)表示删除cookie.
        cookie.setMaxAge(0);
        cookie.setPath("/");
        response.addCookie(cookie);
    }

    //2 保证cas登出
    //可以直接登出  或是指定service,在跳转到某个位置。
    String encodedUrl;
    try {
        encodedUrl = URLEncoder.encode(CLIENT_HOST_URI, "UTF-8").replace("+", "%20");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        encodedUrl = "";
    }
    String result = "redirect:" + CAS_SERVER_URI + "/logout?service=" + encodedUrl;
    return result;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

# 2.5 被动登出

处理其他系统登出后,当前系统也登出的效果

/**
 * cas客户端必要配置。处理被动退出。
 */
@Configuration
public class CasConfig {

    /**
     * cas配置信息
     */
    @Autowired
    CasClientConfigurationProperties casProperties;

    /**
     * 该过滤器用于实现单点登出功能,可触发服务端退出时,所有客户端的退出效果。
     * 单点退出配置,一定要放在其他filter之前
     */
    @Bean
    public FilterRegistrationBean singleSignOutFilter() {
        FilterRegistrationBean filterRegistration = new FilterRegistrationBean();
        filterRegistration.setFilter(new SingleSignOutFilter());
        filterRegistration.setEnabled(true);
        filterRegistration.addUrlPatterns("/*" );
        filterRegistration.addInitParameter("casServerUrlPrefix" , casProperties.getServerUrlPrefix());
        filterRegistration.setOrder(1);
        return filterRegistration ;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

# 2.6 核心maven依赖

<!--    org.jasig.cas.client    -->
<dependency>
    <groupId>net.unicon.cas</groupId>
    <artifactId>cas-client-autoconfig-support</artifactId>
    <version>2.2.0-GA</version>
</dependency>

<!-- 中科软提供的基础类库 用于访问中科软提供的资源信息时使用 非必须 -->
<dependency>
    <groupId>com.sinosoft</groupId>
    <artifactId>platform-client-common</artifactId>
    <version>1.0.0</version>
    <exclusions>
        <exclusion>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </exclusion>
    </exclusions>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 3、测试效果

# 3.1、访问当前项目

访问链接地址 http://127.0.0.1:8100/

image-20220621193617090.png

# 3.2、点击下方蓝字

实际访问的是 http://127.0.0.1:8100/home,因为当前用户未登录,这时将跳转到cas-server端去登陆。

访问链接如下:

http://127.0.0.1:8000/cas/login?service=http%3A%2F%2F127.0.0.1%3A8100%2Fhome

image-20220621193744506.png

# 3.3、输入用户名密码,登陆成功。

image-20220621193849527.png

已经看到后台能获取到用户信息,并返回前台。

# 3.4、登出

点击logout按钮 image-20220621193957281.png

在访问/home时,发现已经登出。

# 4、demo程序

下载地址:client-springboot.zip

依赖jar包:platform-client-common-1.0.0.jar