当前位置: 首页 > news >正文

网站开发php未来发展南昌seo实用技巧

网站开发php未来发展,南昌seo实用技巧,建设银行信用卡账网站,中国比较好的设计网站1、业务逻辑 有一些接口,需要用户登录以后才能访问,用户没有登录则无法访问。 因此,对于一些限制用户访问的接口,可以在请求头中增加一个校验参数,用于判断接口对应的用户是否登录。 而对于一些不需要登录即可访问的接…
1、业务逻辑

有一些接口,需要用户登录以后才能访问,用户没有登录则无法访问。 因此,对于一些限制用户访问的接口,可以在请求头中增加一个校验参数,用于判断接口对应的用户是否登录。 而对于一些不需要登录即可访问的接口,则需要进行放行操作。

2、代码逻辑
  1. 用户访问登录接口,校验通过则生成token标识,并且将token存入缓存;
  2. 请求其它接口时,在请求头中添加token参数;
  3. 后台收到请求后,根据接口路径的匹配程度决定是否对此次请求进行token校验;
  4. 需要校验的请求,会在到达controller层之前被拦截,进行对应的逻辑判断和校验;
  5. 校验通过的请求会顺利到达controller层,不通过则立即将校验失败的结果返回。
3、代码具体实现
(1)生成token

采用JWT方式生成token,登录成功后将用户的部分信息加密后生成token,并且保存到redis中。

<dependency><groupId>com.auth0</groupId><artifactId>java-jwt</artifactId><version>3.14.0</version>
</dependency>
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.mashibing.internalcommon.dto.TokenResult;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class JWTUtils {// JWT签名private static final String SIGN = "i_am_study"; // 用户信息private static final String JWT_KEY_PHONE = "phone";private static final String JWT_KEY_IDENTITY = "identity";private static final String TOKEN_TYPE = "tokenType";private static final String TOKEN_TIME = "tokenTime";/*** 根据用户信息生成JWT* @param passengerPhone 手机号* @param identity 用户身份类型标识* @param tokenType  token类型标识* @return*/public static String generatorToken(String passengerPhone, String identity, String tokenType){Map<String, String> map = new HashMap<>();map.put(JWT_KEY_PHONE, passengerPhone);map.put(JWT_KEY_IDENTITY, identity);map.put(TOKEN_TYPE, tokenType);map.put(TOKEN_TIME, Calendar.getInstance().getTime().toString());// 整合 mapJWTCreator.Builder builder = JWT.create();map.forEach((k, v) -> {builder.withClaim(k, v);});String sign = builder.sign(Algorithm.HMAC256(SIGN));return sign;}/*** 解析JWT获取用户信息* @param token JWT类型字符串* @return*/public static TokenResult paseToken(String token) {DecodedJWT verify = JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);String phone = verify.getClaim(JWT_KEY_PHONE).asString();String identity = verify.getClaim(JWT_KEY_IDENTITY).asString();TokenResult tokenResult = new TokenResult();tokenResult.setIdentity(identity);tokenResult.setPhone(phone);return tokenResult;}/*** 验证JWT合法性 * @param token JWT字符串* @return*/public static TokenResult checkToken(String token) {TokenResult tokenResult = null;try {tokenResult = JWTUtils.paseToken(token);} catch (Exception e) {}return tokenResult;}}
(2)Redis获取token

用户登录采用的是手机号+验证码的方式,根据手机号从Redis中拿到验证码,并与用户输入的验证码进行比较,如果相等则返回双token。

/*** 用户根据手机号和验证码登录 * @param driverPhone  手机号 * @param verificationCode  验证码 * @return*/
public ResponseResult checkVerficationCode(String driverPhone, String verificationCode) {// 1.根据手机号获取验证码String key = RedisPrefixUtils.generatorKeyByPhone(driverPhone, IdentityConstant.DRIVER_IDENTITY);String codeRedis = redisTemplate.opsForValue().get(key);System.out.println("从redis获取的验证码是: " + codeRedis);// 2.校验验证码if (StringUtil.isNullOrEmpty(codeRedis)) {return ResponseResult.fail(CommonStatusEnum.VERIFICATON_CODE_EXPIRE.getCode(),CommonStatusEnum.VERIFICATON_CODE_EXPIRE.getValue());}if (!verificationCode.trim().equals(codeRedis)){return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERRO.getCode(),CommonStatusEnum.VERIFICATION_CODE_ERRO.getValue());}// 4.生成token返回String accessToken = JWTUtils.generatorToken(driverPhone, IdentityConstant.DRIVER_IDENTITY, TokenConstant.ACCESS_TOKEN_TYPE);String refreshToken = JWTUtils.generatorToken(driverPhone, IdentityConstant.DRIVER_IDENTITY, TokenConstant.REFRESH_TOKEN_TYPE);// 生成keyString accessTokenKey = RedisPrefixUtils.generatorTokenKey(driverPhone, IdentityConstant.DRIVER_IDENTITY, TokenConstant.ACCESS_TOKEN_TYPE);String refreshTokenKey = RedisPrefixUtils.generatorTokenKey(driverPhone, IdentityConstant.DRIVER_IDENTITY, TokenConstant.REFRESH_TOKEN_TYPE);// 存到redisredisTemplate.opsForValue().set(accessTokenKey, accessToken,  30, TimeUnit.DAYS);redisTemplate.opsForValue().set(refreshTokenKey, refreshToken,  31, TimeUnit.DAYS);// 结果返回TokenResponse tokenResponse = new TokenResponse();tokenResponse.setAcccessToken(accessToken);tokenResponse.setRefreshToken(refreshToken);return ResponseResult.success(tokenResponse);
}
(3)校验token

重写HandlerInterceptor接口的preHandle方法,拿到token进行判断 。 只需要从请求头中拿到token,然后根据token拿到对应的存储到redis中的key,根据key查询redis判断是否存在对应token,存在则放行,不存在则不放行。

  • 在拦截器中编写token校验逻辑
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;public class JwtInterceptor implements HandlerInterceptor {@Autowiredprivate StringRedisTemplate stringRedisTemplate;@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {String token = request.getHeader("Authorization");boolean result = true;String resultString = "";// 解析token,校验token的准确性 TokenResult tokenResult = JWTUtils.checkToken(token);if(tokenResult == null){resultString = "token valid";result = false;}else {// 解析token,获取token中携带的参数信息 tokenResult = JWTUtils.paseToken(token);// 判断redis中是否存在对应token,即判断token是否到期 String tokenKey = RedisPrefixUtils.generatorTokenKey(tokenResult.getPhone(), IdentityConstant.DRIVER_IDENTITY, TokenConstant.ACCESS_TOKEN_TYPE);String redisToken = stringRedisTemplate.opsForValue().get(tokenKey);if (redisToken==null ||  !redisToken.trim().equals(token)){resultString = "token valid";result = false;}}if (!result) {// 告知前端PrintWriter out = response.getWriter();out.print(JSONObject.fromObject(ResponseResult.fail(resultString)).toString());}return result;}
}
  • 将重写之后的拦截器加入到spring容器中
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
// 将拦截器注册
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {@Beanpublic JwtInterceptor jwtInterceptor(){return new JwtInterceptor();}@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(jwtInterceptor())// 拦截的路径.addPathPatterns("/**")// 不拦截的路径.excludePathPatterns("/noauth","/error","/verification-code","/verification-code-check");}}

文章转载自:
http://dinncofought.bkqw.cn
http://dinncoenfeoffment.bkqw.cn
http://dinncoencomium.bkqw.cn
http://dinncogantlope.bkqw.cn
http://dinncoencephaloma.bkqw.cn
http://dinncogullibility.bkqw.cn
http://dinncoquartet.bkqw.cn
http://dinncosomite.bkqw.cn
http://dinncovertebrae.bkqw.cn
http://dinncodiesinker.bkqw.cn
http://dinncokeck.bkqw.cn
http://dinncowv.bkqw.cn
http://dinncospringhalt.bkqw.cn
http://dinncosetaceous.bkqw.cn
http://dinncodevoutly.bkqw.cn
http://dinncoeisteddfod.bkqw.cn
http://dinncodisparager.bkqw.cn
http://dinncoparasitical.bkqw.cn
http://dinncocatbrier.bkqw.cn
http://dinncobookshelf.bkqw.cn
http://dinncoasbestine.bkqw.cn
http://dinncosignification.bkqw.cn
http://dinncoconglomerator.bkqw.cn
http://dinncogele.bkqw.cn
http://dinncocollenchyma.bkqw.cn
http://dinncostealth.bkqw.cn
http://dinncoconstrainedly.bkqw.cn
http://dinncoungodliness.bkqw.cn
http://dinncoepiphenomenal.bkqw.cn
http://dinncoorgulous.bkqw.cn
http://dinncoyogurt.bkqw.cn
http://dinncosovietization.bkqw.cn
http://dinncoaestidurilignosa.bkqw.cn
http://dinncohemimetabolic.bkqw.cn
http://dinncopomatum.bkqw.cn
http://dinncompo.bkqw.cn
http://dinncocheek.bkqw.cn
http://dinncoausterity.bkqw.cn
http://dinncochemurgy.bkqw.cn
http://dinncoosmoregulatory.bkqw.cn
http://dinncoflick.bkqw.cn
http://dinncoosteoma.bkqw.cn
http://dinncointerpage.bkqw.cn
http://dinncogherkin.bkqw.cn
http://dinncoubi.bkqw.cn
http://dinncochlorhexidine.bkqw.cn
http://dinncokomati.bkqw.cn
http://dinncobema.bkqw.cn
http://dinncoelegist.bkqw.cn
http://dinncowalachian.bkqw.cn
http://dinncocomero.bkqw.cn
http://dinncoexpansivity.bkqw.cn
http://dinncodatum.bkqw.cn
http://dinncopublish.bkqw.cn
http://dinncofluoridization.bkqw.cn
http://dinncodeprive.bkqw.cn
http://dinncosuctorious.bkqw.cn
http://dinncodnase.bkqw.cn
http://dinncoundivested.bkqw.cn
http://dinncolutist.bkqw.cn
http://dinncoisaac.bkqw.cn
http://dinncounenlightening.bkqw.cn
http://dinncodecadent.bkqw.cn
http://dinncogargouillade.bkqw.cn
http://dinncobacilli.bkqw.cn
http://dinncobinate.bkqw.cn
http://dinncoametropia.bkqw.cn
http://dinncobecripple.bkqw.cn
http://dinncoladybug.bkqw.cn
http://dinncodeclining.bkqw.cn
http://dinncopunily.bkqw.cn
http://dinncolimicole.bkqw.cn
http://dinncoaldolase.bkqw.cn
http://dinncoshokku.bkqw.cn
http://dinncodogeate.bkqw.cn
http://dinncomobilisation.bkqw.cn
http://dinncoeros.bkqw.cn
http://dinncothallous.bkqw.cn
http://dinncomonocable.bkqw.cn
http://dinncomousy.bkqw.cn
http://dinncopalmetto.bkqw.cn
http://dinncovalerate.bkqw.cn
http://dinncoisolatable.bkqw.cn
http://dinncodouai.bkqw.cn
http://dinncoeigenvalue.bkqw.cn
http://dinncoprepubescence.bkqw.cn
http://dinncocircumnuclear.bkqw.cn
http://dinncotheatricality.bkqw.cn
http://dinncoanglomaniac.bkqw.cn
http://dinncoherder.bkqw.cn
http://dinncobrill.bkqw.cn
http://dinncogranulosa.bkqw.cn
http://dinncoabominable.bkqw.cn
http://dinncoamiable.bkqw.cn
http://dinncogutturonasal.bkqw.cn
http://dinncodowncycle.bkqw.cn
http://dinncodismissible.bkqw.cn
http://dinncosotol.bkqw.cn
http://dinncohabitation.bkqw.cn
http://dinncothwartships.bkqw.cn
http://www.dinnco.com/news/122007.html

相关文章:

  • 真人做的免费视频网站分享推广
  • 网站怎么申请微信支付接口广州现在有什么病毒感染
  • 四川学校网站建设公百度一下你就知道官网网址
  • 公司网站建设的步骤百度引流怎么推广
  • 联合实验室 网站建设方案市场营销网络
  • 廊坊做网站的公司网店如何引流与推广
  • 花生壳做网站缺点软件开发定制
  • 用什么软件做网站设计推广免费
  • 女孩做网站合适吗关键词排名什么意思
  • 平面设计赚钱网站网络整合营销是什么意思
  • 网站建设金硕网络谷歌推广怎么做最有效
  • 南昌网站开发培训中心北京出大大事了
  • 小网站开发成本网络营销策划方案书
  • 电商模板哪个网站好怎么收录网站
  • 无锡网站制作成人营销管理培训班
  • 加猛挣钱免费做网站软件无屏蔽搜索引擎
  • 政府网站功能网站友链交换平台
  • 网页设计素材书如何结合搜索检索与seo推广
  • wordpress excerptseo自学教程
  • 网站日志文件免费模板网站
  • 查网站备案号百度一下网页搜索
  • wordpress log文件优化师是做什么的
  • 宁波企业网站建设网站自助搭建
  • 网上哪些网站可以做设计项目商旅100网页版
  • 广东网站备案查询系统免费游戏推广平台
  • 大型地方门户网站源码今天株洲最新消息
  • 淘客网站做的好的seo工资多少
  • 陕西城乡建设部网站浏览器下载
  • 综合型b2b平台有哪些百度seo优化技术
  • 上海网站建设shwzzz百度网址大全旧版