CAS 5.3.1系列之客户端对接(五)
【摘要】 CAS 5.3.1系列之客户端对接(五)
我们要接入客户端可以常用第三方的库cas-client-autoconfig-support来对接,比较快捷,迅速实现,或者可以用cas-client-support-springboot集成到boot项目
pom配置:
<!-- CAS依赖包 --> <dependency> <groupId>net.unicon.ca...
CAS 5.3.1系列之客户端对接(五)
我们要接入客户端可以常用第三方的库cas-client-autoconfig-support来对接,比较快捷,迅速实现,或者可以用cas-client-support-springboot集成到boot项目
pom配置:
<!-- CAS依赖包 --> <dependency> <groupId>net.unicon.cas</groupId> <artifactId>cas-client-autoconfig-support</artifactId> <version>1.5.0-GA</version> </dependency>
- 1
- 2
- 3
- 4
- 5
- 6
application.yml配置:
cas:
server-login-url: http://127.0.0.1:8080/cas/login
server-url-prefix: http://127.0.0.1:8080/cas
client-host-url: http://127.0.0.1:8082
- 1
- 2
- 3
- 4
- 5
可以自定义一个重定向策略类,这里还是和默认的策略一样,可以根据项目需要自行更改
package org.muses.jeeplatform.oa.cas;
import org.jasig.cas.client.authentication.AuthenticationRedirectStrategy;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class CustomAuthticationRedirectStrategy implements AuthenticationRedirectStrategy { @Override public void redirect(HttpServletRequest request, HttpServletResponse response, String potentialRedirectUrl) throws IOException {
// response.setCharacterEncoding("utf-8");
// response.setContentType("application/json; charset=utf-8");
// PrintWriter out = response.getWriter();
// out.write("401"); //response重定向 response.sendRedirect(potentialRedirectUrl); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
然后通过配置类,实现CasClientConfigurerAdapter类,记得配置类要加上@EnableCasClient注解,开启CAS支持:
package org.muses.jeeplatform.oa.config;
import net.unicon.cas.client.configuration.CasClientConfigurerAdapter;
import net.unicon.cas.client.configuration.EnableCasClient;
import org.jasig.cas.client.authentication.AuthenticationFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
/**
* <pre>
* CAS配置类
* </pre>
*
* @author nicky
* <pre>
* 修改记录
* 修改后版本: 修改人: 修改日期: 2020年04月11日 修改内容:
* </pre>
*/
@Configuration
@EnableCasClient
public class CASConfig extends CasClientConfigurerAdapter { private static final String CAS_SERVER_URL_LOGIN = "http://127.0.0.1:8080/cas/login"; private static final String SERVER_NAME = "http://127.0.0.1:8082/"; private static final String AUTHENTICATION_REDIRECT_STRATEGY_CLASS = "org.muses.jeeplatform.oa.cas.CustomAuthticationRedirectStrategy"; @Override public void configureAuthenticationFilter(FilterRegistrationBean authenticationFilter) { super.configureAuthenticationFilter(authenticationFilter); authenticationFilter.getInitParameters().put("authenticationRedirectStrategyClass",AUTHENTICATION_REDIRECT_STRATEGY_CLASS); } @Override public void configureValidationFilter(FilterRegistrationBean validationFilter) { Map<String, String> initParameters = validationFilter.getInitParameters(); initParameters.put("encodeServiceUrl", "false"); } @Bean public FilterRegistrationBean filterRegistrationBean(){ FilterRegistrationBean registrationBean = new FilterRegistrationBean(); registrationBean.setFilter(new AuthenticationFilter()); registrationBean.addUrlPatterns("/*"); Map<String, String> initParameters = new HashMap<String,String>(4); initParameters.put("casServerLoginUrl",CAS_SERVER_URL_LOGIN); initParameters.put("serverName",SERVER_NAME); initParameters.put("ignorePattern","/logoutSuccess/*"); // 自定义重定向策略 initParameters.put("authenticationRedirectStrategyClass", AUTHENTICATION_REDIRECT_STRATEGY_CLASS); registrationBean.setInitParameters(initParameters); registrationBean.setOrder(1); return registrationBean; }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
访问项目时候,如果没登录过,会跳到CAS进行认证授权,授权通过才会返回主页
代码例子参考:github下载链接
详情可以参考官方文档:https://apereo.github.io/cas/5.3.x/installation/Configuration-Properties.html
优质参考博客:
https://www.cnblogs.com/jpeanut/tag/CAS/
https://blog.csdn.net/anumbrella/category_7765386.html
文章来源: smilenicky.blog.csdn.net,作者:smileNicky,版权归原作者所有,如需转载,请联系作者。
原文链接:smilenicky.blog.csdn.net/article/details/105837931
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)