All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.jboot.support.jwt.JwtInterceptor Maven / Gradle / Ivy

Go to download

Jboot is a similar SpringCloud project base on JFinal, Dubbo and Undertow.

There is a newer version: 4.1.6
Show newest version
/**
 * Copyright (c) 2015-2019, Michael Yang 杨福海 ([email protected]).
 * 

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

* http://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.jboot.support.jwt; import io.jboot.Jboot; import io.jboot.utils.StrUtil; import io.jboot.web.controller.JbootController; import io.jboot.web.fixedinterceptor.FixedInterceptor; import io.jboot.web.fixedinterceptor.FixedInvocation; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Map; /** * @author Michael Yang 杨福海 ([email protected]) * @version V1.0 * @Title: 用于对Jwt的设置 * @Package io.jboot.web.jwt */ public class JwtInterceptor implements FixedInterceptor { private static JwtConfig jwtConfig = Jboot.config(JwtConfig.class); @Override public void intercept(FixedInvocation inv) { if (!jwtConfig.isEnable()) { inv.invoke(); return; } HttpServletRequest request = inv.getController().getRequest(); String token = request.getHeader(JwtManager.me().getHttpHeaderName()); if (StrUtil.isBlank(token)) { inv.invoke(); processInvokeAfter(inv, null); return; } Map map = JwtManager.me().parseJwtToken(token); if (map == null) { inv.invoke(); processInvokeAfter(inv, null); return; } try { JwtManager.me().holdJwts(map); inv.invoke(); processInvokeAfter(inv, map); } finally { JwtManager.me().releaseJwts(); } } private void processInvokeAfter(FixedInvocation inv, Map oldData) { if (!(inv.getController() instanceof JbootController)) { return; } JbootController jbootController = (JbootController) inv.getController(); Map jwtMap = jbootController.getJwtAttrs(); if (jwtMap == null || jwtMap.isEmpty()) { refreshOldJwtIfNecessary(inv, oldData); return; } String token = JwtManager.me().createJwtToken(jwtMap); HttpServletResponse response = inv.getController().getResponse(); response.addHeader(JwtManager.me().getHttpHeaderName(), token); } private void refreshOldJwtIfNecessary(FixedInvocation inv, Map oldData) { if (oldData == null) { return; } Long isuuedAtMillis = (Long) oldData.get("isuuedAt"); if (isuuedAtMillis == null || jwtConfig.getValidityPeriod() <= 0) { return; } Long nowMillis = System.currentTimeMillis(); long savedMillis = nowMillis - isuuedAtMillis; if (savedMillis > jwtConfig.getValidityPeriod() / 2) { String token = JwtManager.me().createJwtToken(oldData); HttpServletResponse response = inv.getController().getResponse(); response.addHeader(JwtManager.me().getHttpHeaderName(), token); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy