Commit 050fa4b73d5e0d8cfcaeb0cba29d7ed177806b52
1 parent
c00bf49d
1.加入统一平台登录验证方式
Showing
4 changed files
with
94 additions
and
21 deletions
src/main/java/com/bsth/controller/sys/UserController.java
| ... | ... | @@ -76,37 +76,38 @@ public class UserController extends BaseController<SysUser, Integer> { |
| 76 | 76 | public static Map<String, Long> USER_LOCKTIME = new HashMap<>(); |
| 77 | 77 | |
| 78 | 78 | @RequestMapping(value = "/login/token") |
| 79 | - public void login(@RequestParam String token, @RequestParam String account, @RequestParam Long time, HttpServletRequest request, HttpServletResponse response) throws Exception { | |
| 79 | + public void login(@RequestParam String token, HttpServletRequest request, HttpServletResponse response) throws Exception { | |
| 80 | 80 | response.addHeader("Content-Type", "text/html;charset=UTF-8"); |
| 81 | 81 | if (!ssoConfig.isSsoEnabled()) { |
| 82 | 82 | response.getWriter().write("系统未开启统一登录配置,请联系管理员"); |
| 83 | 83 | return; |
| 84 | 84 | } |
| 85 | - if (token == null || account == null || time == null) { | |
| 85 | + if (token == null) { | |
| 86 | 86 | throw new IllegalArgumentException("请检查参数"); |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | Map<String, Object> param = new HashMap<>(), user = new HashMap<>(); |
| 90 | 90 | param.put("token", token); |
| 91 | - param.put("systemCode", ssoConfig.getSystemCode()); | |
| 92 | - StringBuilder sb = HttpClientUtils.post(ssoConfig.getSsoAuthUrl(), mapper.writeValueAsString(param)); | |
| 91 | + param.put("sysCode", ssoConfig.getSystemCode()); | |
| 92 | + StringBuilder sb = HttpClientUtils.post(ssoConfig.getSsoAuthUrl(), HttpClientUtils.createFormEntity(param), HttpClientUtils.createFormHeader()); | |
| 93 | 93 | |
| 94 | 94 | Assert.notNull(sb, "统一登录平台验证数据不为空"); |
| 95 | 95 | logger.info(String.format("统一登录平台:%s", sb.toString())); |
| 96 | - param = mapper.readValue(mapper.writeValueAsString(mapper.readValue(sb.toString(), Map.class).get("data")), Map.class); | |
| 97 | - String jobCode = (String) param.get("account"), realName = (String) param.get("userName"); | |
| 98 | - if (!account.equals(jobCode)) { | |
| 99 | - response.getWriter().write("token与用户不匹配"); | |
| 100 | - return; | |
| 101 | - } | |
| 102 | - | |
| 103 | - if (jobCode == null || realName == null) { | |
| 104 | - response.getWriter().write("token数据异常"); | |
| 96 | + param = mapper.readValue(sb.toString(), Map.class); | |
| 97 | + if (200 == (int) param.get("code")) { | |
| 98 | + param = mapper.readValue(mapper.writeValueAsString(param.get("data")), Map.class); | |
| 99 | + if ("9999".equals(param.get("code")) || "9998".equals(param.get("code"))) { | |
| 100 | + response.getWriter().write(param.get("msg").toString()); | |
| 101 | + return; | |
| 102 | + } else { | |
| 103 | + param = mapper.readValue(mapper.writeValueAsString(param.get("data")), Map.class); | |
| 104 | + } | |
| 105 | + } else { | |
| 106 | + response.getWriter().write(param.get("msg").toString()); | |
| 105 | 107 | return; |
| 106 | 108 | } |
| 107 | - | |
| 108 | - user.put("jobCode_eq", jobCode); | |
| 109 | - user.put("realName_eq", realName); | |
| 109 | + String userName = (String) param.get("userName"); | |
| 110 | + user.put("userName_eq", userName); | |
| 110 | 111 | user.put("enabled", true); |
| 111 | 112 | |
| 112 | 113 | Iterator<SysUser> userIterator = sysUserService.list(user).iterator(); |
| ... | ... | @@ -124,7 +125,7 @@ public class UserController extends BaseController<SysUser, Integer> { |
| 124 | 125 | //session里写入用户名,webSocket连接时标识身份用 |
| 125 | 126 | session.setAttribute(Constants.SSO_TOKEN, token); |
| 126 | 127 | session.setAttribute(Constants.SESSION_USERNAME, sysUser.getUserName()); |
| 127 | - session.setAttribute(Constants.RESOURCE_AUTHORITYS, sysUser.getLinks()); | |
| 128 | + //session.setAttribute(Constants.RESOURCE_AUTHORITYS, sysUser.getLinks()); | |
| 128 | 129 | //获取公司权限数据 |
| 129 | 130 | List<CompanyAuthority> cmyAuths = companyAuthorityService.findByUser(sysUser); |
| 130 | 131 | session.setAttribute(Constants.COMPANY_AUTHORITYS, cmyAuths); | ... | ... |
src/main/java/com/bsth/security/handler/CustomLogoutSuccessHandler.java
| ... | ... | @@ -43,7 +43,7 @@ public class CustomLogoutSuccessHandler implements LogoutSuccessHandler { |
| 43 | 43 | param.put("Authorization", String.format("Bearer %s", token)); |
| 44 | 44 | try { |
| 45 | 45 | request.getSession().invalidate(); |
| 46 | - StringBuilder sb = HttpClientUtils.post(ssoLogoutUrl, null, param); | |
| 46 | + StringBuilder sb = HttpClientUtils.post(ssoLogoutUrl, "", param); | |
| 47 | 47 | log.error(String.format("注销token:%s,返回结果:%s", token, sb.toString())); |
| 48 | 48 | } catch (Exception e) { |
| 49 | 49 | log.error("注销token异常", e); | ... | ... |
src/main/java/com/bsth/service/realcontrol/impl/ScheduleRealInfoServiceImpl.java
| ... | ... | @@ -6515,7 +6515,7 @@ public class ScheduleRealInfoServiceImpl extends BaseServiceImpl<ScheduleRealInf |
| 6515 | 6515 | Map<String, Object> res = new HashMap<>(); |
| 6516 | 6516 | res.put("status", ResponseCode.SUCCESS); |
| 6517 | 6517 | try { |
| 6518 | - StringBuilder sb = HttpClientUtils.post(url, null); | |
| 6518 | + StringBuilder sb = HttpClientUtils.post(url, ""); | |
| 6519 | 6519 | if (sb != null) { |
| 6520 | 6520 | Map<String, Object> response = new ObjectMapper().readValue(sb.toString(), Map.class); |
| 6521 | 6521 | if (!"报修成功".equals(response.get("msg"))) { | ... | ... |
src/main/java/com/bsth/util/HttpClientUtils.java
| 1 | 1 | package com.bsth.util; |
| 2 | 2 | |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; | |
| 3 | 5 | import org.apache.http.HttpEntity; |
| 6 | +import org.apache.http.NameValuePair; | |
| 4 | 7 | import org.apache.http.client.config.RequestConfig; |
| 5 | -import org.apache.http.client.entity.EntityBuilder; | |
| 8 | +import org.apache.http.client.entity.UrlEncodedFormEntity; | |
| 6 | 9 | import org.apache.http.client.methods.CloseableHttpResponse; |
| 7 | 10 | import org.apache.http.client.methods.HttpGet; |
| 8 | 11 | import org.apache.http.client.methods.HttpPost; |
| ... | ... | @@ -10,16 +13,21 @@ import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
| 10 | 13 | import org.apache.http.entity.StringEntity; |
| 11 | 14 | import org.apache.http.impl.client.CloseableHttpClient; |
| 12 | 15 | import org.apache.http.impl.client.HttpClients; |
| 16 | +import org.apache.http.message.BasicNameValuePair; | |
| 13 | 17 | import org.slf4j.Logger; |
| 14 | 18 | import org.slf4j.LoggerFactory; |
| 19 | +import org.springframework.util.StringUtils; | |
| 15 | 20 | |
| 16 | 21 | import javax.net.ssl.*; |
| 17 | 22 | import java.io.BufferedReader; |
| 18 | 23 | import java.io.IOException; |
| 19 | 24 | import java.io.InputStreamReader; |
| 25 | +import java.io.UnsupportedEncodingException; | |
| 20 | 26 | import java.security.cert.CertificateException; |
| 21 | 27 | import java.security.cert.X509Certificate; |
| 22 | 28 | import java.util.HashMap; |
| 29 | +import java.util.List; | |
| 30 | +import java.util.ArrayList; | |
| 23 | 31 | import java.util.Map; |
| 24 | 32 | |
| 25 | 33 | /** |
| ... | ... | @@ -29,6 +37,8 @@ public class HttpClientUtils { |
| 29 | 37 | |
| 30 | 38 | static Logger logger = LoggerFactory.getLogger(HttpClientUtils.class); |
| 31 | 39 | |
| 40 | + private static ObjectMapper mapper = new ObjectMapper(); | |
| 41 | + | |
| 32 | 42 | private final static String HTTPS = "https://"; |
| 33 | 43 | |
| 34 | 44 | private static SSLConnectionSocketFactory sslConnectionSocketFactory; |
| ... | ... | @@ -129,7 +139,7 @@ public class HttpClientUtils { |
| 129 | 139 | .setConnectTimeout(15000).setConnectionRequestTimeout(15000) |
| 130 | 140 | .setSocketTimeout(15000).build(); |
| 131 | 141 | post.setConfig(requestConfig); |
| 132 | - if (data != null) { | |
| 142 | + if (!StringUtils.isEmpty(data)) { | |
| 133 | 143 | post.setEntity((new StringEntity(data, "UTF-8"))); |
| 134 | 144 | } |
| 135 | 145 | |
| ... | ... | @@ -146,6 +156,68 @@ public class HttpClientUtils { |
| 146 | 156 | return stringBuffer; |
| 147 | 157 | } |
| 148 | 158 | |
| 159 | + public static StringBuilder post(String url, HttpEntity entity) throws Exception { | |
| 160 | + return post(url, entity, new HashMap<>()); | |
| 161 | + } | |
| 162 | + | |
| 163 | + public static StringBuilder post(String url, HttpEntity entity, Map<String, Object> headers) throws Exception { | |
| 164 | + CloseableHttpClient httpClient = null; | |
| 165 | + CloseableHttpResponse response = null; | |
| 166 | + StringBuilder stringBuffer = null; | |
| 167 | + try { | |
| 168 | + httpClient = defaultHttpClient(url); | |
| 169 | + HttpPost post = new HttpPost(url); | |
| 170 | + | |
| 171 | + post.setHeader("Accept", "application/json"); | |
| 172 | + post.setHeader("Content-Type", "application/json;charset=UTF-8"); | |
| 173 | + if (headers.size() > 0) { | |
| 174 | + for (Map.Entry<String, Object> header : headers.entrySet()) { | |
| 175 | + post.setHeader(header.getKey(), String.valueOf(header.getValue())); | |
| 176 | + } | |
| 177 | + } | |
| 178 | + //超时时间 | |
| 179 | + RequestConfig requestConfig = RequestConfig.custom() | |
| 180 | + .setConnectTimeout(5000).setConnectionRequestTimeout(5000) | |
| 181 | + .setSocketTimeout(5000).build(); | |
| 182 | + post.setConfig(requestConfig); | |
| 183 | + if (entity != null) { | |
| 184 | + post.setEntity(entity); | |
| 185 | + } | |
| 186 | + | |
| 187 | + response = httpClient.execute(post); | |
| 188 | + stringBuffer = getResult(response.getEntity()); | |
| 189 | + } catch (Exception e) { | |
| 190 | + logger.error("", e); | |
| 191 | + } finally { | |
| 192 | + if (null != httpClient) | |
| 193 | + httpClient.close(); | |
| 194 | + if (null != response) | |
| 195 | + response.close(); | |
| 196 | + } | |
| 197 | + return stringBuffer; | |
| 198 | + } | |
| 199 | + | |
| 200 | + public static StringEntity createJsonEntity(Object data) throws JsonProcessingException, UnsupportedEncodingException { | |
| 201 | + return new StringEntity(mapper.writeValueAsString(data)); | |
| 202 | + } | |
| 203 | + | |
| 204 | + public static UrlEncodedFormEntity createFormEntity(Map<String, Object> data) throws UnsupportedEncodingException { | |
| 205 | + List<NameValuePair> pairs = new ArrayList<>(); | |
| 206 | + for (Map.Entry<String, Object> entry : data.entrySet()) { | |
| 207 | + pairs.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue()))); | |
| 208 | + } | |
| 209 | + | |
| 210 | + return new UrlEncodedFormEntity(pairs); | |
| 211 | + } | |
| 212 | + | |
| 213 | + public static Map<String, Object> createFormHeader() throws UnsupportedEncodingException { | |
| 214 | + Map<String, Object> headers = new HashMap<>(); | |
| 215 | + headers.put("Accept", "*/*"); | |
| 216 | + headers.put("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); | |
| 217 | + | |
| 218 | + return headers; | |
| 219 | + } | |
| 220 | + | |
| 149 | 221 | private static StringBuilder getResult(HttpEntity entity) throws IOException { |
| 150 | 222 | StringBuilder stringBuffer = null; |
| 151 | 223 | if (null != entity) { | ... | ... |