首页 > 其他分享 >springboot集成CAS客户端实现单点登录

springboot集成CAS客户端实现单点登录

时间:2023-09-13 18:34:33浏览次数:46  
标签:registrationBean 单点 String CAS FilterRegistrationBean cas return public springbo

  1. pom中引入依赖
<!-- cas -->
        <dependency>
            <groupId>org.jasig.cas.client</groupId>
            <artifactId>cas-client-core</artifactId>
            <version>3.6.2</version>
        </dependency>

  1. yml中添加cas配置
#cas配置
cas:
  resName: demo
  casServerLoginUrl: http://localhost:8888/cas/login
  casServerUrlPrefix: http://localhost:8888/cas
  casClientLoginUrl: http://localhost:10011/loginCas
  casClientLogoutUrl: http://localhost:8888/cas/logout?service=http://localhost:10011/loginCas
  1. 读取CAS相关配置
@Component
@ConfigurationProperties(prefix = "cas")
public class CasPropertiesConfig {
    /**
     * 资源名称
     */
    private String resName;
    /**
     * cas服务登录地址
     */
    private String casServerLoginUrl;
    /**
     * cas服务地址前缀
     */
    private String casServerUrlPrefix;
    /**
     * cas客户端登录地址
     */
    private String casClientLoginUrl;
    /**
     * cas客户端登出地址
     */
    private String casClientLogoutUrl;

    public String getResName() {
        return resName;
    }

    public void setResName(String resName) {
        this.resName = resName;
    }

    public String getCasServerLoginUrl() {
        return casServerLoginUrl;
    }

    public void setCasServerLoginUrl(String casServerLoginUrl) {
        this.casServerLoginUrl = casServerLoginUrl;
    }

    public String getCasServerUrlPrefix() {
        return casServerUrlPrefix;
    }

    public void setCasServerUrlPrefix(String casServerUrlPrefix) {
        this.casServerUrlPrefix = casServerUrlPrefix;
    }

    public String getCasClientLoginUrl() {
        return casClientLoginUrl;
    }

    public void setCasClientLoginUrl(String casClientLoginUrl) {
        this.casClientLoginUrl = casClientLoginUrl;
    }

    public String getCasClientLogoutUrl() {
        return casClientLogoutUrl;
    }

    public void setCasClientLogoutUrl(String casClientLogoutUrl) {
        this.casClientLogoutUrl = casClientLogoutUrl;
    }
}
  1. cas配置类
@Configuration
public class CasConfig {
    public static Boolean fileFlag = true;
    private static Logger logger = LoggerFactory.getLogger(CasConfig.class);
    
    @Autowired
    private CasPropertiesConfig casPropertiesConfig;
    

    @Bean
    public ServletListenerRegistrationBean servletListenerRegistrationBean() {
        ServletListenerRegistrationBean listenerRegistrationBean = new ServletListenerRegistrationBean();
        listenerRegistrationBean.setListener(new SingleSignOutHttpSessionListener());
        listenerRegistrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return listenerRegistrationBean;
    }

    /**
     * 单点登录身份认证
     *
     * @return org.springframework.boot.web.servlet.FilterRegistrationBean
     */
    @Bean
    public FilterRegistrationBean authenticationFilter() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new AuthenticationFilter());
        registrationBean.addUrlPatterns("/*");
        registrationBean.setName("CAS Authentication Filter");
        registrationBean.addInitParameter("casServerLoginUrl", casPropertiesConfig.getCasServerLoginUrl());
        registrationBean.addInitParameter("service", casPropertiesConfig.getCasClientLoginUrl());
        registrationBean.setOrder(3);
        return registrationBean;
    }

    /**
     * 单点登录票据校验
     *
     * @return org.springframework.boot.web.servlet.FilterRegistrationBean
     */
    @Bean
    public FilterRegistrationBean cas20ProxyReceivingTicketValidationFilter() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new Cas30ProxyReceivingTicketValidationFilter());
        registrationBean.addUrlPatterns("/*");
        registrationBean.setName("CAS Validation Filter");
        registrationBean.addInitParameter("casServerUrlPrefix", casPropertiesConfig.getCasServerUrlPrefix());
        registrationBean.addInitParameter("service", casPropertiesConfig.getCasClientLoginUrl());
        registrationBean.setOrder(4);
        return registrationBean;
    }

    /**
     * 单点登录请求包装
     *
     * @return org.springframework.boot.web.servlet.FilterRegistrationBean
     */
    @Bean
    public FilterRegistrationBean httpServletRequestWrapperFilter() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new HttpServletRequestWrapperFilter());
        registrationBean.addUrlPatterns("/*");
        registrationBean.setName("CAS HttpServletRequest Wrapper Filter");
        registrationBean.setOrder(5);
        return registrationBean;
    }
}
  1. 单点登录接口demo
@Slf4j
@RestController
public class LoginCasController {
    @Autowired
    private CasPropertiesConfig casPropertiesConfig;
    @Autowired
    private SysLoginService loginService;
    //前端地址
    @Value("${casLoginUrl}")
    private String casLoginUrl;

    @RequestMapping("/loginCas")
    public void loginCas(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // CAS统一认证登录帐号,即登录页面用户输入的帐号
        String loginName = "";
        Assertion assertion = (Assertion) request.getSession().getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION);
        if (assertion == null) {
            log.error("assertion为空!");
        }
        AttributePrincipal principal = assertion.getPrincipal();
        if (principal == null) {
            log.error("principal为空!");
        }
        loginName = principal.getName();
        request.getSession().setAttribute("loginName", loginName);
        //此处写自己系统的登录逻辑
        ***
        //目前使用重定向带参数进行跳转
        String url = casLoginUrl + "?tokenCas=" + tokenCas;
        log.info("url:{}", url);
        response.sendRedirect(url);
    }
}

访问loingCas接口时,若未在CASserver登录,则会被拦截跳转到CAS的登陆页面,登陆成功后放行继续访问loginCas接口

标签:registrationBean,单点,String,CAS,FilterRegistrationBean,cas,return,public,springbo
From: https://blog.51cto.com/u_16258535/7463068

相关文章

  • springboot 整合 nacos 实现配置文件统一管理 和 服务注册
    1.我使用的是 nacos-server-1.4.1 直接启动nacos没有配置数据库那些.\startup.cmd-mstandalone2.启动后在nacos中创建命名空间配置文件 注意命名空间的id我是自己定义的还有group到时候配置错了服务又不会报错只是会读取不到3.springboot配置 server-addr......
  • Springboot RocketMQ整合—官方原版
    Doker 技术人自己的数码品牌Doker官网:Doker多克一、添加maven依赖:<dependency><groupId>org.apache.rocketmq</groupId><artifactId>rocketmq-spring-boot-starter</artifactId><version>${RELEASE.VERSION}</version></dependen......
  • 对SpringBoot接口进行操作日志记录
    最近业务有需求要对所有的用户操作进行日志记录,方便管理员查询不同权限级别的用户对系统的操作记录,现有的日志只是记录了异常信息、业务出错、重要功能的执行进行了记录,并不能满足需求要求,最直接的解决方法是在每个接口上去添加log.info之类的代码,但是这种方式对业务代码的切入性......
  • SpringBoot入门(一) springBoot框架搭建和启动
    1.创建maven工程MavenProject      //CODE    <projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xs......
  • SpringBoot教程(二)springboot的配置文件
    一.springboot配置文件的类型application.propertiesapplication.yml项目结构,因为不可以同时使用这两种文件启动时任选一个放到resources下即可 二.properties配置文件的使用packagecom.lpinfo.shop.lpinfoshop;importorg.springframework.beans.factory.annotation.Autowi......
  • springboot发布部署web jar包
    1.在idea中生成jar包文件 2.我这个项目使用的是JavaJDK20,所以要在官网下载这个版本在服务器上安装。https://www.oracle.com/java/technologies/downloads/   有些系统需要重启下服务器才会生效。 3.把第一步生成的 demo-0.0.1-SNAPSHOT.jar文件复制到服务器......
  • Springboot @ConfigurationProperties对象 静态方法调用无效
    一.问题1.springboot使用 @ConfigurationProperties注入对象普通方法调用可以 在静态方法中调用的时候读取不到参数二.文件packagecom.lpinfo.framework.config;@Data@Component@PropertySource("classpath:/oss.properties")@ConfigurationProperties(prefix="oss")......
  • SpringBoot+Mybatis三级查询
    一、概述现有一个需求:查询视频详情。对应三张表,视频表、章节列表、集列表。一个视频对应多个章节,一个章节对应多集视频。请根据视频id查询视频详情,并把视频的章节列表,章节中的集列表都带出来。SpringBoot和MyBatis-plus说明:<!--根模块继承了SpringBoot,子模块也跟着继......
  • springboot日志配置
    配置文件使用xml配置日志保存(并不需要pom配置slf4j依赖,starter里面已经配置了依赖了)在项目的resources目录下创建一个【logback-spring.xml】日志配置文件名称只要是一logback开头就行,测试使用log.xml并不会生成日志。合法名称:logback.xml、logback-spring.xml备注:要配置l......
  • SpringBoot项目启动报错:An incompatible version [1.1.22] of the Apache Tomcat Nati
    问题解释:“安装了不兼容的ApacheTomcat原生库版本[1.1.22],而Tomcat需要版本[1.2.14]”解决方法:①打开网页 http://archive.apache.org/dist/tomcat/tomcat-connectors/native/②        ③        ④     ......