Browse Source

loginbywxcode微信小程序登录接口

dev
yinzuomei 6 years ago
parent
commit
92c6dd80d6
  1. 52
      epmet-auth/pom.xml
  2. 51
      epmet-auth/src/main/java/com/epmet/controller/LoginController.java
  3. 41
      epmet-auth/src/main/java/com/epmet/dao/CustomerUserDao.java
  4. 91
      epmet-auth/src/main/java/com/epmet/entity/CustomerUserEntity.java
  5. 41
      epmet-auth/src/main/java/com/epmet/jwt/JwtTokenProperties.java
  6. 105
      epmet-auth/src/main/java/com/epmet/jwt/JwtTokenUtils.java
  7. 31
      epmet-auth/src/main/java/com/epmet/service/LoginService.java
  8. 193
      epmet-auth/src/main/java/com/epmet/service/impl/LoginServiceImpl.java
  9. 32
      epmet-auth/src/main/resources/bootstrap.yml
  10. 15
      epmet-auth/src/main/resources/mapper/CustomerUserDao.xml
  11. 33
      epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/constant/LoginConstant.java
  12. 32
      epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/dto/DeptDataScopeDTO.java
  13. 37
      epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/dto/TokenDto.java
  14. 48
      epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/dto/WorkUserAuthorizationDTO.java
  15. 29
      epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/dto/WorkUserLoginDTO.java
  16. 25
      epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/dto/WorkUserTokenFormDTO.java
  17. 29
      epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/dto/form/LoginByPassWordFormDTO.java
  18. 34
      epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/dto/form/LoginByWxCodeFormDTO.java
  19. 28
      epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/dto/form/LoginCommonFormDTO.java
  20. 20
      epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/dto/result/UserTokenResultDTO.java
  21. 20
      epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/util/CpUserDetailRedis.java
  22. 18
      epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/util/TokenUtil.java
  23. 2
      epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/util/UserUtil.java
  24. 10
      epmet-commons/epmet-commons-tools-wx-ma/src/main/java/com/epmet/utils/WxMaServiceUtils.java
  25. 4
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/constant/Constant.java
  26. 18
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java

52
epmet-auth/pom.xml

@ -57,6 +57,29 @@
<artifactId>kaptcha</artifactId>
<version>${kaptcha.version}</version>
</dependency>
<dependency>
<groupId>com.epmet</groupId>
<artifactId>epmet-common-clienttoken</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
<version>3.6.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.epmet</groupId>
<artifactId>epmet-commons-tools-wx-ma</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.epmet</groupId>
<artifactId>epmet-commons-mybatis</artifactId>
<version>2.0.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
@ -92,6 +115,12 @@
<server.port>8081</server.port>
<spring.profiles.active>dev</spring.profiles.active>
<!-- 数据库配置-->
<spring.datasource.druid.url>
<![CDATA[jdbc:mysql://47.104.224.45:3308/epmet_common_user?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai]]>
</spring.datasource.druid.url>
<spring.datasource.druid.username>epmet</spring.datasource.druid.username>
<spring.datasource.druid.password>elink@833066</spring.datasource.druid.password>
<!-- redis配置 -->
<spring.redis.index>0</spring.redis.index>
<spring.redis.host>122.152.200.70</spring.redis.host>
@ -107,13 +136,13 @@
<nacos.ip/>
<!--公众号配置-->
<wx.mp.configs.appId>wx3ef8f2cd12a19fcb</wx.mp.configs.appId>
<wx.mp.configs.secret>948aa2f21dbaa3943288ea5b119ac6f2</wx.mp.configs.secret>
<wx.mp.configs.appId>wx67fdf7da3fee1890</wx.mp.configs.appId>
<wx.mp.configs.secret>ae15094f485af9e5c6b5a8a55945332a</wx.mp.configs.secret>
<wx.mp.configs.token>111</wx.mp.configs.token>
<wx.mp.configs.aesKey>111</wx.mp.configs.aesKey>
<!--居民端小程序配置-->
<resi.wx.ma.appId>wxdd8530c5f4926766</resi.wx.ma.appId>
<resi.wx.ma.secret>5bf4fb813145431b3493a10aa7e041e9</resi.wx.ma.secret>
<resi.wx.ma.appId>wx7c95937fa16a58c4</resi.wx.ma.appId>
<resi.wx.ma.secret>5782d372d99820fb4859d6be8e5230b0</resi.wx.ma.secret>
</properties>
</profile>
@ -126,6 +155,12 @@
<server.port>8081</server.port>
<spring.profiles.active>test</spring.profiles.active>
<!-- 数据库配置-->
<spring.datasource.druid.url>
<![CDATA[jdbc:mysql://47.104.224.45:3308/epmet_common_user?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai]]>
</spring.datasource.druid.url>
<spring.datasource.druid.username>epmet</spring.datasource.druid.username>
<spring.datasource.druid.password>elink@833066</spring.datasource.druid.password>
<!-- redis配置 -->
<spring.redis.index>0</spring.redis.index>
<spring.redis.host>122.152.200.70</spring.redis.host>
@ -139,6 +174,15 @@
<nacos.config.group></nacos.config.group>
<nacos.config-enabled>false</nacos.config-enabled>
<nacos.ip/>
<!--公众号配置-->
<wx.mp.configs.appId>wx67fdf7da3fee1890</wx.mp.configs.appId>
<wx.mp.configs.secret>ae15094f485af9e5c6b5a8a55945332a</wx.mp.configs.secret>
<wx.mp.configs.token>111</wx.mp.configs.token>
<wx.mp.configs.aesKey>111</wx.mp.configs.aesKey>
<!--居民端小程序配置-->
<resi.wx.ma.appId>wx7c95937fa16a58c4</resi.wx.ma.appId>
<resi.wx.ma.secret>5782d372d99820fb4859d6be8e5230b0</resi.wx.ma.secret>
</properties>
</profile>
</profiles>

51
epmet-auth/src/main/java/com/epmet/controller/LoginController.java

@ -0,0 +1,51 @@
package com.epmet.controller;
import com.epmet.common.token.annotation.Login;
import com.epmet.common.token.dto.form.LoginByPassWordFormDTO;
import com.epmet.common.token.dto.form.LoginByWxCodeFormDTO;
import com.epmet.common.token.dto.result.UserTokenResultDTO;
import com.epmet.commons.tools.utils.Result;
import com.epmet.commons.tools.validator.ValidatorUtils;
import com.epmet.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* @Description 通用登陆接口
* @Author yinzuomei
* @Date 2020/3/14 13:58
*/
@RestController
@RequestMapping("login")
public class LoginController {
@Autowired
private LoginService loginService;
/**
* @param formDTO
* @return com.epmet.commons.tools.utils.Result<java.lang.String>
* @Author yinzuomei
* @Description 微信小程序登录
* @Date 2020/3/14 14:35
**/
@PostMapping("loginbywxcode")
public Result<UserTokenResultDTO> loginByWxCode(@RequestBody LoginByWxCodeFormDTO formDTO) {
//效验数据
ValidatorUtils.validateEntity(formDTO);
return loginService.loginByWxCode(formDTO);
}
/**
* @param formDTO
* @return com.epmet.commons.tools.utils.Result<com.epmet.dto.UserTokenResultDTO>
* @Author yinzuomei
* @Description 手机号+密码登录接口
* @Date 2020/3/14 19:46
**/
@PostMapping("loginbypassword")
public Result<UserTokenResultDTO> loginByPassword(@RequestBody LoginByPassWordFormDTO formDTO) {
//效验数据
ValidatorUtils.validateEntity(formDTO);
return loginService.loginByPassword(formDTO);
}
}

41
epmet-auth/src/main/java/com/epmet/dao/CustomerUserDao.java

@ -0,0 +1,41 @@
/**
* Copyright 2018 人人开源 https://www.renren.io
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.epmet.dao;
import com.epmet.commons.mybatis.dao.BaseDao;
import com.epmet.entity.CustomerUserEntity;
import org.apache.ibatis.annotations.Mapper;
/**
* 客户用户信息表
*
* @author generator generator@elink-cn.com
* @since v1.0.0 2020-03-14
*/
@Mapper
public interface CustomerUserDao extends BaseDao<CustomerUserEntity> {
/**
* @param openId
* @return userId
* @Author yinzuomei
* @Description 根据openId查询居民用户信息
* @Date 2020/3/14 20:45
**/
String selectByWxOpenId(String openId);
}

91
epmet-auth/src/main/java/com/epmet/entity/CustomerUserEntity.java

@ -0,0 +1,91 @@
/**
* Copyright 2018 人人开源 https://www.renren.io
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.epmet.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import com.epmet.commons.mybatis.entity.BaseEpmetEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.Date;
/**
* 客户用户信息表
*
* @author generator generator@elink-cn.com
* @since v1.0.0 2020-03-14
*/
@Data
@EqualsAndHashCode(callSuper=false)
@TableName("customer_user")
public class CustomerUserEntity extends BaseEpmetEntity {
private static final long serialVersionUID = 1L;
/**
* 客户Id CUSTOMER.id
*/
private String customerId;
/**
* 微信openId
*/
private String wxOpenId;
/**
* 手机号
*/
private String mobile;
/**
* 昵称
*/
private String nickname;
/**
* 性别:0. 1. 字典表(key:sex)
*/
private Integer sex;
/**
* 头像
*/
private String headImgUrl;
/**
* 国家
*/
private String country;
/**
* 省份
*/
private String province;
/**
* 城市
*/
private String city;
/**
* 语言
*/
private String language;
}

41
epmet-auth/src/main/java/com/epmet/jwt/JwtTokenProperties.java

@ -0,0 +1,41 @@
/**
* Copyright (c) 2018 人人开源 All rights reserved.
*
* https://www.renren.io
*
* 版权所有侵权必究
*/
package com.epmet.jwt;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* Jwt
*
* @author Mark sunlightcs@gmail.com
* @since 1.0.0
*/
@Configuration
@ConfigurationProperties(prefix = "jwt.token")
public class JwtTokenProperties {
private String secret;
private int expire;
public String getSecret() {
return secret;
}
public void setSecret(String secret) {
this.secret = secret;
}
public int getExpire() {
return expire;
}
public void setExpire(int expire) {
this.expire = expire;
}
}

105
epmet-auth/src/main/java/com/epmet/jwt/JwtTokenUtils.java

@ -0,0 +1,105 @@
/**
* Copyright (c) 2018 人人开源 All rights reserved.
* <p>
* https://www.renren.io
* <p>
* 版权所有侵权必究
*/
package com.epmet.jwt;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.commons.codec.binary.Base64;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* Jwt工具类
*
* @author Mark sunlightcs@gmail.com
* @since 1.0.0
*/
@Component
public class JwtTokenUtils {
private static final Logger logger = LoggerFactory.getLogger(JwtTokenUtils.class);
@Autowired
private JwtTokenProperties jwtProperties;
/**
* 生成jwt token 弃用
*/
@Deprecated
public String generateToken(String userId) {
return Jwts.builder()
.setHeaderParam("typ", "JWT")
.setSubject(userId)
.setIssuedAt(new Date())
.setExpiration(DateTime.now().plusSeconds(jwtProperties.getExpire()).toDate())
.signWith(SignatureAlgorithm.HS512, jwtProperties.getSecret())
.compact();
}
public Claims getClaimByToken(String token) {
try {
return Jwts.parser()
.setSigningKey(jwtProperties.getSecret())
.parseClaimsJws(token)
.getBody();
} catch (Exception e) {
logger.debug("validate is token error, token = " + token, e);
return null;
}
}
public String createToken(Map<String, Object> map) {
return Jwts.builder()
.setHeaderParam("typ", "JWT")
.setClaims(map)
.setIssuedAt(new Date())
.setExpiration(DateTime.now().plusSeconds(jwtProperties.getExpire()).toDate())
.signWith(SignatureAlgorithm.HS512, jwtProperties.getSecret())
.compact();
}
/**
* token是否过期
*
* @return true过期
*/
public boolean isTokenExpired(Date expiration) {
return expiration.before(new Date());
}
public static void main(String[] args) {
Map<String, Object> map=new HashMap<>();
map.put("app","gov");
map.put("client","wxmp");
map.put("userId","100526ABC");
String tokenStr=Jwts.builder()
.setHeaderParam("typ", "JWT")
.setClaims(map)
.setIssuedAt(new Date())
.setExpiration(DateTime.now().plusSeconds(604800).toDate())
.signWith(SignatureAlgorithm.HS512, "7016867071f0ebf1c46f123eaaf4b9d6[elink.epmet]")
.compact();
System.out.println(tokenStr);
Claims claims= Jwts.parser()
.setSigningKey("7016867071f0ebf1c46f123eaaf4b9d6[elink.epmet]")
.parseClaimsJws(tokenStr)
.getBody();
System.out.println("app="+ claims.get("app"));
System.out.println("client="+ claims.get("client"));
System.out.println("userId="+ claims.get("userId"));
}
}

31
epmet-auth/src/main/java/com/epmet/service/LoginService.java

@ -0,0 +1,31 @@
package com.epmet.service;
import com.epmet.common.token.dto.form.LoginByPassWordFormDTO;
import com.epmet.common.token.dto.form.LoginByWxCodeFormDTO;
import com.epmet.common.token.dto.result.UserTokenResultDTO;
import com.epmet.commons.tools.utils.Result;
/**
* @Description
* @Author yinzuomei
* @Date 2020/3/14 20:21
*/
public interface LoginService {
/**
* @param formDTO
* @return com.epmet.commons.tools.utils.Result<com.epmet.dto.UserTokenResultDTO>
* @Author yinzuomei
* @Description 微信小程序登录
* @Date 2020/3/14 19:34
**/
Result<UserTokenResultDTO> loginByWxCode(LoginByWxCodeFormDTO formDTO);
/**
* @param formDTO
* @return com.epmet.commons.tools.utils.Result<com.epmet.dto.UserTokenResultDTO>
* @Author yinzuomei
* @Description 手机号+密码登录接口
* @Date 2020/3/14 19:54
**/
Result<UserTokenResultDTO> loginByPassword(LoginByPassWordFormDTO formDTO);
}

193
epmet-auth/src/main/java/com/epmet/service/impl/LoginServiceImpl.java

@ -0,0 +1,193 @@
package com.epmet.service.impl;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.epmet.common.token.constant.LoginConstant;
import com.epmet.common.token.dto.TokenDto;
import com.epmet.common.token.dto.form.LoginByPassWordFormDTO;
import com.epmet.common.token.dto.form.LoginByWxCodeFormDTO;
import com.epmet.common.token.dto.result.UserTokenResultDTO;
import com.epmet.common.token.util.CpUserDetailRedis;
import com.epmet.commons.tools.exception.RenException;
import com.epmet.commons.tools.utils.Result;
import com.epmet.dao.CustomerUserDao;
import com.epmet.entity.CustomerUserEntity;
import com.epmet.jwt.JwtTokenProperties;
import com.epmet.jwt.JwtTokenUtils;
import com.epmet.service.LoginService;
import com.epmet.utils.WxMaServiceUtils;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
* @Description
* @Author yinzuomei
* @Date 2020/3/14 20:31
*/
@Slf4j
@Service
public class LoginServiceImpl implements LoginService {
private static final Logger logger = LoggerFactory.getLogger(AuthServiceImpl.class);
@Autowired
private CustomerUserDao customerUserDao;
@Autowired
private WxMaServiceUtils wxMaServiceUtils;
@Autowired
private JwtTokenUtils jwtTokenUtils;
@Autowired
private JwtTokenProperties jwtTokenProperties;
@Autowired
private CpUserDetailRedis cpUserDetailRedis;
/**
* 微信小程序登录
*
* @param formDTO
* @return com.epmet.commons.tools.utils.Result<com.epmet.dto.UserTokenResultDTO>
* @author yinzuomei
* @since 2020/3/14 19:34
*/
@Override
public Result<UserTokenResultDTO> loginByWxCode(LoginByWxCodeFormDTO formDTO) {
//1、根据wxCode获取微信信息
WxMaJscode2SessionResult wxMaJscode2SessionResult = this.getWxMaUser(formDTO);
logger.info("openId=[" + wxMaJscode2SessionResult.getOpenid() + "]unionId=[" + wxMaJscode2SessionResult.getUnionid() + "]");
//2、根据openId查询数据库,没有则直接插入一条记录
String userId = this.getUserId(formDTO, wxMaJscode2SessionResult);
if (StringUtils.isNotBlank(userId)) {
//3、封装token且存到redis
UserTokenResultDTO userTokenResultDTO = new UserTokenResultDTO();
userTokenResultDTO.setToken(this.packagingUserToken(formDTO, userId, wxMaJscode2SessionResult));
return new Result<UserTokenResultDTO>().ok(userTokenResultDTO);
}
return new Result<UserTokenResultDTO>().error("登录失败");
}
/**
* 解析微信code获取小程序用户信息
*
* @param formDTO
* @return cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult
* @author yinzuomei
* @date 2020/3/14 20:16
*/
private WxMaJscode2SessionResult getWxMaUser(LoginByWxCodeFormDTO formDTO) {
WxMaJscode2SessionResult wxMaJscode2SessionResult = null;
try {
if (LoginConstant.APP_GOV.equals(formDTO.getApp())) {
wxMaJscode2SessionResult = wxMaServiceUtils.govWxMaService().jsCode2SessionInfo(formDTO.getWxCode());
} else if (LoginConstant.APP_OPER.equals(formDTO.getApp())) {
wxMaJscode2SessionResult = wxMaServiceUtils.operWxMaService().jsCode2SessionInfo(formDTO.getWxCode());
} else if (LoginConstant.APP_RESI.equals(formDTO.getApp())) {
wxMaJscode2SessionResult = wxMaServiceUtils.resiWxMaService().jsCode2SessionInfo(formDTO.getWxCode());
}
} catch (WxErrorException e) {
log.error("->[getMaOpenId]::error[{}]", "解析微信code失败");
}
if (null == wxMaJscode2SessionResult) {
throw new RenException("解析微信用户信息失败");
} else if (StringUtils.isBlank(wxMaJscode2SessionResult.getOpenid())) {
throw new RenException("获取微信openid失败");
}
return wxMaJscode2SessionResult;
}
/**
* 根据openId查询用户id
*
* @param formDTO
* @param wxMaJscode2SessionResult
* @return java.lang.String
* @author yinzuomei
* @since 2020/3/14 19:34
*/
private String getUserId(LoginByWxCodeFormDTO formDTO, WxMaJscode2SessionResult wxMaJscode2SessionResult) {
String userId = "";
if (LoginConstant.APP_GOV.equals(formDTO.getApp())) {
//查询customer_staff待完善
} else if (LoginConstant.APP_OPER.equals(formDTO.getApp())) {
//查询oper_staff待完善
} else if (LoginConstant.APP_RESI.equals(formDTO.getApp())) {
//查询customer_user
userId = customerUserDao.selectByWxOpenId(wxMaJscode2SessionResult.getOpenid());
if (StringUtils.isBlank(userId)) {
WxMaUserInfo wxMaUserInfo = wxMaServiceUtils.resiWxMaService().getUserService()
.getUserInfo(wxMaJscode2SessionResult.getSessionKey(),
formDTO.getEncryptedData(),
formDTO.getIv());
CustomerUserEntity customerUserEntity = new CustomerUserEntity();
customerUserEntity.setCity(wxMaUserInfo.getCity());
customerUserEntity.setWxOpenId(wxMaUserInfo.getOpenId());
customerUserEntity.setNickname(wxMaUserInfo.getNickName());
customerUserEntity.setCountry(wxMaUserInfo.getCountry());
customerUserEntity.setHeadImgUrl(wxMaUserInfo.getAvatarUrl());
customerUserEntity.setCountry(wxMaUserInfo.getCountry());
customerUserEntity.setProvince(wxMaUserInfo.getProvince());
customerUserEntity.setSex(Integer.valueOf(wxMaUserInfo.getGender()));
customerUserDao.insert(customerUserEntity);
userId = customerUserEntity.getId();
}
}
return userId;
}
/**
* 封装用户token值
*
* @param formDTO
* @param userId
* @param wxMaJscode2SessionResult
* @return java.lang.String
* @author yinzuomei
* @since 2020/3/14 19:34
*/
private String packagingUserToken(LoginByWxCodeFormDTO formDTO,
String userId,
WxMaJscode2SessionResult wxMaJscode2SessionResult) {
// 生成token
Map<String, Object> map = new HashMap<>();
map.put("app", formDTO.getApp());
map.put("client", formDTO.getClient());
map.put("userId", userId);
String token = jwtTokenUtils.createToken(map);
int expire = jwtTokenProperties.getExpire();
TokenDto tokenDto = new TokenDto();
tokenDto.setApp(formDTO.getApp());
tokenDto.setClient(formDTO.getClient());
tokenDto.setUserId(userId);
tokenDto.setOpenId(wxMaJscode2SessionResult.getOpenid());
tokenDto.setSessionKey(wxMaJscode2SessionResult.getSessionKey());
tokenDto.setUnionId(wxMaJscode2SessionResult.getUnionid());
cpUserDetailRedis.set(tokenDto, expire);
return token;
}
/**
* 手机号+密码登录接口
*
* @param formDTO
* @return com.epmet.commons.tools.utils.Result<com.epmet.dto.UserTokenResultDTO>
* @author yinzuomei
* @since 2020/3/14 19:34
*/
@Override
public Result<UserTokenResultDTO> loginByPassword(LoginByPassWordFormDTO formDTO) {
//1、账号是否存在
//2、密码是否正确
//3、生成token返回,且将TokenDto存到redis
return null;
}
}

32
epmet-auth/src/main/resources/bootstrap.yml

@ -23,6 +23,34 @@ spring:
port: @spring.redis.port@
password: @spring.redis.password@
timeout: 30s
datasource:
druid:
#MySQL
driver-class-name: com.mysql.cj.jdbc.Driver
url: @spring.datasource.druid.url@
username: @spring.datasource.druid.username@
password: @spring.datasource.druid.password@
initial-size: 10
max-active: 100
min-idle: 10
max-wait: 60000
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
#Oracle需要打开注释
#validation-query: SELECT 1 FROM DUAL
test-while-idle: true
test-on-borrow: false
test-on-return: false
filter:
stat:
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: false
wall:
config:
multi-statement-allow: true
cloud:
nacos:
discovery:
@ -87,9 +115,9 @@ ribbon:
ReadTimeout: 300000
ConnectTimeout: 300000
jwt:
token:
#秘钥
# secret: 7016867071f0ebf1c46f123eaaf4b9d6[elink.epmet]
secret: f4e2e52034348f86b67cde5[www.renren.io]
secret: 7016867071f0ebf1c46f123eaaf4b9d6[elink.epmet]
#token有效时长,默认7天,单位秒
expire: 604800
wx:

15
epmet-auth/src/main/resources/mapper/CustomerUserDao.xml

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.epmet.dao.CustomerUserDao">
<!-- 根据openId查询居民用户信息 -->
<select id="selectByWxOpenId" parameterType="java.lang.String" resultType="java.lang.String">
SELECT
cu.id
FROM
customer_user cu
WHERE
cu.DEL_FLAG = '0'
AND cu.WX_OPEN_ID = #{openId}
</select>
</mapper>

33
epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/constant/LoginConstant.java

@ -0,0 +1,33 @@
package com.epmet.common.token.constant;
/**
* @Description appclient
* @Author yinzuomei
* @Date 2020/3/14 20:12
*/
public interface LoginConstant {
/**
* 政府端
*/
String APP_GOV = "gov";
/**
* 居民端
*/
String APP_RESI = "resi";
/**
* 运营端
*/
String APP_OPER = "oper";
/**
* web
*/
String CLIENT_WEB = "web";
/**
* 微信小程序
*/
String CLIENT_WXMP = "wxmp";
}

32
epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/dto/DeptDataScopeDTO.java

@ -1,32 +0,0 @@
package com.epmet.common.token.dto;
import lombok.Data;
import java.io.Serializable;
/**
*
* 用户所有角色权限信息
*
* @Authorliuchuang
* @Date2019/11/19 23:48
*/
@Data
public class DeptDataScopeDTO implements Serializable {
private static final long serialVersionUID = -6319876948812713836L;
/**
* 部门ID
*/
private Long deptId;
/**
* 部门名称
*/
private String deptName;
/**
* 部门类型
*/
private String typeKey;
}

37
epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/dto/TokenDto.java

@ -1,14 +1,16 @@
package com.elink.esua.epdc.common.token.dto;
package com.epmet.common.token.dto;
import com.google.gson.annotations.SerializedName;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
/**
* 用户token
*
* @author rongchao
* @Date 18-10-31
* @author yinzuomei
* @Date 2020-03-14
*/
@Data
public class TokenDto implements Serializable {
@ -16,37 +18,32 @@ public class TokenDto implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 用户ID
* 政府端:gov居民端:resi运营端:oper
*/
private String userId;
private String app;
/**
* 昵称
* PC端:web微信小程序:wxmp
*/
private String nickname;
private String client;
/**
* 用户头像
*/
private String faceImg;
/**
* 手机号
* 用户ID
*/
private String mobile;
private String userId;
/**
* 真实姓名
* sessionKey
*/
private String realName;
private String sessionKey;
/**
* 网格ID
* openId
*/
private Long gridId;
private String openId;
/**
* 党员标识 01
* unionId
*/
private String partyFlag;
private String unionId;
}

48
epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/dto/WorkUserAuthorizationDTO.java

@ -1,48 +0,0 @@
package com.epmet.common.token.dto;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
/**
*
* 工作端用户授权信息
*
* @Authorliuchuang
* @Date2019/11/19 20:17
*/
@Data
public class WorkUserAuthorizationDTO implements Serializable {
private static final long serialVersionUID = -4230190448906007120L;
/**
* 令牌
*/
private String token;
/**
* 过期时长单位秒
*/
private Integer expire;
/**
* 部门名称
*/
private String deptName;
/**
* 用户标签key
*/
private String userTagKey;
/**
* 注册状态 0-未注册1-已注册
*/
private String registerState;
/**
* 用户所有角色权限信息
*/
private List<DeptDataScopeDTO> deptDataScopeList;
}

29
epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/dto/WorkUserLoginDTO.java

@ -1,29 +0,0 @@
package com.epmet.common.token.dto;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
/**
*
* 工作端用户登录信息
*
* @Authorliuchuang
* @Date2019/11/19 20:19
*/
@Data
public class WorkUserLoginDTO implements Serializable {
private static final long serialVersionUID = 1905641243346550379L;
@NotBlank(message="用户名不能为空")
private String username;
@NotBlank(message="密码不能为空")
private String password;
@NotBlank(message="微信code不能为空")
private String wxCode;
private String openId;
}

25
epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/dto/WorkUserTokenFormDTO.java

@ -1,25 +0,0 @@
package com.epmet.common.token.dto;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
/**
*
* 工作端获取token Form DTO
*
* @Authorliuchuang
* @Date2019/11/19 20:35
*/
@Data
public class WorkUserTokenFormDTO implements Serializable {
private static final long serialVersionUID = -2239027109939769097L;
@NotNull(message = "用户ID不能为空")
private Long id;
@NotBlank(message = "用户名不能为空")
private String username;
}

29
epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/dto/form/LoginByPassWordFormDTO.java

@ -0,0 +1,29 @@
package com.epmet.common.token.dto.form;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
/**
* @Description 手机号+密码登录接口入参
* @Author yinzuomei
* @Date 2020/3/14 19:46
*/
@Data
public class LoginByPassWordFormDTO extends LoginCommonFormDTO implements Serializable {
private static final long serialVersionUID = -7507437651048051183L;
/**
* 手机号
*/
@NotBlank(message = "手机号不能为空")
private String phone;
/**
* 密码
*/
@NotBlank(message = "密码不能为空")
private String password;
}

34
epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/dto/form/LoginByWxCodeFormDTO.java

@ -0,0 +1,34 @@
package com.epmet.common.token.dto.form;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
/**
* @Description 微信小程序登录接口入参
* @Author yinzuomei
* @Date 2020/3/14 14:39
*/
@Data
public class LoginByWxCodeFormDTO extends LoginCommonFormDTO implements Serializable {
private static final long serialVersionUID = 7950477424010655108L;
/**
* 微信code
*/
@NotBlank(message = "wxCode不能为空")
private String wxCode;
/**
* 用户信息
*/
@NotBlank(message = "用户信息不能为空")
private String encryptedData;
/**
* 加密算法的初始向量
*/
@NotBlank(message = "初始向量不能为空")
private String iv;
}

28
epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/dto/form/LoginCommonFormDTO.java

@ -0,0 +1,28 @@
package com.epmet.common.token.dto.form;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
/**
* @Description 登录接口通用入参
* @Author yinzuomei
* @Date 2020/3/14 19:47
*/
@Data
public class LoginCommonFormDTO implements Serializable {
private static final long serialVersionUID = -5582224784914714820L;
/**
* 政府端:gov居民端:resi运营端:oper
*/
@NotBlank(message = "app不能为空(政府端:gov、居民端:resi、运营端:oper)")
private String app;
/**
* PC端:web微信小程序:wxmp
*/
@NotBlank(message = "client不能为空(PC端:web、微信小程序:wxmp)")
private String client;
}

20
epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/dto/result/UserTokenResultDTO.java

@ -0,0 +1,20 @@
package com.epmet.common.token.dto.result;
import lombok.Data;
import java.io.Serializable;
/**
* @Description 登录接口返参DTO
* @Author yinzuomei
* @Date 2020/3/14 15:10
*/
@Data
public class UserTokenResultDTO implements Serializable {
private static final long serialVersionUID = 5214475907074876716L;
/**
* 令牌
*/
private String token;
}

20
epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/util/CpUserDetailRedis.java

@ -10,7 +10,7 @@ package com.epmet.common.token.util;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.elink.esua.epdc.common.token.dto.TokenDto;
import com.epmet.common.token.dto.TokenDto;
import com.epmet.commons.tools.redis.RedisKeys;
import com.epmet.commons.tools.redis.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
@ -34,7 +34,7 @@ public class CpUserDetailRedis {
if (user == null) {
return;
}
String key = RedisKeys.getCpUserKey(user.getUserId());
String key = RedisKeys.getCpUserKey(user.getApp(), user.getClient(), user.getUserId());
//bean to map
Map<String, Object> map = BeanUtil.beanToMap(user, false, true);
redisUtils.hMSet(key, map, expire);
@ -46,8 +46,8 @@ public class CpUserDetailRedis {
* @param userId
* @return
*/
public TokenDto get(String userId) {
String key = RedisKeys.getCpUserKey(userId);
public TokenDto get(String app, String client, String userId) {
String key = RedisKeys.getCpUserKey(app, client, userId);
Map<String, Object> map = redisUtils.hGetAll(key);
if (MapUtil.isEmpty(map)) {
@ -65,8 +65,8 @@ public class CpUserDetailRedis {
*
* @param userId
*/
public void logout(String userId) {
redisUtils.delete(RedisKeys.getCpUserKey(userId));
public void logout(String app, String client, String userId) {
redisUtils.delete(RedisKeys.getCpUserKey(app, client, userId));
}
/**
@ -76,8 +76,8 @@ public class CpUserDetailRedis {
* @param expire
* @author rongchao
*/
public boolean expire(String userId, long expire) {
return redisUtils.expire(RedisKeys.getCpUserKey(userId), expire);
public boolean expire(String app, String client, String userId, long expire) {
return redisUtils.expire(RedisKeys.getCpUserKey(app, client, userId), expire);
}
/**
@ -88,7 +88,7 @@ public class CpUserDetailRedis {
* @author yujintao
* @date 2019/9/9 14:18
*/
public long getExpire(String userId) {
return redisUtils.getExpire(RedisKeys.getCpUserKey(userId));
public long getExpire(String app, String client, String userId) {
return redisUtils.getExpire(RedisKeys.getCpUserKey(app, client, userId));
}
}

18
epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/util/TokenUtil.java

@ -1,6 +1,6 @@
package com.epmet.common.token.util;
import com.elink.esua.epdc.common.token.dto.TokenDto;
import com.epmet.common.token.dto.TokenDto;
import com.epmet.common.token.property.TokenPropertise;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -24,17 +24,17 @@ public class TokenUtil {
@Autowired
private CpUserDetailRedis redisUtils;
public TokenDto getTokenInfo(String userId) {
TokenDto tokenDto = redisUtils.get(userId);
public TokenDto getTokenInfo(String app,String client,String userId) {
TokenDto tokenDto = redisUtils.get(app,client,userId);
return tokenDto;
}
public void expireToken(String userId) {
redisUtils.logout(userId);
public void expireToken(String app,String client,String userId) {
redisUtils.logout(app,client,userId);
}
public boolean delayToken(String userId) {
return redisUtils.expire(userId, tokenPropertise.getExpire());
public boolean delayToken(String app,String client,String userId) {
return redisUtils.expire(app,client,userId, tokenPropertise.getExpire());
}
/**
@ -45,7 +45,7 @@ public class TokenUtil {
* @author yujintao
* @date 2019/9/9 14:19
*/
public long getExpire(String userId) {
return redisUtils.getExpire(userId);
public long getExpire(String app,String client,String userId) {
return redisUtils.getExpire(app,client,userId);
}
}

2
epmet-commons/epmet-common-clienttoken/src/main/java/com/epmet/common/token/util/UserUtil.java

@ -1,6 +1,6 @@
package com.epmet.common.token.util;
import com.elink.esua.epdc.common.token.dto.TokenDto;
import com.epmet.common.token.dto.TokenDto;
import com.epmet.commons.tools.constant.Constant;
import com.epmet.commons.tools.utils.WebUtil;

10
epmet-commons/epmet-commons-tools-wx-ma/src/main/java/com/epmet/utils/WxMaServiceUtils.java

@ -17,11 +17,11 @@ public class WxMaServiceUtils {
@Value("${wx.ma.appId.resi}")
private String APPID_RESI;
@Value("${wx.ma.appId.gov}")
/*@Value("${wx.ma.appId.gov}")
private String APPID_GOV;
@Value("${wx.ma.appId.oper}")
private String APPID_OPER;
private String APPID_OPER;*/
/**
* 获取党群e事通-居民端小程序配置
@ -43,7 +43,8 @@ public class WxMaServiceUtils {
* @date 2020/03/13 10:44
*/
public final WxMaService govWxMaService() {
final WxMaService wxMaService = WxMaConfig.getMaService(APPID_GOV);
// final WxMaService wxMaService = WxMaConfig.getMaService(APPID_GOV);
final WxMaService wxMaService = WxMaConfig.getMaService(APPID_RESI);
return wxMaService;
}
@ -57,7 +58,8 @@ public class WxMaServiceUtils {
* @since 2020/03/13 10:44
*/
public final WxMaService operWxMaService() {
final WxMaService wxMaService = WxMaConfig.getMaService(APPID_OPER);
// final WxMaService wxMaService = WxMaConfig.getMaService(APPID_OPER);
final WxMaService wxMaService = WxMaConfig.getMaService(APPID_RESI);
return wxMaService;
}
}

4
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/constant/Constant.java

@ -104,8 +104,4 @@ public interface Constant {
* 版本控制用
*/
String VERSION_CONTROL = "/{version}";
/**
* 移动端接口标识
*/
String EPDC_APP = "epdc-app/";
}

18
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java

@ -1,8 +1,8 @@
/**
* Copyright (c) 2018 人人开源 All rights reserved.
*
* <p>
* https://www.renren.io
*
* <p>
* 版权所有侵权必究
*/
@ -17,7 +17,7 @@ public class RedisKeys {
/**
* 党群e事通redis前缀
*/
private static String rootPrefix = "epdc:";
private static String rootPrefix = "epmet:";
/**
* 系统参数Key
@ -75,8 +75,14 @@ public class RedisKeys {
return rootPrefix.concat("sys:user:permissions:").concat(String.valueOf(userId));
}
public static String getCpUserKey(String id) {
return rootPrefix.concat("sys:security:cpuser:").concat(id);
/**
* 用户token Key
* epmet:sys:security:user:resi:wxmp:100051424889632
* epmet:sys:security:user:gov:wxmp:5048212821254821
* epmet:sys:security:user:oper:web:eeedsfsdfds512551
*/
public static String getCpUserKey(String app, String client, String id) {
return rootPrefix.concat("sys:security:user:").concat(app).concat(":").concat(client).concat(":").concat(id);
}
/**
@ -94,6 +100,7 @@ public class RedisKeys {
/**
* 用户请求发送短信接口记录本次请求时间保存一分钟
* ---后面需要改
*
* @param phone
* @return java.lang.String
* @author work@yujt.net.cn
@ -104,5 +111,4 @@ public class RedisKeys {
}
}

Loading…
Cancel
Save