|
@@ -1,5 +1,6 @@
|
|
|
package com.huaxu.security;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
@@ -12,11 +13,18 @@ import com.huaxu.model.LoginUser;
|
|
|
import com.huaxu.model.AjaxMessage;
|
|
|
import com.huaxu.model.ResultStatus;
|
|
|
import com.huaxu.service.UserService;
|
|
|
+import com.huaxu.util.RedisUtil;
|
|
|
import com.huaxu.util.Util;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
|
|
import org.springframework.security.oauth2.common.exceptions.UnapprovedClientAuthenticationException;
|
|
@@ -24,6 +32,10 @@ import org.springframework.security.oauth2.provider.*;
|
|
|
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
|
|
|
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.LinkedMultiValueMap;
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+import sun.misc.BASE64Encoder;
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
@@ -34,6 +46,7 @@ import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Component("myAuthenticationSuccessHandler")
|
|
|
+@Slf4j
|
|
|
public class MyAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
|
|
|
|
|
|
@Autowired
|
|
@@ -49,14 +62,17 @@ public class MyAuthenticationSuccessHandler extends SavedRequestAwareAuthenticat
|
|
|
@Autowired
|
|
|
private UserService userService;
|
|
|
|
|
|
-
|
|
|
+ @Value("${iot.url}")
|
|
|
+ private String iotUrl;
|
|
|
+ @Autowired
|
|
|
+ private RedisUtil redisUtil;
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
@Override
|
|
|
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
|
|
|
Authentication authentication) throws IOException, ServletException {
|
|
|
|
|
|
- logger.info("登录成功");
|
|
|
+ log.info("登录成功");
|
|
|
|
|
|
String clientId = "smart-city-v2";
|
|
|
String clientSecret = "smart-city-v2-123";
|
|
@@ -91,6 +107,14 @@ public class MyAuthenticationSuccessHandler extends SavedRequestAwareAuthenticat
|
|
|
responseUserLoginDto.setToken(token);
|
|
|
responseUserLoginDto.setLoginInfo(JSONArray.parseArray(loginUser.getUserInfos(), UserDto.class));
|
|
|
JSONObject result=new JSONObject();
|
|
|
+
|
|
|
+ //可以加一个映射表
|
|
|
+ /*String iotToken=getLoginIotToken(loginUser.getPhoneNumber());
|
|
|
+ if(iotToken!=null){
|
|
|
+ String iotTokenKey = "iotToken:" + token;
|
|
|
+ redisUtil.set(iotTokenKey,iotToken);
|
|
|
+ redisUtil.setExpire(iotTokenKey,60*30);
|
|
|
+ }*/
|
|
|
result.put("token",token);
|
|
|
result.put("userInfos",loginUser.getUserInfos());
|
|
|
|
|
@@ -100,5 +124,36 @@ public class MyAuthenticationSuccessHandler extends SavedRequestAwareAuthenticat
|
|
|
|
|
|
response.getWriter().write(objectMapper.writeValueAsString(ajaxMessage));
|
|
|
}
|
|
|
+ public String getLoginIotToken(String account){
|
|
|
+ log.info("begin IotService login account={}",account);
|
|
|
+ String url = iotUrl + "/api/integration/auth/login";
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+ MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
|
|
|
+ map.add("source","rmcp");
|
|
|
+ map.add("code",new BASE64Encoder().encode(account.getBytes()));
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map,headers);
|
|
|
+ ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(url, request, JSONObject.class);
|
|
|
+ log.info("responseEntity ={}", JSONObject.toJSONString(responseEntity));
|
|
|
+ if(responseEntity !=null && responseEntity.getStatusCode() == HttpStatus.OK){
|
|
|
+ JSONObject jsonObject = responseEntity.getBody();
|
|
|
+ if(jsonObject != null){
|
|
|
+ if(jsonObject.getInteger("status")==0){
|
|
|
+ String token = jsonObject.getString("data");
|
|
|
+ log.info("IotService getLoginIotToken success res={}",token);
|
|
|
+ return token;
|
|
|
+ }else {
|
|
|
+ log.info("IotService getLoginIotToken fail");
|
|
|
+ return null;
|
|
|
+
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
}
|