first commit

This commit is contained in:
ouburikou
2018-02-05 18:59:21 +08:00
commit bb00973025
957 changed files with 231677 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>shiro-springboot-demo</artifactId>
<groupId>demo.xinwin</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>demo-shiro-service</artifactId>
<dependencies>
<dependency>
<groupId>demo.xinwin</groupId>
<artifactId>demo-shiro-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,12 @@
package com.xinwei.dao;
import java.io.Serializable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.PagingAndSortingRepository;
@NoRepositoryBean
public interface BaseDao<T, pk extends Serializable> extends PagingAndSortingRepository<T, Serializable>,
JpaSpecificationExecutor<T> {
}

View File

@@ -0,0 +1,24 @@
package com.xinwei.dao;
import java.util.List;
import javax.transaction.Transactional;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Service;
import com.xinwei.shirofunction.Function;
@Transactional
@Service
public interface FunctionDao extends BaseDao<Function, Long> {
@Query("from Function")
public List<Function> findList();
public Function findByPermissionName(String permissionName);
public Function findByPid(Integer pid);
@Query("from Function where pid=?")
public List<Function> findListByPid(Integer pid);
}

View File

@@ -0,0 +1,23 @@
package com.xinwei.dao;
import java.util.List;
import javax.transaction.Transactional;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Service;
import com.xinwei.shirofunction.ShiroRole;
@Transactional
@Service
public interface ShiroRoleDao extends BaseDao<ShiroRole, Long> {
@Query("from ShiroRole")
public List<ShiroRole> findRoleList();
public ShiroRole findByRoleName(String roleName);
}

View File

@@ -0,0 +1,22 @@
package com.xinwei.dao;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;
import com.xinwei.shirofunction.ShiroUser;
@Service
public interface ShiroUserDao extends BaseDao<ShiroUser, Long> {
public ShiroUser findByUsername(String userName);
@Query(value = "select j from ShiroUser j where j.username = :username ")
public ShiroUser findByUsernameForUpdate(@Param("username") String username);
// 动态sql分页查询
Page<ShiroUser> findAll(Specification<ShiroUser> spec, Pageable pageable);
}

View File

@@ -0,0 +1,117 @@
/**
*
*/
package com.xinwei.service.impl;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import com.xinwei.common.page.DynamicSpecifications;
import com.xinwei.dao.BaseDao;
import com.xinwei.service.EntityService;
import com.xinwei.utils.ReflectionUtils;
import com.xinwei.utils.SearchFilter;
/**
*
* 所有业务查询实现类的基类
*/
public abstract class EntityServiceImpl<T, PK extends Serializable, EntityDao extends BaseDao<T, PK>> implements
EntityService<T, PK> {
protected Class<T> entityClass;
protected EntityDao entityDao;
@Override
public T get(PK id) {
return entityDao.findOne(id);
}
@Override
public void delete(PK id) {
entityDao.delete(id);
}
public void delete(T entity){
entityDao.delete(entity);
}
@Override
public void save(T t) {
entityDao.save(t);
}
@Override
public Iterable<T> getAll() {
return entityDao.findAll();
}
public EntityServiceImpl() {
this.entityClass = ReflectionUtils.getSuperClassGenricType(getClass());
}
/**
* 创建分页请求.
*/
protected PageRequest buildPageRequest(int pageNumber, int pagzSize, String sortType) {
Sort sort = null;
if ("auto".equals(sortType)) {
sort = new Sort(Direction.ASC, "id");
} else if (sortType != null && !"".equals(sortType) && !sortType.contains("_ASC")) {
sort = new Sort(Direction.DESC, sortType);
} else if(null != sortType && sortType.contains("_ASC")) {
sort = new Sort(Direction.ASC, sortType.split("_ASC")[0]);
}
return new PageRequest(pageNumber - 1, pagzSize, sort);
}
/**
* 创建动态查询条件组合.
*/
protected Specification<T> buildSpecification(Map<String, Object> searchParams) {
Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
Specification<T> spec = DynamicSpecifications.bySearchFilter(filters.values(), entityClass);
return spec;
}
@Override
public Page<T> search(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType) {
PageRequest pageRequest = buildPageRequest(pageNumber, pageSize, sortType);
if (searchParams!=null) {
Specification<T> spec = buildSpecification(searchParams);
return entityDao.findAll(spec, pageRequest);
}
return entityDao.findAll(pageRequest);
}
@Override
public List<T> findAll(Map<String, Object> searchParams, Sort sort) {
return entityDao.findAll(buildSpecification(searchParams), sort);
}
@Override
public List<T> findAll(Map<String, Object> searchParams) {
return entityDao.findAll(buildSpecification(searchParams));
}
public abstract void setEntityDao(EntityDao entityDao);
@Override
public void delete(Iterable<T> entities) {
entityDao.delete(entities);
}
}

View File

@@ -0,0 +1,101 @@
package com.xinwei.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import com.xinwei.common.page.Pagination;
import com.xinwei.common.page.PaginationResult;
import com.xinwei.common.page.SpecificationUtil;
import com.xinwei.dao.FunctionDao;
import com.xinwei.shirofunction.Function;
import com.xinwei.shirofunction.FunctionService;
@Service
public class FunctionServiceImpl extends EntityServiceImpl<Function, Long, FunctionDao> implements FunctionService {
@Override
public List<Function> findList(){
return entityDao.findList();
}
@Override
public Function findById(Integer id){
return entityDao.findOne(id);
}
@Override
public Function findByPermissionName(String permissionName){
return entityDao.findByPermissionName(permissionName);
}
@Override
public Function findByPid(Integer pid){
return entityDao.findByPid(pid);
}
@Override
public List<Function> findListByPid(Integer pid){
return entityDao.findListByPid(pid);
}
@Override
public PaginationResult<Function> findAllByPage(Pagination<Function> pagination){
SpecificationUtil<Function> specificationUtil = new SpecificationUtil<Function>();
Map<String, Object> searchParams = new HashMap<String, Object>();
List<String> properties = new ArrayList<String>();
properties.add("id");
Sort sort = new Sort(Direction.DESC, properties);
sort= pagination.dataTableOrder(pagination,sort,properties);
String id = pagination.getExtra_search();
searchParams.put("EQS_id", id);
Specification<Function> specification = specificationUtil.buildSpecification(searchParams,"and");
// int page= pagination.getStart()/pagination.getPageSize();
PageRequest PageRequest = null;//new PageRequest( page,pagination.getPageSize(), sort);
Page<Function> list = entityDao.findAll(specification, PageRequest);
PaginationResult<Function> pa=new PaginationResult();
pa.setData(list.getContent());
pa.setRecordsFiltered(list.getTotalElements());
pa.setRecordsTotal(list.getTotalElements());
pa.setDraw(new Integer(pagination.getDraw()));
return pa;
}
@Override
@Autowired
public void setEntityDao(FunctionDao entityDao) {
this.entityDao = entityDao;
}
}

View File

@@ -0,0 +1,86 @@
package com.xinwei.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import com.xinwei.common.page.Pagination;
import com.xinwei.common.page.PaginationResult;
import com.xinwei.common.page.SpecificationUtil;
import com.xinwei.dao.ShiroRoleDao;
import com.xinwei.service.ShiroRoleService;
import com.xinwei.shirofunction.ShiroRole;
@Service
public class ShiroRoleServiceImpl extends EntityServiceImpl<ShiroRole, Long, ShiroRoleDao> implements ShiroRoleService {
@Override
public List<ShiroRole> findRoleList(){
return entityDao.findRoleList();
}
@Override
public ShiroRole findByRoleName(String rolename){
return entityDao.findByRoleName(rolename);
}
@Override
public ShiroRole findById(Integer id){
return entityDao.findOne(id);
}
@Override
public PaginationResult<ShiroRole> findAllByPage(Pagination<ShiroRole> pagination){
SpecificationUtil<ShiroRole> specificationUtil = new SpecificationUtil<ShiroRole>();
Map<String, Object> searchParams = new HashMap<String, Object>();
List<String> properties = new ArrayList<String>();
properties.add("id");
Sort sort = new Sort(Direction.DESC, properties);
sort= pagination.dataTableOrder(pagination,sort,properties);
Specification<ShiroRole> specification = specificationUtil.buildSpecification(searchParams,"and");
int page= pagination.getStart()/pagination.getPageSize();
PageRequest PageRequest = new PageRequest( page,pagination.getPageSize(), sort);
Page<ShiroRole> list = entityDao.findAll(specification, PageRequest);
PaginationResult<ShiroRole> pa=new PaginationResult();
pa.setData(list.getContent());
pa.setRecordsFiltered(list.getTotalElements());
pa.setRecordsTotal(list.getTotalElements());
pa.setDraw(new Integer(pagination.getDraw()));
return pa;
}
@Override
@Autowired
public void setEntityDao(ShiroRoleDao entityDao) {
this.entityDao = entityDao;
}
}

View File

@@ -0,0 +1,314 @@
package com.xinwei.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xinwei.common.page.Pagination;
import com.xinwei.common.page.PaginationResult;
import com.xinwei.common.page.SpecificationUtil;
import com.xinwei.dao.ShiroUserDao;
import com.xinwei.service.ShiroRoleService;
import com.xinwei.service.ShiroUserService;
import com.xinwei.shirofunction.ShiroRole;
import com.xinwei.shirofunction.ShiroUser;
import com.xinwei.utils.Constants;
import com.xinwei.utils.ResponseJson;
import lombok.extern.slf4j.Slf4j;
@Service
@Slf4j
public class ShiroUserServiceImpl extends EntityServiceImpl<ShiroUser, Long, ShiroUserDao> implements ShiroUserService {
@Autowired
ShiroRoleService shiroRoleService;
/**
* 管理员修改用户基本信息
*/
@Transactional(rollbackFor=Exception.class)
@Override
public ResponseJson editUser(ShiroUser shiroUser){
ResponseJson responseJson = new ResponseJson();
responseJson.setMsg("success");
log.info(shiroUser.toString());
ShiroUser shiroUserTemp = this.findByUsernameForUpdate(shiroUser.getUsername());
this.save(shiroUserTemp);
return responseJson;
}
/**
* 管理员添加用户
*/
@Transactional(rollbackFor=Exception.class)
@Override
public ResponseJson addUser(ShiroUser shiroUser){
ResponseJson responseJson = new ResponseJson();
ShiroUser shiroUserTemp=null;
try {
shiroUserTemp = this.findByUsername(shiroUser.getUsername());
if(shiroUserTemp!=null){
responseJson.setMsg("usernameExist");
return responseJson;
}
String password = new Md5Hash(Constants.initPassword, "www",1024).toBase64();
shiroUser.setPassword(password);
shiroUser.setStatus(Constants.userStatus_0);
shiroUser.setCreateTime(new Date());
this.save(shiroUser);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
responseJson.setMsg("false");
return responseJson;
}
responseJson.setMsg("success");
return responseJson;
}
@Transactional(rollbackFor=Exception.class)
@Override
public ResponseJson checkNameOrEmallExist(ShiroUser shiroUser){
ResponseJson responseJson = new ResponseJson();
log.info(shiroUser.toString());
ShiroRole shiroRoleTemp=null;
ShiroUser shiroUserTemp=null;
try {
shiroUserTemp = this.findByUsername(shiroUser.getUsername());
if(shiroUserTemp!=null){
responseJson.setMsg("false");
return responseJson;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
responseJson.setMsg("false");
return responseJson;
}
responseJson.setMsg("true");
return responseJson;
}
/**
* 删除
*/
@Override
@Transactional(rollbackFor=Exception.class)
public ResponseJson del( String ids) throws Exception{
ResponseJson responseJson = new ResponseJson();
ShiroUser user;
String idsArr[] = ids.split(",");
for (int i = 0; i < idsArr.length; i++) {
user = entityDao.findByUsername(idsArr[i]);
if(user.getUsername().equals("admin")){
responseJson.setMsg("admin");
throw new Exception("admin");
}
entityDao.delete(user);
}
responseJson.setMsg("success");
return responseJson;
}
public ResponseJson editUserRole(String username,String roles){
ResponseJson responseJson= new ResponseJson();
ShiroUser shiroUserTemp = this.findByUsernameForUpdate(username);
String rolesArr[] = roles.split(",");
List<ShiroRole> roleList = new ArrayList<ShiroRole>();
for(int i=0;i<rolesArr.length;i++){
ShiroRole shiroRoleTemp = shiroRoleService.findByRoleName(rolesArr[i]);
roleList.add(shiroRoleTemp);
}
shiroUserTemp.setRoleList(roleList);
this.save(shiroUserTemp);
responseJson.setMsg("success");
responseJson.setSuccess(true);
return responseJson;
}
@Override
public ShiroUser findByUsernameForUpdate(String username){
return entityDao.findByUsernameForUpdate(username);
}
@Override
public ShiroUser findByUsername(String userName) {
return entityDao.findByUsername(userName);
}
@Transactional(rollbackFor=Exception.class)
@Override
public ResponseJson audit(String userNameStr,String status) throws Exception{
ResponseJson responseJson = new ResponseJson();
responseJson.setMsg("success");
String userNameArray[]=userNameStr.split(",");
int length = userNameArray.length;
log.info("length===="+length);
for(int i =0;i<length;i++){
String username = userNameArray[i];
log.info("username===="+username);
log.info("status===="+status);
ShiroUser shiroUser = entityDao.findByUsername(username);
if(shiroUser.getRoleList()==null||shiroUser.getRoleList().size()==0){//审核之前必须先给用户赋角色
throw new Exception("roleNotExist");
}else{
shiroUser.setStatus(status);
entityDao.save(shiroUser);
responseJson.setMsg("success");
}
}
return responseJson;
}
@Override
public List<ShiroUser> findList() {
List<ShiroUser> list = (List) entityDao.findAll();
return list;
}
@Override
public PaginationResult<ShiroUser> findAllByPage(Pagination<ShiroUser> pagination){
SpecificationUtil<ShiroUser> specificationUtil = new SpecificationUtil<ShiroUser>();
Map<String, Object> searchParams = new HashMap<String, Object>();
ShiroUser ShiroUser = new ShiroUser();
ShiroUser = pagination.dataTable2Entry(ShiroUser, pagination);//封装前段调用的数据。
List<String> properties = new ArrayList<String>();
properties.add("id");
Sort sort = new Sort(Direction.DESC, properties);
sort= pagination.dataTableOrder(pagination,sort,properties);
String extra_search = pagination.getExtra_search();//查询条件
log.info("extra_search="+extra_search);
String paramArray[] = extra_search.split("&");
Map map = new HashMap();
for(int i =0;i<paramArray.length;i++){
String name = paramArray[i].split("=")[0];
if(paramArray[i].split("=").length==2){
String value = paramArray[i].split("=")[1];
map.put(name, value);
}else{
map.put(name, "");
}
}
if(!map.get("username").equals("")){
searchParams.put("LIKES_username", map.get("username"));
}
if(!map.get("status").equals("")){
searchParams.put("EQS_status", map.get("status"));
}
log.info("searchParams.toString()=="+searchParams.toString());
Specification<ShiroUser> specification = specificationUtil.buildSpecification(searchParams,"and");
int page= pagination.getStart()/pagination.getPageSize();
PageRequest PageRequest = new PageRequest( page,pagination.getPageSize(), sort);
Page<ShiroUser> list = entityDao.findAll(specification, PageRequest);
PaginationResult<ShiroUser> pa=new PaginationResult();
pa.setData(list.getContent());
pa.setRecordsFiltered(list.getTotalElements());
pa.setRecordsTotal(list.getTotalElements());
pa.setDraw(new Integer(pagination.getDraw()));
return pa;
}
@Override
@Autowired
public void setEntityDao(ShiroUserDao entityDao) {
this.entityDao = entityDao;
}
}