添加了微信小程序同步网站账号的功能模块

This commit is contained in:
DESKTOP-G8BCEP0\HP 2021-09-09 15:06:03 +08:00
parent 12edd5dc0b
commit 092ab59079
3 changed files with 86 additions and 11 deletions

View File

@ -83,7 +83,6 @@ public class TokenAuthenticationService {
.signWith(key)
.compact();
map.put("token",jwt);
map.put("role",authsList);
map.put("user",sysUserEntity);
// JWT 写入 body
PrintWriter out = null;
@ -97,6 +96,38 @@ public class TokenAuthenticationService {
out.append(OutputEncapsulation.outputEncapsulationObject(PromptMessageEnum.SUCCESS, map, locale));
}
public static String addAuthentications(HttpServletResponse response, String userName,
Collection<? extends GrantedAuthority> authorities) {
Locale locale = new Locale("zh", "CN");
Map map = new HashMap(3);
StringBuffer auths = new StringBuffer();
String authsList = "";
for(GrantedAuthority r : authorities) {
auths.append("," + r.getAuthority());
}
authsList = auths.toString();
if(authsList.length()>1){
authsList=authsList.substring(1,authsList.length());
}else{
logger.warn(userName +" has no permission!");
}
// 生成JWT
String jwt = Jwts.builder()
.setSubject(userName)
.setIssuer("https://www.microservice.com")
.setAudience(userName)
// 保存权限
.claim("auths", authsList)
// 有效期设置
.setExpiration(new Date(System.currentTimeMillis() + EXPIRATIONTIME))
// 签名设置
.signWith(key)
.compact();
return jwt;
}
/**
* JWT验证方法
* @param request

View File

@ -103,4 +103,11 @@ public interface SysUserService {
* @return
*/
SysUserVo getUserByUserName(String userName);
/**
* 判断小程序账号的绑定
* @param openId
* @return
*/
String findUserByOpenId(String openId);
}

View File

@ -1,27 +1,26 @@
package com.xkrs.service.impl;
import com.xkrs.common.account.GrantedAuthorityImpl;
import com.xkrs.common.account.TokenAuthenticationService;
import com.xkrs.common.encapsulation.PromptMessageEnum;
import com.xkrs.dao.RelRoleAuthorityDao;
import com.xkrs.dao.RelUserRoleDao;
import com.xkrs.dao.SysRoleDao;
import com.xkrs.dao.SysUserDao;
import com.xkrs.model.entity.RelRoleAuthorityEntity;
import com.xkrs.model.entity.RelUserRoleEntity;
import com.xkrs.model.entity.SysRoleEntity;
import com.xkrs.model.entity.SysUserEntity;
import com.xkrs.dao.*;
import com.xkrs.model.entity.*;
import com.xkrs.model.qo.SysUserQo;
import com.xkrs.model.vo.SysUserVo;
import com.xkrs.service.SysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.crypto.keygen.KeyGenerators;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject;
import static com.xkrs.utils.DateTimeUtil.getNowTime;
@ -48,6 +47,9 @@ public class SysUserServiceImpl implements SysUserService {
@Resource
private RelRoleAuthorityDao relRoleAuthorityDao;
@Resource
private SysAuthorityDao sysAuthorityDao;
/**
* 检查用户名是否存在
* @param userName
@ -238,4 +240,39 @@ public class SysUserServiceImpl implements SysUserService {
return sysUserDao.selectUserByUserName(userName);
}
/**
* 判断小程序账号的绑定
* @param openId
* @return
*/
@Override
public String findUserByOpenId(String openId) {
Locale locale = LocaleContextHolder.getLocale();
Map map = new HashMap(3);
SysUserEntity byOpenId = sysUserDao.findByOpenId(openId);
if(byOpenId == null){
map.put("type","0");
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL,map,locale);
}else {
// 设置权限列表
ArrayList<GrantedAuthority> permissions = new ArrayList<>();
List<Integer> integers = relRoleAuthorityDao.selectAuthorityByUserId(byOpenId.getId());
List<SysAuthorityEntity> permissionList = sysAuthorityDao.findAllByIdIn(integers);
for(SysAuthorityEntity sysAuthorityEntity : permissionList) {
permissions.add(new GrantedAuthorityImpl(sysAuthorityEntity.getAuthorityName()));
}
// 生成令牌
Authentication authToken = new UsernamePasswordAuthenticationToken(byOpenId.getUserName(), byOpenId.getPassword(), permissions);
HttpServletResponse response = null;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Credentials", "false");
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
String token = TokenAuthenticationService.addAuthentications(response, authToken.getName(), authToken.getAuthorities());
map.put("token",token);
map.put("type",1);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS,map,locale);
}
}
}