SpringBoot整合JustAuth部分代码
gitee:ttt 项目
1.1、思路
一、前端:gitee、github、qq 图标链接
二、后端
1、导入相关:依赖(maven项目)
2、写两个接口:
2-1:根据(用户ID、回调URL) 获取 (授权码)
2-2:根据(用户ID、用户秘钥、回调URL、授权码) 获取 token (并)获取(用户信息)
1.2、jar依赖(maven项目)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.72</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.1.4</version>
</dependency>
<dependency>
<groupId>com.xkcoding.justauth</groupId>
<artifactId>justauth-spring-boot-starter</artifactId>
<version>1.4.0</version>
</dependency>
</dependencies>
1.3、java后端:2个接口
@Controller
public class AuthController {
@Autowired
private UserMapper userMapper;
@Autowired
private AuthRequestFactory factory;
/**
* 1、获取(授权码)
*/
@GetMapping("/login/{type}")
public void login(@PathVariable("type") String type, HttpServletResponse response) throws IOException {
//1、根据type(第三方类型) -> 获取 yaml 中的(用户ID、用户秘钥、回调URL)信息
AuthRequest authRequest = factory.get(type);
//2、重定向:去(第三方)获取(授权码)
response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));
}
/**
* 2、回单函数、第三方信息存入数据库、跳转首页
*/
@GetMapping("/callback/{type}")
public String login(@PathVariable String type, AuthCallback callback, HttpSession session) {
//1、根据type(第三方类型) -> 获取 yaml 中的(用户ID、用户秘钥、回调URL)信息
AuthRequest authRequest = factory.get(type);
//2、根据(授权码code、用户ID、用户秘钥、回调URL)post请求获取:token, 并get请求获取(第三方:用户信息)
AuthResponse response = authRequest.login(callback);
//3、获取(用户信息)
Object data = response.getData();
//4、若(用户信息)为空, 跳转(登录页面)
if(ObjectUtil.isEmpty(data)){
session.setAttribute("msg", "获取:用户信息失败!!!");
return "redirect:/skipIndex";
}
//5、若存在(用户信息), 转为User对象
User user = BeanUtil.toBean(data, User.class);
//6、根据:username 和 UUID 查询
User dbUser = userMapper.selByUsernameAndUUid(user);
//7、若(数据库)存在信息, 跳转到(主页)显示用户名
if(ObjectUtil.isNotEmpty(dbUser)){
session.setAttribute("user", dbUser);
return "redirect:/skipHome";
}
//8、若数据库不存在:设置密码为uuid
user.setPassword(UUID.randomUUID().toString());
//9、添加(第三方:用户信息)到 user 表
userMapper.insert(user);
//10、用户信息:放入session中
session.setAttribute("user", user);
//11、跳转:首页,展示用户信息
return "redirect:/skipHome";
}
}
标签:SpringBoot,JustAuth,代码,boot,用户,获取,user,spring,type
From: https://www.cnblogs.com/yangzaikongzhongfei/p/17490447.html