| | |
| | | package org.dromara.web.controller; |
| | | |
| | | import cn.dev33.satoken.annotation.SaIgnore; |
| | | import cn.dev33.satoken.exception.NotLoginException; |
| | | import cn.hutool.core.codec.Base64; |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import jakarta.servlet.http.HttpServletRequest; |
| | |
| | | import org.dromara.common.core.domain.model.RegisterBody; |
| | | import org.dromara.common.core.domain.model.SocialLoginBody; |
| | | import org.dromara.common.core.utils.*; |
| | | import org.dromara.common.encrypt.annotation.ApiEncrypt; |
| | | import org.dromara.common.json.utils.JsonUtils; |
| | | import org.dromara.common.satoken.utils.LoginHelper; |
| | | import org.dromara.common.social.config.properties.SocialLoginConfigProperties; |
| | | import org.dromara.common.social.config.properties.SocialProperties; |
| | | import org.dromara.common.social.utils.SocialUtils; |
| | | import org.dromara.common.sse.dto.SseMessageDto; |
| | | import org.dromara.common.sse.utils.SseMessageUtils; |
| | | import org.dromara.common.tenant.helper.TenantHelper; |
| | | import org.dromara.system.domain.SysClient; |
| | | import org.dromara.system.domain.bo.SysTenantBo; |
| | | import org.dromara.system.domain.vo.SysClientVo; |
| | | import org.dromara.system.domain.vo.SysTenantVo; |
| | | import org.dromara.system.service.ISysClientService; |
| | | import org.dromara.system.service.ISysConfigService; |
| | |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import java.net.URL; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.concurrent.ScheduledExecutorService; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | /** |
| | | * 认证 |
| | |
| | | */ |
| | | @Slf4j |
| | | @SaIgnore |
| | | @Validated |
| | | @RequiredArgsConstructor |
| | | @RestController |
| | | @RequestMapping("/auth") |
| | |
| | | private final ISysTenantService tenantService; |
| | | private final ISysSocialService socialUserService; |
| | | private final ISysClientService clientService; |
| | | private final ScheduledExecutorService scheduledExecutorService; |
| | | |
| | | |
| | | /** |
| | |
| | | * @param body 登录信息 |
| | | * @return 结果 |
| | | */ |
| | | @ApiEncrypt |
| | | @PostMapping("/login") |
| | | public R<LoginVo> login(@Validated @RequestBody String body) { |
| | | public R<LoginVo> login(@RequestBody String body) { |
| | | LoginBody loginBody = JsonUtils.parseObject(body, LoginBody.class); |
| | | ValidatorUtils.validate(loginBody); |
| | | // 授权类型和客户端id |
| | | String clientId = loginBody.getClientId(); |
| | | String grantType = loginBody.getGrantType(); |
| | | SysClient client = clientService.queryByClientId(clientId); |
| | | SysClientVo client = clientService.queryByClientId(clientId); |
| | | // 查询不到 client 或 client 内不包含 grantType |
| | | if (ObjectUtil.isNull(client) || !StringUtils.contains(client.getGrantType(), grantType)) { |
| | | log.info("客户端id: {} 认证类型:{} 异常!.", clientId, grantType); |
| | |
| | | // 校验租户 |
| | | loginService.checkTenant(loginBody.getTenantId()); |
| | | // 登录 |
| | | return R.ok(IAuthStrategy.login(body, client, grantType)); |
| | | LoginVo loginVo = IAuthStrategy.login(body, client, grantType); |
| | | |
| | | Long userId = LoginHelper.getUserId(); |
| | | scheduledExecutorService.schedule(() -> { |
| | | SseMessageDto dto = new SseMessageDto(); |
| | | dto.setMessage("欢迎登录兰宝车间质量管理系统"); |
| | | dto.setUserIds(List.of(userId)); |
| | | SseMessageUtils.publishMessage(dto); |
| | | }, 5, TimeUnit.SECONDS); |
| | | return R.ok(loginVo); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return 结果 |
| | | */ |
| | | @GetMapping("/binding/{source}") |
| | | public R<String> authBinding(@PathVariable("source") String source) { |
| | | public R<String> authBinding(@PathVariable("source") String source, |
| | | @RequestParam String tenantId, @RequestParam String domain) { |
| | | SocialLoginConfigProperties obj = socialProperties.getType().get(source); |
| | | if (ObjectUtil.isNull(obj)) { |
| | | return R.fail(source + "平台账号暂不支持"); |
| | | } |
| | | AuthRequest authRequest = SocialUtils.getAuthRequest(source, socialProperties); |
| | | String authorizeUrl = authRequest.authorize(AuthStateUtils.createState()); |
| | | Map<String, String> map = new HashMap<>(); |
| | | map.put("tenantId", tenantId); |
| | | map.put("domain", domain); |
| | | map.put("state", AuthStateUtils.createState()); |
| | | String authorizeUrl = authRequest.authorize(Base64.encode(JsonUtils.toJsonString(map), StandardCharsets.UTF_8)); |
| | | return R.ok("操作成功", authorizeUrl); |
| | | } |
| | | |
| | |
| | | /** |
| | | * 用户注册 |
| | | */ |
| | | @ApiEncrypt |
| | | @PostMapping("/register") |
| | | public R<Void> register(@Validated @RequestBody RegisterBody user) { |
| | | if (!configService.selectRegisterEnabled(user.getTenantId())) { |
| | |
| | | */ |
| | | @GetMapping("/tenant/list") |
| | | public R<LoginTenantVo> tenantList(HttpServletRequest request) throws Exception { |
| | | // 返回对象 |
| | | LoginTenantVo result = new LoginTenantVo(); |
| | | boolean enable = TenantHelper.isEnable(); |
| | | result.setTenantEnabled(enable); |
| | | // 如果未开启租户这直接返回 |
| | | if (!enable) { |
| | | return R.ok(result); |
| | | } |
| | | |
| | | List<SysTenantVo> tenantList = tenantService.queryList(new SysTenantBo()); |
| | | List<TenantListVo> voList = MapstructUtils.convert(tenantList, TenantListVo.class); |
| | | try { |
| | | // 如果只超管返回所有租户 |
| | | if (LoginHelper.isSuperAdmin()) { |
| | | result.setVoList(voList); |
| | | return R.ok(result); |
| | | } |
| | | } catch (NotLoginException ignored) { |
| | | } |
| | | |
| | | // 获取域名 |
| | | String host; |
| | | String referer = request.getHeader("referer"); |
| | |
| | | } |
| | | // 根据域名进行筛选 |
| | | List<TenantListVo> list = StreamUtils.filter(voList, vo -> |
| | | StringUtils.equals(vo.getDomain(), host)); |
| | | // 返回对象 |
| | | LoginTenantVo vo = new LoginTenantVo(); |
| | | vo.setVoList(CollUtil.isNotEmpty(list) ? list : voList); |
| | | vo.setTenantEnabled(TenantHelper.isEnable()); |
| | | return R.ok(vo); |
| | | StringUtils.equals(vo.getDomain(), host)); |
| | | result.setVoList(CollUtil.isNotEmpty(list) ? list : voList); |
| | | return R.ok(result); |
| | | } |
| | | |
| | | } |