添加了微信小程序同步网站账号的功能模块
This commit is contained in:
parent
12edd5dc0b
commit
092ab59079
@ -83,7 +83,6 @@ public class TokenAuthenticationService {
|
|||||||
.signWith(key)
|
.signWith(key)
|
||||||
.compact();
|
.compact();
|
||||||
map.put("token",jwt);
|
map.put("token",jwt);
|
||||||
map.put("role",authsList);
|
|
||||||
map.put("user",sysUserEntity);
|
map.put("user",sysUserEntity);
|
||||||
// 将 JWT 写入 body
|
// 将 JWT 写入 body
|
||||||
PrintWriter out = null;
|
PrintWriter out = null;
|
||||||
@ -97,6 +96,38 @@ public class TokenAuthenticationService {
|
|||||||
out.append(OutputEncapsulation.outputEncapsulationObject(PromptMessageEnum.SUCCESS, map, locale));
|
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验证方法
|
* JWT验证方法
|
||||||
* @param request
|
* @param request
|
||||||
|
@ -103,4 +103,11 @@ public interface SysUserService {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
SysUserVo getUserByUserName(String userName);
|
SysUserVo getUserByUserName(String userName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断小程序账号的绑定
|
||||||
|
* @param openId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
String findUserByOpenId(String openId);
|
||||||
}
|
}
|
||||||
|
@ -1,27 +1,26 @@
|
|||||||
package com.xkrs.service.impl;
|
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.common.encapsulation.PromptMessageEnum;
|
||||||
import com.xkrs.dao.RelRoleAuthorityDao;
|
import com.xkrs.dao.*;
|
||||||
import com.xkrs.dao.RelUserRoleDao;
|
import com.xkrs.model.entity.*;
|
||||||
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.model.qo.SysUserQo;
|
import com.xkrs.model.qo.SysUserQo;
|
||||||
import com.xkrs.model.vo.SysUserVo;
|
import com.xkrs.model.vo.SysUserVo;
|
||||||
import com.xkrs.service.SysUserService;
|
import com.xkrs.service.SysUserService;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.context.i18n.LocaleContextHolder;
|
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.security.crypto.keygen.KeyGenerators;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.List;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.util.Locale;
|
import java.util.*;
|
||||||
|
|
||||||
import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject;
|
import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject;
|
||||||
import static com.xkrs.utils.DateTimeUtil.getNowTime;
|
import static com.xkrs.utils.DateTimeUtil.getNowTime;
|
||||||
@ -48,6 +47,9 @@ public class SysUserServiceImpl implements SysUserService {
|
|||||||
@Resource
|
@Resource
|
||||||
private RelRoleAuthorityDao relRoleAuthorityDao;
|
private RelRoleAuthorityDao relRoleAuthorityDao;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private SysAuthorityDao sysAuthorityDao;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查用户名是否存在
|
* 检查用户名是否存在
|
||||||
* @param userName
|
* @param userName
|
||||||
@ -238,4 +240,39 @@ public class SysUserServiceImpl implements SysUserService {
|
|||||||
return sysUserDao.selectUserByUserName(userName);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user