package com.alatus.mall.member.app;
import java.util.Arrays;
import java.util.Map;
import com.alatus.common.exception.BizCodeEnum;
import com.alatus.mall.member.exception.PhoneExistException;
import com.alatus.mall.member.exception.UsernameExistException;
import com.alatus.mall.member.vo.GiteeSocialUser;
import com.alatus.mall.member.vo.MemberLoginVo;
import com.alatus.mall.member.vo.MemberRegisterVo;
import com.alatus.mall.member.vo.WeiboSocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.alatus.mall.member.entity.MemberEntity;
import com.alatus.mall.member.service.MemberService;
import com.alatus.common.utils.PageUtils;
import com.alatus.common.utils.R;
/**
* 会员
*
* @author alatus
* @email 1571345941@qq.com
* @date 2024-03-12 13:40:22
*/
@RestController
@RequestMapping("member/member")
public class MemberController {
@Autowired
private MemberService memberService;
@PostMapping("register")
public R register(@RequestBody MemberRegisterVo memberRegisterVo){
try{
memberService.register(memberRegisterVo);
}
catch (UsernameExistException e){
return R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(), BizCodeEnum.USER_EXIST_EXCEPTION.getMsg());
}
catch (PhoneExistException e){
return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnum.PHONE_EXIST_EXCEPTION.getMsg());
}
return R.ok();
}
@PostMapping("/oauth2/weibo/login")
public R weiboOauthLogin(@RequestBody WeiboSocialUser weiboSocialUser){
MemberEntity member = memberService.login(weiboSocialUser);
if(member!=null){
return R.ok();
}
else{
return R.error(BizCodeEnum.LOGINACCTOUNT_PASSWORD_INVALID_EXCEPTION.getCode(),BizCodeEnum.LOGINACCTOUNT_PASSWORD_INVALID_EXCEPTION.getMsg());
}
}
@PostMapping("/oauth2/gitee/login")
public R giteeOauthLogin(@RequestBody GiteeSocialUser giteeSocialUser){
MemberEntity member = memberService.login(giteeSocialUser);
if(member!=null){
return R.ok();
}
else{
return R.error(BizCodeEnum.LOGINACCTOUNT_PASSWORD_INVALID_EXCEPTION.getCode(),BizCodeEnum.LOGINACCTOUNT_PASSWORD_INVALID_EXCEPTION.getMsg());
}
}
@PostMapping("/login")
public R login(@RequestBody MemberLoginVo memberLoginVo){
MemberEntity member = memberService.login(memberLoginVo);
if(member!=null){
return R.ok();
}
else{
return R.error(BizCodeEnum.LOGINACCTOUNT_PASSWORD_INVALID_EXCEPTION.getCode(),BizCodeEnum.LOGINACCTOUNT_PASSWORD_INVALID_EXCEPTION.getMsg());
}
}
/**
* 列表
*/
@RequestMapping("/list")
public R list(@RequestParam Map<String, Object> params){
PageUtils page = memberService.queryPage(params);
return R.ok().put("page", page);
}
/**
* 验证手机号是否已被使用
*/
@GetMapping("/checkPhone/{phone}")
public R checkPhone(@PathVariable("phone")String phone){
try{
memberService.checkPhoneUnique(phone);
}
catch (PhoneExistException e){
return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnum.PHONE_EXIST_EXCEPTION.getMsg());
}
return R.ok();
}
/**
* 信息
*/
@RequestMapping("/info/{id}")
public R info(@PathVariable("id") Long id){
MemberEntity member = memberService.getById(id);
return R.ok().put("member", member);
}
/**
* 保存
*/
@RequestMapping("/save")
public R save(@RequestBody MemberEntity member){
memberService.save(member);
return R.ok();
}
/**
* 修改
*/
@RequestMapping("/update")
public R update(@RequestBody MemberEntity member){
memberService.updateById(member);
return R.ok();
}
/**
* 删除
*/
@RequestMapping("/delete")
public R delete(@RequestBody Long[] ids){
memberService.removeByIds(Arrays.asList(ids));
return R.ok();
}
}
package com.alatus.mall.member.app;
import java.util.Arrays;
import java.util.Map;
import com.alatus.common.exception.BizCodeEnum;
import com.alatus.mall.member.exception.PhoneExistException;
import com.alatus.mall.member.exception.UsernameExistException;
import com.alatus.mall.member.vo.GiteeSocialUser;
import com.alatus.mall.member.vo.MemberLoginVo;
import com.alatus.mall.member.vo.MemberRegisterVo;
import com.alatus.mall.member.vo.WeiboSocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.alatus.mall.member.entity.MemberEntity;
import com.alatus.mall.member.service.MemberService;
import com.alatus.common.utils.PageUtils;
import com.alatus.common.utils.R;
/**
* 会员
*
* @author alatus
* @email 1571345941@qq.com
* @date 2024-03-12 13:40:22
*/
@RestController
@RequestMapping("member/member")
public class MemberController {
@Autowired
private MemberService memberService;
@PostMapping("register")
public R register(@RequestBody MemberRegisterVo memberRegisterVo){
try{
memberService.register(memberRegisterVo);
}
catch (UsernameExistException e){
return R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(), BizCodeEnum.USER_EXIST_EXCEPTION.getMsg());
}
catch (PhoneExistException e){
return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnum.PHONE_EXIST_EXCEPTION.getMsg());
}
return R.ok();
}
@PostMapping("/oauth2/weibo/login")
public R weiboOauthLogin(@RequestBody WeiboSocialUser weiboSocialUser){
MemberEntity member = memberService.login(weiboSocialUser);
if(member!=null){
return R.ok();
}
else{
return R.error(BizCodeEnum.LOGINACCTOUNT_PASSWORD_INVALID_EXCEPTION.getCode(),BizCodeEnum.LOGINACCTOUNT_PASSWORD_INVALID_EXCEPTION.getMsg());
}
}
@PostMapping("/oauth2/gitee/login")
public R giteeOauthLogin(@RequestBody GiteeSocialUser giteeSocialUser){
MemberEntity member = memberService.login(giteeSocialUser);
if(member!=null){
return R.ok();
}
else{
return R.error(BizCodeEnum.LOGINACCTOUNT_PASSWORD_INVALID_EXCEPTION.getCode(),BizCodeEnum.LOGINACCTOUNT_PASSWORD_INVALID_EXCEPTION.getMsg());
}
}
@PostMapping("/login")
public R login(@RequestBody MemberLoginVo memberLoginVo){
MemberEntity member = memberService.login(memberLoginVo);
if(member!=null){
return R.ok();
}
else{
return R.error(BizCodeEnum.LOGINACCTOUNT_PASSWORD_INVALID_EXCEPTION.getCode(),BizCodeEnum.LOGINACCTOUNT_PASSWORD_INVALID_EXCEPTION.getMsg());
}
}
/**
* 列表
*/
@RequestMapping("/list")
public R list(@RequestParam Map<String, Object> params){
PageUtils page = memberService.queryPage(params);
return R.ok().put("page", page);
}
/**
* 验证手机号是否已被使用
*/
@GetMapping("/checkPhone/{phone}")
public R checkPhone(@PathVariable("phone")String phone){
try{
memberService.checkPhoneUnique(phone);
}
catch (PhoneExistException e){
return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnum.PHONE_EXIST_EXCEPTION.getMsg());
}
return R.ok();
}
/**
* 信息
*/
@RequestMapping("/info/{id}")
public R info(@PathVariable("id") Long id){
MemberEntity member = memberService.getById(id);
return R.ok().put("member", member);
}
/**
* 保存
*/
@RequestMapping("/save")
public R save(@RequestBody MemberEntity member){
memberService.save(member);
return R.ok();
}
/**
* 修改
*/
@RequestMapping("/update")
public R update(@RequestBody MemberEntity member){
memberService.updateById(member);
return R.ok();
}
/**
* 删除
*/
@RequestMapping("/delete")
public R delete(@RequestBody Long[] ids){
memberService.removeByIds(Arrays.asList(ids));
return R.ok();
}
}
package com.alatus.mall.member.service;
import com.alatus.mall.member.exception.PhoneExistException;
import com.alatus.mall.member.exception.UsernameExistException;
import com.alatus.mall.member.vo.GiteeSocialUser;
import com.alatus.mall.member.vo.MemberLoginVo;
import com.alatus.mall.member.vo.MemberRegisterVo;
import com.alatus.mall.member.vo.WeiboSocialUser;
import com.baomidou.mybatisplus.extension.service.IService;
import com.alatus.common.utils.PageUtils;
import com.alatus.mall.member.entity.MemberEntity;
import java.util.Map;
/**
* 会员
*
* @author alatus
* @email 1571345941@qq.com
* @date 2024-03-12 13:40:22
*/
public interface MemberService extends IService<MemberEntity> {
PageUtils queryPage(Map<String, Object> params);
void register(MemberRegisterVo memberRegisterVo);
void checkPhoneUnique(String phone) throws PhoneExistException;
void checkUsernameUnique(String username) throws UsernameExistException;
MemberEntity login(MemberLoginVo memberLoginVo);
MemberEntity login(WeiboSocialUser weiboSocialUser);
MemberEntity login(GiteeSocialUser giteeSocialUser);
}
package com.alatus.mall.member.service;
import com.alatus.mall.member.exception.PhoneExistException;
import com.alatus.mall.member.exception.UsernameExistException;
import com.alatus.mall.member.vo.GiteeSocialUser;
import com.alatus.mall.member.vo.MemberLoginVo;
import com.alatus.mall.member.vo.MemberRegisterVo;
import com.alatus.mall.member.vo.WeiboSocialUser;
import com.baomidou.mybatisplus.extension.service.IService;
import com.alatus.common.utils.PageUtils;
import com.alatus.mall.member.entity.MemberEntity;
import java.util.Map;
/**
* 会员
*
* @author alatus
* @email 1571345941@qq.com
* @date 2024-03-12 13:40:22
*/
public interface MemberService extends IService<MemberEntity> {
PageUtils queryPage(Map<String, Object> params);
void register(MemberRegisterVo memberRegisterVo);
void checkPhoneUnique(String phone) throws PhoneExistException;
void checkUsernameUnique(String username) throws UsernameExistException;
MemberEntity login(MemberLoginVo memberLoginVo);
MemberEntity login(WeiboSocialUser weiboSocialUser);
MemberEntity login(GiteeSocialUser giteeSocialUser);
}
package com.alatus.mall.member.service.impl;
import com.alatus.mall.member.entity.MemberLevelEntity;
import com.alatus.mall.member.exception.PhoneExistException;
import com.alatus.mall.member.exception.UsernameExistException;
import com.alatus.mall.member.service.MemberLevelService;
import com.alatus.mall.member.vo.GiteeSocialUser;
import com.alatus.mall.member.vo.MemberLoginVo;
import com.alatus.mall.member.vo.MemberRegisterVo;
import com.alatus.mall.member.vo.WeiboSocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.alatus.common.utils.PageUtils;
import com.alatus.common.utils.Query;
import com.alatus.mall.member.dao.MemberDao;
import com.alatus.mall.member.entity.MemberEntity;
import com.alatus.mall.member.service.MemberService;
@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {
@Autowired
private MemberLevelService memberLevelService;
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<MemberEntity> page = this.page(
new Query<MemberEntity>().getPage(params),
new QueryWrapper<MemberEntity>()
);
return new PageUtils(page);
}
@Override
public void register(MemberRegisterVo memberRegisterVo) {
// 检查用户名和手机号是否唯一(为了能让上层感知异常,我们这里抛出去)采用异常机制
checkPhoneUnique(memberRegisterVo.getPhone());
checkUsernameUnique(memberRegisterVo.getUsername());
MemberEntity memberEntity = new MemberEntity();
// 设置默认等级
MemberLevelEntity memberLevelEntity = memberLevelService.getDefaultLevel();
memberEntity.setLevelId(memberLevelEntity.getId());
// 密码绝对不能传递明文,一旦数据库泄露,就会导致所有数据曝光,我们应该存储MD5加密后的数据
// MD5任何长度大小的数据,算出的值都是固定的,源数据哪怕任何改动,得到的MD5值都会不同
// 百度网盘的秒传功能本质上就是MD5值相同,在这个情况下,不同文件就不需要再次上传,达到了秒传效果
// MD5的最大特点就说不可逆,无法通过MD5获取源数据的值
// MD5抗修改性意味着MD5的数据可以通过暴力算或者字典查的穷举法获得,网上有一大堆对应的彩虹表
// 因此MD5不能直接用来加密,必须加盐存储
// 使用Spring原生的加密器实现
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
memberEntity.setPassword(passwordEncoder.encode(memberRegisterVo.getPassword()));
// 用户名
memberEntity.setUsername(memberRegisterVo.getUsername());
// 手机号
memberEntity.setMobile(memberRegisterVo.getPhone());
this.baseMapper.insert(memberEntity);
}
@Override
public void checkPhoneUnique(String phone) throws PhoneExistException{
Integer count = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));
if(count>0){
throw new PhoneExistException();
}
}
@Override
public void checkUsernameUnique(String username) throws UsernameExistException{
Integer count = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", username));
if(count>0){
throw new UsernameExistException();
}
}
@Override
public MemberEntity login(MemberLoginVo memberLoginVo) {
String loginAccount = memberLoginVo.getLoginAccount();
String password = memberLoginVo.getPassword();;
// 去数据库查询
MemberEntity memberEntity = this.baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("username", loginAccount)
.or().eq("mobile", loginAccount));
if(memberEntity==null){
// 登陆失败
return null;
}
else{
String memberEntityPassword = memberEntity.getPassword();
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
// 进行密码匹配
boolean matches = passwordEncoder.matches(password, memberEntityPassword);
if(matches){
return memberEntity;
}
else{
return null;
}
}
}
// 社交登录需要同时具有注册和登录的逻辑
@Override
public MemberEntity login(WeiboSocialUser weiboSocialUser) {
String uid = weiboSocialUser.getUid();
return null;
}
@Override
public MemberEntity login(GiteeSocialUser giteeSocialUser) {
String uid = giteeSocialUser.getUid();
return null;
}
}
package com.alatus.mall.member.service.impl;
import com.alatus.mall.member.entity.MemberLevelEntity;
import com.alatus.mall.member.exception.PhoneExistException;
import com.alatus.mall.member.exception.UsernameExistException;
import com.alatus.mall.member.service.MemberLevelService;
import com.alatus.mall.member.vo.GiteeSocialUser;
import com.alatus.mall.member.vo.MemberLoginVo;
import com.alatus.mall.member.vo.MemberRegisterVo;
import com.alatus.mall.member.vo.WeiboSocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.alatus.common.utils.PageUtils;
import com.alatus.common.utils.Query;
import com.alatus.mall.member.dao.MemberDao;
import com.alatus.mall.member.entity.MemberEntity;
import com.alatus.mall.member.service.MemberService;
@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {
@Autowired
private MemberLevelService memberLevelService;
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<MemberEntity> page = this.page(
new Query<MemberEntity>().getPage(params),
new QueryWrapper<MemberEntity>()
);
return new PageUtils(page);
}
@Override
public void register(MemberRegisterVo memberRegisterVo) {
// 检查用户名和手机号是否唯一(为了能让上层感知异常,我们这里抛出去)采用异常机制
checkPhoneUnique(memberRegisterVo.getPhone());
checkUsernameUnique(memberRegisterVo.getUsername());
MemberEntity memberEntity = new MemberEntity();
// 设置默认等级
MemberLevelEntity memberLevelEntity = memberLevelService.getDefaultLevel();
memberEntity.setLevelId(memberLevelEntity.getId());
// 密码绝对不能传递明文,一旦数据库泄露,就会导致所有数据曝光,我们应该存储MD5加密后的数据
// MD5任何长度大小的数据,算出的值都是固定的,源数据哪怕任何改动,得到的MD5值都会不同
// 百度网盘的秒传功能本质上就是MD5值相同,在这个情况下,不同文件就不需要再次上传,达到了秒传效果
// MD5的最大特点就说不可逆,无法通过MD5获取源数据的值
// MD5抗修改性意味着MD5的数据可以通过暴力算或者字典查的穷举法获得,网上有一大堆对应的彩虹表
// 因此MD5不能直接用来加密,必须加盐存储
// 使用Spring原生的加密器实现
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
memberEntity.setPassword(passwordEncoder.encode(memberRegisterVo.getPassword()));
// 用户名
memberEntity.setUsername(memberRegisterVo.getUsername());
// 手机号
memberEntity.setMobile(memberRegisterVo.getPhone());
this.baseMapper.insert(memberEntity);
}
@Override
public void checkPhoneUnique(String phone) throws PhoneExistException{
Integer count = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));
if(count>0){
throw new PhoneExistException();
}
}
@Override
public void checkUsernameUnique(String username) throws UsernameExistException{
Integer count = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", username));
if(count>0){
throw new UsernameExistException();
}
}
@Override
public MemberEntity login(MemberLoginVo memberLoginVo) {
String loginAccount = memberLoginVo.getLoginAccount();
String password = memberLoginVo.getPassword();;
// 去数据库查询
MemberEntity memberEntity = this.baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("username", loginAccount)
.or().eq("mobile", loginAccount));
if(memberEntity==null){
// 登陆失败
return null;
}
else{
String memberEntityPassword = memberEntity.getPassword();
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
// 进行密码匹配
boolean matches = passwordEncoder.matches(password, memberEntityPassword);
if(matches){
return memberEntity;
}
else{
return null;
}
}
}
// 社交登录需要同时具有注册和登录的逻辑
@Override
public MemberEntity login(WeiboSocialUser weiboSocialUser) {
String uid = weiboSocialUser.getUid();
return null;
}
@Override
public MemberEntity login(GiteeSocialUser giteeSocialUser) {
String uid = giteeSocialUser.getUid();
return null;
}
}
package com.alatus.mall.auth.app;
import com.alatus.common.utils.HttpUtils;
import com.alatus.mall.auth.vo.GiteeSocialUser;
import com.alatus.mall.auth.vo.GiteeUserInfo;
import com.alatus.mall.auth.vo.WeiboSocialUser;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
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 java.util.HashMap;
@Controller
public class OAuthController {
@Value("${spring.oauth.properties.weibo.client_id}")
private String weibo_client_id;
@Value("${spring.oauth.properties.weibo.client_secret}")
private String weibo_client_secret;
@Value("${spring.oauth.properties.grant_type}")
private String grant_type;
@Value("${spring.oauth.properties.weibo.redirect_uri}")
private String weibo_redirect_uri;
@Value("${spring.oauth.properties.gitee.client_id}")
private String gitee_client_id;
@Value("${spring.oauth.properties.gitee.redirect_uri}")
private String gitee_redirect_uri;
@Value("${spring.oauth.properties.gitee.client_secret}")
private String gitee_client_secret;
@GetMapping("/oauth2.0/weibo/success")
public String weiboLogin(@RequestParam("code") String code) throws Exception {
// 根据这个code获取当前登录用户的第三方令牌
HashMap<String, String> queryBody = new HashMap<>();
queryBody.put("client_id",weibo_client_id);
queryBody.put("client_secret",weibo_client_secret);
queryBody.put("grant_type",grant_type);
queryBody.put("redirect_uri",weibo_redirect_uri);
queryBody.put("code",code);
HttpResponse oauth = HttpUtils.doPost("https://api.weibo.com", "/oauth2/access_token", "post", new HashMap<>(), new HashMap<>(), queryBody);
if(oauth.getStatusLine().getStatusCode()==200){
// 获取到了accessEntity
String userJson = EntityUtils.toString(oauth.getEntity());
WeiboSocialUser weiboSocialUser = JSON.parseObject(userJson, WeiboSocialUser.class);
return "redirect:http://alatusmall.com";
}
else{
return "redirect:http://auth.alatusmall.com/login.html";
}
}
@GetMapping("/oauth2.0/gitee/success")
public String gitLogin(@RequestParam("code") String code) throws Exception {
// 根据这个code获取当前登录用户的第三方令牌
HashMap<String, String> queryBody = new HashMap<>();
queryBody.put("grant_type",grant_type);
queryBody.put("code",code);
queryBody.put("client_id",gitee_client_id);
queryBody.put("redirect_uri",gitee_redirect_uri);
queryBody.put("client_secret",gitee_client_secret);
HttpResponse oauth = HttpUtils.doPost("https://gitee.com", "/oauth/token", "post", new HashMap<>(), new HashMap<>(), queryBody);
if(oauth.getStatusLine().getStatusCode()==200){
// 获取到了accessEntity
String userJson = EntityUtils.toString(oauth.getEntity());
GiteeSocialUser giteeSocialUser = JSON.parseObject(userJson, GiteeSocialUser.class);
HttpResponse response = HttpUtils.doGet("https://gitee.com", "/api/v5/user?access_token=" + giteeSocialUser.getAccess_token(), "get", new HashMap<>(), new HashMap<>());
if(response.getStatusLine().getStatusCode()==200){
String giteeJSON = EntityUtils.toString(response.getEntity());
GiteeUserInfo giteeUserInfo = JSON.parseObject(giteeJSON, GiteeUserInfo.class);
giteeSocialUser.setUid(giteeUserInfo.getId().toString());
}
else{
return "redirect:http://auth.alatusmall.com/login.html";
}
return "redirect:http://alatusmall.com";
}
else{
return "redirect:http://auth.alatusmall.com/login.html";
}
}
}
package com.alatus.mall.auth.app;
import com.alatus.common.utils.HttpUtils;
import com.alatus.mall.auth.vo.GiteeSocialUser;
import com.alatus.mall.auth.vo.GiteeUserInfo;
import com.alatus.mall.auth.vo.WeiboSocialUser;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
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 java.util.HashMap;
@Controller
public class OAuthController {
@Value("${spring.oauth.properties.weibo.client_id}")
private String weibo_client_id;
@Value("${spring.oauth.properties.weibo.client_secret}")
private String weibo_client_secret;
@Value("${spring.oauth.properties.grant_type}")
private String grant_type;
@Value("${spring.oauth.properties.weibo.redirect_uri}")
private String weibo_redirect_uri;
@Value("${spring.oauth.properties.gitee.client_id}")
private String gitee_client_id;
@Value("${spring.oauth.properties.gitee.redirect_uri}")
private String gitee_redirect_uri;
@Value("${spring.oauth.properties.gitee.client_secret}")
private String gitee_client_secret;
@GetMapping("/oauth2.0/weibo/success")
public String weiboLogin(@RequestParam("code") String code) throws Exception {
// 根据这个code获取当前登录用户的第三方令牌
HashMap<String, String> queryBody = new HashMap<>();
queryBody.put("client_id",weibo_client_id);
queryBody.put("client_secret",weibo_client_secret);
queryBody.put("grant_type",grant_type);
queryBody.put("redirect_uri",weibo_redirect_uri);
queryBody.put("code",code);
HttpResponse oauth = HttpUtils.doPost("https://api.weibo.com", "/oauth2/access_token", "post", new HashMap<>(), new HashMap<>(), queryBody);
if(oauth.getStatusLine().getStatusCode()==200){
// 获取到了accessEntity
String userJson = EntityUtils.toString(oauth.getEntity());
WeiboSocialUser weiboSocialUser = JSON.parseObject(userJson, WeiboSocialUser.class);
return "redirect:http://alatusmall.com";
}
else{
return "redirect:http://auth.alatusmall.com/login.html";
}
}
@GetMapping("/oauth2.0/gitee/success")
public String gitLogin(@RequestParam("code") String code) throws Exception {
// 根据这个code获取当前登录用户的第三方令牌
HashMap<String, String> queryBody = new HashMap<>();
queryBody.put("grant_type",grant_type);
queryBody.put("code",code);
queryBody.put("client_id",gitee_client_id);
queryBody.put("redirect_uri",gitee_redirect_uri);
queryBody.put("client_secret",gitee_client_secret);
HttpResponse oauth = HttpUtils.doPost("https://gitee.com", "/oauth/token", "post", new HashMap<>(), new HashMap<>(), queryBody);
if(oauth.getStatusLine().getStatusCode()==200){
// 获取到了accessEntity
String userJson = EntityUtils.toString(oauth.getEntity());
GiteeSocialUser giteeSocialUser = JSON.parseObject(userJson, GiteeSocialUser.class);
HttpResponse response = HttpUtils.doGet("https://gitee.com", "/api/v5/user?access_token=" + giteeSocialUser.getAccess_token(), "get", new HashMap<>(), new HashMap<>());
if(response.getStatusLine().getStatusCode()==200){
String giteeJSON = EntityUtils.toString(response.getEntity());
GiteeUserInfo giteeUserInfo = JSON.parseObject(giteeJSON, GiteeUserInfo.class);
giteeSocialUser.setUid(giteeUserInfo.getId().toString());
}
else{
return "redirect:http://auth.alatusmall.com/login.html";
}
return "redirect:http://alatusmall.com";
}
else{
return "redirect:http://auth.alatusmall.com/login.html";
}
}
}
/**
* Copyright 2024 bejson.com
*/
package com.alatus.mall.auth.vo;
import lombok.Data;
/**
* Auto-generated: 2024-09-10 15:50:12
*
* @author bejson.com (i@bejson.com)
* @website http://www.bejson.com/java2pojo/
*/
@Data
public class GiteeSocialUser {
private String access_token;
private String token_type;
private Long expires_in;
private String refresh_token;
private String scope;
private Long created_at;
private String uid;
}
/**
* Copyright 2024 bejson.com
*/
package com.alatus.mall.auth.vo;
import lombok.Data;
/**
* Auto-generated: 2024-09-10 15:50:12
*
* @author bejson.com (i@bejson.com)
* @website http://www.bejson.com/java2pojo/
*/
@Data
public class GiteeSocialUser {
private String access_token;
private String token_type;
private Long expires_in;
private String refresh_token;
private String scope;
private Long created_at;
private String uid;
}
/**
* Copyright 2024
*/
package com.alatus.mall.auth.vo;
import lombok.Data;
/**
* Auto-generated: 2024-09-10 15:47:33
*
* @author bejson.com (i@bejson.com)
* @website http://www.bejson.com/java2pojo/
*/
@Data
public class WeiboSocialUser {
private String access_token;
private String remind_in;
private Long expires_in;
private String uid;
private String isRealName;
}
/**
* Copyright 2024
*/
package com.alatus.mall.auth.vo;
import lombok.Data;
/**
* Auto-generated: 2024-09-10 15:47:33
*
* @author bejson.com (i@bejson.com)
* @website http://www.bejson.com/java2pojo/
*/
@Data
public class WeiboSocialUser {
private String access_token;
private String remind_in;
private Long expires_in;
private String uid;
private String isRealName;
}
/**
* Copyright 2024 bejson.com
*/
package com.alatus.mall.auth.vo;
import lombok.Data;
import java.util.Date;
/**
* Auto-generated: 2024-09-10 17:33:26
*
* @author bejson.com (i@bejson.com)
* @website http://www.bejson.com/java2pojo/
*/
@Data
public class GiteeUserInfo {
private Long id;
private String login;
private String name;
private String avatar_url;
private String url;
private String html_url;
private String remark;
private String followers_url;
private String following_url;
private String gists_url;
private String starred_url;
private String subscriptions_url;
private String organizations_url;
private String repos_url;
private String events_url;
private String received_events_url;
private String type;
private String blog;
private String weibo;
private String bio;
private Integer public_repos;
private Integer public_gists;
private Integer followers;
private Integer following;
private Integer stared;
private Integer watched;
private Date created_at;
private Date updated_at;
private String email;
}
/**
* Copyright 2024 bejson.com
*/
package com.alatus.mall.auth.vo;
import lombok.Data;
import java.util.Date;
/**
* Auto-generated: 2024-09-10 17:33:26
*
* @author bejson.com (i@bejson.com)
* @website http://www.bejson.com/java2pojo/
*/
@Data
public class GiteeUserInfo {
private Long id;
private String login;
private String name;
private String avatar_url;
private String url;
private String html_url;
private String remark;
private String followers_url;
private String following_url;
private String gists_url;
private String starred_url;
private String subscriptions_url;
private String organizations_url;
private String repos_url;
private String events_url;
private String received_events_url;
private String type;
private String blog;
private String weibo;
private String bio;
private Integer public_repos;
private Integer public_gists;
private Integer followers;
private Integer following;
private Integer stared;
private Integer watched;
private Date created_at;
private Date updated_at;
private String email;
}
/**
* Copyright 2024 bejson.com
*/
package com.alatus.mall.member.vo;
import lombok.Data;
/**
* Auto-generated: 2024-09-10 15:50:12
*
* @author bejson.com (i@bejson.com)
* @website http://www.bejson.com/java2pojo/
*/
@Data
public class GiteeSocialUser {
private String access_token;
private String token_type;
private Long expires_in;
private String refresh_token;
private String scope;
private Long created_at;
private String uid;
}
/**
* Copyright 2024 bejson.com
*/
package com.alatus.mall.member.vo;
import lombok.Data;
/**
* Auto-generated: 2024-09-10 15:50:12
*
* @author bejson.com (i@bejson.com)
* @website http://www.bejson.com/java2pojo/
*/
@Data
public class GiteeSocialUser {
private String access_token;
private String token_type;
private Long expires_in;
private String refresh_token;
private String scope;
private Long created_at;
private String uid;
}
/**
* Copyright 2024
*/
package com.alatus.mall.member.vo;
import lombok.Data;
/**
* Auto-generated: 2024-09-10 15:47:33
*
* @author bejson.com (i@bejson.com)
* @website http://www.bejson.com/java2pojo/
*/
@Data
public class WeiboSocialUser {
private String access_token;
private String remind_in;
private Long expires_in;
private String uid;
private String isRealName;
}
/**
* Copyright 2024
*/
package com.alatus.mall.member.vo;
import lombok.Data;
/**
* Auto-generated: 2024-09-10 15:47:33
*
* @author bejson.com (i@bejson.com)
* @website http://www.bejson.com/java2pojo/
*/
@Data
public class WeiboSocialUser {
private String access_token;
private String remind_in;
private Long expires_in;
private String uid;
private String isRealName;
}