在这之前由于没有使用过Cas用来做单点登录,结果是一个坑接一个坑,我相信大多数第一次使用这个来做单点登录的大牛们=都会有类似的经历,今天就简单的把CAS客户端以SpringBoot的方式来讲述一下.(重点说一下忽略某些Url不使用CAS的情况)
代码下载地址:暂无
目录结构
简单明了直接上代码相信大家都能看明白:
1.pom.xml中添加以下依赖:
<dependency>
<groupId>net.unicon.cas</groupId>
<artifactId>cas-client-autoconfig-support</artifactId>
<version>1.7.0-GA</version>
</dependency>
2.启动主类中添加以下注解用于开启CAS单点登录:@EnableCasClient
package com.qf.swar;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import net.unicon.cas.client.configuration.EnableCasClient;
@SpringBootApplication
@EnableCasClient
@Controller
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@RequestMapping("/hello")
@ResponseBody
public String hello() {
return "Hello World!";
}
@RequestMapping("/api")
@ResponseBody
public String api() {
return "Hello api!";
}
@RequestMapping(value="/login")
public String requestMethodName() {
return "s/index";
}
}
3.application.properties配置文件中添加以下:
server.port=8083
cas.validation-type=CAS
cas.server-url-prefix=http://localhost:8080/cas
cas.server-login-url=http://localhost:8080/cas/login
cas.client-host-url=http://localhost:8083/login
#指定需要经过CAS验证的链接,未指定的不需要配置
cas.authentication-url-patterns=/login/*,/api/*
4.index.html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
<h1>Hello, world!</h1>
<form action="/hello" method="post">
<button type="submit">提价Form</button>
</form>
<button onclick="cc();">点击</button>
</body>
<script type="text/javascript">
function cc(){
$.ajax({
url : '/hello',
type : 'post',
async: false,//使用同步的方式,true为异步方式
data : {'id':'1', 'name':'sssss'},//这里使用json对象
success : function(data){
//code here...
alert(data);
},
fail:function(){
//code here...
}
});
}
</script>
</html>
5.运行效果如下:
输入以下地址直接跳转到登陆页面:
http://localhost:8083/login
浏览器输入以下地址可以直接访问不用登陆:
http://localhost:8083/hello