feat:添加Sentinel自定义异常

This commit is contained in:
haoxr 2021-04-18 23:44:12 +08:00
parent 0811c7e8c3
commit 29c1a06ec2
8 changed files with 32 additions and 25 deletions

View File

@ -9,7 +9,6 @@ public class AuthMemberDTO {
private String username;
private String password;
private Integer status;
private String clientId;
private String avatar;
private String nickname;

View File

@ -13,7 +13,6 @@ public class UserDTO {
private String username;
private String password;
private Integer status;
private String clientId;
private List<Long> roleIds;
}

View File

@ -7,12 +7,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.youlai.common.result.Result;
import com.youlai.common.result.ResultCode;
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 javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ -27,8 +23,7 @@ public class CustomBlockExceptionHandler implements BlockExceptionHandler {
@Override
public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {
response.setStatus(HttpStatus.ok().status());
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Type", "application/json;charset=utf-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json;charset=utf-8");
if(e instanceof FlowException){

View File

@ -38,7 +38,6 @@ public class User implements UserDetails {
this.setUsername(user.getUsername());
this.setPassword(AuthConstants.BCRYPT + user.getPassword());
this.setEnabled(Integer.valueOf(1).equals(user.getStatus()));
this.setClientId(user.getClientId());
if (CollectionUtil.isNotEmpty(user.getRoleIds())) {
authorities = new ArrayList<>();
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.setPassword(AuthConstants.BCRYPT + member.getPassword());
this.setEnabled( Integer.valueOf(1).equals(member.getStatus()));
this.setClientId(member.getClientId());
}

View File

@ -6,6 +6,7 @@ import com.youlai.auth.domain.User;
import com.youlai.common.constant.AuthConstants;
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.pojo.dto.AuthMemberDTO;
import com.youlai.mall.ums.api.UmsMemberFeignService;
@ -34,26 +35,30 @@ public class UserDetailsServiceImpl implements UserDetailsService {
String clientId = RequestUtils.getAuthClientId();
User user = null;
Result result;
switch (clientId) {
case AuthConstants.ADMIN_CLIENT_ID: // 后台用户
Result<UserDTO> userRes = userFeignService.getUserByUsername(username);
if (ResultCode.USER_NOT_EXIST.getCode().equals(userRes.getCode())) {
throw new UsernameNotFoundException(ResultCode.USER_NOT_EXIST.getMsg());
}
UserDTO userDTO = userRes.getData();
userDTO.setClientId(clientId);
result = userFeignService.getUserByUsername(username);
if (ResultCode.SUCCESS.getCode().equals(result.getCode())) {
UserDTO userDTO = (UserDTO) result.getData();
user = new User(userDTO);
} else {
throw new BizException(ResultCode.getValue(result.getCode()));
}
break;
case AuthConstants.WEAPP_CLIENT_ID: // 小程序会员
Result<AuthMemberDTO> memberRes = memberFeignService.getUserByOpenid(username);
if (ResultCode.USER_NOT_EXIST.getCode().equals(memberRes.getCode())) {
throw new UsernameNotFoundException(ResultCode.USER_NOT_EXIST.getMsg());
}
AuthMemberDTO authMemberDTO = memberRes.getData();
authMemberDTO.setClientId(clientId);
result = memberFeignService.getUserByOpenid(username);
if (ResultCode.SUCCESS.getCode().equals(result.getCode())) {
AuthMemberDTO authMemberDTO = (AuthMemberDTO) result.getData();
user = new User(authMemberDTO);
} else {
throw new BizException(ResultCode.getValue(result.getCode()));
}
break;
}
if (user == null) {
throw new UsernameNotFoundException(ResultCode.USER_NOT_EXIST.getMsg());
}
if (!user.isEnabled()) {
throw new DisabledException("该账户已被禁用!");
} else if (!user.isAccountNonLocked()) {

View File

@ -1,5 +1,6 @@
package com.youlai.common.result;
import com.youlai.common.enums.QueryModeEnum;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
@ -93,4 +94,14 @@ public enum ResultCode implements IResultCode, Serializable {
", \"msg\":\"" + msg + '\"' +
'}';
}
public static ResultCode getValue(String code){
for (ResultCode value : values()) {
if (value.getCode().equals(code)) {
return value;
}
}
return SYSTEM_EXECUTION_ERROR; // 默认分页查询
}
}

View File

@ -12,7 +12,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
* @author hxrui
* @date 2020-02-25 13:54
**/
@RestControllerAdvice
//@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

View File

@ -113,13 +113,13 @@
<version>1.0.0</version>
<executions>
<!--执行mvn package,即执行 mvn clean package docker:build-->
<execution>
<!--<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</execution>-->
</executions>
<configuration>