mirror of
https://gitee.com/youlaitech/youlai-mall.git
synced 2024-12-23 13:03:43 +08:00
feat:添加Sentinel自定义异常
This commit is contained in:
parent
0811c7e8c3
commit
29c1a06ec2
@ -9,7 +9,6 @@ public class AuthMemberDTO {
|
|||||||
private String username;
|
private String username;
|
||||||
private String password;
|
private String password;
|
||||||
private Integer status;
|
private Integer status;
|
||||||
private String clientId;
|
|
||||||
|
|
||||||
private String avatar;
|
private String avatar;
|
||||||
private String nickname;
|
private String nickname;
|
||||||
|
@ -13,7 +13,6 @@ public class UserDTO {
|
|||||||
private String username;
|
private String username;
|
||||||
private String password;
|
private String password;
|
||||||
private Integer status;
|
private Integer status;
|
||||||
private String clientId;
|
|
||||||
private List<Long> roleIds;
|
private List<Long> roleIds;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -7,12 +7,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import com.youlai.common.result.Result;
|
import com.youlai.common.result.Result;
|
||||||
import com.youlai.common.result.ResultCode;
|
import com.youlai.common.result.ResultCode;
|
||||||
import jodd.net.HttpStatus;
|
import jodd.net.HttpStatus;
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
import org.springframework.http.MediaType;
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
@ -27,8 +23,7 @@ public class CustomBlockExceptionHandler implements BlockExceptionHandler {
|
|||||||
@Override
|
@Override
|
||||||
public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {
|
public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {
|
||||||
response.setStatus(HttpStatus.ok().status());
|
response.setStatus(HttpStatus.ok().status());
|
||||||
response.setCharacterEncoding("utf-8");
|
response.setCharacterEncoding("UTF-8");
|
||||||
response.setHeader("Content-Type", "application/json;charset=utf-8");
|
|
||||||
response.setContentType("application/json;charset=utf-8");
|
response.setContentType("application/json;charset=utf-8");
|
||||||
|
|
||||||
if(e instanceof FlowException){
|
if(e instanceof FlowException){
|
||||||
|
@ -38,7 +38,6 @@ public class User implements UserDetails {
|
|||||||
this.setUsername(user.getUsername());
|
this.setUsername(user.getUsername());
|
||||||
this.setPassword(AuthConstants.BCRYPT + user.getPassword());
|
this.setPassword(AuthConstants.BCRYPT + user.getPassword());
|
||||||
this.setEnabled(Integer.valueOf(1).equals(user.getStatus()));
|
this.setEnabled(Integer.valueOf(1).equals(user.getStatus()));
|
||||||
this.setClientId(user.getClientId());
|
|
||||||
if (CollectionUtil.isNotEmpty(user.getRoleIds())) {
|
if (CollectionUtil.isNotEmpty(user.getRoleIds())) {
|
||||||
authorities = new ArrayList<>();
|
authorities = new ArrayList<>();
|
||||||
user.getRoleIds().forEach(roleId -> authorities.add(new SimpleGrantedAuthority(String.valueOf(roleId))));
|
user.getRoleIds().forEach(roleId -> authorities.add(new SimpleGrantedAuthority(String.valueOf(roleId))));
|
||||||
@ -50,7 +49,6 @@ public class User implements UserDetails {
|
|||||||
this.setUsername(member.getUsername());
|
this.setUsername(member.getUsername());
|
||||||
this.setPassword(AuthConstants.BCRYPT + member.getPassword());
|
this.setPassword(AuthConstants.BCRYPT + member.getPassword());
|
||||||
this.setEnabled( Integer.valueOf(1).equals(member.getStatus()));
|
this.setEnabled( Integer.valueOf(1).equals(member.getStatus()));
|
||||||
this.setClientId(member.getClientId());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ import com.youlai.auth.domain.User;
|
|||||||
import com.youlai.common.constant.AuthConstants;
|
import com.youlai.common.constant.AuthConstants;
|
||||||
import com.youlai.common.result.Result;
|
import com.youlai.common.result.Result;
|
||||||
import com.youlai.common.result.ResultCode;
|
import com.youlai.common.result.ResultCode;
|
||||||
|
import com.youlai.common.web.exception.BizException;
|
||||||
import com.youlai.common.web.util.RequestUtils;
|
import com.youlai.common.web.util.RequestUtils;
|
||||||
import com.youlai.mall.ums.pojo.dto.AuthMemberDTO;
|
import com.youlai.mall.ums.pojo.dto.AuthMemberDTO;
|
||||||
import com.youlai.mall.ums.api.UmsMemberFeignService;
|
import com.youlai.mall.ums.api.UmsMemberFeignService;
|
||||||
@ -34,26 +35,30 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
|||||||
String clientId = RequestUtils.getAuthClientId();
|
String clientId = RequestUtils.getAuthClientId();
|
||||||
|
|
||||||
User user = null;
|
User user = null;
|
||||||
|
Result result;
|
||||||
switch (clientId) {
|
switch (clientId) {
|
||||||
case AuthConstants.ADMIN_CLIENT_ID: // 后台用户
|
case AuthConstants.ADMIN_CLIENT_ID: // 后台用户
|
||||||
Result<UserDTO> userRes = userFeignService.getUserByUsername(username);
|
result = userFeignService.getUserByUsername(username);
|
||||||
if (ResultCode.USER_NOT_EXIST.getCode().equals(userRes.getCode())) {
|
if (ResultCode.SUCCESS.getCode().equals(result.getCode())) {
|
||||||
throw new UsernameNotFoundException(ResultCode.USER_NOT_EXIST.getMsg());
|
UserDTO userDTO = (UserDTO) result.getData();
|
||||||
}
|
|
||||||
UserDTO userDTO = userRes.getData();
|
|
||||||
userDTO.setClientId(clientId);
|
|
||||||
user = new User(userDTO);
|
user = new User(userDTO);
|
||||||
|
} else {
|
||||||
|
throw new BizException(ResultCode.getValue(result.getCode()));
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case AuthConstants.WEAPP_CLIENT_ID: // 小程序会员
|
case AuthConstants.WEAPP_CLIENT_ID: // 小程序会员
|
||||||
Result<AuthMemberDTO> memberRes = memberFeignService.getUserByOpenid(username);
|
result = memberFeignService.getUserByOpenid(username);
|
||||||
if (ResultCode.USER_NOT_EXIST.getCode().equals(memberRes.getCode())) {
|
if (ResultCode.SUCCESS.getCode().equals(result.getCode())) {
|
||||||
throw new UsernameNotFoundException(ResultCode.USER_NOT_EXIST.getMsg());
|
AuthMemberDTO authMemberDTO = (AuthMemberDTO) result.getData();
|
||||||
}
|
|
||||||
AuthMemberDTO authMemberDTO = memberRes.getData();
|
|
||||||
authMemberDTO.setClientId(clientId);
|
|
||||||
user = new User(authMemberDTO);
|
user = new User(authMemberDTO);
|
||||||
|
} else {
|
||||||
|
throw new BizException(ResultCode.getValue(result.getCode()));
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (user == null) {
|
||||||
|
throw new UsernameNotFoundException(ResultCode.USER_NOT_EXIST.getMsg());
|
||||||
|
}
|
||||||
if (!user.isEnabled()) {
|
if (!user.isEnabled()) {
|
||||||
throw new DisabledException("该账户已被禁用!");
|
throw new DisabledException("该账户已被禁用!");
|
||||||
} else if (!user.isAccountNonLocked()) {
|
} else if (!user.isAccountNonLocked()) {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.youlai.common.result;
|
package com.youlai.common.result;
|
||||||
|
|
||||||
|
import com.youlai.common.enums.QueryModeEnum;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
@ -93,4 +94,14 @@ public enum ResultCode implements IResultCode, Serializable {
|
|||||||
", \"msg\":\"" + msg + '\"' +
|
", \"msg\":\"" + msg + '\"' +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static ResultCode getValue(String code){
|
||||||
|
for (ResultCode value : values()) {
|
||||||
|
if (value.getCode().equals(code)) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return SYSTEM_EXECUTION_ERROR; // 默认分页查询
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|||||||
* @author hxrui
|
* @author hxrui
|
||||||
* @date 2020-02-25 13:54
|
* @date 2020-02-25 13:54
|
||||||
**/
|
**/
|
||||||
@RestControllerAdvice
|
//@RestControllerAdvice
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class GlobalExceptionHandler {
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
|
@ -113,13 +113,13 @@
|
|||||||
<version>1.0.0</version>
|
<version>1.0.0</version>
|
||||||
<executions>
|
<executions>
|
||||||
<!--执行mvn package,即执行 mvn clean package docker:build-->
|
<!--执行mvn package,即执行 mvn clean package docker:build-->
|
||||||
<execution>
|
<!--<execution>
|
||||||
<id>build-image</id>
|
<id>build-image</id>
|
||||||
<phase>package</phase>
|
<phase>package</phase>
|
||||||
<goals>
|
<goals>
|
||||||
<goal>build</goal>
|
<goal>build</goal>
|
||||||
</goals>
|
</goals>
|
||||||
</execution>
|
</execution>-->
|
||||||
</executions>
|
</executions>
|
||||||
|
|
||||||
<configuration>
|
<configuration>
|
||||||
|
Loading…
Reference in New Issue
Block a user