添加了生成微信二维码登录的接口,并且可以跳转到配置的回调地址,修改了权限配置文件

This commit is contained in:
DESKTOP-G8BCEP0\HP 2021-09-18 16:59:44 +08:00
parent ef228be1ec
commit fd3cfa03f7
8 changed files with 97 additions and 19 deletions

View File

@ -2,6 +2,8 @@ package com.xkrs.common.account;
import com.xkrs.common.encapsulation.OutputEncapsulation;
import com.xkrs.common.encapsulation.PromptMessageEnum;
import com.xkrs.dao.AppletsUserDao;
import com.xkrs.model.entity.AppletsUser;
import com.xkrs.model.vo.SysUserVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
@ -12,7 +14,6 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import javax.crypto.SecretKey;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

View File

@ -52,6 +52,8 @@ class WebSecurityConfig extends WebSecurityConfigurerAdapter {
.antMatchers(HttpMethod.GET,"/callback").permitAll()
.antMatchers(HttpMethod.GET,"/weChatScanCodeLogin").permitAll()
.antMatchers(HttpMethod.GET,"/api/user/findUserAndWeChatUser").permitAll()
.antMatchers(HttpMethod.GET,"/api/user/findUserAndWeChatByUserName").permitAll()
.antMatchers(HttpMethod.GET,"/api/user/verificationCode").permitAll()
// 所有其它请求需要身份认证
.anyRequest().authenticated()
.and()

View File

@ -1,5 +1,7 @@
package com.xkrs.controller;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.xkrs.common.encapsulation.PromptMessageEnum;
import com.xkrs.common.tool.TokenUtil;
import com.xkrs.dao.AppletsUserDao;
@ -10,7 +12,9 @@ import com.xkrs.model.qo.SysUserQo;
import com.xkrs.model.validation.SysUserQoInsert;
import com.xkrs.model.validation.SysUserQoUpdate;
import com.xkrs.model.vo.SysUserVo;
import com.xkrs.service.RedisService;
import com.xkrs.service.SysUserService;
import com.xkrs.utils.RandomUtil;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
@ -24,9 +28,11 @@ import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationErrorList;
import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject;
import static com.xkrs.utils.AliYunSmsUtils.sendSms;
/**
* 系统用户Controller
@ -45,6 +51,9 @@ public class SysUserController {
@Resource
private AppletsUserDao appletsUserDao;
@Resource
private RedisService redisService;
/**
* 登录用户Token验证
* @return
@ -269,6 +278,7 @@ public class SysUserController {
Locale locale = LocaleContextHolder.getLocale();
String openId = (String) map.get("openId");
String phone = (String) map.get("phone");
String code = (String) map.get("code");
SysUserEntity byUserName = sysUserDao.findByUserName(phone);
if(byUserName == null){
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE,"输入错误或未注册,请检查注册!",locale);
@ -277,6 +287,9 @@ public class SysUserController {
if(byOpenId != null){
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL,"您已绑定网站账号,请勿重复绑定!",locale);
}
if(!redisService.get(phone).equals(code)){
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG,"验证码错误,请重新输入!",locale);
}
sysUserDao.updateOpenIdByPhone(phone,openId);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS,"绑定成功!",locale);
}
@ -305,4 +318,40 @@ public class SysUserController {
return outputEncapsulationObject(PromptMessageEnum.SUCCESS,map,locale);
}
/**
* 根据用户名查询用户信息
* @param userName
* @return
*/
@GetMapping("/findUserAndWeChatByUserName")
public String findUserAndWeChatByUserName(@RequestParam("userName") String userName){
Locale locale = LocaleContextHolder.getLocale();
Map map = new HashMap(3);
SysUserVo sysUserVo = sysUserDao.selectUserByUserName(userName);
map.put("user",sysUserVo);
if(sysUserVo.getOpenId() == null || "".equals(sysUserVo.getOpenId())){
map.put("weChat",null);
}else {
AppletsUser byOpenId = appletsUserDao.findByOpenId(sysUserVo.getOpenId());
map.put("weChat",byOpenId);
}
return outputEncapsulationObject(PromptMessageEnum.SUCCESS,map,locale);
}
/**
* 发送手机号
* @param phoneNumber
* @return
* @throws ClientException
*/
@GetMapping("/verificationCode")
public String verificationCode(@RequestParam("phoneNumber") String phoneNumber) throws ClientException {
// 获取区域信息
Locale locale = LocaleContextHolder.getLocale();
String optCode = String.valueOf(RandomUtil.returnCode());
redisService.set(phoneNumber,optCode,10, TimeUnit.MINUTES);
SendSmsResponse response =sendSms(phoneNumber,optCode);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS,"",locale);
}
}

View File

@ -92,7 +92,7 @@ public class WeChatController {
//使用jwt根据member对象生成token字符串
//String jwtToken = JwtUtils.getJwtToken(member.getId(), member.getNickname());
return outputEncapsulationObject(PromptMessageEnum.SUCCESS,"操作成功!",locale);
return "redirect:http://localhost:3000?openId="+openid;
}catch(Exception e){
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL,"操作失败!",locale);
}
@ -127,7 +127,6 @@ public class WeChatController {
String url =String.format(
baseUrl,
ConstantConfig.WX_OPEN_APP_ID,
ConstantConfig.WX_OPEN_APP_SECRET,
redirect_url,
"atguigu"
);

View File

@ -83,7 +83,7 @@ public interface SysUserDao extends JpaRepository<SysUserEntity,Integer> {
* 查询用户信息
* @return
*/
@Query(value = "select new com.xkrs.model.vo.SysUserVo (id,reallyName,telephone,userDepartment,activeFlag,addTime) " +
@Query(value = "select new com.xkrs.model.vo.SysUserVo (id,reallyName,telephone,userDepartment,activeFlag,openId,addTime) " +
"from SysUserEntity")
List<SysUserVo> selectAll();
@ -92,7 +92,7 @@ public interface SysUserDao extends JpaRepository<SysUserEntity,Integer> {
* @param userName
* @return
*/
@Query(value = "select new com.xkrs.model.vo.SysUserVo (id,reallyName,telephone,userDepartment,activeFlag,addTime) " +
@Query(value = "select new com.xkrs.model.vo.SysUserVo (id,reallyName,telephone,userDepartment,activeFlag,openId,addTime) " +
"from SysUserEntity where userName = :userName")
SysUserVo selectUserByUserName(String userName);
@ -101,7 +101,7 @@ public interface SysUserDao extends JpaRepository<SysUserEntity,Integer> {
* @param openId
* @return
*/
@Query(value = "select new com.xkrs.model.vo.SysUserVo (id,reallyName,telephone,userDepartment,activeFlag,addTime) " +
@Query(value = "select new com.xkrs.model.vo.SysUserVo (id,reallyName,telephone,userDepartment,activeFlag,openId,addTime) " +
"from SysUserEntity where openId = :openId")
SysUserVo selectUserByOpenId(String openId);

View File

@ -74,6 +74,12 @@ public class SysUserEntity implements Serializable {
@Column(columnDefinition = "varchar(88)")
private String openId;
/**
* 微信扫码登录的openId
*/
@Column(columnDefinition = "varchar(88)")
private String weChatOpenId;
public Integer getId() {
return id;
}
@ -194,6 +200,14 @@ public class SysUserEntity implements Serializable {
this.openId = openId;
}
public String getWeChatOpenId() {
return weChatOpenId;
}
public void setWeChatOpenId(String weChatOpenId) {
this.weChatOpenId = weChatOpenId;
}
@Override
public String toString() {
return "SysUserEntity{" +
@ -212,6 +226,7 @@ public class SysUserEntity implements Serializable {
", deleteFlag=" + deleteFlag +
", lastEntryIp='" + lastEntryIp + '\'' +
", openId='" + openId + '\'' +
", weChatOpenId='" + weChatOpenId + '\'' +
'}';
}
}

View File

@ -18,14 +18,17 @@ public class SysUserVo implements Serializable {
private Integer activeFlag;
private String openId;
private LocalDateTime addTime;
public SysUserVo(Integer id, String reallyName, String telephone, String userDepartment, Integer activeFlag, LocalDateTime addTime) {
public SysUserVo(Integer id, String reallyName, String telephone, String userDepartment, Integer activeFlag, String openId, LocalDateTime addTime) {
this.id = id;
this.reallyName = reallyName;
this.telephone = telephone;
this.userDepartment = userDepartment;
this.activeFlag = activeFlag;
this.openId = openId;
this.addTime = addTime;
}
@ -69,6 +72,14 @@ public class SysUserVo implements Serializable {
this.activeFlag = activeFlag;
}
public String getOpenId() {
return openId;
}
public void setOpenId(String openId) {
this.openId = openId;
}
public LocalDateTime getAddTime() {
return addTime;
}
@ -85,6 +96,7 @@ public class SysUserVo implements Serializable {
", telephone='" + telephone + '\'' +
", userDepartment='" + userDepartment + '\'' +
", activeFlag=" + activeFlag +
", openId='" + openId + '\'' +
", addTime=" + addTime +
'}';
}

View File

@ -29,7 +29,7 @@ spring.datasource.hikari.validation-timeout = 3000
## Spring Data JPA 配置
spring.jpa.database = POSTGRESQL
spring.jpa.database-platform = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql = true
spring.jpa.show-sql = false
# 指定 ddl mode (none, validate, create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# 命名策略
@ -39,17 +39,17 @@ spring.jpa.properties.hibernate.dialect = org.hibernate.spatial.dialect.postgis.
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
## Redis配置
#spring.cache.type = redis
#spring.redis.database = 9
#spring.redis.host = localhost
#spring.redis.port = 6379
#spring.redis.password = sdust2020
#spring.redis.timeout = 10000
#spring.redis.lettuce.pool.max-active = 100
#spring.redis.lettuce.pool.max-wait = 10000
#spring.redis.lettuce.pool.max-idle = 100
#spring.redis.lettuce.pool.min-idle = 1
#spring.redis.lettuce.shutdown-timeout = 0
spring.cache.type = redis
spring.redis.database = 9
spring.redis.host = localhost
spring.redis.port = 6379
spring.redis.password = sdust2020
spring.redis.timeout = 10000
spring.redis.lettuce.pool.max-active = 100
spring.redis.lettuce.pool.max-wait = 10000
spring.redis.lettuce.pool.max-idle = 100
spring.redis.lettuce.pool.min-idle = 1
spring.redis.lettuce.shutdown-timeout = 0
## Devtools配置
spring.devtools.livereload.enabled = true