CAS 单点登陆 spring boot starter
安装
maven
1.添加JitPack仓库到pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
2.添加依赖
<dependency>
<groupId>com.github.itning</groupId>
<artifactId>cas-spring-boot-starter</artifactId>
<version>1.2.0-RELEASE</version>
</dependency>
gradle
1.Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
2.Add the dependency
dependencies {
implementation 'com.github.itning:cas-spring-boot-starter:1.2.0-RELEASE'
}
使用
配置
在yml或properties中配置
属性 | 说明 | 默认 |
---|---|---|
cas.allow-cors | 是否允许跨域 | true |
cas.debug | 调试模式 | false |
cas.client-login-path | 登陆地址(访问该地址会跳转到loginUrl) | /login |
cas.client-logout-path | 登出地址(访问该地址会跳转到登出地址) | /logout |
cas.session-attribute-name | 存储获取到的Attributes的Session Name | _cas_attributes_ |
cas.request-connect-timeout | 请求连接超时(ms) | 15000 |
cas.request-read-timeout | 请求读超时(ms) | 5000 |
cas.local-server-url | 本地服务端地址(该项目地址) | null |
cas.login-success-url | 登陆成功后跳转的网址 | null |
cas.login-url | 登陆地址(CAS服务端地址) | null |
cas.logout-url | 登出网址(CAS服务端地址) | null |
cas.server-url | CAS服务端地址 | null |
cas.exclude | 排除过滤地址(会排除以其开头的请求) | |
cas.enabled | 是否开启 | true |
注意:默认为null的必须配置
实现登陆状态回调
回调有默认实现类: CasAutoConfigure,即如果你不自己实现接口则会使用默认的实现
各个接口的作用请看接口注释。
写两个类实现 callback和config 包下的接口
import top.itning.cas.callback.login.ILoginFailureCallBack;
import top.itning.cas.callback.login.ILoginNeverCallBack;
import top.itning.cas.callback.login.ILoginSuccessCallBack;
import top.itning.cas.callback.option.IOptionsHttpMethodCallBack;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
/**
* @author itning
* @date 2019/6/23 10:26
*/
public class CallBackImpl implements ILoginFailureCallBack, ILoginNeverCallBack, ILoginSuccessCallBack, IOptionsHttpMethodCallBack {
@Override
public void onLoginFailure(HttpServletResponse resp, HttpServletRequest req, Exception e) throws IOException, ServletException {
}
@Override
public void onNeverLogin(HttpServletResponse resp, HttpServletRequest req) throws IOException, ServletException {
}
@Override
public void onLoginSuccess(HttpServletResponse resp, HttpServletRequest req, Map<String, String> attributesMap) throws IOException, ServletException {
}
@Override
public void onOptionsHttpMethodRequest(HttpServletResponse resp, HttpServletRequest req) throws IOException, ServletException {
}
}
import top.itning.cas.config.IAnalysisResponseBody;
import top.itning.cas.config.ICheckIsLoginConfig;
import top.itning.cas.config.INeedSetMap2SessionConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
/**
* @author itning
* @date 2019/6/23 10:27
*/
public class ConfigImpl implements IAnalysisResponseBody, ICheckIsLoginConfig, INeedSetMap2SessionConfig {
@Override
public Map<String, String> analysisBody2Map(String body) {
return null;
}
@Override
public boolean isLogin(HttpServletResponse resp, HttpServletRequest req) {
return false;
}
@Override
public boolean needSetMapSession() {
return false;
}
}
如何
-
如何获取CAS服务器登陆后传过来的属性?
我们将它放在session中了,你可以使用
HttpSession#getAttribute
方法来获取,参数默认为_cas_attributes_
,当然你可以在配置中更改cas.session-attribute-name
的值 -
如何配置只过滤某些URL路径?
只需在过滤器初始化之前调用该静态方法:
top.itning.cas.CasAutoConfigure.setUrlPatterns(String... urlPatterns);
源码:
/** * 设置拦截路径 * * @param urlPatterns 路径 */ public static void setUrlPatterns(String... urlPatterns) { up = urlPatterns; }