| | |
| | | import com.fzzy.api.utils.ContextUtil; |
| | | import com.fzzy.api.utils.RedisConst; |
| | | import com.fzzy.api.utils.RedisUtil; |
| | | import com.fzzy.gateway.entity.GatewayConf; |
| | | import com.fzzy.gateway.hx2023.data.GatewayAuthData; |
| | | import com.fzzy.gateway.service.GatewayConfService; |
| | | import com.ld.license.LicenseVerify; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Controller; |
| | | import org.springframework.stereotype.Repository; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.util.List; |
| | | import java.util.UUID; |
| | | |
| | | /** |
| | | * |
| | | */ |
| | | @Slf4j |
| | | @Controller |
| | | @RequestMapping |
| | | @RequestMapping("/sc2023/gateway") |
| | | public class GatewayController { |
| | | |
| | | @Resource |
| | | private RedisUtil redisUtil; |
| | | @Resource |
| | | private GatewayConfService confService; |
| | | |
| | | |
| | | /** |
| | | * 鉴权接口 |
| | |
| | | public @ResponseBody |
| | | JSONObject authorize(@RequestBody GatewayAuthData data) { |
| | | |
| | | log.debug("============鉴权==========={}--{}", data.getUsername(), data.getPassword()); |
| | | |
| | | List<GatewayConf> list = confService.getCacheConfList(); |
| | | |
| | | JSONObject json = new JSONObject(); |
| | | json.put("timestamp", System.currentTimeMillis()); |
| | | if (null == list || list.isEmpty()) { |
| | | json.put("code", 500); |
| | | json.put("message", "未获取网关信息"); |
| | | return json; |
| | | } |
| | | |
| | | String gatewayId = null; |
| | | for (GatewayConf conf : list) { |
| | | if (data.getUsername().equals(conf.getGatewayUsername()) && data.getPassword().equals(conf.getGatewayPassword())) { |
| | | gatewayId = conf.getGatewayId(); |
| | | break; |
| | | } |
| | | } |
| | | |
| | | if (null == gatewayId) { |
| | | json.put("code", 500); |
| | | json.put("message", "未匹配到用户名和密码"); |
| | | return json; |
| | | } |
| | | |
| | | |
| | | //TODO 验证用户名和密码 |
| | | String token = "fzzy-" + UUID.randomUUID(); |
| | | |
| | | |
| | | String token = ContextUtil.getUUID(); |
| | | log.debug("============鉴权==========={}--{}--{}", data.getUsername(), data.getPassword(), token); |
| | | |
| | | this.updateGatewayToken(token, data.getUsername()); |
| | | |
| | | JSONObject json = new JSONObject(); |
| | | |
| | | JSONObject result = new JSONObject(); |
| | | |
| | | result.put("token", token); |
| | | |
| | | json.put("result", result); |
| | | json.put("message", "成功"); |
| | | json.put("status", 0); |
| | | json.put("code", 200); |
| | | json.put("timestamp", System.currentTimeMillis()); |
| | | try{ |
| | | LicenseVerify licenseVerify = new LicenseVerify(); |
| | | //校验证书是否有效 |
| | | boolean verifyResult = licenseVerify.verify(); |
| | | |
| | | return json; |
| | | if(verifyResult){ |
| | | return json; |
| | | }else{ |
| | | log.error("证书验证失败!拒绝访问"); |
| | | return null; |
| | | } |
| | | }catch (Exception e){ |
| | | log.error("证书验证失败:" + e.getMessage(),e); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | public void updateGatewayToken(String token, String username) { |