Skip to content

Commit

Permalink
新增 CommonResult 。
Browse files Browse the repository at this point in the history
将使用手机号进行注册登陆的逻辑,进行变更~
  • Loading branch information
YunaiV committed Feb 25, 2019
1 parent 4ae211d commit 4162eda
Show file tree
Hide file tree
Showing 27 changed files with 351 additions and 85 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*.iws
*.iml
*.ipr
target/*

### NetBeans ###
/nbproject/private/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import cn.iocoder.common.framework.constant.SysErrorCodeEnum;
import cn.iocoder.common.framework.exception.ServiceException;
import cn.iocoder.common.framework.util.ExceptionUtil;
import cn.iocoder.common.framework.vo.RestResult;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.UndeclaredThrowableException;

@ControllerAdvice
Expand All @@ -21,19 +21,13 @@ public RestResult serviceExceptionHandler(HttpServletRequest req, ServiceExcepti
return RestResult.error(ex.getCode(), ex.getMessage());
}

// 处理 Spring 动态代理调用时,发生 UndeclaredThrowableException 的情况。
// 不了解的胖友,可以看看 https://segmentfault.com/a/1190000012262244 文章
@ResponseBody
@ExceptionHandler(value = UndeclaredThrowableException.class)
public RestResult undeclaredThrowableExceptionHandler(HttpServletRequest req, UndeclaredThrowableException e) {
// 尝试获得 ServiceException 异常。如果是,则使用 serviceExceptionHandler 方法处理。
Throwable undeclaredThrowable = e.getUndeclaredThrowable();
if (undeclaredThrowable instanceof InvocationTargetException) {
InvocationTargetException invocationTargetException = (InvocationTargetException) undeclaredThrowable;
Throwable targetException = invocationTargetException.getTargetException();
if (targetException != null & targetException instanceof ServiceException) {
return serviceExceptionHandler(req, (ServiceException) targetException);
}
ServiceException serviceException = ExceptionUtil.getServiceException(e);
if (serviceException != null) {
return serviceExceptionHandler(req, serviceException);
}
// 获得不到,使用 异常日志 方法处理。
return resultExceptionHandler(req, e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.iocoder.common.framework.config;

import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.common.framework.vo.RestResult;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
Expand All @@ -21,6 +22,9 @@ public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType
if (body instanceof RestResult) {
return body;
}
if (body instanceof CommonResult) { // TODO 芋艿,后续要改下
return body;
}
return RestResult.ok(body);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cn.iocoder.common.framework.util;

import cn.iocoder.common.framework.exception.ServiceException;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.UndeclaredThrowableException;

public class ExceptionUtil {

public static ServiceException getServiceException(Exception e) {
if (e instanceof UndeclaredThrowableException) {
return getServiceException((UndeclaredThrowableException) e);
}
return null;
}

// 处理 Spring 动态代理调用时,发生 UndeclaredThrowableException 的情况。
// 不了解的胖友,可以先看看 https://segmentfault.com/a/1190000012262244 文章
// 原因是:
// 1. Dubbo 动态代理 Wrapper 会将抛出的异常,包装成 InvocationTargetException 异常
// 2. Spring AOP 发现是 InvocationTargetException 异常是非方法定义的异常,则会包装成 UndeclaredThrowableException 异常。
public static ServiceException getServiceException(UndeclaredThrowableException e) {
Throwable undeclaredThrowable = e.getUndeclaredThrowable();
if (undeclaredThrowable instanceof InvocationTargetException) {
InvocationTargetException invocationTargetException = (InvocationTargetException) undeclaredThrowable;
Throwable targetException = invocationTargetException.getTargetException();
if (targetException != null & targetException instanceof ServiceException) {
return (ServiceException) targetException;
}
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.iocoder.common.framework.util;

import cn.iocoder.common.framework.exception.ServiceException;
import cn.iocoder.common.framework.vo.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -38,6 +39,16 @@ public static void put(Integer code, String message) {
ServiceExceptionUtil.messages.put(code, message);
}

// TODO 芋艿,可能不是目前最优解,目前暂时这样
public static <T> CommonResult<T> error(Integer code) {
return CommonResult.error(code, messages.get(code));
}

public static CommonResult error(Integer code, Object... params) {
String message = doFormat(code, messages.get(code), params);
return CommonResult.error(code, message);
}

/**
* 创建指定编号的 ServiceException 的异常
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package cn.iocoder.common.framework.vo;

import org.springframework.util.Assert;

public class CommonResult<T> {

public static Integer CODE_SUCCESS = 0;

/**
* 错误码
*/
private Integer code;
/**
* 错误提示
*/
private String message;
/**
* 返回数据
*/
private T data;

/**
* 将传入的 result 对象,转换成另外一个泛型结果的对象
*
* 因为 A 方法返回的 CommonResult 对象,不满足调用其的 B 方法的返回,所以需要进行转换。
*
* @param result 传入的 result 对象
* @param <T> 返回的泛型
* @return 新的 CommonResult 对象
*/
public static <T> CommonResult<T> error(CommonResult<?> result) {
return error(result.getCode(), result.getMessage());
}

public static <T> CommonResult<T> error(Integer code, String message) {
Assert.isTrue(!CODE_SUCCESS.equals(code), "code 必须是错误的!");
CommonResult<T> result = new CommonResult<>();
result.code = code;
result.message = message;
return result;
}

public static <T> CommonResult<T> success(T data) {
CommonResult<T> result = new CommonResult<>();
result.code = CODE_SUCCESS;
result.data = data;
result.message = "";
return result;
}

public Integer getCode() {
return code;
}

public void setCode(Integer code) {
this.code = code;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public T getData() {
return data;
}

public void setData(T data) {
this.data = data;
}

public boolean isSuccess() {
return CODE_SUCCESS.equals(code);
}

public boolean isError() {
return !isSuccess();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ public Object getData() {
public void setData(Object data) {
this.data = data;
}

}
6 changes: 6 additions & 0 deletions user/user-application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId> <!-- use mapstruct-jdk8 for Java 8 or higher -->
<version>${org.mapstruct.version}</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package cn.iocoder.mall.user.controller;

import cn.iocoder.common.framework.exception.ServiceException;
import cn.iocoder.common.framework.util.ExceptionUtil;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.user.sdk.annotation.PermitAll;
import cn.iocoder.mall.user.service.api.MobileCodeService;
import cn.iocoder.mall.user.service.api.OAuth2Service;
import cn.iocoder.mall.user.service.api.UserService;
import cn.iocoder.mall.user.service.api.bo.OAuth2AccessTokenBO;
import cn.iocoder.mall.user.service.api.constant.UserErrorCodeEnum;
import cn.iocoder.mall.user.service.api.dto.OAuth2AccessTokenBO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -35,7 +37,7 @@ public class PassportController {
* 手机号 + 验证码登陆
*
* @param mobile 手机号
* @param code 验证码
* @param code 验证码
* @return 授权信息
*/
@PermitAll
Expand All @@ -47,15 +49,23 @@ public OAuth2AccessTokenBO mobileRegister(@RequestParam("mobile") String mobile,
try {
accessTokenDTO = oauth2Service.getAccessToken(mobile, code);
return accessTokenDTO;
} catch (ServiceException serviceException) {
} catch (Exception ex) {
ServiceException serviceException = ExceptionUtil.getServiceException(ex);
if (serviceException == null) {
throw ex;
}
if (!serviceException.getCode().equals(UserErrorCodeEnum.USER_MOBILE_NOT_REGISTERED.getCode())) { // 如果是未注册异常,忽略。下面发起自动注册逻辑。
throw serviceException;
}
}
// 上面尝试授权失败,说明用户未注册,发起自动注册。
try {
userService.createUser(mobile, code);
} catch (ServiceException serviceException) {
} catch (Exception ex) {
ServiceException serviceException = ExceptionUtil.getServiceException(ex);
if (serviceException == null) {
throw ex;
}
if (!serviceException.getCode().equals(UserErrorCodeEnum.USER_MOBILE_ALREADY_REGISTERED.getCode())) { // 如果是已注册异常,忽略。下面再次发起授权
throw serviceException;
}
Expand All @@ -65,11 +75,26 @@ public OAuth2AccessTokenBO mobileRegister(@RequestParam("mobile") String mobile,
return accessTokenDTO;
}

/**
* 手机号 + 验证码登陆
*
* @param mobile 手机号
* @param code 验证码
* @return 授权信息
*/
@PermitAll
@PostMapping("/mobile/login2")
public CommonResult<OAuth2AccessTokenBO> mobileRegister2(@RequestParam("mobile") String mobile,
@RequestParam("code") String code) {
return oauth2Service.getAccessToken2(mobile, code);
}

/**
* 发送手机验证码
*
* @param mobile 手机号
*/
@PermitAll
@PostMapping("mobile/send")
public void mobileSend(@RequestParam("mobile") String mobile) {
mobileCodeService.send(mobile);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package cn.iocoder.mall.user.controller;

import cn.iocoder.mall.user.sdk.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//@RestController
//@RequestMapping("/user")
@RestController
@RequestMapping("/user")
public class UserController {

@GetMapping("/info")
public Long info() {
// TODO 芋艿,正在实现中
// return SecurityContextHolder.getContext().getUid();
return null;
return SecurityContextHolder.getContext().getUid();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import cn.iocoder.mall.user.sdk.context.SecurityContext;
import cn.iocoder.mall.user.sdk.context.SecurityContextHolder;
import cn.iocoder.mall.user.service.api.OAuth2Service;
import cn.iocoder.mall.user.service.api.dto.OAuth2AuthenticationDTO;
import cn.iocoder.mall.user.service.api.bo.OAuth2AuthenticationBO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
Expand All @@ -29,7 +29,7 @@ public class SecurityInterceptor extends HandlerInterceptorAdapter {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 校验访问令牌是否正确。若正确,返回授权信息
String accessToken = obtainAccess(request);
OAuth2AuthenticationDTO authentication = null;
OAuth2AuthenticationBO authentication = null;
if (accessToken != null) {
authentication = oauth2Service.checkToken(accessToken);
// 添加到 SecurityContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@


import cn.iocoder.common.framework.exception.ServiceException;
import cn.iocoder.mall.user.service.api.dto.OAuth2AccessTokenBO;
import cn.iocoder.mall.user.service.api.dto.OAuth2AuthenticationDTO;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.user.service.api.bo.OAuth2AccessTokenBO;
import cn.iocoder.mall.user.service.api.bo.OAuth2AuthenticationBO;

public interface OAuth2Service {

Expand All @@ -19,13 +20,15 @@ public interface OAuth2Service {
OAuth2AccessTokenBO getAccessToken(String mobile, String code)
throws ServiceException;

CommonResult<OAuth2AccessTokenBO> getAccessToken2(String mobile, String code);

/**
* 校验访问令牌,获取身份信息( 不包括 accessToken 等等 )
*
* @param accessToken 访问令牌
* @return 授权信息
*/
OAuth2AuthenticationDTO checkToken(String accessToken)
OAuth2AuthenticationBO checkToken(String accessToken)
throws ServiceException;

// @see 刷新 token
Expand Down
Loading

0 comments on commit 4162eda

Please sign in to comment.