From 092ab59079225df175ef38252c78c671afeeedae Mon Sep 17 00:00:00 2001 From: "DESKTOP-G8BCEP0\\HP" <2037158277@qq.com> Date: Thu, 9 Sep 2021 15:06:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=E5=B0=8F=E7=A8=8B=E5=BA=8F=E5=90=8C=E6=AD=A5=E7=BD=91=E7=AB=99?= =?UTF-8?q?=E8=B4=A6=E5=8F=B7=E7=9A=84=E5=8A=9F=E8=83=BD=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../account/TokenAuthenticationService.java | 33 ++++++++++- .../java/com/xkrs/service/SysUserService.java | 7 +++ .../xkrs/service/impl/SysUserServiceImpl.java | 57 +++++++++++++++---- 3 files changed, 86 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/xkrs/common/account/TokenAuthenticationService.java b/src/main/java/com/xkrs/common/account/TokenAuthenticationService.java index 54a20fe..e8c5af3 100644 --- a/src/main/java/com/xkrs/common/account/TokenAuthenticationService.java +++ b/src/main/java/com/xkrs/common/account/TokenAuthenticationService.java @@ -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 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 diff --git a/src/main/java/com/xkrs/service/SysUserService.java b/src/main/java/com/xkrs/service/SysUserService.java index c88d920..43d840e 100644 --- a/src/main/java/com/xkrs/service/SysUserService.java +++ b/src/main/java/com/xkrs/service/SysUserService.java @@ -103,4 +103,11 @@ public interface SysUserService { * @return */ SysUserVo getUserByUserName(String userName); + + /** + * 判断小程序账号的绑定 + * @param openId + * @return + */ + String findUserByOpenId(String openId); } diff --git a/src/main/java/com/xkrs/service/impl/SysUserServiceImpl.java b/src/main/java/com/xkrs/service/impl/SysUserServiceImpl.java index 780da16..90397e0 100644 --- a/src/main/java/com/xkrs/service/impl/SysUserServiceImpl.java +++ b/src/main/java/com/xkrs/service/impl/SysUserServiceImpl.java @@ -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 permissions = new ArrayList<>(); + List integers = relRoleAuthorityDao.selectAuthorityByUserId(byOpenId.getId()); + List 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); + } + } + }