First Commit
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package quant.rich.emoney.controller.common;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
import quant.rich.emoney.service.AuthService;
|
||||
|
||||
@Controller
|
||||
public abstract class BaseController {
|
||||
|
||||
@Autowired
|
||||
protected HttpServletRequest request;
|
||||
|
||||
@Autowired
|
||||
protected HttpServletResponse response;
|
||||
|
||||
@Autowired
|
||||
protected HttpSession session;
|
||||
|
||||
@Autowired
|
||||
AuthService authService;
|
||||
|
||||
protected Boolean isLogin() {
|
||||
return authService.isLogin();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package quant.rich.emoney.controller.common;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.web.error.ErrorAttributeOptions;
|
||||
import org.springframework.boot.web.error.ErrorAttributeOptions.Include;
|
||||
import org.springframework.boot.web.servlet.error.ErrorAttributes;
|
||||
import org.springframework.boot.web.servlet.error.ErrorController;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
|
||||
import jakarta.servlet.RequestDispatcher;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import java.util.Map;
|
||||
|
||||
@Controller
|
||||
public class ErrorPageController implements ErrorController {
|
||||
|
||||
@Autowired
|
||||
ErrorAttributes errorAttributes;
|
||||
|
||||
public final static String ERROR_PATH = "/error";
|
||||
|
||||
@GetMapping(value = ERROR_PATH)
|
||||
@PostMapping(value = ERROR_PATH)
|
||||
public String errorHtml(HttpServletRequest request) {
|
||||
HttpStatus status = getStatus(request);
|
||||
String prefix = "error/";
|
||||
|
||||
return prefix + "error_400";
|
||||
|
||||
// switch (status) {
|
||||
// case BAD_REQUEST:
|
||||
// return prefix + "error_400";
|
||||
// case NOT_FOUND:
|
||||
// return prefix + "error_404";
|
||||
// case METHOD_NOT_ALLOWED:
|
||||
// return prefix + "error_405";
|
||||
// default:
|
||||
// return prefix + "error_5xx";
|
||||
// }
|
||||
}
|
||||
|
||||
@GetMapping(value = ERROR_PATH, produces = "application/json")
|
||||
@PostMapping(value = ERROR_PATH, produces = "application/json")
|
||||
@ResponseBody
|
||||
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
|
||||
Map<String, Object> body = getErrorAttributes(request, getTraceParameter(request));
|
||||
HttpStatus status = getStatus(request);
|
||||
return new ResponseEntity<Map<String, Object>>(body, status);
|
||||
}
|
||||
|
||||
private boolean getTraceParameter(HttpServletRequest request) {
|
||||
String parameter = request.getParameter("trace");
|
||||
if (parameter == null) {
|
||||
return false;
|
||||
}
|
||||
return !"false".equals(parameter.toLowerCase());
|
||||
}
|
||||
|
||||
protected Map<String, Object> getErrorAttributes(HttpServletRequest request, boolean includeStackTrace) {
|
||||
WebRequest webRequest = new ServletWebRequest(request);
|
||||
if (includeStackTrace) {
|
||||
return this.errorAttributes.getErrorAttributes(webRequest,
|
||||
ErrorAttributeOptions.defaults().including(Include.STACK_TRACE));
|
||||
}
|
||||
return this.errorAttributes.getErrorAttributes(webRequest, ErrorAttributeOptions.defaults());
|
||||
}
|
||||
|
||||
private HttpStatus getStatus(HttpServletRequest request) {
|
||||
Integer statusCode = (Integer) request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
|
||||
if (statusCode != null) {
|
||||
try {
|
||||
return HttpStatus.valueOf(statusCode);
|
||||
} catch (Exception ex) {
|
||||
}
|
||||
}
|
||||
return HttpStatus.INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package quant.rich.emoney.controller.common;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.google.code.kaptcha.impl.DefaultKaptcha;
|
||||
|
||||
import quant.rich.emoney.service.AuthService;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/captcha")
|
||||
public class KaptchaController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
DefaultKaptcha kaptcha;
|
||||
|
||||
@GetMapping(value = "/get", produces = MediaType.IMAGE_JPEG_VALUE)
|
||||
public byte[] getCaptcha() throws Exception {
|
||||
String createText = kaptcha.createText();
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
session.setAttribute(AuthService.CAPTCHA, createText);
|
||||
ImageIO.write(kaptcha.createImage(createText), "jpg", os);
|
||||
byte[] result = os.toByteArray();
|
||||
os.close();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user