重构中

This commit is contained in:
zhujj 2018-12-05 18:12:18 +08:00
parent 0f9e5f4d53
commit 4dd5f7df3d
9 changed files with 331 additions and 35 deletions

View File

@ -1,14 +1,6 @@
package com.ruoyi.framework.util;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URLDecoder;
import java.util.Date;
/**
@ -43,7 +35,7 @@ public class EntityUtils {
Method[] methods = entity.getClass().getMethods();
for(Method m : methods){
if(m.getName().equals("setCreateBy")){
m.invoke(entity, ShiroUtils.getUserId());
m.invoke(entity, com.ruoyi.framework.web.util.ShiroUtils.getUserId());
}
}
} catch (Exception e) {
@ -62,7 +54,7 @@ public class EntityUtils {
Method[] methods = entity.getClass().getMethods();
for(Method m : methods){
if(m.getName().equals("setUpdateBy")){
m.invoke(entity, ShiroUtils.getUserId());
m.invoke(entity, com.ruoyi.framework.web.util.ShiroUtils.getUserId());
}
}
} catch (Exception e) {

View File

@ -1,14 +1,15 @@
package com.ruoyi.framework.util;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/**
* 客户端工具类

View File

@ -1,16 +1,16 @@
package com.ruoyi.framework.util;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.framework.shiro.realm.UserRealm;
import com.ruoyi.system.domain.SysUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;
/**
* shiro 工具类

View File

@ -6,7 +6,6 @@ import com.ruoyi.framework.util.EntityUtils;
import com.ruoyi.framework.web.page.PageDomain;
import com.ruoyi.framework.web.page.TableSupport;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.common.Mapper;
import java.util.List;
@ -16,7 +15,7 @@ import java.util.List;
* Time: 15:13
* Version 1.0.0
*/
public abstract class AbstractBaseServiceImpl<M extends MyMapper<T>, T> implements AbstractBaseService<T>{
public abstract class AbstractBaseServiceImpl<M extends MyMapper<T>, T> implements AbstractBaseService<T> {
@Autowired
protected M mapper;
public void setMapper(M mapper) {

View File

@ -1,21 +1,18 @@
package com.ruoyi.framework.web.base;
import java.beans.PropertyEditorSupport;
import java.util.Date;
import java.util.List;
import com.ruoyi.system.domain.SysUser;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.framework.web.page.PageDomain;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.page.TableSupport;
import com.ruoyi.system.domain.SysUser;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import java.beans.PropertyEditorSupport;
import java.util.Date;
import java.util.List;
/**
* web层通用数据处理

View File

@ -22,6 +22,13 @@
<artifactId>ruoyi-common</artifactId>
<version>${ruoyi.version}</version>
</dependency>
<!-- 通用工具-->
<dependency>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-framework</artifactId>
<version>${ruoyi.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,65 @@
package com.ruoyi.framework.web.util;
import java.lang.reflect.Method;
/**
* 实体类相关工具类
* 解决问题 1快速对实体的常驻字段createUserupdateUser等值快速注入
*
* @author Ace
* @version 1.0
* @date 2016年4月18日
* @since 1.7
*/
public class EntityUtils {
/**
* 快速将bean的crtUsercrtHostcrtTimeupdUserupdHostupdTime附上相关值
*
* @param entity 实体bean
* @author 王浩彬
*/
public static <T> void setCreatAndUpdatInfo(T entity) {
setCreateInfo(entity);
setUpdatedInfo(entity);
}
/**
* 快速将bean的crtUsercrtHostcrtTime附上相关值
*
* @param entity 实体bean
* @author 王浩彬
*/
public static <T> void setCreateInfo(T entity){
try {
Method[] methods = entity.getClass().getMethods();
for(Method m : methods){
if(m.getName().equals("setCreateBy")){
m.invoke(entity, com.ruoyi.framework.web.util.ShiroUtils.getUserId());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 快速将bean的updUserupdHostupdTime附上相关值
*
* @param entity 实体bean
* @author 王浩彬
*/
public static <T> void setUpdatedInfo(T entity){
try {
Method[] methods = entity.getClass().getMethods();
for(Method m : methods){
if(m.getName().equals("setUpdateBy")){
m.invoke(entity, com.ruoyi.framework.web.util.ShiroUtils.getUserId());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,139 @@
package com.ruoyi.framework.web.util;
import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/**
* 客户端工具类
*
* @author ruoyi
*/
public class ServletUtils
{
/**
* 获取String参数
*/
public static String getParameter(String name)
{
return getRequest().getParameter(name);
}
/**
* 获取String参数
*/
public static String getParameter(String name, String defaultValue)
{
return Convert.toStr(getRequest().getParameter(name), defaultValue);
}
/**
* 获取Integer参数
*/
public static Integer getParameterToInt(String name)
{
return Convert.toInt(getRequest().getParameter(name));
}
/**
* 获取Integer参数
*/
public static Integer getParameterToInt(String name, Integer defaultValue)
{
return Convert.toInt(getRequest().getParameter(name), defaultValue);
}
/**
* 获取request
*/
public static HttpServletRequest getRequest()
{
return getRequestAttributes().getRequest();
}
/**
* 获取response
*/
public static HttpServletResponse getResponse()
{
return getRequestAttributes().getResponse();
}
/**
* 获取session
*/
public static HttpSession getSession()
{
return getRequest().getSession();
}
public static ServletRequestAttributes getRequestAttributes()
{
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
return (ServletRequestAttributes) attributes;
}
/**
* 将字符串渲染到客户端
*
* @param response 渲染对象
* @param string 待渲染的字符串
* @return null
*/
public static String renderString(HttpServletResponse response, String string)
{
try
{
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().print(string);
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
/**
* 是否是Ajax异步请求
*
* @param request
*/
public static boolean isAjaxRequest(HttpServletRequest request)
{
String accept = request.getHeader("accept");
if (accept != null && accept.indexOf("application/json") != -1)
{
return true;
}
String xRequestedWith = request.getHeader("X-Requested-With");
if (xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1)
{
return true;
}
String uri = request.getRequestURI();
if (StringUtils.inStringIgnoreCase(uri, ".json", ".xml"))
{
return true;
}
String ajax = request.getParameter("__ajax");
if (StringUtils.inStringIgnoreCase(ajax, "json", "xml"))
{
return true;
}
return false;
}
}

View File

@ -0,0 +1,96 @@
package com.ruoyi.framework.web.util;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.framework.shiro.realm.UserRealm;
import com.ruoyi.system.domain.SysUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;
/**
* shiro 工具类
*
* @author ruoyi
*/
public class ShiroUtils
{
public static Subject getSubjct()
{
return SecurityUtils.getSubject();
}
public static Session getSession()
{
return SecurityUtils.getSubject().getSession();
}
public static void logout()
{
getSubjct().logout();
}
public static SysUser getSysUser()
{
SysUser user = null;
Object obj = getSubjct().getPrincipal();
if (StringUtils.isNotNull(obj))
{
user = new SysUser();
BeanUtils.copyBeanProp(user, obj);
}
return user;
}
public static void setSysUser(SysUser user)
{
Subject subject = getSubjct();
PrincipalCollection principalCollection = subject.getPrincipals();
String realmName = principalCollection.getRealmNames().iterator().next();
PrincipalCollection newPrincipalCollection = new SimplePrincipalCollection(user, realmName);
// 重新加载Principal
subject.runAs(newPrincipalCollection);
}
public static void clearCachedAuthorizationInfo()
{
RealmSecurityManager rsm = (RealmSecurityManager) SecurityUtils.getSecurityManager();
UserRealm realm = (UserRealm) rsm.getRealms().iterator().next();
realm.clearCachedAuthorizationInfo();
}
public static Long getUserId()
{
return getSysUser().getUserId().longValue();
}
public static String getLoginName()
{
return getSysUser().getLoginName();
}
public static String getIp()
{
return getSubjct().getSession().getHost();
}
public static String getSessionId()
{
return String.valueOf(getSubjct().getSession().getId());
}
/**
* 生成随机盐
*/
public static String randomSalt()
{
// 一个Byte占两个字节此处生成的3字节字符串长度为6
SecureRandomNumberGenerator secureRandom = new SecureRandomNumberGenerator();
String hex = secureRandom.nextBytes(3).toHex();
return hex;
}
}