Commit fca4572057f9b00a66dde85c24695450d058017f
Merge branch '20220428' into 'master'
refactor: 第三方登录接口 See merge request huang/thingsboard3.3.2!87
Showing
7 changed files
with
71 additions
and
10 deletions
... | ... | @@ -138,7 +138,7 @@ public class DeviceProfileController extends BaseController { |
138 | 138 | "The implementation limits the number of devices that participate in search to 100 as a trade of between accurate results and time-consuming queries. " + |
139 | 139 | TENANT_AUTHORITY_PARAGRAPH, |
140 | 140 | produces = "application/json") |
141 | - @PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") | |
141 | + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") | |
142 | 142 | @RequestMapping(value = "/deviceProfile/devices/keys/timeseries", method = RequestMethod.GET) |
143 | 143 | @ResponseBody |
144 | 144 | public List<String> getTimeseriesKeys( | ... | ... |
... | ... | @@ -5,6 +5,8 @@ import io.swagger.annotations.Api; |
5 | 5 | import io.swagger.annotations.ApiOperation; |
6 | 6 | import lombok.RequiredArgsConstructor; |
7 | 7 | import org.jetbrains.annotations.NotNull; |
8 | +import org.springframework.http.HttpStatus; | |
9 | +import org.springframework.http.ResponseEntity; | |
8 | 10 | import org.springframework.validation.annotation.Validated; |
9 | 11 | import org.springframework.web.bind.annotation.*; |
10 | 12 | import org.thingsboard.server.common.data.StringUtils; |
... | ... | @@ -76,11 +78,15 @@ public class YtThirdPlatformController extends BaseController { |
76 | 78 | return thirdService.unbindUser(getCurrentUser().getCurrentTenantId(), dto.getAppUserId(),dto.getThirdUserId()); |
77 | 79 | } |
78 | 80 | |
79 | - @GetMapping("login/{thirdId}") | |
81 | + @GetMapping("login/{loginCode}") | |
80 | 82 | @ApiOperation("第三方登录") |
81 | - public JwtTokenPair login(@PathVariable("thirdId") String thirdId) | |
83 | + public JwtTokenPair login(@PathVariable("loginCode") String loginCode) | |
82 | 84 | throws ThingsboardException { |
83 | - UserDTO userDto = thirdService.login(thirdId); | |
85 | + String thirdUserId = thirdService.thirdLogin(loginCode); | |
86 | + UserDTO userDto = thirdService.login(thirdUserId); | |
87 | + if(userDto == null){ | |
88 | + return new JwtTokenPair("", thirdUserId); | |
89 | + } | |
84 | 90 | return buildJwtToken(userDto); |
85 | 91 | } |
86 | 92 | ... | ... |
... | ... | @@ -528,7 +528,7 @@ spring: |
528 | 528 | open-in-view: "false" |
529 | 529 | hibernate: |
530 | 530 | ddl-auto: "none" |
531 | - show-sql: "true" | |
531 | + show-sql: "false" | |
532 | 532 | database-platform: "${SPRING_JPA_DATABASE_PLATFORM:org.hibernate.dialect.PostgreSQLDialect}" |
533 | 533 | datasource: |
534 | 534 | driverClassName: "${SPRING_DRIVER_CLASS_NAME:org.postgresql.Driver}" |
... | ... | @@ -1131,9 +1131,14 @@ account: |
1131 | 1131 | info: |
1132 | 1132 | emailSuffix: yunteng.com |
1133 | 1133 | defaultPassword: 123456 |
1134 | +third: | |
1135 | + wechat: | |
1136 | + url: https://api.weixin.qq.com | |
1137 | + appId: wxd5d018355f38262b | |
1138 | + appSecret: f6975fcc2248b1de2fdd2e40e9d21476 | |
1134 | 1139 | logging: |
1135 | 1140 | level: |
1136 | - org.thingsboard.server.dao.yunteng.mapper: debug | |
1137 | -mybatis-plus: | |
1138 | - configuration: | |
1139 | - log-impl: org.apache.ibatis.logging.stdout.StdOutImpl | |
1141 | + org.thingsboard.server.dao.yunteng.mapper: info | |
1142 | +#mybatis-plus: | |
1143 | +# configuration: | |
1144 | +# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl | ... | ... |
1 | +package org.thingsboard.server.common.data.yunteng.config.third; | |
2 | + | |
3 | +import lombok.Data; | |
4 | +import org.springframework.boot.context.properties.ConfigurationProperties; | |
5 | +import org.springframework.stereotype.Component; | |
6 | + | |
7 | +/** | |
8 | + * @author Administrator | |
9 | + */ | |
10 | +@Component | |
11 | +@Data | |
12 | +@ConfigurationProperties(prefix = "third.wechat") | |
13 | +public class WechatProperties { | |
14 | + private String url; | |
15 | + private String appId; | |
16 | + private String appSecret; | |
17 | + | |
18 | +} | ... | ... |
... | ... | @@ -60,7 +60,8 @@ public enum ErrorMessage { |
60 | 60 | SCENE_REACT_NOT_EXTIED(400040,"场景联动不存在"), |
61 | 61 | DEVICE_USED_SCENE_REACT(400041,"场景联动【%s】正在使用该设备"), |
62 | 62 | SCENE_REACT_USED_ALARM_PROFILE(400042,"场景联动正在使用该告警配置"), |
63 | - APP_USER_BINDED(400043,"账号【%s】已绑定"), | |
63 | + APP_USER_BINDED(400043,"平台用户【%s】已被其它账号绑定"), | |
64 | + THIRD_PLATFORM_EXCEPTION(400044,"【%s】,第三方异常【%s】"), | |
64 | 65 | HAVE_NO_PERMISSION(500002,"没有修改权限"); |
65 | 66 | private final int code; |
66 | 67 | private String message; | ... | ... |
... | ... | @@ -6,12 +6,15 @@ import com.baomidou.mybatisplus.core.metadata.IPage; |
6 | 6 | import com.fasterxml.jackson.databind.JsonNode; |
7 | 7 | import lombok.RequiredArgsConstructor; |
8 | 8 | import lombok.extern.slf4j.Slf4j; |
9 | +import org.springframework.http.ResponseEntity; | |
9 | 10 | import org.springframework.security.authentication.BadCredentialsException; |
10 | 11 | import org.springframework.security.core.userdetails.UsernameNotFoundException; |
11 | 12 | import org.springframework.security.crypto.password.PasswordEncoder; |
12 | 13 | import org.springframework.stereotype.Service; |
13 | 14 | import org.springframework.transaction.annotation.Transactional; |
15 | +import org.springframework.web.client.RestTemplate; | |
14 | 16 | import org.thingsboard.server.common.data.StringUtils; |
17 | +import org.thingsboard.server.common.data.yunteng.config.third.WechatProperties; | |
15 | 18 | import org.thingsboard.server.common.data.yunteng.core.cache.CacheUtils; |
16 | 19 | import org.thingsboard.server.common.data.yunteng.core.exception.YtDataValidationException; |
17 | 20 | import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage; |
... | ... | @@ -23,6 +26,7 @@ import org.thingsboard.server.common.data.yunteng.dto.request.CodeTTL; |
23 | 26 | import org.thingsboard.server.common.data.yunteng.enums.MessageTypeEnum; |
24 | 27 | import org.thingsboard.server.common.data.yunteng.enums.MsgTemplatePurposeEnum; |
25 | 28 | import org.thingsboard.server.common.data.yunteng.enums.ThirdPlatformEnum; |
29 | +import org.thingsboard.server.common.data.yunteng.utils.JacksonUtil; | |
26 | 30 | import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData; |
27 | 31 | import org.thingsboard.server.dao.yunteng.entities.User; |
28 | 32 | import org.thingsboard.server.dao.yunteng.entities.YtThirdUserEntity; |
... | ... | @@ -47,6 +51,9 @@ public class YtThirdPlatformServiceImpl extends AbstractBaseService<YtThirdPlatf |
47 | 51 | private final CacheUtils cacheUtils; |
48 | 52 | private final PasswordEncoder passwordEncoder; |
49 | 53 | |
54 | + private final RestTemplate restTemplate = new RestTemplate(); | |
55 | + private final WechatProperties wechatProperties; | |
56 | + | |
50 | 57 | @Override |
51 | 58 | public YtPageData<YtThirdUserDTO> pageDatas(IPage<YtThirdUserEntity> pageInfrom, ThirdPlatformEnum platformName, String name) { |
52 | 59 | Wrapper pageFilter = new QueryWrapper<YtThirdUserEntity>() |
... | ... | @@ -171,6 +178,19 @@ public class YtThirdPlatformServiceImpl extends AbstractBaseService<YtThirdPlatf |
171 | 178 | return baseMapper.delete(filter) > 0; |
172 | 179 | } |
173 | 180 | |
181 | + private static final String loginApi="/sns/jscode2session?appid={appid}&secret={appid}&js_code={appid}&grant_type=authorization_code"; | |
182 | + @Override | |
183 | + public String thirdLogin(String loginCode) { | |
184 | + String response = restTemplate.getForObject(wechatProperties.getUrl()+loginApi,String.class, wechatProperties.getAppId(),wechatProperties.getAppSecret(),loginCode); | |
185 | + JsonNode result = JacksonUtil.toJsonNode(response); | |
186 | + | |
187 | + if(result.has("errcode")){ | |
188 | + throw new YtDataValidationException(String.format(ErrorMessage.THIRD_PLATFORM_EXCEPTION.getMessage(),ThirdPlatformEnum.WECHAT,result.get("errmsg").asText())); | |
189 | + } | |
190 | + String thirdUserId = result.get("openid").asText(); | |
191 | + return thirdUserId; | |
192 | + } | |
193 | + | |
174 | 194 | @Override |
175 | 195 | public UserDTO login(String thirdUserId) { |
176 | 196 | User user = baseMapper.login(thirdUserId); |
... | ... | @@ -186,4 +206,7 @@ public class YtThirdPlatformServiceImpl extends AbstractBaseService<YtThirdPlatf |
186 | 206 | public JsonNode message(String receiver, JsonNode message) { |
187 | 207 | return null; |
188 | 208 | } |
209 | + | |
210 | + | |
211 | + | |
189 | 212 | } | ... | ... |
... | ... | @@ -44,6 +44,14 @@ public interface YtThirdPlatformService extends BaseService<YtThirdUserEntity> { |
44 | 44 | /** |
45 | 45 | * 第三方登录 |
46 | 46 | * |
47 | + * @param loginCode | |
48 | + * @return | |
49 | + */ | |
50 | + String thirdLogin(String loginCode); | |
51 | + | |
52 | + /** | |
53 | + * 第三方登录 | |
54 | + * | |
47 | 55 | * @param thirdUserId |
48 | 56 | * @return |
49 | 57 | */ | ... | ... |