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,7 +138,7 @@ public class DeviceProfileController extends BaseController { | ||
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. " + | 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 | TENANT_AUTHORITY_PARAGRAPH, | 139 | TENANT_AUTHORITY_PARAGRAPH, |
140 | produces = "application/json") | 140 | produces = "application/json") |
141 | - @PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") | 141 | + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") |
142 | @RequestMapping(value = "/deviceProfile/devices/keys/timeseries", method = RequestMethod.GET) | 142 | @RequestMapping(value = "/deviceProfile/devices/keys/timeseries", method = RequestMethod.GET) |
143 | @ResponseBody | 143 | @ResponseBody |
144 | public List<String> getTimeseriesKeys( | 144 | public List<String> getTimeseriesKeys( |
@@ -5,6 +5,8 @@ import io.swagger.annotations.Api; | @@ -5,6 +5,8 @@ import io.swagger.annotations.Api; | ||
5 | import io.swagger.annotations.ApiOperation; | 5 | import io.swagger.annotations.ApiOperation; |
6 | import lombok.RequiredArgsConstructor; | 6 | import lombok.RequiredArgsConstructor; |
7 | import org.jetbrains.annotations.NotNull; | 7 | import org.jetbrains.annotations.NotNull; |
8 | +import org.springframework.http.HttpStatus; | ||
9 | +import org.springframework.http.ResponseEntity; | ||
8 | import org.springframework.validation.annotation.Validated; | 10 | import org.springframework.validation.annotation.Validated; |
9 | import org.springframework.web.bind.annotation.*; | 11 | import org.springframework.web.bind.annotation.*; |
10 | import org.thingsboard.server.common.data.StringUtils; | 12 | import org.thingsboard.server.common.data.StringUtils; |
@@ -76,11 +78,15 @@ public class YtThirdPlatformController extends BaseController { | @@ -76,11 +78,15 @@ public class YtThirdPlatformController extends BaseController { | ||
76 | return thirdService.unbindUser(getCurrentUser().getCurrentTenantId(), dto.getAppUserId(),dto.getThirdUserId()); | 78 | return thirdService.unbindUser(getCurrentUser().getCurrentTenantId(), dto.getAppUserId(),dto.getThirdUserId()); |
77 | } | 79 | } |
78 | 80 | ||
79 | - @GetMapping("login/{thirdId}") | 81 | + @GetMapping("login/{loginCode}") |
80 | @ApiOperation("第三方登录") | 82 | @ApiOperation("第三方登录") |
81 | - public JwtTokenPair login(@PathVariable("thirdId") String thirdId) | 83 | + public JwtTokenPair login(@PathVariable("loginCode") String loginCode) |
82 | throws ThingsboardException { | 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 | return buildJwtToken(userDto); | 90 | return buildJwtToken(userDto); |
85 | } | 91 | } |
86 | 92 |
@@ -528,7 +528,7 @@ spring: | @@ -528,7 +528,7 @@ spring: | ||
528 | open-in-view: "false" | 528 | open-in-view: "false" |
529 | hibernate: | 529 | hibernate: |
530 | ddl-auto: "none" | 530 | ddl-auto: "none" |
531 | - show-sql: "true" | 531 | + show-sql: "false" |
532 | database-platform: "${SPRING_JPA_DATABASE_PLATFORM:org.hibernate.dialect.PostgreSQLDialect}" | 532 | database-platform: "${SPRING_JPA_DATABASE_PLATFORM:org.hibernate.dialect.PostgreSQLDialect}" |
533 | datasource: | 533 | datasource: |
534 | driverClassName: "${SPRING_DRIVER_CLASS_NAME:org.postgresql.Driver}" | 534 | driverClassName: "${SPRING_DRIVER_CLASS_NAME:org.postgresql.Driver}" |
@@ -1131,9 +1131,14 @@ account: | @@ -1131,9 +1131,14 @@ account: | ||
1131 | info: | 1131 | info: |
1132 | emailSuffix: yunteng.com | 1132 | emailSuffix: yunteng.com |
1133 | defaultPassword: 123456 | 1133 | defaultPassword: 123456 |
1134 | +third: | ||
1135 | + wechat: | ||
1136 | + url: https://api.weixin.qq.com | ||
1137 | + appId: wxd5d018355f38262b | ||
1138 | + appSecret: f6975fcc2248b1de2fdd2e40e9d21476 | ||
1134 | logging: | 1139 | logging: |
1135 | level: | 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,7 +60,8 @@ public enum ErrorMessage { | ||
60 | SCENE_REACT_NOT_EXTIED(400040,"场景联动不存在"), | 60 | SCENE_REACT_NOT_EXTIED(400040,"场景联动不存在"), |
61 | DEVICE_USED_SCENE_REACT(400041,"场景联动【%s】正在使用该设备"), | 61 | DEVICE_USED_SCENE_REACT(400041,"场景联动【%s】正在使用该设备"), |
62 | SCENE_REACT_USED_ALARM_PROFILE(400042,"场景联动正在使用该告警配置"), | 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 | HAVE_NO_PERMISSION(500002,"没有修改权限"); | 65 | HAVE_NO_PERMISSION(500002,"没有修改权限"); |
65 | private final int code; | 66 | private final int code; |
66 | private String message; | 67 | private String message; |
@@ -6,12 +6,15 @@ import com.baomidou.mybatisplus.core.metadata.IPage; | @@ -6,12 +6,15 @@ import com.baomidou.mybatisplus.core.metadata.IPage; | ||
6 | import com.fasterxml.jackson.databind.JsonNode; | 6 | import com.fasterxml.jackson.databind.JsonNode; |
7 | import lombok.RequiredArgsConstructor; | 7 | import lombok.RequiredArgsConstructor; |
8 | import lombok.extern.slf4j.Slf4j; | 8 | import lombok.extern.slf4j.Slf4j; |
9 | +import org.springframework.http.ResponseEntity; | ||
9 | import org.springframework.security.authentication.BadCredentialsException; | 10 | import org.springframework.security.authentication.BadCredentialsException; |
10 | import org.springframework.security.core.userdetails.UsernameNotFoundException; | 11 | import org.springframework.security.core.userdetails.UsernameNotFoundException; |
11 | import org.springframework.security.crypto.password.PasswordEncoder; | 12 | import org.springframework.security.crypto.password.PasswordEncoder; |
12 | import org.springframework.stereotype.Service; | 13 | import org.springframework.stereotype.Service; |
13 | import org.springframework.transaction.annotation.Transactional; | 14 | import org.springframework.transaction.annotation.Transactional; |
15 | +import org.springframework.web.client.RestTemplate; | ||
14 | import org.thingsboard.server.common.data.StringUtils; | 16 | import org.thingsboard.server.common.data.StringUtils; |
17 | +import org.thingsboard.server.common.data.yunteng.config.third.WechatProperties; | ||
15 | import org.thingsboard.server.common.data.yunteng.core.cache.CacheUtils; | 18 | import org.thingsboard.server.common.data.yunteng.core.cache.CacheUtils; |
16 | import org.thingsboard.server.common.data.yunteng.core.exception.YtDataValidationException; | 19 | import org.thingsboard.server.common.data.yunteng.core.exception.YtDataValidationException; |
17 | import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage; | 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,6 +26,7 @@ import org.thingsboard.server.common.data.yunteng.dto.request.CodeTTL; | ||
23 | import org.thingsboard.server.common.data.yunteng.enums.MessageTypeEnum; | 26 | import org.thingsboard.server.common.data.yunteng.enums.MessageTypeEnum; |
24 | import org.thingsboard.server.common.data.yunteng.enums.MsgTemplatePurposeEnum; | 27 | import org.thingsboard.server.common.data.yunteng.enums.MsgTemplatePurposeEnum; |
25 | import org.thingsboard.server.common.data.yunteng.enums.ThirdPlatformEnum; | 28 | import org.thingsboard.server.common.data.yunteng.enums.ThirdPlatformEnum; |
29 | +import org.thingsboard.server.common.data.yunteng.utils.JacksonUtil; | ||
26 | import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData; | 30 | import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData; |
27 | import org.thingsboard.server.dao.yunteng.entities.User; | 31 | import org.thingsboard.server.dao.yunteng.entities.User; |
28 | import org.thingsboard.server.dao.yunteng.entities.YtThirdUserEntity; | 32 | import org.thingsboard.server.dao.yunteng.entities.YtThirdUserEntity; |
@@ -47,6 +51,9 @@ public class YtThirdPlatformServiceImpl extends AbstractBaseService<YtThirdPlatf | @@ -47,6 +51,9 @@ public class YtThirdPlatformServiceImpl extends AbstractBaseService<YtThirdPlatf | ||
47 | private final CacheUtils cacheUtils; | 51 | private final CacheUtils cacheUtils; |
48 | private final PasswordEncoder passwordEncoder; | 52 | private final PasswordEncoder passwordEncoder; |
49 | 53 | ||
54 | + private final RestTemplate restTemplate = new RestTemplate(); | ||
55 | + private final WechatProperties wechatProperties; | ||
56 | + | ||
50 | @Override | 57 | @Override |
51 | public YtPageData<YtThirdUserDTO> pageDatas(IPage<YtThirdUserEntity> pageInfrom, ThirdPlatformEnum platformName, String name) { | 58 | public YtPageData<YtThirdUserDTO> pageDatas(IPage<YtThirdUserEntity> pageInfrom, ThirdPlatformEnum platformName, String name) { |
52 | Wrapper pageFilter = new QueryWrapper<YtThirdUserEntity>() | 59 | Wrapper pageFilter = new QueryWrapper<YtThirdUserEntity>() |
@@ -171,6 +178,19 @@ public class YtThirdPlatformServiceImpl extends AbstractBaseService<YtThirdPlatf | @@ -171,6 +178,19 @@ public class YtThirdPlatformServiceImpl extends AbstractBaseService<YtThirdPlatf | ||
171 | return baseMapper.delete(filter) > 0; | 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 | @Override | 194 | @Override |
175 | public UserDTO login(String thirdUserId) { | 195 | public UserDTO login(String thirdUserId) { |
176 | User user = baseMapper.login(thirdUserId); | 196 | User user = baseMapper.login(thirdUserId); |
@@ -186,4 +206,7 @@ public class YtThirdPlatformServiceImpl extends AbstractBaseService<YtThirdPlatf | @@ -186,4 +206,7 @@ public class YtThirdPlatformServiceImpl extends AbstractBaseService<YtThirdPlatf | ||
186 | public JsonNode message(String receiver, JsonNode message) { | 206 | public JsonNode message(String receiver, JsonNode message) { |
187 | return null; | 207 | return null; |
188 | } | 208 | } |
209 | + | ||
210 | + | ||
211 | + | ||
189 | } | 212 | } |
@@ -44,6 +44,14 @@ public interface YtThirdPlatformService extends BaseService<YtThirdUserEntity> { | @@ -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 | * @param thirdUserId | 55 | * @param thirdUserId |
48 | * @return | 56 | * @return |
49 | */ | 57 | */ |