使用buji-pac4j和CAS实现单点登录 -- 知识铺
VIP文章 H阿布 于 2018-02-01 18:22:35 发布
在pac4j探索的上一篇文章大致讲述了一下buji-pac4j+CAS的认证流程。这里记录一下本人实现的最简单的单点登录,仅作为笔记、学习交流之用,戳这里获取本文源码。
一、项目框架
1、 buji-pac4j(v.3.0.0)
2、shiro (v.1.4.0)
3、springmvc (v.4.3.2)
4、CAS (v.4.2.6)
5、pac4j-cas(v.2.2.1)
在maven项目的pom.xml里配置以上相关依赖,具体依赖配置可以查看我的项目,这里不再赘述。
二、目录结构
客户端项目(pac4jtest1)目录结构如下:
1、java文件目录中,Redirect2CasLoginFilter是测试用的,可以不管,MyCasClient类是继承自CasClient的自定义客户端,ShiroCasLogoutHandler类是单点登出时对shiro的一些操作,Controller类是请求控制器,util包里的是单点登出相关的类;
2、配置文件目录中,log4j.properties是日志管理文件,url.properties配置了项目中用到的各种url,spring-comm.xml配置了shiro集成pac4j的配置,spring-mvc.xml是springmvc的相关配置;
3、另外还有个index.jsp,就是受保护的页面,请求访问前需要先认证。
二、springMvc配置
这里springmvc作最简单的配置:
<span><!-- 自动扫描的包名 --></span>
<span><<span>context:component-scan</span> <span>base-package</span>=<span>"com.pac4j.rest"</span>/></span>
<span><!-- 默认的注解映射的支持,自动注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter --></span>
<span><<span>mvc:annotation-driven</span> /></span>
<span><!-- 视图解释类 --></span>
<span><<span>bean</span> <span>class</span>=<span>"org.springframework.web.servlet.view.InternalResourceViewResolver"</span>></span>
<span><<span>property</span> <span>name</span>=<span>"prefix"</span> <span>value</span>=<span>"/WEB-INF/jsp/"</span>/></span>
<span><<span>property</span> <span>name</span>=<span>"suffix"</span> <span>value</span>=<span>".jsp"</span>/></span>
<span><<span>property</span> <span>name</span>=<span>"viewClass"</span> <span>value</span>=<span>"org.springframework.web.servlet.view.JstlView"</span> /></span>
<span></<span>bean</span>></span>
<span><!-- 对静态资源文件的访问--></span>
<span><<span>mvc:resources</span> <span>mapping</span>=<span>"/images/**"</span> <span>location</span>=<span>"/WEB-INF/images/"</span>/></span>
<span><<span>mvc:resources</span> <span>mapping</span>=<span>"/js/**"</span> <span>location</span>=<span>"/WEB-INF/js/"</span> /></span>
<span><<span>mvc:resources</span> <span>mapping</span>=<span>"/css/**"</span> <span>location</span>=<span>"/WEB-INF/css/"</span>/></span>
三、pac4j配置
spring-comm.xml是shiro整合pac4j的配置,具体配置如下:
<span><!-- 地址配置 --></span>
<span><<span>bean</span> <span>id</span>=<span>"propertyConfigurer"</span> <span>class</span>=<span>"org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"</span>></span>
<span><<span>property</span> <span>name</span>=<span>"locations"</span>></span>
<span><<span>list</span>></span>
<span><<span>value</span>></span>/WEB-INF/classes/url.properties<span></<span>value</span>></span>
<span></<span>list</span>></span>
<span></<span>property</span>></span>
<span></<span>bean</span>></span>
<span><!-- pac4j configurations --></span>
<span><!-- 请求cas服务端配置 --></span>
<span><<span>bean</span> <span>id</span>=<span>"casConfig"</span> <span>class</span>=<span>"org.pac4j.cas.config.CasConfiguration"</span>></span>
<span><!-- CAS server登录链接 --></span>
<span><<span>property</span> <span>name</span>=<span>"loginUrl"</span> <span>value</span>=<span>"${sso.cas.server.loginUrl}"</span>></span><span></<span>property</span>></span>
<span><!-- CAS server服务前缀 --></span>
<span><<span>property</span> <span>name</span>=<span>"prefixUrl"</span> <span>value</span>=<span>"${sso.cas.server.prefixUrl}"</span>></span><span></<span>property</span>></span>
<span><!-- 登出处理器,单点登出时所需要的操作在这里实现--></span>
<span><<span>property</span> <span>name</span>=<span>"logoutHandler"</span> <span>ref</span>=<span>"casLogoutHandler"</span>></span><span></<span>property</span>></span>
<span></<span>bean</span>></span>
<span><!-- cas客户端配置 --></span>
<span><<span>bean</span> <span>id</span>=<span>"casClient"</span> <span>class</span>=<span>"com.pac4j.client.MyCasClient"</span>></span>
<span><<span>constructor-arg</span> <span>ref</span>=<span>"casConfig"</span> /></span>
<span><<span>property</span> <span>name</span>=<span>"includeClientNameInCallbackUrl"</span> <span>value</span>=<span>"false"</span>></span><span></<span>property</span>></span>
<span><!-- 客户端回调地址 --></span>
<span><<span>property</span> <span>name</span>=<span>"callbackUrl"</span> <span>value</span>=<span>"${sso.cas.client.callbackUrl}"</span>></span><span></<span>property</span>></span>
<span></<span>bean</span>></span>
<span><!-- shiro登出处理器,销毁session及登录状态等--></span>
<span><<span>bean</span> <span>id</span>=<span>"casLogoutHandler"</span> <span>class</span>=<span>"com.pac4j.handler.ShiroCasLogoutHandler"</span>></span>
<span><<span>property</span> <span>name</span>=<span>"destroySession"</span> <span>value</span>=<span>"true"</span>></span><span></<span>property</span>></span>
<span></<span>bean</span>></span>
<span><<span>bean</span> <span>id</span>=<span>"sessionStore"</span> <span>class</span>=<span>"com.pac4j.util.MyShiroSessionStore"</span>></span><span></<span>bean</span>></span>
<span><!-- pac4j配置 --></span>
<span><<span>bean</span> <span>id</span>=<span>"authcConfig"</span> <span>class</span>=<span>"org.pac4j.core.config.Config"</span>></span>
<span><<span>constructor-arg</span> <span>ref</span>=<span>"casClient"</span>></span><span></<span>constructor-arg</span>></span>
<span><<span>property</span> <span>name</span>=<span>&</span></span>
- 原文作者:知识铺
- 原文链接:https://index.zshipu.com/geek001/post/20240428/%E4%BD%BF%E7%94%A8buji-pac4j%E5%92%8CCAS%E5%AE%9E%E7%8E%B0%E5%8D%95%E7%82%B9%E7%99%BB%E5%BD%95--%E7%9F%A5%E8%AF%86%E9%93%BA/
- 版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。
- 免责声明:本页面内容均来源于站内编辑发布,部分信息来源互联网,并不意味着本站赞同其观点或者证实其内容的真实性,如涉及版权等问题,请立即联系客服进行更改或删除,保证您的合法权益。转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。也可以邮件至 sblig@126.com