feat(auth): 添加 Keycloak 认证支持
- 新增 Keycloak 认证请求类和配置类
- 实现 Keycloak 用户登录和绑定逻辑
- 更新登录服务以支持第三方认证
- 优化登出处理,支持 Keycloak会话结束
| | |
| | | <scope>system</scope> |
| | | <systemPath>${basedir}/lib/fel.jar</systemPath> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>me.zhyd.oauth</groupId> |
| | | <artifactId>JustAuth</artifactId> |
| | | <version>1.16.7</version> |
| | | <scope>compile</scope> |
| | | </dependency> |
| | | </dependencies> |
| | | |
| | | <build> |
| | |
| | | .filter(f -> StringUtils.isBlank(f.getMeterId())) |
| | | .map(EnergyIndex::getIndexId) |
| | | .collect(Collectors.toList()); |
| | | /** |
| | | * å¤çè½æºææ å表并çæéè¦ç§»é¤çææ IDéå |
| | | * 1. è¿æ»¤åºmeterIdé空çè½æºææ 对象 |
| | | * 2. æåè¿äºå¯¹è±¡çindexIdåæ®µ |
| | | * 3. å°æåçææ IDæ¶éå°å符串éåä¸ |
| | | */ |
| | | List<String> removeLink = energyIndexList.stream() |
| | | .filter(f -> StringUtils.isNotBlank(f.getMeterId())) |
| | | .map(EnergyIndex::getIndexId) |
| | | .collect(Collectors.toList()); |
| | | |
| | | if (!removeLink.isEmpty()) { |
| | | energyIndexService.removeNodeIndex(nodeId, removeLink); |
| | | } |
| | |
| | | return AjaxResult.success(); |
| | | } |
| | | |
| | | /** |
| | | * æ°å¢éè¿idå é¤ééç¹æ¥å£ |
| | | */ |
| | | @PreAuthorize("@ss.hasPermi('energyindex:energyindex:remove')") |
| | | @Log(title = "ææ ä¿¡æ¯", businessType = BusinessType.DELETE) |
| | | @DeleteMapping("/{indexId}") |
| | | public AjaxResult deleteCollectIndex(@PathVariable String indexId) { |
| | | return toAjax(energyIndexService.deleteByIndexId(indexId)); |
| | | } |
| | | |
| | | @Log(title = "å¢å 计éå¨å
·ééç¹", businessType = BusinessType.INSERT) |
| | | @PostMapping("/meterIndex/{meterId}") |
| | | public AjaxResult addCollectIndex(@PathVariable("meterId") String meterId) { |
| | |
| | | package com.zhitan.web.controller.system; |
| | | |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import javax.annotation.Resource; |
| | | import org.springframework.web.bind.annotation.GetMapping; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RequestBody; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | |
| | | import cn.hutool.core.codec.Base64; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.zhitan.common.utils.SocialUtils; |
| | | import me.zhyd.oauth.request.AuthRequest; |
| | | import me.zhyd.oauth.utils.AuthStateUtils; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import com.zhitan.common.constant.Constants; |
| | | import com.zhitan.common.core.domain.AjaxResult; |
| | | import com.zhitan.common.core.domain.entity.SysMenu; |
| | |
| | | { |
| | | AjaxResult ajax = AjaxResult.success(); |
| | | // çæä»¤ç |
| | | String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(), |
| | | String token = ""; |
| | | if (loginBody.getGrantType() != null && !"".equals(loginBody.getGrantType())) { |
| | | token = loginService.loginByCode(loginBody.getSocialCode(), loginBody.getSocialState()); |
| | | } else { |
| | | token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(), |
| | | loginBody.getUuid()); |
| | | } |
| | | ajax.put(Constants.TOKEN, token); |
| | | return ajax; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * è·åç¨æ·ä¿¡æ¯ |
| | |
| | | List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId); |
| | | return AjaxResult.success(menuService.buildMenus(menus)); |
| | | } |
| | | /** |
| | | * è·å跳转URL |
| | | * |
| | | * @return ç»æ |
| | | */ |
| | | @GetMapping("/binding") |
| | | public AjaxResult authBinding() { |
| | | |
| | | AuthRequest authRequest = SocialUtils.getAuthKeyloakRequest(); |
| | | String authorizeUrl = authRequest.authorize(Base64.encode(AuthStateUtils.createState(), StandardCharsets.UTF_8)); |
| | | return AjaxResult.success("æä½æå", authorizeUrl); |
| | | } |
| | | |
| | | } |
| | |
| | | druid: |
| | | # ä¸»åºæ°æ®æº |
| | | master: |
| | | url: jdbc:postgresql://127.0.0.1:5432/energy |
| | | username: root |
| | | url: jdbc:postgresql://127.0.0.1:5432/postgres |
| | | username: postgres |
| | | password: 123456 |
| | | # ä»åºæ°æ®æº |
| | | slave: |
| | |
| | | wall: |
| | | config: |
| | | multi-statement-allow: true |
| | | keycloak: |
| | | # keycloak æå¡å¨å°å |
| | | server-url: https://lanbaosystem.shlanbao.cn:8443 |
| | | realm: lanbao |
| | | client-id: DataCapture |
| | | client-secret: kplisa4lJHEIM6knqefVbxln85QbA5NX |
| | | redirect-uri: http://192.168.12.236:80/social-callback |
| | | scopes: [openid, email, phone, profile] |
| | |
| | | # 踢åºä¹åç»å½ç/ä¹åç»å½çç¨æ·ï¼é»è®¤è¸¢åºä¹åç»å½çç¨æ· |
| | | kickoutAfter: false |
| | | rtdb: |
| | | host: http://127.0.0.1:8086 |
| | | token: == |
| | | org: org |
| | | bucket: bucket |
| | | host: http://127.0.0.1:8086 # ç¨æ·åroot å¯ç 12345678 |
| | | token: AminQagYp5rjb09mFPYvriK0T0vlF-zmwboqtUzdcq3nkXNuhnEpMuG_Ht5vtfWC4xBIVOThvoxy5reTer9XcQ== |
| | | org: lanbao |
| | | bucket: nygl |
| | | measurement: data |
| | | |
| | | ###################### MQTT ################################# |
| | | mqtt: |
| | | # æå¡å¨å°å |
| | | host: tcp://127.0.0.1:1883 |
| | | host: tcp://lanpucloud.cn:1883 |
| | | # IDå¯ä¸ |
| | | clientId: MQTT_WK |
| | | # ä¸»é¢ å¤ä¸ªä¸»é¢ç¨éå·(,)åå² #表示è¿ä¸ªä¸»é¢ä¸é¢ææï¼topic1,topic2,topic2/topic22/#(é»è®¤ä¼å第ä¸ä¸ªä¸»é¢) |
| | | topics: topic1 |
| | | # ç¨æ·å |
| | | username: admin |
| | | username: tongjitang |
| | | # å¯ç |
| | | password: 1q2w3e4r. |
| | | password: 123456 |
| | | # è¿æ¥è¶
æ¶ |
| | | timeout: 30 |
| | | # å¿è·³æ£æµ |
| | |
| | | <groupId>javax.servlet</groupId> |
| | | <artifactId>javax.servlet-api</artifactId> |
| | | </dependency> |
| | | <!-- <dependency>--> |
| | | <!-- <groupId>cn.hutool</groupId>--> |
| | | <!-- <artifactId>hutool-core</artifactId>--> |
| | | <!-- <version>5.8.21</version>--> |
| | | <!-- </dependency>--> |
| | | |
| | | <!-- <dependency>--> |
| | | <!-- <groupId>cn.hutool</groupId>--> |
| | | <!-- <artifactId>hutool-http</artifactId>--> |
| | | <!-- <version>5.8.21</version>--> |
| | | <!-- </dependency>--> |
| | | <dependency> |
| | | <groupId>cn.hutool</groupId> |
| | | <artifactId>hutool-all</artifactId> |
| | | <version>5.8.28</version> |
| | | <!-- 强å¶å£°æçæ¬ä¼å
级 --> |
| | | <optional>true</optional> |
| | | </dependency> |
| | | <!-- <dependency>--> |
| | | <!-- <groupId>cn.hutool</groupId>--> |
| | | <!-- <artifactId>hutool-extra</artifactId>--> |
| | | <!-- <version>5.8.21</version>--> |
| | | <!-- </dependency>--> |
| | | |
| | | <dependency> |
| | | <groupId>me.zhyd.oauth</groupId> |
| | | <artifactId>JustAuth</artifactId> |
| | | <version>1.16.7</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>io.github.linpeilie</groupId> |
| | | <artifactId>mapstruct-plus-spring-boot-starter</artifactId> |
| | | <version>1.4.6</version> |
| | | </dependency> |
| | | </dependencies> |
| | | |
| | | </project> |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.zhitan.common.config.keycloak; |
| | | |
| | | import cn.hutool.core.codec.Base64; |
| | | import cn.hutool.core.lang.Dict; |
| | | import cn.hutool.core.util.StrUtil; |
| | | |
| | | import cn.hutool.http.HttpRequest; |
| | | import cn.hutool.http.HttpResponse; |
| | | import com.xkcoding.http.support.HttpHeader; |
| | | import com.zhitan.common.utils.JsonUtils; |
| | | import com.zhitan.common.utils.spring.SpringUtils; |
| | | import me.zhyd.oauth.cache.AuthStateCache; |
| | | import me.zhyd.oauth.config.AuthConfig; |
| | | import me.zhyd.oauth.exception.AuthException; |
| | | import me.zhyd.oauth.model.AuthCallback; |
| | | import me.zhyd.oauth.model.AuthToken; |
| | | import me.zhyd.oauth.model.AuthUser; |
| | | import me.zhyd.oauth.request.AuthDefaultRequest; |
| | | import me.zhyd.oauth.utils.HttpUtils; |
| | | import me.zhyd.oauth.utils.UrlBuilder; |
| | | |
| | | |
| | | /** |
| | | * Keycloak OAuth2 认è¯è¯·æ± |
| | | */ |
| | | |
| | | public class AuthKeycloakRequest extends AuthDefaultRequest { |
| | | |
| | | public static final String SERVER_URL = SpringUtils.getProperty("keycloak.server-url"); |
| | | public static final String REALM = SpringUtils.getProperty("keycloak.realm"); |
| | | |
| | | public AuthKeycloakRequest(AuthConfig config) { |
| | | super(config, AuthKeycloakSource.KEYCLOAK); |
| | | } |
| | | |
| | | public AuthKeycloakRequest(AuthConfig config, AuthStateCache authStateCache) { |
| | | super(config, AuthKeycloakSource.KEYCLOAK, authStateCache); |
| | | } |
| | | |
| | | |
| | | |
| | | @Override |
| | | public AuthToken getAccessToken(AuthCallback authCallback) { |
| | | String body = doPostAuthorizationCode(authCallback.getCode()); |
| | | Dict object = JsonUtils.parseMap(body); |
| | | |
| | | if (object.containsKey("error")) { |
| | | throw new AuthException(object.getStr("error_description")); |
| | | } |
| | | if (object.containsKey("message")) { |
| | | throw new AuthException(object.getStr("message")); |
| | | } |
| | | |
| | | return AuthToken.builder() |
| | | .accessToken(object.getStr("access_token")) |
| | | .refreshToken(object.getStr("refresh_token")) |
| | | .idToken(object.getStr("id_token")) |
| | | .tokenType(object.getStr("token_type")) |
| | | .expireIn(object.getInt("expires_in")) |
| | | .build(); |
| | | } |
| | | |
| | | @Override |
| | | public AuthUser getUserInfo(AuthToken authToken) { |
| | | String body = doGetUserInfo(authToken); |
| | | Dict object = JsonUtils.parseMap(body); |
| | | |
| | | if (object.containsKey("error")) { |
| | | throw new AuthException(object.getStr("error_description")); |
| | | } |
| | | if (object.containsKey("message")) { |
| | | throw new AuthException(object.getStr("message")); |
| | | } |
| | | |
| | | return AuthUser.builder() |
| | | .uuid(object.getStr("sub")) |
| | | .username(object.getStr("preferred_username")) |
| | | .nickname(object.getStr("name")) |
| | | .email(object.getStr("email")) |
| | | .token(authToken) |
| | | .source(this.source.toString()) |
| | | .build(); |
| | | } |
| | | |
| | | @Override |
| | | protected String doPostAuthorizationCode(String code) { |
| | | HttpRequest request = HttpRequest.post(source.accessToken()) |
| | | .header("Authorization", "Basic " + Base64.encode(config.getClientId()+":"+config.getClientSecret())) |
| | | .form("grant_type", "authorization_code") |
| | | .form("code", code) |
| | | .form("redirect_uri", config.getRedirectUri()); |
| | | HttpResponse response = request.execute(); |
| | | return response.body(); |
| | | } |
| | | |
| | | @Override |
| | | protected String doGetUserInfo(AuthToken authToken) { |
| | | try { |
| | | return new HttpUtils(config.getHttpConfig()).get(source.userInfo(), null, new HttpHeader() |
| | | .add("Content-Type", "application/json") |
| | | .add("Authorization", "Bearer " + authToken.getAccessToken()), false).getBody(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public String authorize(String state) { |
| | | return UrlBuilder.fromBaseUrl(super.authorize(state)) |
| | | .queryParam("scope","openid") |
| | | .build(); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.zhitan.common.config.keycloak; |
| | | |
| | | import me.zhyd.oauth.config.AuthSource; |
| | | import me.zhyd.oauth.request.AuthDefaultRequest; |
| | | |
| | | public enum AuthKeycloakSource implements AuthSource { |
| | | KEYCLOAK { |
| | | /** |
| | | * ææçapi |
| | | */ |
| | | @Override |
| | | public String authorize() { |
| | | return String.format("%s/realms/%s/protocol/openid-connect/auth", AuthKeycloakRequest.SERVER_URL, AuthKeycloakRequest.REALM); |
| | | } |
| | | |
| | | @Override |
| | | public String accessToken() { |
| | | return String.format("%s/realms/%s/protocol/openid-connect/token", AuthKeycloakRequest.SERVER_URL, AuthKeycloakRequest.REALM); |
| | | } |
| | | |
| | | @Override |
| | | public String userInfo() { |
| | | return String.format("%s/realms/%s/protocol/openid-connect/userinfo", AuthKeycloakRequest.SERVER_URL, AuthKeycloakRequest.REALM); |
| | | } |
| | | |
| | | |
| | | public String logout() { |
| | | return String.format("%s/realms/%s/protocol/openid-connect/logout", AuthKeycloakRequest.SERVER_URL, AuthKeycloakRequest.REALM); |
| | | } |
| | | |
| | | @Override |
| | | public Class<? extends AuthDefaultRequest> getTargetClass() { |
| | | return AuthKeycloakRequest.class; |
| | | } |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.zhitan.common.config.keycloak; |
| | | |
| | | import com.zhitan.common.core.redis.RedisCache; |
| | | import lombok.AllArgsConstructor; |
| | | import me.zhyd.oauth.cache.AuthStateCache; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.time.Duration; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | /** |
| | | * ææç¶æç¼å |
| | | */ |
| | | @AllArgsConstructor |
| | | @Component |
| | | public class AuthRedisStateCache implements AuthStateCache { |
| | | |
| | | @Autowired |
| | | private final RedisCache redisCache; |
| | | |
| | | /** |
| | | * åå
¥ç¼å |
| | | * |
| | | * @param key ç¼åkey |
| | | * @param value ç¼åå
容 |
| | | */ |
| | | @Override |
| | | public void cache(String key, String value) { |
| | | // ææè¶
æ¶æ¶é´ é»è®¤ä¸åé |
| | | redisCache.setCacheObject("social_auth_codes:" + key, value, 3, TimeUnit.MINUTES); |
| | | } |
| | | |
| | | @Override |
| | | public void cache(String s, String s1, long l) { |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * è·åç¼åå
容 |
| | | * |
| | | * @param key ç¼åkey |
| | | * @return ç¼åå
容 |
| | | */ |
| | | @Override |
| | | public String get(String key) { |
| | | return redisCache.getCacheObject("social_auth_codes:" + key); |
| | | } |
| | | |
| | | /** |
| | | * æ¯å¦åå¨keyï¼å¦æå¯¹åºkeyçvalueå¼å·²è¿æï¼ä¹è¿åfalse |
| | | * |
| | | * @param key ç¼åkey |
| | | * @return trueï¼åå¨keyï¼å¹¶ä¸valueæ²¡è¿æï¼falseï¼keyä¸å卿è
å·²è¿æ |
| | | */ |
| | | @Override |
| | | public boolean containsKey(String key) { |
| | | return redisCache.hasKey("social_auth_codes:" + key); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.zhitan.common.config.keycloak; |
| | | |
| | | import lombok.Data; |
| | | import org.springframework.boot.context.properties.ConfigurationProperties; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 社交ç»å½é
ç½® |
| | | * |
| | | * @author thiszhc |
| | | */ |
| | | @Data |
| | | @Component |
| | | @ConfigurationProperties(prefix = "keycloak") |
| | | public class SocialLoginConfigProperties { |
| | | |
| | | /** |
| | | * åºç¨ ID |
| | | */ |
| | | private String clientId; |
| | | |
| | | /** |
| | | * åºç¨å¯é¥ |
| | | */ |
| | | private String clientSecret; |
| | | |
| | | /** |
| | | * åè°å°å |
| | | */ |
| | | private String redirectUri; |
| | | |
| | | /** |
| | | * æ¯å¦è·åunionId |
| | | */ |
| | | private boolean unionId; |
| | | |
| | | /** |
| | | * Coding ä¼ä¸åç§° |
| | | */ |
| | | private String codingGroupName; |
| | | |
| | | /** |
| | | * æ¯ä»å®å
¬é¥ |
| | | */ |
| | | private String alipayPublicKey; |
| | | |
| | | /** |
| | | * ä¼ä¸å¾®ä¿¡åºç¨ID |
| | | */ |
| | | private String agentId; |
| | | |
| | | /** |
| | | * stackoverflow api key |
| | | */ |
| | | private String stackOverflowKey; |
| | | |
| | | /** |
| | | * 设å¤ID |
| | | */ |
| | | private String deviceId; |
| | | |
| | | /** |
| | | * 客æ·ç«¯ç³»ç»ç±»å |
| | | */ |
| | | private String clientOsType; |
| | | |
| | | /** |
| | | * maxkey æå¡å¨å°å |
| | | */ |
| | | private String serverUrl; |
| | | |
| | | /** |
| | | * 请æ±èå´ |
| | | */ |
| | | private List<String> scopes; |
| | | |
| | | private String realm; |
| | | |
| | | } |
| | |
| | | */ |
| | | private String uuid; |
| | | |
| | | private String socialState; |
| | | |
| | | private String socialCode; |
| | | |
| | | private String source; |
| | | |
| | | |
| | | private String grantType; |
| | | |
| | | public String getSocialState() { |
| | | return socialState; |
| | | } |
| | | |
| | | public void setSocialState(String socialState) { |
| | | this.socialState = socialState; |
| | | } |
| | | |
| | | public String getSocialCode() { |
| | | return socialCode; |
| | | } |
| | | |
| | | public void setSocialCode(String socialCode) { |
| | | this.socialCode = socialCode; |
| | | } |
| | | |
| | | public String getSource() { |
| | | return source; |
| | | } |
| | | |
| | | public void setSource(String source) { |
| | | this.source = source; |
| | | } |
| | | |
| | | public String getGrantType() { |
| | | return grantType; |
| | | } |
| | | |
| | | public void setGrantType(String grantType) { |
| | | this.grantType = grantType; |
| | | } |
| | | |
| | | public String getUsername() |
| | | { |
| | | return username; |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.zhitan.common.utils; |
| | | |
| | | import cn.hutool.core.lang.Dict; |
| | | import cn.hutool.core.util.ArrayUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.fasterxml.jackson.core.JsonProcessingException; |
| | | import com.fasterxml.jackson.core.type.TypeReference; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.fasterxml.jackson.databind.exc.MismatchedInputException; |
| | | import com.zhitan.common.utils.spring.SpringUtils; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | | |
| | | import java.io.IOException; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * JSON å·¥å
·ç±» |
| | | * |
| | | * @author èéæºç |
| | | */ |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class JsonUtils { |
| | | |
| | | private static final ObjectMapper OBJECT_MAPPER = SpringUtils.getBean(ObjectMapper.class); |
| | | |
| | | public static ObjectMapper getObjectMapper() { |
| | | return OBJECT_MAPPER; |
| | | } |
| | | |
| | | /** |
| | | * å°å¯¹è±¡è½¬æ¢ä¸ºJSONæ ¼å¼çå符串 |
| | | * |
| | | * @param object è¦è½¬æ¢ç对象 |
| | | * @return JSONæ ¼å¼çå符串ï¼å¦æå¯¹è±¡ä¸ºnullï¼åè¿ånull |
| | | * @throws RuntimeException å¦æè½¬æ¢è¿ç¨ä¸åçJSONå¤çå¼å¸¸ï¼åæåºè¿è¡æ¶å¼å¸¸ |
| | | */ |
| | | public static String toJsonString(Object object) { |
| | | if (ObjectUtil.isNull(object)) { |
| | | return null; |
| | | } |
| | | try { |
| | | return OBJECT_MAPPER.writeValueAsString(object); |
| | | } catch (JsonProcessingException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å°JSONæ ¼å¼çå符串转æ¢ä¸ºæå®ç±»åç对象 |
| | | * |
| | | * @param text JSONæ ¼å¼çå符串 |
| | | * @param clazz è¦è½¬æ¢çç®æ 对象类å |
| | | * @param <T> ç®æ å¯¹è±¡çæ³åç±»å |
| | | * @return 转æ¢åç对象ï¼å¦æå符串为空åè¿ånull |
| | | * @throws RuntimeException å¦æè½¬æ¢è¿ç¨ä¸åçIOå¼å¸¸ï¼åæåºè¿è¡æ¶å¼å¸¸ |
| | | */ |
| | | public static <T> T parseObject(String text, Class<T> clazz) { |
| | | if (StringUtils.isEmpty(text)) { |
| | | return null; |
| | | } |
| | | try { |
| | | return OBJECT_MAPPER.readValue(text, clazz); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å°åèæ°ç»è½¬æ¢ä¸ºæå®ç±»åç对象 |
| | | * |
| | | * @param bytes åèæ°ç» |
| | | * @param clazz è¦è½¬æ¢çç®æ 对象类å |
| | | * @param <T> ç®æ å¯¹è±¡çæ³åç±»å |
| | | * @return 转æ¢åç对象ï¼å¦æåèæ°ç»ä¸ºç©ºåè¿ånull |
| | | * @throws RuntimeException å¦æè½¬æ¢è¿ç¨ä¸åçIOå¼å¸¸ï¼åæåºè¿è¡æ¶å¼å¸¸ |
| | | */ |
| | | public static <T> T parseObject(byte[] bytes, Class<T> clazz) { |
| | | if (ArrayUtil.isEmpty(bytes)) { |
| | | return null; |
| | | } |
| | | try { |
| | | return OBJECT_MAPPER.readValue(bytes, clazz); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å°JSONæ ¼å¼çå符串转æ¢ä¸ºæå®ç±»åçå¯¹è±¡ï¼æ¯æå¤æç±»å |
| | | * |
| | | * @param text JSONæ ¼å¼çå符串 |
| | | * @param typeReference æå®ç±»åçTypeReference对象 |
| | | * @param <T> ç®æ å¯¹è±¡çæ³åç±»å |
| | | * @return 转æ¢åç对象ï¼å¦æå符串为空åè¿ånull |
| | | * @throws RuntimeException å¦æè½¬æ¢è¿ç¨ä¸åçIOå¼å¸¸ï¼åæåºè¿è¡æ¶å¼å¸¸ |
| | | */ |
| | | public static <T> T parseObject(String text, TypeReference<T> typeReference) { |
| | | if (StringUtils.isBlank(text)) { |
| | | return null; |
| | | } |
| | | try { |
| | | return OBJECT_MAPPER.readValue(text, typeReference); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å°JSONæ ¼å¼çå符串转æ¢ä¸ºDict对象 |
| | | * |
| | | * @param text JSONæ ¼å¼çå符串 |
| | | * @return 转æ¢åçDict对象ï¼å¦æå符串为空æè
䏿¯JSONæ ¼å¼åè¿ånull |
| | | * @throws RuntimeException å¦æè½¬æ¢è¿ç¨ä¸åçIOå¼å¸¸ï¼åæåºè¿è¡æ¶å¼å¸¸ |
| | | */ |
| | | public static Dict parseMap(String text) { |
| | | if (StringUtils.isBlank(text)) { |
| | | return null; |
| | | } |
| | | try { |
| | | return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructType(Dict.class)); |
| | | } catch (MismatchedInputException e) { |
| | | // ç±»åä¸å¹é
说æä¸æ¯json |
| | | return null; |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å°JSONæ ¼å¼çå符串转æ¢ä¸ºDict对象çå表 |
| | | * |
| | | * @param text JSONæ ¼å¼çå符串 |
| | | * @return 转æ¢åçDict对象çå表ï¼å¦æå符串为空åè¿ånull |
| | | * @throws RuntimeException å¦æè½¬æ¢è¿ç¨ä¸åçIOå¼å¸¸ï¼åæåºè¿è¡æ¶å¼å¸¸ |
| | | */ |
| | | public static List<Dict> parseArrayMap(String text) { |
| | | if (StringUtils.isBlank(text)) { |
| | | return null; |
| | | } |
| | | try { |
| | | return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, Dict.class)); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å°JSONæ ¼å¼çå符串转æ¢ä¸ºæå®ç±»å对象çå表 |
| | | * |
| | | * @param text JSONæ ¼å¼çå符串 |
| | | * @param clazz è¦è½¬æ¢çç®æ 对象类å |
| | | * @param <T> ç®æ å¯¹è±¡çæ³åç±»å |
| | | * @return 转æ¢åç对象çå表ï¼å¦æå符串为空åè¿å空å表 |
| | | * @throws RuntimeException å¦æè½¬æ¢è¿ç¨ä¸åçIOå¼å¸¸ï¼åæåºè¿è¡æ¶å¼å¸¸ |
| | | */ |
| | | public static <T> List<T> parseArray(String text, Class<T> clazz) { |
| | | if (StringUtils.isEmpty(text)) { |
| | | return new ArrayList<>(); |
| | | } |
| | | try { |
| | | return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, clazz)); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.zhitan.common.utils; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.map.MapUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.zhitan.common.utils.spring.SpringUtils; |
| | | import io.github.linpeilie.Converter; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * Mapstruct å·¥å
·ç±» |
| | | * <p>åèææ¡£ï¼<a href="https://mapstruct.plus/introduction/quick-start.html">mapstruct-plus</a></p> |
| | | * |
| | | * |
| | | * @author Michelle.Chung |
| | | */ |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class MapstructUtils { |
| | | |
| | | private final static Converter CONVERTER = SpringUtils.getBean(Converter.class); |
| | | |
| | | /** |
| | | * å° T ç±»å对象ï¼è½¬æ¢ä¸º desc ç±»åç对象并è¿å |
| | | * |
| | | * @param source æ°æ®æ¥æºå®ä½ |
| | | * @param desc æè¿°å¯¹è±¡ 转æ¢åç对象 |
| | | * @return desc |
| | | */ |
| | | public static <T, V> V convert(T source, Class<V> desc) { |
| | | if (ObjectUtil.isNull(source)) { |
| | | return null; |
| | | } |
| | | if (ObjectUtil.isNull(desc)) { |
| | | return null; |
| | | } |
| | | return CONVERTER.convert(source, desc); |
| | | } |
| | | |
| | | /** |
| | | * å° T ç±»åå¯¹è±¡ï¼æç
§é
ç½®çæ å°å段è§åï¼ç» desc ç±»åç对象èµå¼å¹¶è¿å desc 对象 |
| | | * |
| | | * @param source æ°æ®æ¥æºå®ä½ |
| | | * @param desc 转æ¢åç对象 |
| | | * @return desc |
| | | */ |
| | | public static <T, V> V convert(T source, V desc) { |
| | | if (ObjectUtil.isNull(source)) { |
| | | return null; |
| | | } |
| | | if (ObjectUtil.isNull(desc)) { |
| | | return null; |
| | | } |
| | | return CONVERTER.convert(source, desc); |
| | | } |
| | | |
| | | /** |
| | | * å° T ç±»åçéåï¼è½¬æ¢ä¸º desc ç±»åçéåå¹¶è¿å |
| | | * |
| | | * @param sourceList æ°æ®æ¥æºå®ä½å表 |
| | | * @param desc æè¿°å¯¹è±¡ 转æ¢åç对象 |
| | | * @return desc |
| | | */ |
| | | public static <T, V> List<V> convert(List<T> sourceList, Class<V> desc) { |
| | | if (ObjectUtil.isNull(sourceList)) { |
| | | return null; |
| | | } |
| | | if (CollUtil.isEmpty(sourceList)) { |
| | | return CollUtil.newArrayList(); |
| | | } |
| | | return CONVERTER.convert(sourceList, desc); |
| | | } |
| | | |
| | | /** |
| | | * å° Map 转æ¢ä¸º beanClass ç±»åçéåå¹¶è¿å |
| | | * |
| | | * @param map æ°æ®æ¥æº |
| | | * @param beanClass beanç±» |
| | | * @return bean对象 |
| | | */ |
| | | public static <T> T convert(Map<String, Object> map, Class<T> beanClass) { |
| | | if (MapUtil.isEmpty(map)) { |
| | | return null; |
| | | } |
| | | if (ObjectUtil.isNull(beanClass)) { |
| | | return null; |
| | | } |
| | | return CONVERTER.convert(map, beanClass); |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.zhitan.common.utils; |
| | | |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.zhitan.common.config.keycloak.AuthKeycloakRequest; |
| | | import com.zhitan.common.config.keycloak.AuthRedisStateCache; |
| | | import com.zhitan.common.config.keycloak.SocialLoginConfigProperties; |
| | | import com.zhitan.common.utils.spring.SpringUtils; |
| | | import me.zhyd.oauth.config.AuthConfig; |
| | | import me.zhyd.oauth.exception.AuthException; |
| | | import me.zhyd.oauth.model.AuthCallback; |
| | | import me.zhyd.oauth.model.AuthResponse; |
| | | import me.zhyd.oauth.model.AuthUser; |
| | | import me.zhyd.oauth.request.AuthDingTalkRequest; |
| | | import me.zhyd.oauth.request.AuthRequest; |
| | | |
| | | |
| | | /** |
| | | * è®¤è¯ææå·¥å
·ç±» |
| | | * |
| | | * @author thiszhc |
| | | */ |
| | | public class SocialUtils { |
| | | |
| | | private static final AuthRedisStateCache STATE_CACHE = SpringUtils.getBean(AuthRedisStateCache.class); |
| | | private static final SocialLoginConfigProperties obj = SpringUtils.getBean(SocialLoginConfigProperties.class); |
| | | @SuppressWarnings("unchecked") |
| | | public static AuthResponse<AuthUser> loginAuth( String code, String state) throws AuthException { |
| | | AuthRequest authRequest = getAuthKeyloakRequest(); |
| | | AuthCallback callback = new AuthCallback(); |
| | | callback.setCode(code); |
| | | callback.setState(state); |
| | | return authRequest.login(callback); |
| | | } |
| | | |
| | | |
| | | |
| | | public static AuthKeycloakRequest getAuthKeyloakRequest( ) { |
| | | |
| | | AuthConfig.AuthConfigBuilder builder = AuthConfig.builder() |
| | | .clientId(obj.getClientId()) |
| | | .clientSecret(obj.getClientSecret()) |
| | | .redirectUri(obj.getRedirectUri()) |
| | | .scopes(obj.getScopes()); |
| | | return new AuthKeycloakRequest(builder.build(), STATE_CACHE); |
| | | } |
| | | } |
| | | |
¶Ô±ÈÐÂÎļþ |
| | |
| | | //package com.zhitan.common.utils; |
| | | // |
| | | //import cn.hutool.extra.spring.SpringUtil; |
| | | //import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
| | | //import org.springframework.context.ApplicationContext; |
| | | //import org.springframework.core.env.Environment; |
| | | //import org.springframework.stereotype.Component; |
| | | // |
| | | ///** |
| | | // * springå·¥å
·ç±» |
| | | // * |
| | | // * @author Lion Li |
| | | // */ |
| | | //@Component |
| | | //public final class SpringUtils extends SpringUtil { |
| | | // |
| | | // /** |
| | | // * 妿BeanFactoryå
å«ä¸ä¸ªä¸æç»åç§°å¹é
çbeanå®ä¹ï¼åè¿åtrue |
| | | // */ |
| | | // public static boolean containsBean(String name) { |
| | | // return getBeanFactory().containsBean(name); |
| | | // } |
| | | // |
| | | // /** |
| | | // * 夿以ç»å®ååæ³¨åçbeanå®ä¹æ¯ä¸ä¸ªsingletonè¿æ¯ä¸ä¸ªprototypeã |
| | | // * 妿ä¸ç»å®ååç¸åºçbeanå®ä¹æ²¡æè¢«æ¾å°ï¼å°ä¼æåºä¸ä¸ªå¼å¸¸ï¼NoSuchBeanDefinitionExceptionï¼ |
| | | // */ |
| | | // public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { |
| | | // return getBeanFactory().isSingleton(name); |
| | | // } |
| | | // |
| | | // /** |
| | | // * @return Class 注å对象çç±»å |
| | | // */ |
| | | // public static Class<?> getType(String name) throws NoSuchBeanDefinitionException { |
| | | // return getBeanFactory().getType(name); |
| | | // } |
| | | // |
| | | // /** |
| | | // * 妿ç»å®çbeanååå¨beanå®ä¹ä¸æå«åï¼åè¿åè¿äºå«å |
| | | // */ |
| | | // public static String[] getAliases(String name) throws NoSuchBeanDefinitionException { |
| | | // return getBeanFactory().getAliases(name); |
| | | // } |
| | | // |
| | | // /** |
| | | // * è·åaop代ç对象 |
| | | // */ |
| | | // @SuppressWarnings("unchecked") |
| | | // public static <T> T getAopProxy(T invoker) { |
| | | // return (T) getBean(invoker.getClass()); |
| | | // } |
| | | // |
| | | // |
| | | // /** |
| | | // * è·åspringä¸ä¸æ |
| | | // */ |
| | | // public static ApplicationContext context() { |
| | | // return getApplicationContext(); |
| | | // } |
| | | // |
| | | // |
| | | //} |
| | |
| | | package com.zhitan.common.utils.spring; |
| | | |
| | | import cn.hutool.extra.spring.SpringUtil; |
| | | import org.springframework.aop.framework.AopContext; |
| | | import org.springframework.beans.BeansException; |
| | | import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.config.BeanFactoryPostProcessor; |
| | | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| | | import org.springframework.context.ApplicationContext; |
| | | import org.springframework.context.ApplicationContextAware; |
| | | import org.springframework.core.env.Environment; |
| | | import org.springframework.stereotype.Component; |
| | | import com.zhitan.common.utils.StringUtils; |
| | | |
| | |
| | | public static <T> T getBean(String name) throws BeansException |
| | | { |
| | | return (T) beanFactory.getBean(name); |
| | | } |
| | | |
| | | public static String getProperty(String key) |
| | | { |
| | | return applicationContext == null ? null : applicationContext.getEnvironment().getProperty(key); |
| | | } |
| | | |
| | | /** |
| | |
| | | { |
| | | return applicationContext.getEnvironment().getRequiredProperty(key); |
| | | } |
| | | |
| | | |
| | | } |
| | |
| | | </exclusions> |
| | | </dependency> |
| | | |
| | | |
| | | |
| | | <!-- è·åç³»ç»ä¿¡æ¯ --> |
| | | <dependency> |
| | | <groupId>com.github.oshi</groupId> |
| | |
| | | <groupId>org.springframework.integration</groupId> |
| | | <artifactId>spring-integration-mqtt</artifactId> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>org.springframework.security</groupId> |
| | | <artifactId>spring-security-core</artifactId> |
| | | </dependency> |
| | | </dependencies> |
| | | |
| | | </project> |
| | |
| | | // è¿æ»¤è¯·æ± |
| | | .authorizeRequests() |
| | | // 对äºç»å½login 注åregister éªè¯ç captchaImage å
许å¿åè®¿é® |
| | | .antMatchers("/login", "/register", "/captchaImage").permitAll() |
| | | .antMatchers("/login", "/register", "/captchaImage", "/binding").permitAll() |
| | | // éæèµæºï¼å¯å¿åè®¿é® |
| | | .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll() |
| | | .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll() |
| | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import javax.annotation.Resource; |
| | | |
| | | import cn.hutool.http.HttpRequest; |
| | | import cn.hutool.http.HttpResponse; |
| | | import com.zhitan.system.domain.SysSocial; |
| | | import com.zhitan.system.service.ISysSocialService; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.security.core.Authentication; |
| | | import org.springframework.security.web.authentication.logout.LogoutSuccessHandler; |
| | |
| | | @Resource |
| | | private TokenService tokenService; |
| | | |
| | | @Value("${keycloak.server-url}") |
| | | private String keycloakServerUrl; |
| | | |
| | | @Value("${keycloak.realm}") |
| | | private String keycloakRealm; |
| | | |
| | | |
| | | @Resource |
| | | private ISysSocialService sysSocialService; |
| | | |
| | | /** |
| | | * éåºå¤ç |
| | | * |
| | |
| | | tokenService.delLoginUser(loginUser.getToken()); |
| | | // è®°å½ç¨æ·éåºæ¥å¿ |
| | | AsyncManager.me().execute(AsyncFactory.recordLoginInfo(userName, Constants.LOGOUT, MessageUtils.message("user.logout.success"))); |
| | | SysSocial social = sysSocialService.selectByUserId(loginUser.getUserId()); |
| | | if (social == null) { |
| | | return; |
| | | } |
| | | |
| | | String logoutUrl = keycloakServerUrl + "/realms/" + keycloakRealm + "/protocol/openid-connect/logout"; |
| | | HttpRequest req = HttpRequest.get(logoutUrl) |
| | | .form("refresh_token", social.getRefreshToken()) |
| | | .form("id_token_hint", social.getIdToken()); |
| | | |
| | | |
| | | HttpResponse rep = req.execute(); |
| | | if (rep.isOk()) { |
| | | System.out.println("1234"); |
| | | } |
| | | } |
| | | ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.success(MessageUtils.message("user.logout.success")))); |
| | | } |
| | |
| | | package com.zhitan.framework.web.service; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import com.zhitan.common.config.keycloak.AuthKeycloakRequest; |
| | | import com.zhitan.common.constant.CacheConstants; |
| | | import com.zhitan.common.constant.Constants; |
| | | import com.zhitan.common.constant.UserConstants; |
| | |
| | | import com.zhitan.common.exception.user.*; |
| | | import com.zhitan.common.utils.DateUtils; |
| | | import com.zhitan.common.utils.MessageUtils; |
| | | import com.zhitan.common.utils.SocialUtils; |
| | | import com.zhitan.common.utils.StringUtils; |
| | | import com.zhitan.common.utils.ip.IpUtils; |
| | | import com.zhitan.framework.manager.AsyncManager; |
| | | import com.zhitan.framework.manager.factory.AsyncFactory; |
| | | import com.zhitan.framework.security.context.AuthenticationContextHolder; |
| | | import com.zhitan.framework.security.single.SingleAuthenticationToken; |
| | | import com.zhitan.system.domain.SysSocial; |
| | | import com.zhitan.system.domain.bo.SysSocialBo; |
| | | import com.zhitan.system.domain.vo.SysSocialVo; |
| | | import com.zhitan.system.service.ISysConfigService; |
| | | import com.zhitan.system.service.ISysSocialService; |
| | | import com.zhitan.system.service.ISysUserService; |
| | | import me.zhyd.oauth.model.AuthCallback; |
| | | import me.zhyd.oauth.model.AuthResponse; |
| | | import me.zhyd.oauth.model.AuthToken; |
| | | import me.zhyd.oauth.model.AuthUser; |
| | | import org.springframework.security.authentication.AuthenticationManager; |
| | | import org.springframework.security.authentication.BadCredentialsException; |
| | | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| | |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * ç»å½æ ¡éªæ¹æ³ |
| | |
| | | |
| | | @Resource |
| | | private ISysConfigService configService; |
| | | |
| | | @Resource |
| | | private ISysSocialService sysSocialService; |
| | | |
| | | /** |
| | | * ç»å½éªè¯ |
| | |
| | | return tokenService.createToken(loginUser); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | public String loginByCode(String code,String state) |
| | | { |
| | | AuthKeycloakRequest authRequest = SocialUtils.getAuthKeyloakRequest(); |
| | | // AuthToken accessToken = authRequest.getAccessToken(passwordLoginBody); |
| | | AuthCallback callback = new AuthCallback(); |
| | | callback.setCode(code); |
| | | callback.setState(state); |
| | | AuthResponse<AuthUser> res = authRequest.login(callback); |
| | | AuthUser authUserData = res.getData(); |
| | | // æ°å¢KEYCLOAKç¨æ·èªå¨å建é»è¾ |
| | | String authId = authUserData.getSource() + authUserData.getUuid(); |
| | | List<SysSocial> list = sysSocialService.selectByAuthId(authId); |
| | | if (CollUtil.isEmpty(list)) { |
| | | // èªå¨å建æ°ç¨æ· |
| | | SysUser newUser = new SysUser(); |
| | | newUser.setUserName(authUserData.getUsername()); |
| | | newUser.setEmail(authUserData.getEmail()); |
| | | newUser.setNickName(authUserData.getNickname()); |
| | | newUser.setPassword("Initial123@"); // åå§å¯ç é符åå®å
¨çç¥ |
| | | newUser.setStatus("0"); |
| | | |
| | | userService.insertUser(newUser); // å设å卿å
¥æ¹æ³ |
| | | |
| | | // å建社交ç»å®è®°å½ |
| | | SysSocialBo newSocial = new SysSocialBo(); |
| | | newSocial.setUserId(newUser.getUserId()); |
| | | newSocial.setUserName(newUser.getUserName()); |
| | | newSocial.setAuthId(authId); |
| | | newSocial.setSource(authUserData.getSource()); |
| | | newSocial.setOpenId(authUserData.getUuid()); |
| | | newSocial.setAccessToken(authUserData.getToken().getAccessToken()); |
| | | newSocial.setRefreshToken(authUserData.getToken().getRefreshToken()); |
| | | newSocial.setIdToken(authUserData.getToken().getIdToken()); |
| | | sysSocialService.insertByBo(newSocial); // éç¡®ä¿æå¡ææ°å¢æ¹æ³ |
| | | |
| | | // éæ°æ¥è¯¢ç¡®ä¿æ°æ®å¯ç¨ |
| | | list = sysSocialService.selectByAuthId(authId); |
| | | } else { |
| | | // æ´æ°ç¤¾äº¤ç»å®è®°å½ |
| | | SysSocialBo socialBo = new SysSocialBo(); |
| | | socialBo.setId(list.get(0).getId()); |
| | | socialBo.setAccessToken(authUserData.getToken().getAccessToken()); |
| | | socialBo.setRefreshToken(authUserData.getToken().getRefreshToken()); |
| | | socialBo.setIdToken(authUserData.getToken().getIdToken()); |
| | | sysSocialService.updateByBo(socialBo); |
| | | |
| | | } |
| | | list = sysSocialService.selectByAuthId(authUserData.getSource() + authUserData.getUuid()); |
| | | if (CollUtil.isEmpty(list)) { |
| | | throw new ServiceException("ä½ è¿æ²¡æç»å®ç¬¬ä¸æ¹è´¦å·ï¼ç»å®åæå¯ä»¥ç»å½ï¼"); |
| | | } |
| | | LoginUser loginUser = new LoginUser(); |
| | | loginUser.setUser(userService.selectUserById(list.get(0).getUserId())); |
| | | loginUser.setUserId(list.get(0).getUserId()); |
| | | // çætoken |
| | | return tokenService.createToken(loginUser); |
| | | } |
| | | |
| | | /** |
| | | * æ ¡éªéªè¯ç |
| | | * |
| | |
| | | List<EnergyIndex> getIndexByCode(@Param("code")String code, @Param("nodeId")String nodeId); |
| | | |
| | | List<EnergyIndex> getIndexByMeterIdIndexCode(@Param("meterId") String meterId, @Param("indexCode") String indexCode, @Param("nodeId") String nodeId); |
| | | |
| | | int deleteByIndexId(String indexId); |
| | | } |
| | |
| | | * @return |
| | | */ |
| | | List<EnergyIndex> listDeviceIndex(String nodeId, String meterId); |
| | | |
| | | /** |
| | | * éè¿idå é¤ééç¹ |
| | | */ |
| | | boolean deleteByIndexId(String indexId); |
| | | |
| | | } |
| | |
| | | List<EnergyIndex> energyIndexList = energyIndexMapper.getIndexByMeterIdIndexCode(meterId,null,nodeId); |
| | | return energyIndexList; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public boolean deleteByIndexId(String indexId) { |
| | | return energyIndexMapper.deleteByIndexId(indexId) > 0; |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.zhitan.system.domain; |
| | | |
| | | import com.baomidou.mybatisplus.annotation.TableId; |
| | | import com.baomidou.mybatisplus.annotation.TableName; |
| | | import lombok.Data; |
| | | import lombok.EqualsAndHashCode; |
| | | |
| | | /** |
| | | * 社ä¼åå
³ç³»å¯¹è±¡ sys_social |
| | | * |
| | | * @author thiszhc |
| | | */ |
| | | @Data |
| | | |
| | | @TableName("sys_social") |
| | | public class SysSocial { |
| | | |
| | | |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | /** |
| | | * ä¸»é® |
| | | */ |
| | | @TableId(value = "id") |
| | | private Long id; |
| | | |
| | | /** |
| | | * ç¨æ·ID |
| | | */ |
| | | private Long userId; |
| | | |
| | | /** |
| | | * çå¯ä¸ID |
| | | */ |
| | | private String authId; |
| | | |
| | | /** |
| | | * ç¨æ·æ¥æº |
| | | */ |
| | | private String source; |
| | | |
| | | /** |
| | | * ç¨æ·çææä»¤ç |
| | | */ |
| | | private String accessToken; |
| | | |
| | | /** |
| | | * ç¨æ·çææä»¤ççæææï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private int expireIn; |
| | | |
| | | /** |
| | | * å·æ°ä»¤çï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String refreshToken; |
| | | |
| | | /** |
| | | * ç¨æ·ç open id |
| | | */ |
| | | private String openId; |
| | | |
| | | /** |
| | | * ææçç¬¬ä¸æ¹è´¦å· |
| | | */ |
| | | private String userName; |
| | | |
| | | /** |
| | | * ææçç¬¬ä¸æ¹æµç§° |
| | | */ |
| | | private String nickName; |
| | | |
| | | /** |
| | | * ææçç¬¬ä¸æ¹é®ç®± |
| | | */ |
| | | private String email; |
| | | |
| | | /** |
| | | * ææçç¬¬ä¸æ¹å¤´åå°å |
| | | */ |
| | | private String avatar; |
| | | |
| | | /** |
| | | * å¹³å°çææä¿¡æ¯ï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String accessCode; |
| | | |
| | | /** |
| | | * ç¨æ·ç unionid |
| | | */ |
| | | private String unionId; |
| | | |
| | | /** |
| | | * æäºçæéï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String scope; |
| | | |
| | | /** |
| | | * 个å«å¹³å°çææä¿¡æ¯ï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String tokenType; |
| | | |
| | | /** |
| | | * id tokenï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String idToken; |
| | | |
| | | /** |
| | | * å°ç±³å¹³å°ç¨æ·çé另屿§ï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String macAlgorithm; |
| | | |
| | | /** |
| | | * å°ç±³å¹³å°ç¨æ·çé另屿§ï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String macKey; |
| | | |
| | | /** |
| | | * ç¨æ·çææcodeï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String code; |
| | | |
| | | /** |
| | | * Twitterå¹³å°ç¨æ·çé另屿§ï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String oauthToken; |
| | | |
| | | /** |
| | | * Twitterå¹³å°ç¨æ·çé另屿§ï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String oauthTokenSecret; |
| | | |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.zhitan.system.domain.bo; |
| | | |
| | | |
| | | import com.zhitan.system.domain.SysSocial; |
| | | import lombok.Data; |
| | | import lombok.EqualsAndHashCode; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | | import javax.validation.constraints.NotNull; |
| | | |
| | | |
| | | /** |
| | | * 社ä¼åå
³ç³»ä¸å¡å¯¹è±¡ sys_social |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Data |
| | | @NoArgsConstructor |
| | | public class SysSocialBo { |
| | | |
| | | /** |
| | | * ä¸»é® |
| | | */ |
| | | private Long id; |
| | | |
| | | /** |
| | | * 认è¯å¯ä¸ID |
| | | */ |
| | | private String authId; |
| | | |
| | | /** |
| | | * ç¨æ·æ¥æº |
| | | */ |
| | | private String source; |
| | | |
| | | /** |
| | | * ç¨æ·çææä»¤ç |
| | | */ |
| | | private String accessToken; |
| | | |
| | | /** |
| | | * ç¨æ·çææä»¤ççæææï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private int expireIn; |
| | | |
| | | /** |
| | | * å·æ°ä»¤çï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String refreshToken; |
| | | |
| | | /** |
| | | * å¹³å°å¯ä¸id |
| | | */ |
| | | private String openId; |
| | | |
| | | /** |
| | | * ç¨æ·ç ID |
| | | */ |
| | | private Long userId; |
| | | |
| | | /** |
| | | * å¹³å°çææä¿¡æ¯ï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String accessCode; |
| | | |
| | | /** |
| | | * ç¨æ·ç unionid |
| | | */ |
| | | private String unionId; |
| | | |
| | | /** |
| | | * æäºçæéï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String scope; |
| | | |
| | | /** |
| | | * ææçç¬¬ä¸æ¹è´¦å· |
| | | */ |
| | | private String userName; |
| | | |
| | | /** |
| | | * ææçç¬¬ä¸æ¹æµç§° |
| | | */ |
| | | private String nickName; |
| | | |
| | | /** |
| | | * ææçç¬¬ä¸æ¹é®ç®± |
| | | */ |
| | | private String email; |
| | | |
| | | /** |
| | | * ææçç¬¬ä¸æ¹å¤´åå°å |
| | | */ |
| | | private String avatar; |
| | | |
| | | /** |
| | | * 个å«å¹³å°çææä¿¡æ¯ï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String tokenType; |
| | | |
| | | /** |
| | | * id tokenï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String idToken; |
| | | |
| | | /** |
| | | * å°ç±³å¹³å°ç¨æ·çé另屿§ï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String macAlgorithm; |
| | | |
| | | /** |
| | | * å°ç±³å¹³å°ç¨æ·çé另屿§ï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String macKey; |
| | | |
| | | /** |
| | | * ç¨æ·çææcodeï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String code; |
| | | |
| | | /** |
| | | * Twitterå¹³å°ç¨æ·çé另屿§ï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String oauthToken; |
| | | |
| | | /** |
| | | * Twitterå¹³å°ç¨æ·çé另屿§ï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String oauthTokenSecret; |
| | | |
| | | |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.zhitan.system.domain.vo; |
| | | |
| | | import lombok.Data; |
| | | |
| | | import java.io.Serializable; |
| | | import java.util.Date; |
| | | |
| | | |
| | | /** |
| | | * 社ä¼åå
³ç³»è§å¾å¯¹è±¡ sys_social |
| | | * |
| | | * @author thiszhc |
| | | */ |
| | | @Data |
| | | public class SysSocialVo implements Serializable { |
| | | |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | /** |
| | | * ä¸»é® |
| | | */ |
| | | private Long id; |
| | | |
| | | /** |
| | | * ç¨æ·ID |
| | | */ |
| | | private Long userId; |
| | | |
| | | /** |
| | | * ç§æ·ID |
| | | */ |
| | | private String tenantId; |
| | | |
| | | /** |
| | | * çå¯ä¸ID |
| | | */ |
| | | private String authId; |
| | | |
| | | /** |
| | | * ç¨æ·æ¥æº |
| | | */ |
| | | private String source; |
| | | |
| | | /** |
| | | * ç¨æ·çææä»¤ç |
| | | */ |
| | | private String accessToken; |
| | | |
| | | /** |
| | | * ç¨æ·çææä»¤ççæææï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private int expireIn; |
| | | |
| | | /** |
| | | * å·æ°ä»¤çï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String refreshToken; |
| | | |
| | | /** |
| | | * ç¨æ·ç open id |
| | | */ |
| | | private String openId; |
| | | |
| | | /** |
| | | * ææçç¬¬ä¸æ¹è´¦å· |
| | | */ |
| | | private String userName; |
| | | |
| | | /** |
| | | * ææçç¬¬ä¸æ¹æµç§° |
| | | */ |
| | | private String nickName; |
| | | |
| | | /** |
| | | * ææçç¬¬ä¸æ¹é®ç®± |
| | | */ |
| | | private String email; |
| | | |
| | | /** |
| | | * ææçç¬¬ä¸æ¹å¤´åå°å |
| | | */ |
| | | private String avatar; |
| | | |
| | | |
| | | /** |
| | | * å¹³å°çææä¿¡æ¯ï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String accessCode; |
| | | |
| | | /** |
| | | * ç¨æ·ç unionid |
| | | */ |
| | | private String unionId; |
| | | |
| | | /** |
| | | * æäºçæéï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String scope; |
| | | |
| | | /** |
| | | * 个å«å¹³å°çææä¿¡æ¯ï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String tokenType; |
| | | |
| | | /** |
| | | * id tokenï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String idToken; |
| | | |
| | | /** |
| | | * å°ç±³å¹³å°ç¨æ·çé另屿§ï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String macAlgorithm; |
| | | |
| | | /** |
| | | * å°ç±³å¹³å°ç¨æ·çé另屿§ï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String macKey; |
| | | |
| | | /** |
| | | * ç¨æ·çææcodeï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String code; |
| | | |
| | | /** |
| | | * Twitterå¹³å°ç¨æ·çé另屿§ï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String oauthToken; |
| | | |
| | | /** |
| | | * Twitterå¹³å°ç¨æ·çé另屿§ï¼é¨åå¹³å°å¯è½æ²¡æ |
| | | */ |
| | | private String oauthTokenSecret; |
| | | |
| | | /** |
| | | * å建æ¶é´ |
| | | */ |
| | | private Date createTime; |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.zhitan.system.mapper; |
| | | |
| | | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
| | | import com.zhitan.system.domain.SysSocial; |
| | | import com.zhitan.system.domain.vo.SysSocialVo; |
| | | |
| | | /** |
| | | * 社ä¼åå
³ç³»Mapperæ¥å£ |
| | | * |
| | | * @author thiszhc |
| | | */ |
| | | public interface SysSocialMapper extends BaseMapper<SysSocial> { |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.zhitan.system.service; |
| | | |
| | | |
| | | import com.zhitan.system.domain.SysSocial; |
| | | import com.zhitan.system.domain.bo.SysSocialBo; |
| | | import com.zhitan.system.domain.vo.SysSocialVo; |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 社ä¼åå
³ç³»Serviceæ¥å£ |
| | | * |
| | | * @author thiszhc |
| | | */ |
| | | public interface ISysSocialService { |
| | | |
| | | |
| | | /** |
| | | * æ°å¢ææå
³ç³» |
| | | */ |
| | | Boolean insertByBo(SysSocialBo bo); |
| | | |
| | | /** |
| | | * æ´æ°ç¤¾ä¼åå
³ç³» |
| | | */ |
| | | Boolean updateByBo(SysSocialBo bo); |
| | | |
| | | /** |
| | | * æ ¹æ® authId æ¥è¯¢ |
| | | */ |
| | | List<SysSocial> selectByAuthId(String authId); |
| | | |
| | | SysSocial selectByUserId(Long userId); |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.zhitan.system.service.impl; |
| | | |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.zhitan.common.utils.MapstructUtils; |
| | | import com.zhitan.common.utils.StringUtils; |
| | | import com.zhitan.common.utils.bean.BeanUtils; |
| | | import com.zhitan.system.domain.SysSocial; |
| | | import com.zhitan.system.domain.bo.SysSocialBo; |
| | | import com.zhitan.system.domain.vo.SysSocialVo; |
| | | import com.zhitan.system.mapper.SysSocialMapper; |
| | | import com.zhitan.system.service.ISysSocialService; |
| | | import lombok.RequiredArgsConstructor; |
| | | |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 社ä¼åå
³ç³»Serviceä¸å¡å±å¤ç |
| | | * |
| | | * @author thiszhc |
| | | * @date 2023-06-12 |
| | | */ |
| | | @RequiredArgsConstructor |
| | | @Service |
| | | public class SysSocialServiceImpl implements ISysSocialService { |
| | | |
| | | private final SysSocialMapper baseMapper; |
| | | |
| | | |
| | | /** |
| | | * æ°å¢ç¤¾ä¼åå
³ç³» |
| | | */ |
| | | @Override |
| | | public Boolean insertByBo(SysSocialBo bo) { |
| | | SysSocial add = new SysSocial(); |
| | | BeanUtils.copyProperties(bo, add); |
| | | boolean flag = baseMapper.insert(add) > 0; |
| | | if (flag) { |
| | | if (add != null) { |
| | | bo.setId(add.getId()); |
| | | } else { |
| | | return false; |
| | | } |
| | | } |
| | | return flag; |
| | | } |
| | | |
| | | /** |
| | | * æ´æ°ç¤¾ä¼åå
³ç³» |
| | | */ |
| | | @Override |
| | | public Boolean updateByBo(SysSocialBo bo) { |
| | | SysSocial update = new SysSocial(); |
| | | BeanUtils.copyProperties(bo, update); |
| | | |
| | | return baseMapper.updateById(update) > 0; |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ® authId æ¥è¯¢ç¨æ·ä¿¡æ¯ |
| | | * |
| | | * @param authId 认è¯id |
| | | * @return ææä¿¡æ¯ |
| | | */ |
| | | @Override |
| | | public List<SysSocial> selectByAuthId(String authId) { |
| | | List<SysSocial> sysSocials = baseMapper.selectList(new LambdaQueryWrapper<SysSocial>().eq(SysSocial::getAuthId, authId)); |
| | | // è½¬æ¢æVO |
| | | |
| | | return sysSocials; |
| | | } |
| | | |
| | | @Override |
| | | public SysSocial selectByUserId(Long userId) { |
| | | SysSocial socialVo = baseMapper.selectOne(new LambdaQueryWrapper<SysSocial>().eq(SysSocial::getUserId, userId)); |
| | | return socialVo; |
| | | } |
| | | |
| | | |
| | | } |
| | |
| | | #{indexId} |
| | | </foreach>; |
| | | </delete> |
| | | <delete id="deleteByIndexId"> |
| | | delete |
| | | from energy_index |
| | | where index_id = #{indexId} |
| | | </delete> |
| | | |
| | | <select id="getEnergyIndexMeterByCodes" resultMap="EnergyIndexResult"> |
| | | select ei.index_id, |
| | |
| | | |
| | | # ç³»ç»/å¼åç¯å¢ |
| | | # test |
| | | VITE_APP_BASE_API = 'http://139.159.201.118:8201' |
| | | VITE_APP_BASE_API = '/dev-api' |
| | | # hangmingjun |
| | | |
| | |
| | | }) |
| | | } |
| | | |
| | | export function loginBySocial(data) { |
| | | console.log("loginBySocial",data); |
| | | return request({ |
| | | url: '/login', |
| | | headers: { |
| | | isToken: false, |
| | | repeatSubmit: false |
| | | }, |
| | | method: 'post', |
| | | data: data |
| | | }) |
| | | } |
| | | |
| | | |
| | | // ç»å®è´¦å· |
| | | export function authBinding() { |
| | | return request({ |
| | | url: '/binding', |
| | | method: 'get' |
| | | }); |
| | | } |
| | | |
| | | // æ³¨åæ¹æ³ |
| | | export function register(data) { |
| | | return request({ |
¶Ô±ÈÐÂÎļþ |
| | |
| | | /** |
| | | * 注å |
| | | */ |
| | | export type RegisterForm = { |
| | | tenantId: string; |
| | | username: string; |
| | | password: string; |
| | | confirmPassword?: string; |
| | | code?: string; |
| | | uuid?: string; |
| | | userType?: string; |
| | | }; |
| | | |
| | | /** |
| | | * ç»å½è¯·æ± |
| | | */ |
| | | export interface LoginData { |
| | | tenantId?: string; |
| | | username?: string; |
| | | password?: string; |
| | | rememberMe?: boolean; |
| | | socialCode?: string; |
| | | socialState?: string; |
| | | source?: string; |
| | | code?: string; |
| | | uuid?: string; |
| | | clientId: string; |
| | | grantType: string; |
| | | } |
| | | |
| | | /** |
| | | * ç»å½ååº |
| | | */ |
| | | export interface LoginResult { |
| | | access_token: string; |
| | | } |
| | | |
| | | /** |
| | | * éªè¯ç è¿å |
| | | */ |
| | | export interface VerifyCodeResult { |
| | | captchaEnabled: boolean; |
| | | uuid?: string; |
| | | img?: string; |
| | | } |
| | | |
| | | /** |
| | | * ç§æ· |
| | | */ |
| | | export interface TenantVO { |
| | | companyName: string; |
| | | domain: any; |
| | | tenantId: string; |
| | | } |
| | | |
| | | export interface TenantInfo { |
| | | tenantEnabled: boolean; |
| | | voList: TenantVO[]; |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | <template> |
| | | <div v-loading="loading" class="social-callback"></div> |
| | | </template> |
| | | |
| | | <script setup lang="ts"> |
| | | import { login,loginBySocial } from '@/api/login'; |
| | | import { setToken, getToken } from '@/utils/auth'; |
| | | |
| | | import { LoginData } from '@/api/types'; |
| | | import {ElMessage} from "element-plus"; |
| | | import {useRoute} from "vue-router"; |
| | | import useUserStore from '@/store/modules/user' |
| | | |
| | | const route = useRoute(); |
| | | const loading = ref(true); |
| | | |
| | | |
| | | /** |
| | | * æ¥æ¶Routeä¼ éçåæ° |
| | | * @param {Object} route.query. |
| | | */ |
| | | const code = route.query.code as string; |
| | | const state = route.query.state as string; |
| | | console.log("state", atob(state)) |
| | | const source = route.query.source as string; |
| | | // const stateJson = JSON.parse(atob(state)); |
| | | // const domain = stateJson.domain as string; |
| | | console.log("code", code) |
| | | const processResponse = async (res: any) => { |
| | | if (res.code !== 200) { |
| | | throw new Error(res.msg); |
| | | } |
| | | if (res.data !== null) { |
| | | setToken(res.token); |
| | | useUserStore().token = res.token; |
| | | } |
| | | ElMessage.success(res.msg); |
| | | setTimeout(() => { |
| | | location.href = '/index'; |
| | | }, 2000); |
| | | }; |
| | | |
| | | const handleError = (error: any) => { |
| | | ElMessage.error(error.message); |
| | | setTimeout(() => { |
| | | location.href = '/index'; |
| | | }, 2000); |
| | | }; |
| | | // |
| | | // const callbackByCode = async (data: LoginData) => { |
| | | // try { |
| | | // const res = await callback(data); |
| | | // await processResponse(res); |
| | | // loading.value = false; |
| | | // } catch (error) { |
| | | // handleError(error); |
| | | // } |
| | | // }; |
| | | |
| | | const loginByCode = async (data: LoginData) => { |
| | | try { |
| | | const res = await loginBySocial(data); |
| | | await processResponse(res); |
| | | loading.value = false; |
| | | } catch (error) { |
| | | handleError(error); |
| | | } |
| | | }; |
| | | |
| | | const init = async () => { |
| | | // // 妿ååä¸ç¸ç åéå®åå¤ç |
| | | // let host = window.location.host; |
| | | // if (domain !== host) { |
| | | // let urlFull = new URL(window.location.href); |
| | | // urlFull.host = domain; |
| | | // window.location.href = urlFull.toString(); |
| | | // return; |
| | | // } |
| | | |
| | | const data: LoginData = { |
| | | socialCode: code, |
| | | socialState: state, |
| | | source: 'keycloak', |
| | | grantType: 'social' |
| | | }; |
| | | |
| | | |
| | | await loginByCode(data); |
| | | |
| | | }; |
| | | |
| | | onMounted(() => { |
| | | nextTick(() => { |
| | | init(); |
| | | }); |
| | | }); |
| | | </script> |
| | |
| | | |
| | | NProgress.configure({ showSpinner: false }); |
| | | |
| | | const whiteList = ['/login', '/register', '/energy'] |
| | | const whiteList = ['/login', '/register', '/energy','/social-callback'] |
| | | |
| | | router.beforeEach((to, from, next) => { |
| | | NProgress.start() |
| | |
| | | hidden: true |
| | | }, |
| | | { |
| | | path: '/social-callback', |
| | | component: () => import('@/layout/components/SocialCallback/index'), |
| | | hidden: true |
| | | }, |
| | | { |
| | | path: '/register', |
| | | component: () => import('@/views/register'), |
| | | hidden: true |
| | |
| | | // æ¯å¦éè¦é²æ¢æ°æ®éå¤æäº¤ |
| | | const isRepeatSubmit = (config.headers || {}).repeatSubmit === false |
| | | if (getToken() && !isToken) { |
| | | console.log('getToken', getToken()) |
| | | config.headers['Authorization'] = 'Bearer ' + getToken() // 让æ¯ä¸ªè¯·æ±æºå¸¦èªå®ä¹token è¯·æ ¹æ®å®é
æ
åµèªè¡ä¿®æ¹ |
| | | } |
| | | // getè¯·æ±æ å°paramsåæ° |
| | |
| | | </template> |
| | | |
| | | <script setup> |
| | | import { getCodeImg } from "@/api/login" |
| | | import { getCodeImg, authBinding } from "@/api/login" |
| | | import Cookies from "js-cookie" |
| | | import { encrypt, decrypt } from "@/utils/jsencrypt" |
| | | import useUserStore from "@/store/modules/user" |
| | | import {ElMessage} from "element-plus"; |
| | | |
| | | const userStore = useUserStore() |
| | | const route = useRoute() |
| | |
| | | const register = ref(false) |
| | | const redirect = ref(undefined) |
| | | |
| | | watch( |
| | | route, |
| | | (newRoute) => { |
| | | redirect.value = newRoute.query && newRoute.query.redirect |
| | | }, |
| | | { immediate: true } |
| | | ) |
| | | // watch( |
| | | // route, |
| | | // (newRoute) => { |
| | | // redirect.value = newRoute.query && newRoute.query.redirect |
| | | // }, |
| | | // { immediate: true } |
| | | // ) |
| | | |
| | | function handleLogin() { |
| | | proxy.$refs.loginRef.validate((valid) => { |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * ç¬¬ä¸æ¹ç»å½ |
| | | * @param type |
| | | */ |
| | | const doSocialLogin = () => { |
| | | console.log("doSocialLogin") |
| | | authBinding().then((res) => { |
| | | console.log(res); |
| | | if (res.code === 200) { |
| | | // è·åææå°å跳转 |
| | | window.location.href = res.data; |
| | | } else { |
| | | ElMessage.error(res.msg); |
| | | } |
| | | }); |
| | | }; |
| | | // éåç¹ç»å½æ¾å¼ä¸è¾¹ä¸¤è¡ï¼å¦åæ³¨éæ |
| | | getCode() |
| | | getCookie() |
| | | // åç¹ç»å½æ¾å¼ä¸è¾¹ä¸è¡ï¼å¦åæ³¨éæ |
| | | // doSocialLogin() |
| | | </script> |
| | | |
| | | <style lang="scss" scoped> |
| | |
| | | function handleDialog(type) { |
| | | if (type == "add") { |
| | | dialogTitle = "æ°å¢ééåæ°æ¨¡ç" |
| | | form.value = {} |
| | | } else { |
| | | dialogTitle = "ä¿®æ¹ééåæ°æ¨¡ç" |
| | | } |
| | |
| | | |
| | | // https://vitejs.dev/config/ |
| | | export default defineConfig(({ mode, command }) => { |
| | | mode = "production" |
| | | mode = "development" |
| | | const env = loadEnv(mode, process.cwd()) |
| | | console.log(mode, "==========env") |
| | | const { VITE_APP_ENV } = env |
| | |
| | | proxy: { |
| | | // https://cn.vitejs.dev/config/#server-proxy |
| | | "/dev-api": { |
| | | target: "http://localhost", |
| | | target: "http://localhost:8080", |
| | | changeOrigin: true, |
| | | rewrite: (p) => p.replace(/^\/dev-api/, ""), |
| | | }, |