在分布式环境中,如何支持PC、APP(ios、android)等多端的会话共享,这也是所有公司都需要的解决方案,用传统的session方式来解决,我想已经out了,我们是否可以找一个通用的方案,比如用传统cas来实现多系统之间的sso单点登录或使用oauth的第三方登录方案? 今天给大家简单讲解一下使用spring拦截器Interceptor机制、jwt认证方式、redis分布式缓存实现sso单点登录,闲话少说,直接把步骤记录下来分享给大家:
1. 引入jwt的相关jar包,在项目pom.xml中引入:
![strip](http://upload-images.jianshu.io/upload_images/5712789-f0234f4d36258d9b.png?imageMogr2/auto-orient/strip)
2. 拦截器配置:
![strip](http://upload-images.jianshu.io/upload_images/5712789-bf6a3b13b4532349.png?imageMogr2/auto-orient/strip)
我这里简单配置了要拦截的url和过滤的url(这个根据自己项目来定)
3. 编写jwt的加密或者解密工具类:
![strip](http://upload-images.jianshu.io/upload_images/5712789-d87bf17a9e1cb63d.png?imageMogr2/auto-orient/strip)
这个加密工具类是我从网上找的,如果各位要修改,可以按照自己业务修改即可。
4. 创建Login.java对象,用来进行jwt的加密或者解密:
![strip](http://upload-images.jianshu.io/upload_images/5712789-9afde7c6681e4b6d.png?imageMogr2/auto-orient/strip)
![strip](http://upload-images.jianshu.io/upload_images/5712789-7819e4c34a2ee2f1.png?imageMogr2/auto-orient/strip)
5. 定义RedisLogin对象,用来通过uid往redis进行user对象存储:
![strip](http://upload-images.jianshu.io/upload_images/5712789-a1ab77ecc0d61caa.png?imageMogr2/auto-orient/strip)
![strip](http://upload-images.jianshu.io/upload_images/5712789-0eda71eba8ddb107.png?imageMogr2/auto-orient/strip)
6. 编写LoginInterceptor.java拦截器
![strip](http://upload-images.jianshu.io/upload_images/5712789-8373c2d2a2ce74eb.png?imageMogr2/auto-orient/strip)
![strip](http://upload-images.jianshu.io/upload_images/5712789-d8476325ad18274c.png?imageMogr2/auto-orient/strip)
7. 定义异常的LoginResponseCode
![strip](http://upload-images.jianshu.io/upload_images/5712789-734bcb227979fd3a.png?imageMogr2/auto-orient/strip)
8. 编写统一sso单点登录接口:
![strip](http://upload-images.jianshu.io/upload_images/5712789-8fac33f6fbb34fc6.png?imageMogr2/auto-orient/strip)
9. 测试sso单点登录:
![strip](http://upload-images.jianshu.io/upload_images/5712789-b09d0f196964ade4.png?imageMogr2/auto-orient/strip)
返回结果集:
![strip](http://upload-images.jianshu.io/upload_images/5712789-dc5525098b87e708.png?imageMogr2/auto-orient/strip)
愿意了解框架技术或者源码的朋友直接求求交流分享技术:2042849237
分布式的一些解决方案,有愿意了解的朋友可以找我们团队探讨
更多详细源码参考来源:http://minglisoft.cn/technology
标签:spring,springmvc,Spring MVC,web开发,java分布式架构,shiro,mybatis,kafka,J2ee分布式架构
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31452580/viewspace-2146634/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/31452580/viewspace-2146634/