|
|
@@ -0,0 +1,57 @@
|
|
|
+package com.uas.sso.sso.backend.web;
|
|
|
+
|
|
|
+import com.uas.sso.sso.backend.api.AppManagerController;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.PrintWriter;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
+
|
|
|
+/**
|
|
|
+ * An implementation class for app.
|
|
|
+ *
|
|
|
+ * @author huxz
|
|
|
+ */
|
|
|
+@Controller
|
|
|
+@RequestMapping(path = "/api/app")
|
|
|
+public class AppController {
|
|
|
+
|
|
|
+ @RequestMapping(method = RequestMethod.GET, path = "/private//download",
|
|
|
+ produces = MediaType.TEXT_HTML_VALUE)
|
|
|
+ public ResponseEntity<byte[]> downloadKey(HttpServletRequest request,
|
|
|
+ HttpServletResponse response) throws IOException {
|
|
|
+ String privateKey = (String) request.getSession()
|
|
|
+ .getAttribute(AppManagerController.SESSION_PRIVATE_KEY);
|
|
|
+
|
|
|
+ if (StringUtils.isEmpty(privateKey)) {
|
|
|
+ printErrors(response, "未找到密钥,请重新生成");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // Avoid download private key repeatedly
|
|
|
+ request.getSession().removeAttribute(AppManagerController.SESSION_PRIVATE_KEY);
|
|
|
+ return outputStream("privateKey.txt", privateKey.getBytes());
|
|
|
+ }
|
|
|
+
|
|
|
+ private ResponseEntity<byte[]> outputStream(String fileName, byte[] bytes) {
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
|
|
|
+ headers.setContentDispositionFormData("attachment", fileName);
|
|
|
+ return new ResponseEntity<>(bytes, headers, HttpStatus.CREATED);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void printErrors(HttpServletResponse response,
|
|
|
+ String error) throws IOException {
|
|
|
+ response.addHeader("Content-Type", "text/html; charset=UTF-8");
|
|
|
+ PrintWriter printWriter = response.getWriter();
|
|
|
+ printWriter.append(error);
|
|
|
+ printWriter.flush();
|
|
|
+ printWriter.close();
|
|
|
+ }
|
|
|
+}
|