refactor:项目调整

This commit is contained in:
haoxr 2021-05-21 00:44:02 +08:00
parent 8fc8468219
commit 74722c6e68
26 changed files with 142 additions and 143 deletions

View File

@ -102,7 +102,7 @@
<dependency>
<groupId>com.youlai</groupId>
<artifactId>common-elasticsearch</artifactId>
<artifactId>common-es</artifactId>
</dependency>
<dependency>

View File

@ -47,7 +47,7 @@ public class BrandController {
Page<PmsBrand> result = iPmsBrandService.page(new Page<>(page, limit), queryWrapper);
return Result.success(result.getRecords(), result.getTotal());
case LIST:
queryWrapper.eq(PmsBrand::getStatus, GlobalConstants.STATUS_NORMAL_VALUE)
queryWrapper.eq(PmsBrand::getStatus, GlobalConstants.STATUS_YES)
.select(PmsBrand::getId, PmsBrand::getName);
List<PmsBrand> list = iPmsBrandService.list(queryWrapper);
return Result.success(list);

View File

@ -34,7 +34,7 @@ public class CategoryController {
public Result list(Long parentId) {
PmsCategory category = new PmsCategory();
category.setParentId(parentId);
category.setStatus(GlobalConstants.STATUS_NORMAL_VALUE);
category.setStatus(GlobalConstants.STATUS_YES);
List<CategoryVO> list = iPmsCategoryService.listForTree(category);
return Result.success(list);
}

View File

@ -217,7 +217,7 @@
<dependency>
<groupId>com.youlai</groupId>
<artifactId>common-elasticsearch</artifactId>
<artifactId>common-es</artifactId>
<version>${youlai.version}</version>
</dependency>

View File

@ -13,11 +13,14 @@ import java.util.List;
@Data
public class SysRole extends BaseEntity {
@TableId(type= IdType.AUTO)
@TableId(type = IdType.AUTO)
private Long id;
private String name;
@ApiModelProperty("角色编码")
private String code;
private Integer sort;
private Integer status;

View File

@ -94,7 +94,7 @@
<dependency>
<groupId>com.youlai</groupId>
<artifactId>common-elasticsearch</artifactId>
<artifactId>common-es</artifactId>
</dependency>
</dependencies>

View File

@ -13,7 +13,7 @@ import lombok.Setter;
public enum PermTypeEnum {
ROUTE(1, "路由权限"),
ROUTE(1, "接口权限"),
BUTTON(2, "按钮权限");
@Getter

View File

@ -67,7 +67,7 @@ public class RoleController {
return Result.success(result.getRecords(), result.getTotal());
case LIST:
List list = iSysRoleService.list(new LambdaQueryWrapper<SysRole>()
.eq(SysRole::getStatus, GlobalConstants.STATUS_NORMAL_VALUE));
.eq(SysRole::getStatus, GlobalConstants.STATUS_YES));
return Result.success(list);
default:
return Result.failed(ResultCode.QUERY_MODE_IS_NULL);

View File

@ -13,7 +13,6 @@ import com.youlai.admin.pojo.entity.SysUserRole;
import com.youlai.admin.pojo.dto.UserDTO;
import com.youlai.admin.pojo.vo.UserVO;
import com.youlai.admin.service.*;
import com.youlai.common.base.BaseController;
import com.youlai.common.result.Result;
import com.youlai.common.result.ResultCode;
import com.youlai.common.web.util.RequestUtils;
@ -35,7 +34,7 @@ import java.util.stream.Collectors;
@RequestMapping("/api.admin/v1/users")
@Slf4j
@AllArgsConstructor
public class UserController extends BaseController {
public class UserController {
private final ISysUserService iSysUserService;
private final ISysUserRoleService iSysUserRoleService;

View File

@ -68,7 +68,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
menu.getRoles()
));
// 菜单显示隐藏
routerVO.setHidden(!GlobalConstants.VISIBLE_SHOW_VALUE.equals(menu.getVisible()) ? true : false);
routerVO.setHidden(!GlobalConstants.STATUS_YES.equals(menu.getVisible()) );
List<RouterVO> children = recursionForRoutes(menu.getId(), menuList);
routerVO.setChildren(children);
if(CollectionUtil.isNotEmpty(children)){

View File

@ -3,8 +3,8 @@ package com.youlai.auth.config;
import cn.hutool.http.HttpStatus;
import cn.hutool.json.JSONUtil;
import com.youlai.auth.domain.User;
import com.youlai.auth.service.JdbcClientDetailsServiceImpl;
import com.youlai.auth.service.UserDetailsServiceImpl;
import com.youlai.auth.service.impl.JdbcClientDetailsServiceImpl;
import com.youlai.auth.service.impl.UserDetailsServiceImpl;
import com.youlai.common.constant.AuthConstants;
import com.youlai.common.result.Result;
import com.youlai.common.result.ResultCode;
@ -135,9 +135,8 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
return (accessToken, authentication) -> {
Map<String, Object> map = new HashMap<>(8);
User user = (User) authentication.getUserAuthentication().getPrincipal();
map.put(AuthConstants.USER_ID_KEY, user.getId());
map.put(AuthConstants.CLIENT_ID_KEY, user.getClientId());
map.put(AuthConstants.USER_NAME_KEY, user.getUsername());
map.put("userId", user.getId());
map.put("username", user.getUsername());
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(map);
return accessToken;
};

View File

@ -1,27 +1,14 @@
package com.youlai.auth.controller;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import cn.hutool.core.util.StrUtil;
import com.youlai.auth.service.IAuthService;
import com.youlai.common.constant.AuthConstants;
import com.youlai.common.constant.GlobalConstants;
import com.youlai.common.result.Result;
import com.youlai.common.result.ResultCode;
import com.youlai.common.web.exception.BizException;
import com.youlai.common.web.util.RequestUtils;
import com.youlai.mall.ums.api.MemberFeignClient;
import com.youlai.mall.ums.pojo.domain.UmsMember;
import com.youlai.mall.ums.pojo.dto.AuthMemberDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.endpoint.TokenEndpoint;
import org.springframework.web.HttpRequestMethodNotSupportedException;
@ -42,6 +29,8 @@ import java.util.Map;
public class AuthController {
private TokenEndpoint tokenEndpoint;
private IAuthService weAppAuthServiceImpl;
@ApiOperation(value = "OAuth2认证", notes = "login")
@ApiImplicitParams({
@ -51,11 +40,6 @@ public class AuthController {
@ApiImplicitParam(name = "refresh_token", value = "刷新token"),
@ApiImplicitParam(name = "username", defaultValue = "admin", value = "登录用户名"),
@ApiImplicitParam(name = "password", defaultValue = "123456", value = "登录密码"),
// 微信小程序认证参数无小程序可忽略
@ApiImplicitParam(name = "code", value = "小程序授权code"),
@ApiImplicitParam(name = "encryptedData", value = "包括敏感数据在内的完整用户信息的加密数据"),
@ApiImplicitParam(name = "iv", value = "加密算法的初始向量"),
})
@PostMapping("/token")
public OAuth2AccessToken postAccessToken(
@ -74,7 +58,7 @@ public class AuthController {
String clientId = RequestUtils.getAuthClientId();
switch (clientId) {
case AuthConstants.WEAPP_CLIENT_ID: // 微信认证
oAuth2AccessToken = this.handleForWxAuth(principal, parameters);
oAuth2AccessToken = weAppAuthServiceImpl.login(principal, parameters);
break;
default:
oAuth2AccessToken = tokenEndpoint.postAccessToken(principal, parameters).getBody();
@ -84,57 +68,4 @@ public class AuthController {
}
private WxMaService wxService;
private MemberFeignClient memberFeignClient;
private PasswordEncoder passwordEncoder;
@SneakyThrows
public OAuth2AccessToken handleForWxAuth(Principal principal, Map<String, String> parameters) throws HttpRequestMethodNotSupportedException {
String code = parameters.get("code");
if (StrUtil.isBlank(code)) {
throw new BizException("code不能为空");
}
WxMaJscode2SessionResult session = null;
// 根据授权code获取微信用户信息
session = wxService.getUserService().getSessionInfo(code);
String openid = session.getOpenid();
String sessionKey = session.getSessionKey();
Result<AuthMemberDTO> result = memberFeignClient.getUserByOpenid(openid);
if (ResultCode.USER_NOT_EXIST.getCode().equals(result.getCode())) { // 微信授权登录 会员信息不存在时 注册会员
String encryptedData = parameters.get("encryptedData");
String iv = parameters.get("iv");
WxMaUserInfo userInfo = wxService.getUserService().getUserInfo(sessionKey, encryptedData, iv);
if (userInfo == null) {
throw new BizException("获取用户信息失败");
}
UmsMember user = new UmsMember()
.setNickname(userInfo.getNickName())
.setAvatar(userInfo.getAvatarUrl())
.setGender(Integer.valueOf(userInfo.getGender()))
.setOpenid(openid)
.setUsername(openid)
.setPassword(passwordEncoder.encode(openid).replace(AuthConstants.BCRYPT, Strings.EMPTY)) // 加密密码移除前缀加密方式 {bcrypt}
.setStatus(GlobalConstants.STATUS_NORMAL_VALUE);
Result res = memberFeignClient.add(user);
if (!ResultCode.SUCCESS.getCode().equals(res.getCode())) {
throw new BizException("注册会员失败");
}
}
// oauth2认证参数对应授权登录时注册会员的usernamepassword信息模拟通过oauth2的密码模式认证生成JWT
parameters.put("username", openid);
parameters.put("password", openid);
OAuth2AccessToken oAuth2AccessToken = tokenEndpoint.postAccessToken(principal, parameters).getBody();
return oAuth2AccessToken;
}
}

View File

@ -0,0 +1,17 @@
package com.youlai.auth.service;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import java.security.Principal;
import java.util.Map;
/**
* @author haoxr
* @description TODO
* @createTime 2021/5/20 23:35
*/
public interface IAuthService {
OAuth2AccessToken login(Principal principal, Map<String, String> parameters) ;
}

View File

@ -1,4 +1,4 @@
package com.youlai.auth.service;
package com.youlai.auth.service.impl;
import lombok.SneakyThrows;
import org.springframework.security.oauth2.provider.ClientDetails;

View File

@ -1,4 +1,4 @@
package com.youlai.auth.service;
package com.youlai.auth.service.impl;
import com.youlai.admin.api.UserFeignClient;
import com.youlai.admin.pojo.dto.UserDTO;

View File

@ -0,0 +1,97 @@
package com.youlai.auth.service.impl;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import cn.hutool.core.util.StrUtil;
import com.youlai.auth.service.IAuthService;
import com.youlai.common.constant.AuthConstants;
import com.youlai.common.constant.GlobalConstants;
import com.youlai.common.result.Result;
import com.youlai.common.result.ResultCode;
import com.youlai.common.web.exception.BizException;
import com.youlai.mall.ums.api.MemberFeignClient;
import com.youlai.mall.ums.pojo.domain.UmsMember;
import com.youlai.mall.ums.pojo.dto.AuthMemberDTO;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.logging.log4j.util.Strings;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.endpoint.TokenEndpoint;
import org.springframework.stereotype.Service;
import java.security.Principal;
import java.util.Map;
/**
* @author haoxr
* @description 微信小程序认证接口
* @createTime 2021/5/20 23:37
*/
@Service
@AllArgsConstructor
public class WeAppAuthServiceImpl implements IAuthService {
private WxMaService wxService;
private MemberFeignClient memberFeignClient;
private PasswordEncoder passwordEncoder;
private TokenEndpoint tokenEndpoint;
/**
* @param principal
* @param parameters code=小程序授权code
* encryptedData=包括敏感数据在内的完整用户信息的加密数据
* iv=加密算法的初始向量
* @return
*/
@Override
@SneakyThrows
public OAuth2AccessToken login(Principal principal, Map<String, String> parameters) {
String code = parameters.get("code");
if (StrUtil.isBlank(code)) {
throw new BizException("code不能为空");
}
WxMaJscode2SessionResult session = null;
// 根据授权code获取微信用户信息
session = wxService.getUserService().getSessionInfo(code);
String openid = session.getOpenid();
String sessionKey = session.getSessionKey();
Result<AuthMemberDTO> result = memberFeignClient.getUserByOpenid(openid);
if (ResultCode.USER_NOT_EXIST.getCode().equals(result.getCode())) { // 微信授权登录 会员信息不存在时 注册会员
String encryptedData = parameters.get("encryptedData");
String iv = parameters.get("iv");
WxMaUserInfo userInfo = wxService.getUserService().getUserInfo(sessionKey, encryptedData, iv);
if (userInfo == null) {
throw new BizException("获取用户信息失败");
}
UmsMember user = new UmsMember()
.setNickname(userInfo.getNickName())
.setAvatar(userInfo.getAvatarUrl())
.setGender(Integer.valueOf(userInfo.getGender()))
.setOpenid(openid)
.setUsername(openid)
.setPassword(passwordEncoder.encode(openid).replace(AuthConstants.BCRYPT, Strings.EMPTY)) // 加密密码移除前缀加密方式 {bcrypt}
.setStatus(GlobalConstants.STATUS_YES);
Result res = memberFeignClient.add(user);
if (!ResultCode.SUCCESS.getCode().equals(res.getCode())) {
throw new BizException("注册会员失败");
}
}
// oauth2认证参数对应授权登录时注册会员的usernamepassword信息模拟通过oauth2的密码模式认证生成JWT
parameters.put("username", openid);
parameters.put("password", openid);
OAuth2AccessToken oAuth2AccessToken = tokenEndpoint.postAccessToken(principal, parameters).getBody();
return oAuth2AccessToken;
}
}

View File

@ -1,7 +0,0 @@
package com.youlai.common.base;
public class BaseController<T> {
}

View File

@ -2,9 +2,8 @@ package com.youlai.common.constant;
public interface GlobalConstants {
Integer STATUS_NORMAL_VALUE = 1;
Integer VISIBLE_SHOW_VALUE = 1;
Integer STATUS_YES = 1;
Integer STATUS_NO= 0;
String DEFAULT_USER_PASSWORD = "123456";

View File

@ -9,7 +9,7 @@
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>common-elasticsearch</artifactId>
<artifactId>common-es</artifactId>
<dependencies>

View File

@ -20,45 +20,6 @@
</pattern>
</encoder>
</appender>
<!-- Logstash收集登录日志输出到ElasticSearch -->
<appender name="LOGIN_LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>g.youlai.tech:5044</destination>
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<timestamp>
<timeZone>Asia/Shanghai</timeZone>
</timestamp>
<!--自定义日志输出格式-->
<pattern>
<pattern>
{
"project": "${APP_NAME}",
"date": "%X{date}", <!-- 索引名时区同步 -->
"action":"login",
"pid": "${PID:-}",
"thread": "%thread",
"message": "%message",
"elapsedTime": "%X{elapsedTime}",
"username":"%X{username}",
"clientIP": "%X{clientIP}",
"region":"%X{region}",
"token":"%X{token}",
"loginTime": "%date{\"yyyy-MM-dd HH:mm:ss\"}",
"description":"%X{description}"
}
</pattern>
</pattern>
</providers>
</encoder>
<keepAliveDuration>5 minutes</keepAliveDuration>
</appender>
<!-- additivity 默认是true标识该logger的日志会在root的appender再打印一次 -->
<logger name="com.youlai.common.web.aspect.LoginLogAspect" level="INFO" additivity="false">
<appender-ref ref="LOGIN_LOGSTASH"/>
</logger>
<logger name="com.alibaba.nacos.client.naming" level="ERROR" additivity="false">
<appender-ref ref="CONSOLE"/>
</logger>

View File

@ -19,6 +19,6 @@
<module>common-web</module>
<module>common-mybatis</module>
<module>common-rabbitmq</module>
<module>common-elasticsearch</module>
<module>common-es</module>
</modules>
</project>