SpringBoot + CASクライアントがシングルサインオンを実現
この記事では、主にCASクライアントを統合して、SpringBootを使用した開発プロセスでシングルサインオン機能を実現する方法を紹介します。
準備オーケー
(1)CASサーバーを構築します(参照:https://blog.csdn.net/u014553029/article/details/88102311)
(2)通常のSpringBootWebプロジェクトを準備します
統合
1.CASクライアントの依存関係を導入します
pom.xmlにCASクライアントの依存関係パッケージを導入します。コードは次のように表示されます。
<dependency>
<groupId>net.unicon.cas</groupId>
<artifactId>cas-client-autoconfig-support</artifactId>
<version>2.1.0-GA</version>
</dependency>
二、配置
application.propertiesまたはapplication.ymlに関連する構成を追加します。メインの構成コンテンツには、サーバーの関連するアドレス、クライアントの関連するアドレスなどが含まれます。これがapplication.ymlで、構成内容は次のとおりです。
cas:
#后端服务地址
client-host-url: http://127.0.0.1:8888
#cas认证中心地址
server-url-prefix: http://146.56.192.87:8080/cas
#cas认证中心登录地址
server-login-url: http://146.56.192.87:8080/cas/login
validation-type: cas3
3つ目は、スタートアップクラスにenableアノテーションを追加することです。
//启用CAS
@EnableCasClient
@SpringBootApplication
public class SpringBootSsoApplication {
//省略部分内容
}
第四に、テストメソッドを書く
@Controller
public class TestController {
@Value(value = "${cas.server-url-prefix}")
private String serverUrlPrefix = "";
@Value(value = "${cas.client-host-url}")
private String clientHostUrl = "";
@GetMapping("user")
@ResponseBody
public String user(HttpServletRequest request) {
Assertion assertion = (Assertion) request.getSession().getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION);
String loginName = null;
if (assertion != null) {
AttributePrincipal principal = assertion.getPrincipal();
loginName = principal.getName();
System.out.println("访问者:" + loginName);
}
return "访问者:" + loginName;
}
@RequestMapping("/logout")
public String logout(HttpSession session) {
session.invalidate();
return "redirect:" + serverUrlPrefix + "/logout?service=" + clientHostUrl + "/sso-client/user";
}
}
5、ログインとログアウトをテストします
5.1ログイン:
(1)ブラウザを開いてアクセスします:http://127.0.0.1:8888 / sso-client / user
(2)ログインがないため、次の場所にリダイレクトします:http://146.56.192.87:8080 / cas / login? service = http://127.0.0.1:8888/sso-client/user(3
)ユーザー名とパスワードを入力し、[ログイン]をクリックします。成功したら、http://127.0.0.1:8888 / sso-client / userにジャンプします。
5.2終了:
(4)アクセス:http://127.0.0.1:8888 / sso-client / logout
(5)このアドレスが必要なため、Casは終了し、http://127.0.0.1:8888 / sso-client / userにジャンプします。ログインするには、引き続きログインインターフェイスにリダイレクトされます
5.3単一の検証ポイント:
(6)クライアントを起動するとき、ポートは8889で、プロジェクト名はsso-client1です。
(7)http://127.0.0.1:8889/sso-client1/userにアクセスして、ログインせずに直接ユーザー情報を取得します。シングルポイント認証の検証が完了しました。
注意:
(1)認証および承認されていないサービスは、CASを使用してアクセスしたターゲットアプリケーションを認証することはできません。
WEB-INF \ classes \ services \ HTTPSandIMAPS-10000001.jsonを変更します
"serviceId" : "^(https|imaps)://.*"
改为
"serviceId" : "^(https|imaps|http)://.*"
(2)WEB-INF \ classes \ application.properties
# Service Registry(服务注册)
# 开启识别Json文件,默认false
cas.serviceRegistry.initFromJson=true
# 保存tgc
cas.tgc.secure=false
# 默认情况下退出登录时,页面将会跳转到CAS服务器内部的注销页面 casLogoutView.jsp ,
# 如果我们需要在退出登录后,跳转到指定页面,需要将下列参数设为true,在退出登录的url里需要添加service参数,
# 该参数指定在注销后需要跳转的页面,配置允许登出后跳转到指定页面
cas.logout.followServiceRedirects=true
#跳转到指定页面需要的参数名为 service(default)
cas.logout.redirectParameter=service
#在退出时是否需要 确认一下 true确认 false直接退出
cas.logout.confirmLogout=false
#是否移除子系统的票据
cas.logout.removeDescendantTickets=true