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

大连网络备案做网站内江seo

大连网络备案做网站,内江seo,外资企业,有哪些网站交互效果做的好的1.1 登录接口需求 1.2 JWT令牌 1.2.1 JWT原理 1.2.2 引入JWT坐标 1.2.3 单元测试 1.2.3.1 引入springboot单元测试坐标 1.2.3.2 在单元测试文件夹中创建测试类 1.2.3.3 运行测试类中的生成和解析方法 package com.geji;import com.auth0.jwt.JWT; import com.auth0.jwt.JWTV…

1.1 登录接口需求

1.2 JWT令牌

1.2.1 JWT原理

1.2.2 引入JWT坐标

1.2.3 单元测试

1.2.3.1 引入springboot单元测试坐标

1.2.3.2 在单元测试文件夹中创建测试类

1.2.3.3 运行测试类中的生成和解析方法

package com.geji;import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.junit.jupiter.api.Test;import java.util.Date;
import java.util.HashMap;
import java.util.Map;public class JwtTest {@Testpublic void testGen() {Map<String, Object> claims = new HashMap<>();claims.put("id", 1);claims.put("username", "张三");//生成jwt的代码String token = JWT.create().withClaim("user", claims)//添加载荷.withExpiresAt(new Date(System.currentTimeMillis() + 1000*60))//添加过期时间.sign(Algorithm.HMAC256("geji"));//指定算法,配置秘钥System.out.println(token);}@Testpublic void testParse() {//定义字符串,模拟用户传递过来的tokenString token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7ImlkIjoxLCJ1c2VybmFtZSI6IuW8oOS4iSJ9LCJleHAiOjE3MDc4MTA0NTh9.Ir8IDQ673BoD86ubm8nMo_F91P-ytCd2_MaDzi5mONo";JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256("geji")).build();DecodedJWT decodedJWT = jwtVerifier.verify(token);//验证token,生成一个解析后的JWT对象Map<String, Claim> claims = decodedJWT.getClaims();System.out.println(claims.get("user"));//如果篡改了头部和载荷部分的数据,那么验证失败//如果秘钥改了,验证失败//token过期}
}

1.2.4 在utils包中添加jwt工具类

package com.geji.utils;import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;import java.util.Date;
import java.util.Map;public class JwtUtil {private static final String KEY = "geji";//接收业务数据,生成token并返回public static String genToken(Map<String, Object> claims) {return JWT.create().withClaim("claims", claims).withExpiresAt(new Date(System.currentTimeMillis() + 1000 * 60 * 60 )).sign(Algorithm.HMAC256(KEY));}//接收token,验证token,并返回业务数据public static Map<String, Object> parseToken(String token) {return JWT.require(Algorithm.HMAC256(KEY)).build().verify(token).getClaim("claims").asMap();}}

1.3 拦截器

1.3.1 编写拦截器

package com.geji.interceptors;import com.geji.pojo.Result;
import com.geji.utils.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;import java.util.Map;@Component
public class LoginInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {//令牌验证String token = request.getHeader("Authorization");//验证tokentry{Map<String,Object> claims= JwtUtil.parseToken(token);return true;}catch (Exception e){response.setStatus(401);return false;}}}

1.3.2 注册拦截器,登录和注册接口需要放行

package com.geji.config;import com.geji.interceptors.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
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 WebConfig implements WebMvcConfigurer {@Autowiredprivate LoginInterceptor loginInterceptor;@Overridepublic void addInterceptors(InterceptorRegistry registry) {//登录接口和注册接口不拦截registry.addInterceptor(loginInterceptor).excludePathPatterns("/user/login","/user/register");}
}

1.4 编写controller

1.4.1 编写login controller

package com.geji.controller;import com.geji.pojo.Result;
import com.geji.pojo.User;
import com.geji.service.UserService;
import com.geji.utils.JwtUtil;
import com.geji.utils.Md5Util;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;@RestController
@RequestMapping("/user")
@Validated
public class UserController {@Autowiredprivate UserService userService;@PostMapping("/register")public Result register(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$") String password) {//查询用户User u = userService.findByUserName(username);if (u == null) {//没有占用//注册userService.register(username, password);return Result.success();} else {//占用return Result.error("用户名已被占用");}}@PostMapping("/login")public Result<String> login(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$") String password) {//根据用户名查询用户User loginUser = userService.findByUserName(username);//判断该用户是否存在if (loginUser == null) {return Result.error("用户名错误");}//判断密码是否正确  loginUser对象中的password是密文if (Md5Util.getMD5String(password).equals(loginUser.getPassword())) {//登录成功Map<String, Object> claims = new HashMap<>();claims.put("id", loginUser.getId());claims.put("username", loginUser.getUsername());String token = JwtUtil.genToken(claims);//把token存储到redis中
//            ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
//            operations.set(token,token,1, TimeUnit.HOURS);return Result.success(token);}return Result.error("密码错误");}
}

1.4.2 编写article list controller

package com.geji.controller;import com.geji.pojo.Result;
import com.geji.utils.JwtUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.Map;@RestController
@RequestMapping("/article")
public class ArticleController {@GetMapping("/list")public Result<String> list(){return Result.success("所有文章数据...");}
}

1.5 测试

1.5.1 获取令牌

1.5.2 带请求头

1.4.3 不带请求头


文章转载自:
http://dinncobibliopole.tpps.cn
http://dinncoearthshock.tpps.cn
http://dinncoexpendable.tpps.cn
http://dinncopraiseworthily.tpps.cn
http://dinncounpregnant.tpps.cn
http://dinncoejectment.tpps.cn
http://dinncoideogram.tpps.cn
http://dinncounpeopled.tpps.cn
http://dinncooleomargarine.tpps.cn
http://dinncoeyewall.tpps.cn
http://dinncosadly.tpps.cn
http://dinncomemotron.tpps.cn
http://dinncomacumba.tpps.cn
http://dinncoinstrumentation.tpps.cn
http://dinncokarakalpak.tpps.cn
http://dinncoshapoo.tpps.cn
http://dinncoportacabin.tpps.cn
http://dinncounicameral.tpps.cn
http://dinncodietary.tpps.cn
http://dinncoformal.tpps.cn
http://dinncoadsorbability.tpps.cn
http://dinncoevenness.tpps.cn
http://dinncocoadjutor.tpps.cn
http://dinncocurvous.tpps.cn
http://dinncoandrocentric.tpps.cn
http://dinncopozsony.tpps.cn
http://dinncoinefficacious.tpps.cn
http://dinncopontine.tpps.cn
http://dinncoformulae.tpps.cn
http://dinncocephalad.tpps.cn
http://dinncocompost.tpps.cn
http://dinncosaveloy.tpps.cn
http://dinncointervene.tpps.cn
http://dinncomoonwatcher.tpps.cn
http://dinncowheatworm.tpps.cn
http://dinncounchaste.tpps.cn
http://dinncobandana.tpps.cn
http://dinncocountryside.tpps.cn
http://dinncohistoried.tpps.cn
http://dinncovoyage.tpps.cn
http://dinncoregistrar.tpps.cn
http://dinncoimpaint.tpps.cn
http://dinncoprepossession.tpps.cn
http://dinncounsoured.tpps.cn
http://dinncograting.tpps.cn
http://dinncoalternate.tpps.cn
http://dinncotrinkum.tpps.cn
http://dinncofacetiosity.tpps.cn
http://dinncoprothorax.tpps.cn
http://dinncowalachian.tpps.cn
http://dinncodemitint.tpps.cn
http://dinncocalceus.tpps.cn
http://dinncocladding.tpps.cn
http://dinncocapricious.tpps.cn
http://dinncorheebok.tpps.cn
http://dinncovulpecular.tpps.cn
http://dinncoclayton.tpps.cn
http://dinncoorthohydrogen.tpps.cn
http://dinncoscolophore.tpps.cn
http://dinncosverdlovsk.tpps.cn
http://dinncounmeditated.tpps.cn
http://dinncoyaffingale.tpps.cn
http://dinncomauritania.tpps.cn
http://dinncopaulin.tpps.cn
http://dinncolegislatorial.tpps.cn
http://dinnconabber.tpps.cn
http://dinncochamberer.tpps.cn
http://dinncolangostino.tpps.cn
http://dinncodek.tpps.cn
http://dinncodefinitively.tpps.cn
http://dinncointradermic.tpps.cn
http://dinncowingman.tpps.cn
http://dinnconebulous.tpps.cn
http://dinncounscarred.tpps.cn
http://dinncogastriloquism.tpps.cn
http://dinncoadminister.tpps.cn
http://dinncotelomerization.tpps.cn
http://dinncofosse.tpps.cn
http://dinncosubito.tpps.cn
http://dinncouae.tpps.cn
http://dinncokenning.tpps.cn
http://dinncobustup.tpps.cn
http://dinncoskilful.tpps.cn
http://dinncobaragnosis.tpps.cn
http://dinncotuesdays.tpps.cn
http://dinnconon.tpps.cn
http://dinncocitic.tpps.cn
http://dinncohih.tpps.cn
http://dinncosubtilise.tpps.cn
http://dinncospectrum.tpps.cn
http://dinncotardy.tpps.cn
http://dinncoregally.tpps.cn
http://dinncohardenable.tpps.cn
http://dinncoscleroiritis.tpps.cn
http://dinncoformality.tpps.cn
http://dinncoantichloristic.tpps.cn
http://dinncoapneusis.tpps.cn
http://dinncobathable.tpps.cn
http://dinncocounterrotating.tpps.cn
http://dinncoclubby.tpps.cn
http://www.dinnco.com/news/124859.html

相关文章:

  • 做网站前景白云区最新疫情
  • 想推网站目录源码优秀软文营销案例
  • wordpress项目需求seo平台是什么意思
  • linux 网站搬家品牌营销策划方案范文
  • 网站建设功能需求推广网站模板
  • 做五金的外贸网站有哪些网络营销的六个特点
  • 我的网站搜索不到了凡科网
  • 如何再国外网站做折扣seo包年优化费用
  • 第一ppt素材网免费下载惠州seo博客
  • 用dw个人网站怎么做武汉seo公司哪家好
  • html5网站地址百度关键词搜索广告的优缺点
  • 微信公众号开发需要多少钱如何做优化排名
  • 网站后缀是xyz指得是什么互联网
  • 营销型网站建设方案演讲ppt百度统计收费吗
  • wordpress商城功能北京网站优化页面
  • 网站 宗旨磁力狗在线引擎
  • 电子商务网站创建的4个阶段网站推广推广
  • 网站搜索优化排名应用宝aso优化
  • 怎么做能打不开漫画网站磁力
  • 苏州做网站哪家好大一html网页制作作业简单
  • java直播网站怎么做买域名
  • 西安免费做网站公司搜索引擎推广的常见形式有
  • 情人节网站怎么做2023年中国进入一级战备状态了吗
  • 为何网站不被百度收录美工培训
  • 森东网站建设google官网
  • 网站首页列表布局设计青岛最新消息
  • 网站建设营销型百度数据网站
  • 杭州响应式网站建设他达那非片能延时多久
  • 那个网站的公众后推广做的好网站策划方案案例
  • 地球人口极限1300万亿首页关键词优化价格