單點(diǎn)登錄服務(wù)端搭建
1、下載cas包
https://github.com/apereo/cas-overlay-template/tree/5.3
這好像是最后一個(gè)maven版本的,之后都是grade版本的
2、使用idea打開(kāi)代碼,導(dǎo)入依賴
3、新建src目錄、resource目錄
4、復(fù)制cas-server-webapp-tomcat下的services、applicaiton.properties目錄到resources目錄下
5、修改resources/services/HTTPSandIMAPS-10000001.json文件,添加如下內(nèi)容,支持http訪問(wèn)
6、添加數(shù)據(jù)庫(kù)依賴
<!-- 數(shù)據(jù)庫(kù) -->
<dependency>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-support-jdbc</artifactId>
<version>${cas.version}</version>
</dependency>
<dependency>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-support-jdbc-drivers</artifactId>
<version>${cas.version}</version>
</dependency>
<!-- MySQL JAR 包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
7、修改applicaiton.properties配置
##
# 普通MD5用戶jdbc驗(yàn)證
##
#配置數(shù)據(jù)庫(kù)連接
cas.authn.jdbc.query[0].driverClass=com.mysql.cj.jdbc.Driver
cas.authn.jdbc.query[0].url=jdbc:mysql://localhost:3306/leecx?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
cas.authn.jdbc.query[0].user=root
cas.authn.jdbc.query[0].password=root
#添加jdbc認(rèn)證
cas.authn.jdbc.query[0].sql=SELECT * FROM sys_user WHERE USER_NAME = ?
#哪個(gè)字段作為密碼字段
cas.authn.jdbc.query[0].fieldPassword=PASSWORD
#哪個(gè)字段作為過(guò)期字段 0:未過(guò)期 1:已過(guò)期
cas.authn.jdbc.query[0].fieldExpired=EXPIRED
#哪個(gè)字段作為是否可用字段 0:未禁用 1:已禁用
cas.authn.jdbc.query[0].fieldDisabled=STATUS
#MD5設(shè)置
cas.authn.jdbc.query[0].passwordEncoder.type=DEFAULT
cas.authn.jdbc.query[0].passwordEncoder.characterEncoding=UTF-8
cas.authn.jdbc.query[0].passwordEncoder.encodingAlgorithm=MD5
8、配置tomcat
9、啟動(dòng)項(xiàng)目,訪問(wèn)http://localhost:8080/cas_overlay_war_exploded/login
10、自定義頁(yè)面
復(fù)制cas-server-webapp-tomcat下的templates到resources下,不要修改文件名稱
頁(yè)面使用thymeleaf模板語(yǔ)法,可參考鏈接里的第一個(gè)鏈接
cas客戶端搭建
1、建立一個(gè)springboot項(xiàng)目
2、引入依賴
<dependency>
<groupId>org.jasig.cas.client</groupId>
<artifactId>cas-client-support-springboot</artifactId>
<version>3.6.4</version>
</dependency>
3、在啟動(dòng)類上添加@EnableCasClient注解
4、修改application.properties
server.port=8082
cas.server-url-prefix=http://localhost:8080/cas_overlay_war_exploded
cas.server-login-url=${cas.server-url-prefix}/login
cas.client-host-url=http://localhost:${server.port}
cas.validation-type=cas3
cas.single-logout.enabled=true
5、寫(xiě)個(gè)測(cè)試代碼測(cè)試一下
@Controller
public class TestController {
@Autowired
CasClientConfigurationProperties casProps;
@ResponseBody
@RequestMapping("/sso-test1")
public String test1(HttpSession session) {
Assertion assertion = (Assertion) session.getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION);
AttributePrincipal principal = assertion.getPrincipal();
String loginName = principal.getName();
return "sso-test1,當(dāng)前登錄賬戶" + loginName;
}
/**
* 退出 后自動(dòng)重定向自定義接口
*/
@RequestMapping("/system/logout1")
public String logout1(HttpServletRequest request) {
HttpSession session = request.getSession();
session.invalidate();
return "redirect:" + casProps.getServerUrlPrefix() + "/logout?service="+ casProps.getClientHostUrl()+"/system/logoutSuccess";
}
/**
* 退出成功頁(yè)
*/
@RequestMapping("/system/logoutSuccess")
@ResponseBody
public String logoutSuccess() {
return "test2成功退出!";
}
}
6、訪問(wèn)http://localhost:8082/sso-test1
會(huì)跳轉(zhuǎn)到cas登錄頁(yè),登錄成功后成功訪問(wèn)
參考鏈接:
https://www.cnblogs.com/hooly/p/12784397.html
https://www.cnblogs.com/kgdxpr/p/9933106.html文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-693598.html
https://blog.csdn.net/bigdata_dog/article/details/123896404文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-693598.html
到了這里,關(guān)于cas服務(wù)端、客戶端搭建的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!