package com.alatus.mall.member.service.impl; import com.alatus.common.utils.HttpUtils; import com.alatus.common.vo.GiteeUserInfo; import com.alatus.mall.member.entity.MemberLevelEntity; import com.alatus.mall.member.entity.OauthInfoEntity; 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.service.OauthInfoService; import com.alatus.mall.member.vo.MemberLoginVo; import com.alatus.mall.member.vo.MemberRegisterVo; import com.alatus.common.vo.GiteeSocialUser; import com.alatus.common.vo.WeiboSocialUser; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.apache.http.HttpResponse; import org.apache.http.util.EntityUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; import java.util.Date; import java.util.HashMap; 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; @Autowired private OauthInfoService oauthInfoService; @Autowired private StringRedisTemplate redisTemplate; @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(); // 设置默认等级 memberEntity.setLevelId(memberLevelService.getDefaultLevel().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()); memberEntity.setCreateTime(new Date()); 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(); OauthInfoEntity oauthInfoEntity = oauthInfoService.getOne(new QueryWrapper<OauthInfoEntity>().eq("source", "微博").eq("social_uid", uid)); if(oauthInfoEntity != null){ MemberEntity memberEntity = this.getById(oauthInfoEntity.getMemberId()); redisTemplate.opsForValue().set("user_access_token:"+memberEntity.getId(),weiboSocialUser.getAccess_token()); redisTemplate.opsForValue().set("user_expires_in:"+memberEntity.getId(), weiboSocialUser.getExpires_in().toString()); return memberEntity; } else{ MemberEntity register = new MemberEntity(); try{ HashMap<String, String> query = new HashMap<>(); query.put("access_token", weiboSocialUser.getAccess_token()); query.put("uid", weiboSocialUser.getUid()); HttpResponse response = HttpUtils.doGet("https://api.weibo.com", "/2/users/show.json", "get", new HashMap<>(),query); if(response.getStatusLine().getStatusCode()==200){ String json = EntityUtils.toString(response.getEntity()); JSONObject jsonObject = JSON.parseObject(json); register.setNickname(jsonObject.getString("name")); register.setGender("m".equals(jsonObject.getString("gender"))?1:0); register.setCreateTime(new Date()); this.save(register); OauthInfoEntity registerOauthInfo = new OauthInfoEntity(); registerOauthInfo.setMemberId(register.getId()); registerOauthInfo.setSocialUid(uid.toString()); registerOauthInfo.setSource("微博"); oauthInfoService.save(registerOauthInfo); redisTemplate.opsForValue().set("user_access_token:"+register.getId(),weiboSocialUser.getAccess_token()); redisTemplate.opsForValue().set("user_expires_in:"+register.getId(), weiboSocialUser.getExpires_in().toString()); } } catch (Exception e){ e.printStackTrace(); } return register; } } @Override public MemberEntity login(GiteeSocialUser giteeSocialUser) throws Exception { GiteeUserInfo giteeUserInfo = null; 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 = JSON.parseObject(giteeJSON, GiteeUserInfo.class); } Long uid = giteeUserInfo.getId(); OauthInfoEntity oauthInfoEntity = oauthInfoService.getOne(new QueryWrapper<OauthInfoEntity>().eq("source", "gitee").eq("social_uid", uid)); if(oauthInfoEntity != null){ MemberEntity memberEntity = this.getById(oauthInfoEntity.getMemberId()); redisTemplate.opsForValue().set("user_access_token:"+memberEntity.getId(),giteeSocialUser.getAccess_token()); redisTemplate.opsForValue().set("user_expires_in:"+memberEntity.getId(), giteeSocialUser.getExpires_in().toString()); return memberEntity; } else{ MemberEntity register = new MemberEntity(); register.setLevelId(memberLevelService.getDefaultLevel().getId()); register.setEmail(giteeUserInfo.getEmail()); register.setNickname(giteeUserInfo.getName()); register.setCreateTime(new Date()); this.save(register); OauthInfoEntity registerOauthInfo = new OauthInfoEntity(); registerOauthInfo.setMemberId(register.getId()); registerOauthInfo.setSocialUid(uid.toString()); registerOauthInfo.setSource("gitee"); oauthInfoService.save(registerOauthInfo); redisTemplate.opsForValue().set("user_access_token:"+register.getId(),giteeSocialUser.getAccess_token()); redisTemplate.opsForValue().set("user_expires_in:"+register.getId(), giteeSocialUser.getExpires_in().toString()); return register; } } }
package com.alatus.mall.member.service.impl; import com.alatus.common.utils.HttpUtils; import com.alatus.common.vo.GiteeUserInfo; import com.alatus.mall.member.entity.MemberLevelEntity; import com.alatus.mall.member.entity.OauthInfoEntity; 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.service.OauthInfoService; import com.alatus.mall.member.vo.MemberLoginVo; import com.alatus.mall.member.vo.MemberRegisterVo; import com.alatus.common.vo.GiteeSocialUser; import com.alatus.common.vo.WeiboSocialUser; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.apache.http.HttpResponse; import org.apache.http.util.EntityUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; import java.util.Date; import java.util.HashMap; 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; @Autowired private OauthInfoService oauthInfoService; @Autowired private StringRedisTemplate redisTemplate; @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(); // 设置默认等级 memberEntity.setLevelId(memberLevelService.getDefaultLevel().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()); memberEntity.setCreateTime(new Date()); 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(); OauthInfoEntity oauthInfoEntity = oauthInfoService.getOne(new QueryWrapper<OauthInfoEntity>().eq("source", "微博").eq("social_uid", uid)); if(oauthInfoEntity != null){ MemberEntity memberEntity = this.getById(oauthInfoEntity.getMemberId()); redisTemplate.opsForValue().set("user_access_token:"+memberEntity.getId(),weiboSocialUser.getAccess_token()); redisTemplate.opsForValue().set("user_expires_in:"+memberEntity.getId(), weiboSocialUser.getExpires_in().toString()); return memberEntity; } else{ MemberEntity register = new MemberEntity(); try{ HashMap<String, String> query = new HashMap<>(); query.put("access_token", weiboSocialUser.getAccess_token()); query.put("uid", weiboSocialUser.getUid()); HttpResponse response = HttpUtils.doGet("https://api.weibo.com", "/2/users/show.json", "get", new HashMap<>(),query); if(response.getStatusLine().getStatusCode()==200){ String json = EntityUtils.toString(response.getEntity()); JSONObject jsonObject = JSON.parseObject(json); register.setNickname(jsonObject.getString("name")); register.setGender("m".equals(jsonObject.getString("gender"))?1:0); register.setCreateTime(new Date()); this.save(register); OauthInfoEntity registerOauthInfo = new OauthInfoEntity(); registerOauthInfo.setMemberId(register.getId()); registerOauthInfo.setSocialUid(uid.toString()); registerOauthInfo.setSource("微博"); oauthInfoService.save(registerOauthInfo); redisTemplate.opsForValue().set("user_access_token:"+register.getId(),weiboSocialUser.getAccess_token()); redisTemplate.opsForValue().set("user_expires_in:"+register.getId(), weiboSocialUser.getExpires_in().toString()); } } catch (Exception e){ e.printStackTrace(); } return register; } } @Override public MemberEntity login(GiteeSocialUser giteeSocialUser) throws Exception { GiteeUserInfo giteeUserInfo = null; 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 = JSON.parseObject(giteeJSON, GiteeUserInfo.class); } Long uid = giteeUserInfo.getId(); OauthInfoEntity oauthInfoEntity = oauthInfoService.getOne(new QueryWrapper<OauthInfoEntity>().eq("source", "gitee").eq("social_uid", uid)); if(oauthInfoEntity != null){ MemberEntity memberEntity = this.getById(oauthInfoEntity.getMemberId()); redisTemplate.opsForValue().set("user_access_token:"+memberEntity.getId(),giteeSocialUser.getAccess_token()); redisTemplate.opsForValue().set("user_expires_in:"+memberEntity.getId(), giteeSocialUser.getExpires_in().toString()); return memberEntity; } else{ MemberEntity register = new MemberEntity(); register.setLevelId(memberLevelService.getDefaultLevel().getId()); register.setEmail(giteeUserInfo.getEmail()); register.setNickname(giteeUserInfo.getName()); register.setCreateTime(new Date()); this.save(register); OauthInfoEntity registerOauthInfo = new OauthInfoEntity(); registerOauthInfo.setMemberId(register.getId()); registerOauthInfo.setSocialUid(uid.toString()); registerOauthInfo.setSource("gitee"); oauthInfoService.save(registerOauthInfo); redisTemplate.opsForValue().set("user_access_token:"+register.getId(),giteeSocialUser.getAccess_token()); redisTemplate.opsForValue().set("user_expires_in:"+register.getId(), giteeSocialUser.getExpires_in().toString()); return register; } } }
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.MemberLoginVo; import com.alatus.mall.member.vo.MemberRegisterVo; import com.baomidou.mybatisplus.extension.service.IService; import com.alatus.common.utils.PageUtils; import com.alatus.mall.member.entity.MemberEntity; import com.alatus.common.vo.GiteeSocialUser; import com.alatus.common.vo.WeiboSocialUser; 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) throws Exception; }
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.MemberLoginVo; import com.alatus.mall.member.vo.MemberRegisterVo; import com.baomidou.mybatisplus.extension.service.IService; import com.alatus.common.utils.PageUtils; import com.alatus.mall.member.entity.MemberEntity; import com.alatus.common.vo.GiteeSocialUser; import com.alatus.common.vo.WeiboSocialUser; 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) throws Exception; }
package com.alatus.mall.auth.app; import com.alatus.common.utils.HttpUtils; import com.alatus.common.utils.R; import com.alatus.common.vo.GiteeSocialUser; import com.alatus.common.vo.WeiboSocialUser; import com.alatus.mall.auth.feign.MemberFeignService; import com.alibaba.fastjson.JSON; import org.apache.http.HttpResponse; import org.apache.http.util.EntityUtils; 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 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; @Autowired private MemberFeignService memberFeignService; @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); R r = memberFeignService.weiboOauthLogin(weiboSocialUser); System.out.println(r); 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); R r = memberFeignService.giteeOauthLogin(giteeSocialUser); System.out.println(r); 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.common.utils.R; import com.alatus.common.vo.GiteeSocialUser; import com.alatus.common.vo.WeiboSocialUser; import com.alatus.mall.auth.feign.MemberFeignService; import com.alibaba.fastjson.JSON; import org.apache.http.HttpResponse; import org.apache.http.util.EntityUtils; 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 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; @Autowired private MemberFeignService memberFeignService; @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); R r = memberFeignService.weiboOauthLogin(weiboSocialUser); System.out.println(r); 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); R r = memberFeignService.giteeOauthLogin(giteeSocialUser); System.out.println(r); return "redirect:http://alatusmall.com"; } else{ return "redirect:http://auth.alatusmall.com/login.html"; } } }
package com.alatus.mall.member.app; import java.util.Arrays; import java.util.Map; import com.alatus.common.exception.BizCodeEnum; import com.alatus.common.vo.GiteeSocialUser; import com.alatus.common.vo.WeiboSocialUser; import com.alatus.mall.member.exception.PhoneExistException; import com.alatus.mall.member.exception.UsernameExistException; import com.alatus.mall.member.vo.MemberLoginVo; import com.alatus.mall.member.vo.MemberRegisterVo; 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) throws Exception { 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.common.vo.GiteeSocialUser; import com.alatus.common.vo.WeiboSocialUser; import com.alatus.mall.member.exception.PhoneExistException; import com.alatus.mall.member.exception.UsernameExistException; import com.alatus.mall.member.vo.MemberLoginVo; import com.alatus.mall.member.vo.MemberRegisterVo; 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) throws Exception { 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.auth.feign; import com.alatus.common.utils.R; import com.alatus.common.vo.GiteeSocialUser; import com.alatus.common.vo.WeiboSocialUser; import com.alatus.mall.auth.vo.UserLoginVo; import com.alatus.mall.auth.vo.UserRegisterVo; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @FeignClient(name = "Mall-member") public interface MemberFeignService { @PostMapping("/member/member/register") R register(@RequestBody UserRegisterVo memberRegisterVo); @GetMapping("/member/member/checkPhone/{phone}") R checkPhone(@PathVariable("phone")String phone); @PostMapping("/member/member/login") R login(@RequestBody UserLoginVo userLoginVo); @PostMapping("/member/member/oauth2/weibo/login") R weiboOauthLogin(@RequestBody WeiboSocialUser weiboSocialUser); @PostMapping("/member/member/oauth2/gitee/login") R giteeOauthLogin(@RequestBody GiteeSocialUser giteeSocialUser) throws Exception; }
package com.alatus.mall.auth.feign; import com.alatus.common.utils.R; import com.alatus.common.vo.GiteeSocialUser; import com.alatus.common.vo.WeiboSocialUser; import com.alatus.mall.auth.vo.UserLoginVo; import com.alatus.mall.auth.vo.UserRegisterVo; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @FeignClient(name = "Mall-member") public interface MemberFeignService { @PostMapping("/member/member/register") R register(@RequestBody UserRegisterVo memberRegisterVo); @GetMapping("/member/member/checkPhone/{phone}") R checkPhone(@PathVariable("phone")String phone); @PostMapping("/member/member/login") R login(@RequestBody UserLoginVo userLoginVo); @PostMapping("/member/member/oauth2/weibo/login") R weiboOauthLogin(@RequestBody WeiboSocialUser weiboSocialUser); @PostMapping("/member/member/oauth2/gitee/login") R giteeOauthLogin(@RequestBody GiteeSocialUser giteeSocialUser) throws Exception; }