spring jasypt加密

下载 jasypt-1.9.2.jar

 

 


		
			
				classpath:config.properties
			
		
		
		
			
				orcl.password
				sql.password
			
		


 

public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

	private Set encryptedProps = Collections.emptySet();

	public void setEncryptedProps(Set encryptedProps) {
		this.encryptedProps = encryptedProps;
	}
	
	@Override
	protected String convertProperty(String propertyName, String propertyValue) {
		if (encryptedProps.contains(propertyName)) {
			return EncryptUtil.decode(propertyValue);
		}
		return super.convertProperty(propertyName, propertyValue);
	}

}


 




	
	
	
    
    
    
    
    
    
    
      
	      
	      
	    
	      
	      
	  
    
      
          
        
          
      

 

public class ExceptionHandler implements HandlerExceptionResolver {
	
	private static Logger LOGGER = LoggerFactory.getLogger(ExceptionHandler.class); 

	@Override
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object obj, Exception e) {
		LOGGER.error("异常:" + e.getMessage(), e);
		ModelAndView mav = new ModelAndView("exception");
		boolean ajaxRequest = isAjaxRequest(request);
		
		// 处理文件太大异常
		if (e instanceof MaxUploadSizeExceededException) {
			if (ajaxRequest) {
				JSONObject json = new JSONObject();
				json.put(ExceptionConstants.SYS_EXCEPTION_FLAG, true);
				json.put(ExceptionConstants.SYS_EXCEPTION_REASON, "文件太大!");
				
				write(response, json.toString());
				return null;
			} else {
				mav.addObject(ExceptionConstants.SYS_EXCEPTION_REASON, "文件太大!");
				return mav;
			}
		}
		
		// 系统自定义异常
		if (e instanceof AppException) {
			if (ajaxRequest) {
				JSONObject json = new JSONObject();
				json.put(ExceptionConstants.SYS_EXCEPTION_FLAG, true);
				json.put(ExceptionConstants.SYS_EXCEPTION_REASON, e.getMessage());
				
				write(response, json.toString());
				return null;
			} else {
				mav.addObject(ExceptionConstants.SYS_EXCEPTION_REASON, e.getMessage());
				return mav;
			}
		}
		
		if (ajaxRequest) {
			JSONObject json = new JSONObject();
			json.put(ExceptionConstants.SYS_EXCEPTION_FLAG, true);
			json.put(ExceptionConstants.SYS_EXCEPTION_REASON, "出错了,请联系管理员!");
			
			write(response, json.toString());
			return null;
		}
		mav.addObject(ExceptionConstants.SYS_EXCEPTION_REASON, "出错了,请联系管理员!");
		return mav;  
	}
	
	/**
	 * 判断是否为ajax请求
	 * @param request
	 * @return
	 */
	private boolean isAjaxRequest(HttpServletRequest request) {
		// jquery 1.7+(之前版本未测试) 会在 XMLHttpRequest 的 Header 设置 X-Requested-With
		// 一些非 jquery插件的ajax请求 采用 url验证
		String xhr = request.getHeader("X-Requested-With");
		boolean jQueryAjax = "XMLHttpRequest".equals(xhr);
		
		// 采用url区分ajax请求
		boolean urlAjax = request.getRequestURI().matches(".*_Ajax$");
		return jQueryAjax || urlAjax;
	}
	
	private void write(HttpServletResponse response, String message)  {
		try {
			response.setContentType("text/json;charset=utf-8");   
			PrintWriter writer = response.getWriter();
			writer.write(message);   
			writer.flush();   
		} catch (IOException e) {
			e.printStackTrace();
		}   
	}
	
}


 

(function($){   
   //备份jquery的ajax方法   
   var _ajax=$.ajax;   
      
   //重写jquery的ajax方法   
   $.ajax=function(opt){   
       //备份opt中error和success方法   
       var fn = {   
           error:function(XMLHttpRequest, textStatus, errorThrown){},   
            success:function(data, textStatus){}   
        }   
        if(opt.error){   
            fn.error=opt.error;   
        }   
        if(opt.success){   
            fn.success=opt.success;   
        }   
           
        //扩展增强处理   
        var _opt = $.extend(opt,{   
            error:function(XMLHttpRequest, textStatus, errorThrown){   
                //错误方法增强处理   
            	if (layer) {
            		layer.msg("出错了,请联系管理员!", 2, 3, null, true);
            	} else {
            		alert("出错了,请联系管理员!");
            	}
                fn.error(XMLHttpRequest, textStatus, errorThrown);   
            },   
            success:function(data, textStatus){   
                //成功回调方法增强处理   
                var error = data.error;
            	if (error != undefined && error == true) {
            		if (layer) {
            			layer.msg(data.reason, 2, 3, null, true);
            		} else {
            			alert(data.reason);
            		}
					return;
				}
                fn.success(data, textStatus);   
            }   
        });   
        _ajax(_opt);   
    };   
})(jQuery);  



 

@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public void upload(
			@RequestParam("files") CommonsMultipartFile[] files,
			HttpServletRequest request,
			HttpServletResponse response) throws Exception {
	
		if (files != null || files.length > 0) {
			String fileName = files[0].getOriginalFilename();
			files[0].getInputStream();
			
			// do ...
		}
	}


你可能感兴趣的:(java)