既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
**后端可以通过之前申请的appID、appSecret和前端传来的code获取到用户的openID与session\_key**
创建springboot项目,添加依赖
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.7.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--lombok用来简化实体类-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
配置好实体类与数据相关代码后,将自己的appID、appSecret放在配置文件中
wx.open.app_id=xxxxxxxx
wx.open.app_secret=xxxxxxxxx
创建获取配置信息类
@Component
//@PropertySource(“classpath:application.properties”)
public class ConstantPropertiesUtil implements InitializingBean {
//读取配置文件并赋值
@Value("${wx.open.app_id}")
private String appId;
@Value("${wx.open.app_secret}")
private String appSecret;
public static String WX_OPEN_APP_ID;
public static String WX_OPEN_APP_SECRET;
@Override
public void afterPropertiesSet() throws Exception {
WX_OPEN_APP_ID = appId;
WX_OPEN_APP_SECRET = appSecret;
}
}
> 编写用户登录控制层,这里我的实现逻辑是根据前端传来的code,获取用户openID作为用户的唯一标识。首先在数据库中查询有无当前用户,要有创建token返回给前端对应信息。因为前端写的是一次性将code与用户信息全传过来,用户点击登陆后会跳转到授权页面,用户若点击拒绝那么用户信息将不会传过来,只有code,这时我的处理逻辑是判断有无用户信息,若没有不存如数据库,这里由于用户点击授权会有时间响应所以做了一个短暂的休眠处理。
public class LoginController {
@Autowired
private UserService userService;
@PostMapping("token")
public R login(@RequestBody LoginBO loginBO) throws IOException, InterruptedException {
//拼接对应信息
StringBuffer baseAccessTokenUrl = new StringBuffer()
.append("https://api.weixin.qq.com/sns/jscode2session")
.append("?appid=%s")
.append("&secret=%s")
.append("&js_code=%s")
.append("&grant_type=authorization_code");
String accessTokenUrl = String.format(baseAccessTokenUrl.toString(),
ConstantPropertiesUtil.WX_OPEN_APP_ID,
ConstantPropertiesUtil.WX_OPEN_APP_SECRET,
loginBO.getCode());
//像网站发送请求
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(accessTokenUrl).build();
Response response = client.newCall(request).execute();
//请求成功会返回对应信息,解析为json
if (response.isSuccessful()){
String body = response.body().string();
JSONObject jsonObject = JSONObject.parseObject(body);
String session_key = jsonObject.getString("session_key");
String openid = jsonObject.getString("openid");
HashMap<String, Object> map = new HashMap<>();
Thread.sleep(1000);
//判断数据中有无当前用户
User userInfo = userService.getByOpenId(openid);
if (userInfo==null){
User user = new User();
user.setOpenid(openid);
if (loginBO.getName().equals("")){
return R.error().message("授权失败");
}
user.setNickName(loginBO.getName());
user.setAvatarUrl(loginBO.getImg());
user.setStat(1);
userService.save(user);
userInfo = userService.getByOpenId(openid);
}
String token = JwtHelper.createToken(userInfo.getOpenid(),userInfo.getNickName(),userInfo.getAvatarUrl());
map.put("token",token);
map.put("nickname",userInfo.getNickName());
map.put("img",userInfo.getAvatarUrl());
return R.ok().data(map);
}
return R.error().message("授权失败,请重试");
}
![](https://img-blog.csdnimg.cn/d1deff990e65467d8dd097a8841b5c3b.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA6L-c6LWw5LiO5qKm5ri4,size_11,color_FFFFFF,t_70,g_se,x_16)
创建JWT生成token工具类
public class JwtHelper {
//过期时间 毫秒
private static long tokenExpiration = 60*60*1000;
//自定义秘钥
private static String tokenSignKey = "123456";
public static String createToken(String openid,String nickName,String img) {
String token = Jwts.builder()
//设置分组
.setSubject("DSXS-USER")
//设置字符串过期时间
.setExpiration(new Date(System.currentTimeMillis() + tokenExpiration))
//私有部分
.claim("userId", openid)
.claim("userName", nickName)
.claim("img",img)
//设置秘钥
.signWith(SignatureAlgorithm.HS512, tokenSignKey)
.compressWith(CompressionCodecs.GZIP)
.compact();
return token;
}
//从生成token字符串获取userId值
public static String getUserId(String token) {
if(StringUtils.isEmpty(token)) return null;
Jws<Claims> claimsJws = Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token);
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
tSigningKey(tokenSignKey).parseClaimsJws(token);
[外链图片转存中…(img-dMEsIXJP-1715319993048)]
[外链图片转存中…(img-4LNSV2SH-1715319993049)]
[外链图片转存中…(img-459lGsCa-1715319993049)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新