设为首页收藏本站我的广告

运维网

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
总共321条微博

每日一博

查看: 206|回复: 1

[其它] 【站长推荐】spring+springmvc+Interceptor+jwt+redis实现sso单点登录【顶】

[复制链接]

该用户从未签到

啊拉 发表于 2017-11-9 10:54:35 | 显示全部楼层 |阅读模式
【站长推荐】在分布式环境中,如何支持PC、APP(ios、android)等多端的会话共享,这也是所有公司都需要的解决方案,用传统的session方式来解决,我想已经out了,我们是否可以找一个通用的方案,比如用传统cas来实现多系统之间的sso单点登录或使用oauth的第三方登录方案? 今天给大家简单讲解一下使用spring拦截器Interceptor机制、jwt认证方式、redis分布式缓存实现sso单点登录

}

publicvoid setUid(String uid) {

this.uid = uid;

}

public String getToken() {

return token;

}

publicvoid setToken(String token) {

this.token = token;

}

publiclong getRefTime() {

return refTime;

}

publicvoid setRefTime(long refTime) {

this.refTime = refTime;

}

}

public class RedisLogin implements Serializable{

/**

*

*/

private static final long serialVersionUID = 8116817810829835862L;

/**

* 用户id

*/

private String uid;

/**

* jwt生成的token信息

*/

private String token;

/**

* 登录或刷新应用的时间

*/

private long refTime;

public RedisLogin(){

}

public RedisLogin(String uid, String token, long refTime){

this.uid = uid;

this.token = token;

this.refTime = refTime;

}

public String getUid() {

return uid;

}

public void setUid(String uid) {

this.uid = uid;

}

public String getToken() {

return token;

}

public void setToken(String token) {

this.token = token;

}

public long getRefTime() {

return refTime;

}

public void setRefTime(long refTime) {

this.refTime = refTime;

}

}

6. 编写LoginInterceptor.java拦截器

Java代码

publicclass LoginInterceptorimplements HandlerInterceptor{

publicboolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)

throws Exception {

PrintWriter writer =null;

HandlerMethod method =null;

try {

method = (HandlerMethod) handler;

}catch (Exception e) {

writer = response.getWriter();

ResponseVO responseVO = ResponseCode.buildEnumResponseVO(ResponseCode.REQUEST_URL_NOT_SERVICE,false);

responseMessage(response, writer, responseVO);

returnfalse;

}

IsLogin isLogin = method.getMethodAnnotation(IsLogin.class);

if(null == isLogin){

returntrue;

}

response.setCharacterEncoding("utf-8");

String token = request.getHeader("token");

String uid = request.getHeader("uid");

//token不存在

if(StringUtils.isEmpty(token)) {

writer = response.getWriter();

ResponseVO responseVO = LoginResponseCode.buildEnumResponseVO(LoginResponseCode.LOGIN_TOKEN_NOT_NULL,false);

responseMessage(response, writer, responseVO);

returnfalse;

}

if(StringUtils.isEmpty(uid)){

writer = response.getWriter();

ResponseVO responseVO = LoginResponseCode.buildEnumResponseVO(LoginResponseCode.USERID_NOT_NULL,false);

responseMessage(response, writer, responseVO);

returnfalse;

}

Login login = JWT.unsign(token, Login.class);

//解密token后的loginId与用户传来的loginId判断是否一致

if(null == login || !StringUtils.equals(login.getUid(), uid)){

writer = response.getWriter();

ResponseVO responseVO = LoginResponseCode.buildEnumResponseVO(LoginResponseCode.USERID_NOT_UNAUTHORIZED,false);

responseMessage(response, writer, responseVO);

returnfalse;

}

//验证登录时间

RedisLogin redisLogin = (RedisLogin)JedisUtils.getObject(uid);

if(null == redisLogin){

writer = response.getWriter();

ResponseVO responseVO = LoginResponseCode.buildEnumResponseVO(LoginResponseCode.RESPONSE_CODE_UNLOGIN_ERROR,false);

responseMessage(response, writer, responseVO);

returnfalse;

}

if(!StringUtils.equals(token, redisLogin.getToken())){

writer = response.getWriter();

ResponseVO responseVO = LoginResponseCode.buildEnumResponseVO(LoginResponseCode.USERID_NOT_UNAUTHORIZED,false);

responseMessage(response, writer, responseVO);

returnfalse;

}

//系统时间>有效期(说明已经超过有效期)

if (System.currentTimeMillis() > redisLogin.getRefTime()) {

writer = response.getWriter();

ResponseVO responseVO = LoginResponseCode.buildEnumResponseVO(LoginResponseCode.LOGIN_TIME_EXP,false);

responseMessage(response, writer, responseVO);

returnfalse;

}

//重新刷新有效期

redisLogin =new RedisLogin(uid, token, System.currentTimeMillis() + 60L* 1000L* 30L);

JedisUtils.setObject(uid , redisLogin,360000000);

returntrue;

}

publicvoid postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,

ModelAndView modelAndView)throws Exception {

}

publicvoid afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)

throws Exception {

}

privatevoid responseMessage(HttpServletResponse response, PrintWriter out, ResponseVO responseVO) {

response.setContentType("application/json; charset=utf-8");

JSONObject result =new JSONObject();

result.put("result", responseVO);

out.print(result);

out.flush();

out.close();

}

}

[java]view plaincopyprint?

publicclass LoginInterceptorimplements HandlerInterceptor{

publicboolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)

throws Exception {

PrintWriter writer =null;

HandlerMethod method =null;

try {

method = (HandlerMethod) handler;

}catch (Exception e) {

writer = response.getWriter();

ResponseVO responseVO = ResponseCode.buildEnumResponseVO(ResponseCode.REQUEST_URL_NOT_SERVICE,false);

responseMessage(response, writer, responseVO);

returnfalse;

}

IsLogin isLogin = method.getMethodAnnotation(IsLogin.class);

if(null == isLogin){

returntrue;

}

response.setCharacterEncoding("utf-8");

String token = request.getHeader("token");

String uid = request.getHeader("uid");

//token不存在

if(StringUtils.isEmpty(token)) {

writer = response.getWriter();

ResponseVO responseVO = LoginResponseCode.buildEnumResponseVO(LoginResponseCode.LOGIN_TOKEN_NOT_NULL,false);

responseMessage(response, writer, responseVO);

returnfalse;

}

if(StringUtils.isEmpty(uid)){

writer = response.getWriter();

ResponseVO responseVO = LoginResponseCode.buildEnumResponseVO(LoginResponseCode.USERID_NOT_NULL,false);

responseMessage(response, writer, responseVO);

returnfalse;

}

Login login = JWT.unsign(token, Login.class);

//解密token后的loginId与用户传来的loginId判断是否一致

if(null == login || !StringUtils.equals(login.getUid(), uid)){

writer = response.getWriter();

ResponseVO responseVO = LoginResponseCode.buildEnumResponseVO(LoginResponseCode.USERID_NOT_UNAUTHORIZED,false);

responseMessage(response, writer, responseVO);

returnfalse;

}

//验证登录时间

RedisLogin redisLogin = (RedisLogin)JedisUtils.getObject(uid);

if(null == redisLogin){

writer = response.getWriter();

ResponseVO responseVO = LoginResponseCode.buildEnumResponseVO(LoginResponseCode.RESPONSE_CODE_UNLOGIN_ERROR,false);

responseMessage(response, writer, responseVO);

returnfalse;

}

if(!StringUtils.equals(token, redisLogin.getToken())){

writer = response.getWriter();

ResponseVO responseVO = LoginResponseCode.buildEnumResponseVO(LoginResponseCode.USERID_NOT_UNAUTHORIZED,false);

responseMessage(response, writer, responseVO);

returnfalse;

}

//系统时间>有效期(说明已经超过有效期)

if (System.currentTimeMillis() > redisLogin.getRefTime()) {

writer = response.getWriter();

ResponseVO responseVO = LoginResponseCode.buildEnumResponseVO(LoginResponseCode.LOGIN_TIME_EXP,false);

responseMessage(response, writer, responseVO);

returnfalse;

}

//重新刷新有效期

redisLogin =new RedisLogin(uid, token, System.currentTimeMillis() + 60L* 1000L* 30L);

JedisUtils.setObject(uid , redisLogin,360000000);

returntrue;愿意了解框架技术或者源码的朋友直接求求交流分享技术:3133806896

分布式的一些解决方案,有愿意了解的朋友可以找我们团队探讨

更多详细源码参考来源

运维网 感谢您的阅读
[发帖际遇]: 啊拉 被钱袋砸中进医院,看病花了 3 金钱. 幸运榜 / 衰神榜
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|申请友链|sitemap|手机版|小黑屋|Archiver|运维网 ( 京ICP备16008201号  

GMT+8, 2018-1-23 11:44 , Processed in 0.227996 second(s), 40 queries , XCache On.

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表