CAS 5.3.1系列之客户端对接(五)

网友投稿 614 2022-05-30

CAS 5.3.1系列之客户端对接(五)

我们要接入客户端可以常用第三方的库cas-client-autoconfig-support来对接,比较快捷,迅速实现,或者可以用cas-client-support-springboot集成到boot项目

pom配置:

net.unicon.cas cas-client-autoconfig-support 1.5.0-GA

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; /** *

 * CAS配置类 * 
* * @author nicky *
 * 修改记录 * 修改后版本: 修改人: 修改日期: 2020年04月11日 修改内容: * 
*/ @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 initParameters = validationFilter.getInitParameters(); initParameters.put("encodeServiceUrl", "false"); } @Bean public FilterRegistrationBean filterRegistrationBean(){ FilterRegistrationBean registrationBean = new FilterRegistrationBean(); registrationBean.setFilter(new AuthenticationFilter()); registrationBean.addUrlPatterns("/*"); Map initParameters = new HashMap(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

CAS 5.3.1系列之客户端对接(五)

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

其他

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Nginx安装手册
下一篇:关于php网络爬虫phpspider
相关文章