新增 user 模块
调整新的项目结构,将 service 和 dao 模块,从 application 拆除,独立成 service-impl 模块 增加 sdk 模块,用于提供一个封装过的功能。例如说,认证和授权~
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.mall.user;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication(scanBasePackages = "cn.iocoder.mall.user")
|
||||
public class UserApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(UserApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package cn.iocoder.mall.user.config;
|
||||
|
||||
import cn.iocoder.common.framework.config.GlobalExceptionHandler;
|
||||
import cn.iocoder.common.framework.config.GlobalResponseBodyAdvice;
|
||||
import cn.iocoder.mall.user.sdk.interceptor.SecurityInterceptor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@EnableWebMvc
|
||||
@Configuration
|
||||
@Import(value = {GlobalResponseBodyAdvice.class, GlobalExceptionHandler.class, // 统一全局返回
|
||||
SecurityInterceptor.class}) // 安全拦截器,实现认证和授权功能。
|
||||
public class MVCConfiguration implements WebMvcConfigurer {
|
||||
|
||||
@Autowired
|
||||
private SecurityInterceptor securityInterceptor;
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(securityInterceptor);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package cn.iocoder.mall.user.config;
|
||||
|
||||
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
|
||||
import cn.iocoder.mall.user.service.api.constant.UserErrorCodeEnum;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.event.EventListener;
|
||||
|
||||
@Configuration
|
||||
public class ServiceExceptionConfiguration {
|
||||
|
||||
@EventListener(ApplicationReadyEvent.class) // 可参考 https://www.cnblogs.com/ssslinppp/p/7607509.html
|
||||
public void initMessages() {
|
||||
// 从 service_exception_message.properties 加载错误码的方案
|
||||
// Properties properties;
|
||||
// try {
|
||||
// properties = PropertiesLoaderUtils.loadAllProperties("classpath:service_exception_message.properties");
|
||||
// } catch (IOException e) {
|
||||
// throw new RuntimeException(e);
|
||||
// }
|
||||
for (UserErrorCodeEnum item : UserErrorCodeEnum.values()) {
|
||||
ServiceExceptionUtil.put(item.getCode(), item.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package cn.iocoder.mall.user.controller;
|
||||
|
||||
import cn.iocoder.common.framework.exception.ServiceException;
|
||||
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.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;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/passport")
|
||||
public class PassportController {
|
||||
|
||||
@Reference
|
||||
private OAuth2Service oauth2Service;
|
||||
@Reference
|
||||
private UserService userService;
|
||||
@Reference
|
||||
private MobileCodeService mobileCodeService;
|
||||
|
||||
// TODO 功能:手机密码登陆
|
||||
// @PostMapping("/mobile/pwd/login")
|
||||
// public OAuth2AccessToken mobileLogin(@RequestParam("mobile") String mobile,
|
||||
// @RequestParam("password") String password) {
|
||||
// return oauth2Service.getAccessToken(clientId, clientSecret, mobile, password);
|
||||
// }
|
||||
|
||||
/**
|
||||
* 手机号 + 验证码登陆
|
||||
*
|
||||
* @param mobile 手机号
|
||||
* @param code 验证码
|
||||
* @return 授权信息
|
||||
*/
|
||||
@PermitAll
|
||||
@PostMapping("/mobile/login")
|
||||
public OAuth2AccessTokenBO mobileRegister(@RequestParam("mobile") String mobile,
|
||||
@RequestParam("code") String code) {
|
||||
// 尝试直接授权
|
||||
OAuth2AccessTokenBO accessTokenDTO;
|
||||
try {
|
||||
accessTokenDTO = oauth2Service.getAccessToken(mobile, code);
|
||||
return accessTokenDTO;
|
||||
} catch (ServiceException serviceException) {
|
||||
if (!serviceException.getCode().equals(UserErrorCodeEnum.USER_MOBILE_NOT_REGISTERED.getCode())) { // 如果是未注册异常,忽略。下面发起自动注册逻辑。
|
||||
throw serviceException;
|
||||
}
|
||||
}
|
||||
// 上面尝试授权失败,说明用户未注册,发起自动注册。
|
||||
try {
|
||||
userService.createUser(mobile, code);
|
||||
} catch (ServiceException serviceException) {
|
||||
if (!serviceException.getCode().equals(UserErrorCodeEnum.USER_MOBILE_ALREADY_REGISTERED.getCode())) { // 如果是已注册异常,忽略。下面再次发起授权
|
||||
throw serviceException;
|
||||
}
|
||||
}
|
||||
// 再次发起授权
|
||||
accessTokenDTO = oauth2Service.getAccessToken(mobile, code);
|
||||
return accessTokenDTO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送手机验证码
|
||||
*
|
||||
* @param mobile 手机号
|
||||
*/
|
||||
@PostMapping("mobile/send")
|
||||
public void mobileSend(@RequestParam("mobile") String mobile) {
|
||||
mobileCodeService.send(mobile);
|
||||
}
|
||||
|
||||
// TODO 功能:qq 登陆
|
||||
@PermitAll
|
||||
@PostMapping("/qq/login")
|
||||
public String qqLogin() {
|
||||
return null;
|
||||
}
|
||||
|
||||
// TODO 功能:qq 绑定
|
||||
@PermitAll
|
||||
@PostMapping("/qq/bind")
|
||||
public String qqBind() {
|
||||
return null;
|
||||
}
|
||||
|
||||
// TODO 功能:刷新 token
|
||||
|
||||
// TODO 功能:退出,销毁 token
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package cn.iocoder.mall.user.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
//@RestController
|
||||
//@RequestMapping("/user")
|
||||
public class UserController {
|
||||
|
||||
@GetMapping("/info")
|
||||
public Long info() {
|
||||
// TODO 芋艿,正在实现中
|
||||
// return SecurityContextHolder.getContext().getUid();
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
spring:
|
||||
application:
|
||||
name: user-application
|
||||
|
||||
# server
|
||||
server:
|
||||
port: 8082
|
||||
Reference in New Issue
Block a user