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

作者: smileNicky | 来源:发表于2020-04-29 13:46 被阅读0次

    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>
    

    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
    
    

    可以自定义一个重定向策略类,这里还是和默认的策略一样,可以根据项目需要自行更改

    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);
        }
    }
    
    

    然后通过配置类,实现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;
        }
    
    
    }
    
    

    访问项目时候,如果没登录过,会跳到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

    相关文章

      网友评论

        本文标题:CAS 5.3.1系列之客户端对接(五)

        本文链接:https://www.haomeiwen.com/subject/bhbgwhtx.html