package com.majiang.community.controller;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.majiang.community.dto.AccessTokenDTO;
import com.majiang.community.dto.GiteeUserDTO;
import com.majiang.community.mapper.UserMapper;
import com.majiang.community.model.User;
import com.majiang.community.util.CookieUtil;
import com.majiang.community.util.HttpClientUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
/**
* @date 2022-04-13
*/
@Controller
public class AuthorizeController {
Logger logger=LoggerFactory.getLogger(AuthorizeController.class);
@Autowired(required = false)
private UserMapper userMapper;
@Autowired
private CookieUtil cookieUtil;
@Autowired
private HttpClientUtil httpClientUtil;
@Value("${gitee.client.id}")
private String client_id;
@Value("${gitee.client.secrets}")
private String client_secret;
@Value("${gitee.redirect_uri}")
private String redirect_uri;
@GetMapping("/callBack")
public String callBack(@RequestParam(name = "state")String state,
@RequestParam(name = "code")String code,
HttpServletRequest request,
HttpServletResponse response){
String cookieValue = cookieUtil.getCookieValue(request);
//cookie已经过期
if(StrUtil.isEmpty(cookieValue)){
AccessTokenDTO accessTokenDto = new AccessTokenDTO();
accessTokenDto.setCode(code);
accessTokenDto.setClient_id(client_id);
accessTokenDto.setRedirect_uri(redirect_uri);
accessTokenDto.setClient_secret(client_secret);
accessTokenDto.setState(state);
//获取token
String access_token = httpClientUtil.httpResult(accessTokenDto);
//根据token获取用户信息
GiteeUserDTO giteeUserDto = httpClientUtil.getUser(access_token);
logger.info(giteeUserDto.getId()+" "+giteeUserDto.getLogin()+" "+giteeUserDto.getBio()+" "+giteeUserDto.getAvatar_url());
//将用户信息存储到数据库
if(giteeUserDto!=null&&StrUtil.isNotEmpty(giteeUserDto.getId().toString())){
QueryWrapper queryWrapper=new QueryWrapper();
queryWrapper.eq("fd_account_id", giteeUserDto.getId().toString());
User selectOne = userMapper.selectOne(queryWrapper);
String fdToken = UUID.randomUUID().toString();
if(selectOne!=null){
selectOne.setFdToken(fdToken);
userMapper.updateById(selectOne);
}else{
User user=new User();
user.setFdName(giteeUserDto.getLogin());
user.setFdToken(fdToken);
user.setFdAccountId(giteeUserDto.getId().toString());
user.setGmtCreateTime(System.currentTimeMillis());
user.setAvatarUrl(giteeUserDto.getAvatar_url());
user.setGmtAlterTime(System.currentTimeMillis());
userMapper.insert(user);
}
Cookie cookie = new Cookie("token", fdToken);
response.addCookie(cookie);
}
}
//重定向
return "redirect:/";
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
码匠社区学习笔记(二)
共189个文件
xml:97个
java:18个
class:18个
需积分: 0 0 下载量 136 浏览量
2022-04-27
17:04:23
上传
评论
收藏 1.09MB ZIP 举报
温馨提示
学习视频地址:https://www.bilibili.com/video/av65117012?p=1 代码笔记地址:https://blog.csdn.net/Mr_wo7/article/details/124277263#comments_21101645
资源详情
资源评论
资源推荐
收起资源包目录
码匠社区学习笔记(二) (189个子文件)
QuestionDTO.class 6KB
Question.class 6KB
PageUtils.class 6KB
AuthorizeController.class 5KB
User.class 4KB
QuestionServiceImp.class 4KB
PublishController.class 4KB
IndexController.class 3KB
AccessTokenDTO.class 3KB
GiteeUserDTO.class 3KB
HttpClientUtil.class 3KB
CommunityApplicationTests.class 1KB
MyBatisConfig.class 1KB
CookieUtil.class 1KB
CommunityApplication.class 854B
IQuestionService.class 394B
QuestionMapper.class 323B
UserMapper.class 311B
mvnw.cmd 7KB
bootstrap.css 143KB
bootstrap.css 143KB
bootstrap.min.css 119KB
bootstrap.min.css 119KB
bootstrap-theme.css 25KB
bootstrap-theme.css 25KB
bootstrap-theme.min.css 23KB
bootstrap-theme.min.css 23KB
community.css 2KB
community.css 2KB
glyphicons-halflings-regular.eot 20KB
glyphicons-halflings-regular.eot 20KB
.gitignore 395B
index.html 4KB
index.html 4KB
publish.html 3KB
publish.html 3KB
navigation.html 2KB
navigation.html 2KB
community.iml 11KB
maven-wrapper.jar 57KB
AuthorizeController.java 4KB
PublishController.java 3KB
QuestionServiceImp.java 3KB
IndexController.java 2KB
PageUtils.java 2KB
HttpClientUtil.java 2KB
MyBatisConfig.java 1KB
Question.java 908B
CookieUtil.java 797B
User.java 727B
CommunityApplicationTests.java 616B
QuestionDTO.java 493B
CommunityApplication.java 449B
IQuestionService.java 339B
AccessTokenDTO.java 291B
GiteeUserDTO.java 266B
QuestionMapper.java 260B
UserMapper.java 249B
jquery.js 262KB
jquery.js 262KB
jquery.min.js 90KB
jquery.min.js 90KB
bootstrap.js 74KB
bootstrap.js 74KB
bootstrap.min.js 39KB
bootstrap.min.js 39KB
npm.js 484B
npm.js 484B
community.kotlin_module 16B
bootstrap.min.css.map 528KB
bootstrap.min.css.map 528KB
bootstrap.css.map 382KB
bootstrap.css.map 382KB
bootstrap-theme.min.css.map 74KB
bootstrap-theme.min.css.map 74KB
bootstrap-theme.css.map 47KB
bootstrap-theme.css.map 47KB
HELP.md 894B
mvnw 10KB
application.properties 599B
application.properties 423B
maven-wrapper.properties 233B
glyphicons-halflings-regular.svg 106KB
glyphicons-halflings-regular.svg 106KB
glyphicons-halflings-regular.ttf 44KB
glyphicons-halflings-regular.ttf 44KB
glyphicons-halflings-regular.woff 23KB
glyphicons-halflings-regular.woff 23KB
glyphicons-halflings-regular.woff2 18KB
glyphicons-halflings-regular.woff2 18KB
workspace.xml 14KB
uiDesigner.xml 9KB
pom.xml 3KB
Project_Default.xml 1KB
compiler.xml 920B
Maven__org_springframework_boot_spring_boot_test_autoconfigure_2_1_4_RELEASE.xml 769B
Maven__org_springframework_boot_spring_boot_starter_thymeleaf_2_1_4_RELEASE.xml 762B
Maven__org_springframework_boot_spring_boot_starter_logging_2_1_4_RELEASE.xml 748B
Maven__org_springframework_boot_spring_boot_starter_tomcat_2_1_4_RELEASE.xml 741B
Maven__org_springframework_boot_spring_boot_autoconfigure_2_1_4_RELEASE.xml 734B
共 189 条
- 1
- 2
Mr_wa7
- 粉丝: 41
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0