diff --git a/src/main/java/com/xkrs/common/account/TokenAuthenticationService.java b/src/main/java/com/xkrs/common/account/TokenAuthenticationService.java index e8c5af3..1675ea4 100644 --- a/src/main/java/com/xkrs/common/account/TokenAuthenticationService.java +++ b/src/main/java/com/xkrs/common/account/TokenAuthenticationService.java @@ -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; diff --git a/src/main/java/com/xkrs/common/config/WebSecurityConfig.java b/src/main/java/com/xkrs/common/config/WebSecurityConfig.java index bb716cc..400bc16 100644 --- a/src/main/java/com/xkrs/common/config/WebSecurityConfig.java +++ b/src/main/java/com/xkrs/common/config/WebSecurityConfig.java @@ -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() diff --git a/src/main/java/com/xkrs/controller/SysUserController.java b/src/main/java/com/xkrs/controller/SysUserController.java index 9029ded..3e1dd8e 100644 --- a/src/main/java/com/xkrs/controller/SysUserController.java +++ b/src/main/java/com/xkrs/controller/SysUserController.java @@ -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); + } + } diff --git a/src/main/java/com/xkrs/controller/WeChatController.java b/src/main/java/com/xkrs/controller/WeChatController.java index 9f31d33..0ddf3a1 100644 --- a/src/main/java/com/xkrs/controller/WeChatController.java +++ b/src/main/java/com/xkrs/controller/WeChatController.java @@ -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" ); diff --git a/src/main/java/com/xkrs/dao/SysUserDao.java b/src/main/java/com/xkrs/dao/SysUserDao.java index 89e6843..8216e4a 100644 --- a/src/main/java/com/xkrs/dao/SysUserDao.java +++ b/src/main/java/com/xkrs/dao/SysUserDao.java @@ -83,7 +83,7 @@ public interface SysUserDao extends JpaRepository { * 查询用户信息 * @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 selectAll(); @@ -92,7 +92,7 @@ public interface SysUserDao extends JpaRepository { * @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 { * @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); diff --git a/src/main/java/com/xkrs/model/entity/SysUserEntity.java b/src/main/java/com/xkrs/model/entity/SysUserEntity.java index dd3a2c4..6f6bb2d 100644 --- a/src/main/java/com/xkrs/model/entity/SysUserEntity.java +++ b/src/main/java/com/xkrs/model/entity/SysUserEntity.java @@ -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 + '\'' + '}'; } } diff --git a/src/main/java/com/xkrs/model/vo/SysUserVo.java b/src/main/java/com/xkrs/model/vo/SysUserVo.java index d677eb3..f58bfa0 100644 --- a/src/main/java/com/xkrs/model/vo/SysUserVo.java +++ b/src/main/java/com/xkrs/model/vo/SysUserVo.java @@ -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 + '}'; } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 347b22a..8c35754 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -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