mirror of
https://gitee.com/dromara/MaxKey.git
synced 2026-05-20 12:48:09 +08:00
Configuration
This commit is contained in:
@@ -1,10 +0,0 @@
|
||||
package org.maxkey;
|
||||
|
||||
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
||||
|
||||
public class T extends SpringBootServletInitializer {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("MaxKeyMgtApplication");
|
||||
}
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
package org.maxkey.web.endpoint;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.maxkey.web.WebConstants;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import com.google.code.kaptcha.Producer;
|
||||
|
||||
/**
|
||||
* @author Crystal.Sea
|
||||
*
|
||||
*/
|
||||
@Controller
|
||||
@RequestMapping(value = "/captcha")
|
||||
public class CaptchaEndpoint {
|
||||
private static final Logger _logger = LoggerFactory.getLogger(CaptchaEndpoint.class);
|
||||
|
||||
@Autowired
|
||||
private Producer captchaProducer;
|
||||
|
||||
/**
|
||||
* captcha image Producer
|
||||
* @param request
|
||||
* @param response
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
|
||||
@RequestMapping
|
||||
public ModelAndView captchaHandleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception {
|
||||
// Set to expire far in the past.
|
||||
response.setDateHeader("Expires", 0);
|
||||
// Set standard HTTP/1.1 no-cache headers.
|
||||
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
|
||||
// Set IE extended HTTP/1.1 no-cache headers (use addHeader).
|
||||
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
|
||||
// Set standard HTTP/1.0 no-cache header.
|
||||
response.setHeader("Pragma", "no-cache");
|
||||
// return a jpeg
|
||||
response.setContentType("image/jpeg");
|
||||
// create the text for the image
|
||||
String capText = captchaProducer.createText();
|
||||
_logger.debug("Captcha Text : "+capText);
|
||||
// store the text in the session
|
||||
request.getSession().setAttribute(WebConstants.KAPTCHA_SESSION_KEY, capText);
|
||||
// create the image with the text
|
||||
BufferedImage bi = captchaProducer.createImage(capText);
|
||||
ServletOutputStream out = response.getOutputStream();
|
||||
// write the data out
|
||||
ImageIO.write(bi, "jpg", out);
|
||||
try{
|
||||
out.flush();
|
||||
}finally{
|
||||
out.close();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@ package org.maxkey.web.endpoint;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
@@ -9,30 +11,73 @@ import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.maxkey.web.WebConstants;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import com.google.code.kaptcha.Producer;
|
||||
|
||||
/**
|
||||
* @author Crystal.Sea
|
||||
*
|
||||
*/
|
||||
@Controller
|
||||
@RequestMapping(value = "/image")
|
||||
public class ImageEndpoint {
|
||||
|
||||
private static final Logger _logger = LoggerFactory.getLogger(ImageEndpoint.class);
|
||||
|
||||
@Autowired
|
||||
private Producer captchaProducer;
|
||||
|
||||
/**
|
||||
* captcha image Producer
|
||||
* @param request
|
||||
* @param response
|
||||
*/
|
||||
@RequestMapping(value = "/captcha")
|
||||
public void captchaHandleRequest(HttpServletRequest request,HttpServletResponse response){
|
||||
try{
|
||||
// Set to expire far in the past.
|
||||
response.setDateHeader("Expires", 0);
|
||||
// Set standard HTTP/1.1 no-cache headers.
|
||||
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
|
||||
// Set IE extended HTTP/1.1 no-cache headers (use addHeader).
|
||||
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
|
||||
// Set standard HTTP/1.0 no-cache header.
|
||||
response.setHeader("Pragma", "no-cache");
|
||||
// return a jpeg
|
||||
response.setContentType("image/jpeg");
|
||||
// create the text for the image
|
||||
String capText = captchaProducer.createText();
|
||||
_logger.debug("Sesssion id " + request.getSession().getId() + " , Captcha Text is " + capText);
|
||||
// store the text in the session
|
||||
request.getSession().setAttribute(WebConstants.KAPTCHA_SESSION_KEY, capText);
|
||||
// create the image with the text
|
||||
BufferedImage bi = captchaProducer.createImage(capText);
|
||||
ServletOutputStream out = response.getOutputStream();
|
||||
// write the data out
|
||||
ImageIO.write(bi, "jpg", out);
|
||||
|
||||
out.flush();
|
||||
out.close();
|
||||
}catch(Exception e) {
|
||||
_logger.error("captcha Producer Error " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* image Producer
|
||||
* @param request
|
||||
* @param response
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
|
||||
@RequestMapping("/{id}")
|
||||
public ModelAndView imageHandleRequest(HttpServletRequest request,HttpServletResponse response,@PathVariable("id") String id) throws Exception {
|
||||
@RequestMapping("/image/{id}")
|
||||
public void imageHandleRequest(HttpServletRequest request,HttpServletResponse response,@PathVariable("id") String id) throws Exception {
|
||||
// Set to expire far in the past.
|
||||
response.setDateHeader("Expires", 0);
|
||||
// Set standard HTTP/1.1 no-cache headers.
|
||||
@@ -45,20 +90,49 @@ public class ImageEndpoint {
|
||||
response.setContentType("image/gif");
|
||||
// create the text for the image
|
||||
byte[]image=(byte[]) request.getSession().getAttribute(id);
|
||||
request.getSession().removeAttribute(id);
|
||||
//request.getSession().removeAttribute(id);
|
||||
// create the image with the text
|
||||
if(image!=null){
|
||||
InputStream in = new ByteArrayInputStream(image);
|
||||
BufferedImage bi = ImageIO.read(in);
|
||||
ServletOutputStream out = response.getOutputStream();
|
||||
// write the data out
|
||||
ImageIO.write(bi, "gif", out);
|
||||
ImageIO.write(byte2BufferedImage(image), "gif", out);
|
||||
try{
|
||||
out.flush();
|
||||
}finally{
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static BufferedImage byte2BufferedImage(byte[]imageByte){
|
||||
try {
|
||||
InputStream in = new ByteArrayInputStream(imageByte);
|
||||
BufferedImage bufferedImage = ImageIO.read(in);
|
||||
return bufferedImage;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static byte[] bufferedImage2Byte(BufferedImage bufferedImage ){
|
||||
try {
|
||||
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
|
||||
ImageIO.write(bufferedImage,"gif",byteArrayOutputStream);
|
||||
return byteArrayOutputStream.toByteArray();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Producer getCaptchaProducer() {
|
||||
return captchaProducer;
|
||||
}
|
||||
|
||||
public void setCaptchaProducer(Producer captchaProducer) {
|
||||
this.captchaProducer = captchaProducer;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package org.maxkey.web.interceptor;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import javax.servlet.RequestDispatcher;
|
||||
@@ -8,8 +7,6 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.maxkey.config.ApplicationConfig;
|
||||
import org.maxkey.domain.Navigations;
|
||||
import org.maxkey.domain.UserInfo;
|
||||
import org.maxkey.web.WebContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -40,55 +37,17 @@ public class PermissionAdapter extends HandlerInterceptorAdapter {
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
|
||||
_logger.debug("PermissionAdapter preHandle");
|
||||
//加载定义的功能菜单地址
|
||||
/*if(navigationsMap==null){
|
||||
List<Navigations> navigationsList=((NavigationsService)WebContext.getBean("navigationsService")).query(null);
|
||||
navigationsMap=new ConcurrentHashMap<String ,String >();
|
||||
for(Navigations nav : navigationsList){
|
||||
if(nav.getUrl()==null)continue;
|
||||
if(nav.getUrl().endsWith("/")){
|
||||
navigationsMap.put("/"+nav.getUrl(), nav.getId());
|
||||
}else{
|
||||
navigationsMap.put("/"+nav.getUrl()+"/", nav.getId());
|
||||
}
|
||||
}
|
||||
_logger.debug("navigationsMap : "+navigationsMap);
|
||||
}
|
||||
|
||||
UserInfo userInfo =WebContext.getUserInfo();//取得登录用户
|
||||
|
||||
if(userInfo==null||WebContext.getRoles()==null){//判断用户和角色,判断用户是否登录用户
|
||||
//判断用户是否登录
|
||||
if(WebContext.getAuthentication()==null||WebContext.getAuthentication().getAuthorities()==null){//判断用户和角色,判断用户是否登录用户
|
||||
RequestDispatcher dispatcher = request.getRequestDispatcher("/login");
|
||||
dispatcher.forward(request, response);
|
||||
return false;
|
||||
}
|
||||
|
||||
//取得当前访问地址 Access URL
|
||||
String accessURI=request.getRequestURI().substring(request.getContextPath().length());
|
||||
if(!accessURI.endsWith("/")){
|
||||
accessURI=accessURI+"/";
|
||||
}
|
||||
//定义匿名可以访问URL地址
|
||||
if(applicationConfig.getAnonymousAccessUrls().containsKey(accessURI)){
|
||||
_logger.debug("Access URI : "+accessURI+" , AnonymousAccessUrls .");
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean hasNavAccess=true;
|
||||
//菜单权限匹配
|
||||
if(navigationsMap.containsKey(accessURI)){//判断当前访问URL地址是否需要进行权限校验
|
||||
hasNavAccess=false;
|
||||
for(Navigations nav : WebContext.getNavigations()){//获取当前登录用户拥有URL访问列表
|
||||
String haveURL=nav.getUrl();
|
||||
if(haveURL==null)continue;
|
||||
if(!haveURL.endsWith("/")){haveURL="/"+haveURL+"/";}
|
||||
if(haveURL.endsWith(accessURI)){
|
||||
hasNavAccess=true;
|
||||
}
|
||||
}
|
||||
_logger.debug("Access URI : "+accessURI+" , hasNavAccess "+hasNavAccess);
|
||||
if(hasNavAccess)return true;
|
||||
}
|
||||
boolean hasAccess=true;
|
||||
|
||||
|
||||
/*
|
||||
boolean preHandler = super.preHandle(request, response, handler);
|
||||
@@ -104,6 +63,6 @@ public class PermissionAdapter extends HandlerInterceptorAdapter {
|
||||
return false;
|
||||
}
|
||||
}*/
|
||||
return true;
|
||||
return hasAccess;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user