refactor:代码优化

This commit is contained in:
haoxr 2021-01-07 18:23:17 +08:00
parent e4b45afb19
commit 94ddd46c1e
29 changed files with 92 additions and 101 deletions

View File

@ -1,37 +1,33 @@
package com.youlai.mall.ums.api; package com.youlai.mall.ums.api;
import com.youlai.common.core.result.Result; import com.youlai.common.core.result.Result;
import com.youlai.mall.ums.pojo.dto.MemberDTO;
import com.youlai.mall.ums.pojo.dto.MemberInfoDTO;
import com.youlai.mall.ums.pojo.UmsMember; import com.youlai.mall.ums.pojo.UmsMember;
import com.youlai.mall.ums.pojo.dto.AuthMemberDTO;
import com.youlai.mall.ums.pojo.dto.MemberDTO;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@FeignClient("mall-ums") @FeignClient("mall-ums")
public interface MemberFeignService { public interface MemberFeignService {
@PostMapping("/members") @PostMapping("/api.app/v1/members")
Result add(@RequestBody UmsMember member); Result add(@RequestBody UmsMember member);
/** /**
* 获取会员信息 * 获取会员信息
* @param id
* @param queryMode 查询模式2-订单会员
* @return
*/ */
@GetMapping("/members/{id}") @GetMapping("/api.app/v1/members/{id}")
Result<MemberInfoDTO> getMember(@PathVariable Long id, @RequestParam(value = "queryMode") Integer queryMode); Result<MemberDTO> getMemberById(@PathVariable Long id);
/** /**
* 获取会员信息 * 获取认证会员信息
* @param openid
* @param queryMode 查询模式1-认证会员
* @return
*/ */
@GetMapping("/members/{openid}") @GetMapping("/api.app/v1/members/openid/{openid}")
Result<MemberDTO> loadMemberByOpenid(@PathVariable String openid, @RequestParam(value = "queryMode") Integer queryMode); Result<AuthMemberDTO> getMemberByOpenid(@PathVariable String openid);
} }

View File

@ -3,7 +3,6 @@ package com.youlai.mall.ums.pojo;
import lombok.Data; import lombok.Data;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
import java.time.LocalDate; import java.time.LocalDate;
@Data @Data

View File

@ -0,0 +1,17 @@
package com.youlai.mall.ums.pojo.dto;
import lombok.Data;
@Data
public class AuthMemberDTO {
private Long id;
private String username;
private String password;
private Integer status;
private String clientId;
private String avatar;
private String nickname;
}

View File

@ -6,12 +6,8 @@ import lombok.Data;
public class MemberDTO { public class MemberDTO {
private Long id; private Long id;
private String username;
private String password;
private Integer status;
private String clientId;
private String avatar;
private String nickname; private String nickname;
private String mobile;
} }

View File

@ -1,13 +0,0 @@
package com.youlai.mall.ums.pojo.dto;
import lombok.Data;
@Data
public class MemberInfoDTO {
private Long id;
private String nickname;
private String mobile;
}

View File

@ -1,15 +1,14 @@
package com.youlai.admin.api; package com.youlai.admin.api;
import com.youlai.admin.dto.UserDTO; import com.youlai.admin.pojo.dto.UserDTO;
import com.youlai.common.core.result.Result; import com.youlai.common.core.result.Result;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient("youlai-admin") @FeignClient("youlai-admin")
public interface UserFeignService { public interface UserFeignService {
@GetMapping("/users/{id}") @GetMapping("/users/username/{username}")
Result<UserDTO> loadUserByUsername(@PathVariable Object id, @RequestParam Integer queryMode); Result<UserDTO> getUserByUsername(@PathVariable String username);
} }

View File

@ -9,7 +9,7 @@ import lombok.Data;
public class SysDept extends BaseEntity { public class SysDept extends BaseEntity {
@TableId(type= IdType.AUTO) @TableId(type= IdType.AUTO)
private Integer id; private Long id;
private String name; private String name;

View File

@ -11,7 +11,7 @@ import lombok.experimental.Accessors;
public class SysDict extends BaseEntity { public class SysDict extends BaseEntity {
@TableId(type= IdType.AUTO) @TableId(type= IdType.AUTO)
private Integer id; private Long id;
private String name; private String name;

View File

@ -9,7 +9,7 @@ import lombok.Data;
public class SysDictType extends BaseEntity { public class SysDictType extends BaseEntity {
@TableId(type = IdType.AUTO) @TableId(type = IdType.AUTO)
private Integer id; private Long id;
private String code; private String code;

View File

@ -18,11 +18,11 @@ import java.util.List;
public class SysMenu extends BaseEntity { public class SysMenu extends BaseEntity {
@TableId(type = IdType.AUTO) @TableId(type = IdType.AUTO)
private Integer id; private Long id;
private String title; private String title;
private Integer parentId; private Long parentId;
private String name; private String name;

View File

@ -14,7 +14,7 @@ import java.util.List;
public class SysResource extends BaseEntity { public class SysResource extends BaseEntity {
@TableId(type = IdType.AUTO) @TableId(type = IdType.AUTO)
private Integer id; private Long id;
private String name; private String name;
private String url; private String url;

View File

@ -12,7 +12,7 @@ import java.util.List;
public class SysRole extends BaseEntity { public class SysRole extends BaseEntity {
@TableId(type= IdType.AUTO) @TableId(type= IdType.AUTO)
private Integer id; private Long id;
private String name; private String name;
@ -27,9 +27,9 @@ public class SysRole extends BaseEntity {
private String remark; private String remark;
@TableField(exist = false) @TableField(exist = false)
private List<Integer> menuIds; private List<Long> menuIds;
@TableField(exist = false) @TableField(exist = false)
private List<Integer> resourceIds; private List<Long> resourceIds;
} }

View File

@ -7,8 +7,8 @@ import lombok.experimental.Accessors;
@Accessors(chain = true) @Accessors(chain = true)
public class SysRoleMenu { public class SysRoleMenu {
private Integer roleId; private Long roleId;
private Integer menuId; private Long menuId;
} }

View File

@ -6,6 +6,6 @@ import lombok.experimental.Accessors;
@Data @Data
@Accessors(chain = true) @Accessors(chain = true)
public class SysRoleResource { public class SysRoleResource {
private Integer roleId; private Long roleId;
private Integer resourceId; private Long resourceId;
} }

View File

@ -7,6 +7,6 @@ public class SysUserRole {
private Long userId; private Long userId;
private Integer roleId; private Long roleId;
} }

View File

@ -1,4 +1,4 @@
package com.youlai.admin.dto; package com.youlai.admin.pojo.dto;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@ -14,6 +14,6 @@ public class UserDTO {
private String password; private String password;
private Integer status; private Integer status;
private String clientId; private String clientId;
private List<Integer> roles; private List<Long> roles;
} }

View File

@ -1,4 +1,4 @@
package com.youlai.admin.vo; package com.youlai.admin.pojo.vo;
import lombok.Data; import lombok.Data;

View File

@ -1,4 +1,4 @@
package com.youlai.admin.vo; package com.youlai.admin.pojo.vo;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.youlai.common.core.base.BaseEntity; import com.youlai.common.core.base.BaseEntity;

View File

@ -1,7 +1,6 @@
package com.youlai.admin.vo; package com.youlai.admin.pojo.vo;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;

View File

@ -1,4 +1,4 @@
package com.youlai.admin.vo; package com.youlai.admin.pojo.vo;
import lombok.Data; import lombok.Data;
@ -8,7 +8,7 @@ import java.util.List;
@Data @Data
public class TreeSelectVO { public class TreeSelectVO {
private Integer id; private Long id;
private String label; private String label;

View File

@ -1,4 +1,4 @@
package com.youlai.admin.vo; package com.youlai.admin.pojo.vo;
import lombok.Data; import lombok.Data;
@ -7,12 +7,12 @@ import java.util.List;
@Data @Data
public class UserVO { public class UserVO {
private Integer id; private Long id;
private String nickname; private String nickname;
private String avatar; private String avatar;
private List<Integer> roles; private List<Long> roles;
} }

View File

@ -12,7 +12,7 @@ import com.youlai.common.core.result.ResultCode;
import com.youlai.common.web.exception.BizException; import com.youlai.common.web.exception.BizException;
import com.youlai.mall.ums.api.MemberFeignService; import com.youlai.mall.ums.api.MemberFeignService;
import com.youlai.mall.ums.pojo.UmsMember; import com.youlai.mall.ums.pojo.UmsMember;
import com.youlai.mall.ums.pojo.dto.MemberDTO; import com.youlai.mall.ums.pojo.dto.AuthMemberDTO;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
@ -80,9 +80,6 @@ public class AuthController {
private WxMaService wxService; private WxMaService wxService;
private MemberFeignService memberFeignService; private MemberFeignService memberFeignService;
private PasswordEncoder passwordEncoder; private PasswordEncoder passwordEncoder;
@ -104,7 +101,7 @@ public class AuthController {
String openid = session.getOpenid(); String openid = session.getOpenid();
String sessionKey = session.getSessionKey(); String sessionKey = session.getSessionKey();
Result<MemberDTO> result = memberFeignService.loadMemberByOpenid(openid, 1); Result<AuthMemberDTO> result = memberFeignService.getMemberByOpenid(openid);
String username; String username;
if (ResultCode.USER_NOT_EXIST.getCode().equals(result.getCode())) { // 微信授权登录 会员信息不存在时 注册会员 if (ResultCode.USER_NOT_EXIST.getCode().equals(result.getCode())) { // 微信授权登录 会员信息不存在时 注册会员
@ -130,8 +127,8 @@ public class AuthController {
} }
username = openid; username = openid;
} else { } else {
MemberDTO memberDTO = result.getData(); AuthMemberDTO authMemberDTO = result.getData();
username = memberDTO.getUsername(); username = authMemberDTO.getUsername();
} }
// oauth2认证参数对应授权登录时注册会员的usernamepassword信息模拟通过oauth2的密码模式认证 // oauth2认证参数对应授权登录时注册会员的usernamepassword信息模拟通过oauth2的密码模式认证

View File

@ -1,8 +1,8 @@
package com.youlai.auth.domain; package com.youlai.auth.domain;
import com.youlai.admin.dto.UserDTO; import com.youlai.admin.pojo.dto.UserDTO;
import com.youlai.common.core.constant.AuthConstants; import com.youlai.common.core.constant.AuthConstants;
import com.youlai.mall.ums.pojo.dto.MemberDTO; import com.youlai.mall.ums.pojo.dto.AuthMemberDTO;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
@ -44,7 +44,7 @@ public class User implements UserDetails {
} }
} }
public User(MemberDTO member){ public User(AuthMemberDTO member){
this.setId(member.getId()); this.setId(member.getId());
this.setUsername(member.getUsername()); this.setUsername(member.getUsername());
this.setPassword(AuthConstants.BCRYPT + member.getPassword()); this.setPassword(AuthConstants.BCRYPT + member.getPassword());

View File

@ -1,12 +1,12 @@
package com.youlai.auth.service; package com.youlai.auth.service;
import com.youlai.admin.dto.UserDTO; import com.youlai.admin.pojo.dto.UserDTO;
import com.youlai.admin.api.UserFeignService; import com.youlai.admin.api.UserFeignService;
import com.youlai.auth.domain.User; import com.youlai.auth.domain.User;
import com.youlai.common.core.constant.AuthConstants; import com.youlai.common.core.constant.AuthConstants;
import com.youlai.common.core.result.Result; import com.youlai.common.core.result.Result;
import com.youlai.common.core.result.ResultCode; import com.youlai.common.core.result.ResultCode;
import com.youlai.mall.ums.pojo.dto.MemberDTO; import com.youlai.mall.ums.pojo.dto.AuthMemberDTO;
import com.youlai.mall.ums.api.MemberFeignService; import com.youlai.mall.ums.api.MemberFeignService;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springframework.security.authentication.AccountExpiredException; import org.springframework.security.authentication.AccountExpiredException;
@ -16,7 +16,6 @@ import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -39,7 +38,7 @@ public class UserDetailsServiceImpl implements UserDetailsService {
User user = null; User user = null;
switch (clientId) { switch (clientId) {
case AuthConstants.ADMIN_CLIENT_ID: // 后台用户 case AuthConstants.ADMIN_CLIENT_ID: // 后台用户
Result<UserDTO> userRes = userFeignService.loadUserByUsername(username, 2); Result<UserDTO> userRes = userFeignService.getUserByUsername(username);
if (ResultCode.USER_NOT_EXIST.getCode().equals(userRes.getCode())) { if (ResultCode.USER_NOT_EXIST.getCode().equals(userRes.getCode())) {
throw new UsernameNotFoundException(ResultCode.USER_NOT_EXIST.getMsg()); throw new UsernameNotFoundException(ResultCode.USER_NOT_EXIST.getMsg());
} }
@ -48,13 +47,13 @@ public class UserDetailsServiceImpl implements UserDetailsService {
user = new User(userDTO); user = new User(userDTO);
break; break;
case AuthConstants.WEAPP_CLIENT_ID: // 小程序会员 case AuthConstants.WEAPP_CLIENT_ID: // 小程序会员
Result<MemberDTO> memberRes = memberFeignService.loadMemberByOpenid(username, 1); Result<AuthMemberDTO> memberRes = memberFeignService.getMemberByOpenid(username);
if (ResultCode.USER_NOT_EXIST.getCode().equals(memberRes.getCode())) { if (ResultCode.USER_NOT_EXIST.getCode().equals(memberRes.getCode())) {
throw new UsernameNotFoundException(ResultCode.USER_NOT_EXIST.getMsg()); throw new UsernameNotFoundException(ResultCode.USER_NOT_EXIST.getMsg());
} }
MemberDTO memberDTO = memberRes.getData(); AuthMemberDTO authMemberDTO = memberRes.getData();
memberDTO.setClientId(clientId); authMemberDTO.setClientId(clientId);
user = new User(memberDTO); user = new User(authMemberDTO);
break; break;
} }
if (!user.isEnabled()) { if (!user.isEnabled()) {

View File

@ -2,15 +2,8 @@ package com.youlai.common.core.constant;
public interface AuthConstants { public interface AuthConstants {
/**
* JWT存储权限前缀
*/
String AUTHORITY_PREFIX = "ROLE_";
/**
* JWT存储权限属性
*/
String AUTHORITY_CLAIM_NAME = "authorities";
/** /**
* 认证信息Http请求头 * 认证信息Http请求头
@ -56,6 +49,17 @@ public interface AuthConstants {
String JWT_CLIENT_ID_KEY = "client_id"; String JWT_CLIENT_ID_KEY = "client_id";
/**
* JWT存储权限前缀
*/
String AUTHORITY_PREFIX = "ROLE_";
/**
* JWT存储权限属性
*/
String JWT_AUTHORITIES_KEY = "authorities";
/** /**
* 有来商城后台管理客户端ID * 有来商城后台管理客户端ID
*/ */

View File

@ -6,6 +6,5 @@ public interface SystemConstants {
Integer VISIBLE_SHOW_VALUE = 1; Integer VISIBLE_SHOW_VALUE = 1;
String SYSTEM_TYPE_KEY = "SystemType";
} }

View File

@ -9,6 +9,8 @@ public enum QueryModeEnum {
LIST("list","列表查询"), LIST("list","列表查询"),
TREE("tree","树形列表"), TREE("tree","树形列表"),
CASCADER("cascader","级联列表"), // 对应级联选择器的下拉格式数据 CASCADER("cascader","级联列表"), // 对应级联选择器的下拉格式数据
ROUTER("router","路由列表"),
TREESELECT("treeselect","树形下拉列表")
; ;
@Getter @Getter

View File

@ -1,15 +1,15 @@
package com.youlai.common.web.util; package com.youlai.common.web.util;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.youlai.common.core.constant.AuthConstants; import com.youlai.common.core.constant.AuthConstants;
import com.youlai.common.core.constant.SystemConstants;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j @Slf4j
public class WebUtils extends org.springframework.web.util.WebUtils { public class WebUtils extends org.springframework.web.util.WebUtils {
@ -36,12 +36,9 @@ public class WebUtils extends org.springframework.web.util.WebUtils {
return clientId; return clientId;
} }
public static Integer getSystemType() { public static List<Long> getAuthorities() {
String systemType = getHttpServletRequest().getHeader(SystemConstants.SYSTEM_TYPE_KEY); List<String> list = getJwtPayload().get(AuthConstants.JWT_AUTHORITIES_KEY, List.class);
if (StrUtil.isNotBlank(systemType)) { List<Long> authorities = list.stream().map(Long::valueOf).collect(Collectors.toList());
return Integer.valueOf(systemType); return authorities;
}
return null;
} }
} }

View File

@ -116,7 +116,7 @@ public class ResourceServerConfig {
public Converter<Jwt, ? extends Mono<? extends AbstractAuthenticationToken>> jwtAuthenticationConverter() { public Converter<Jwt, ? extends Mono<? extends AbstractAuthenticationToken>> jwtAuthenticationConverter() {
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter(); JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
jwtGrantedAuthoritiesConverter.setAuthorityPrefix(AuthConstants.AUTHORITY_PREFIX); jwtGrantedAuthoritiesConverter.setAuthorityPrefix(AuthConstants.AUTHORITY_PREFIX);
jwtGrantedAuthoritiesConverter.setAuthoritiesClaimName(AuthConstants.AUTHORITY_CLAIM_NAME); jwtGrantedAuthoritiesConverter.setAuthoritiesClaimName(AuthConstants.JWT_AUTHORITIES_KEY);
JwtAuthenticationConverter jwtAuthenticationConverter = new JwtAuthenticationConverter(); JwtAuthenticationConverter jwtAuthenticationConverter = new JwtAuthenticationConverter();
jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(jwtGrantedAuthoritiesConverter); jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(jwtGrantedAuthoritiesConverter);