在這之前由於沒有使用過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