src/main/java/com/moral/common/aop/ControllerAOP.java
@@ -52,29 +52,27 @@ Object target = pjp.getTarget(); Method currentMethod = target.getClass().getMethod(msig.getName(), msig.getParameterTypes()); Type type = currentMethod.getGenericReturnType(); String message = ""; String message = e.getMessage(); if (e instanceof BusinessException) { message = e.getLocalizedMessage(); } else if (e instanceof ValidateException) { message = e.getLocalizedMessage(); } else { log.error(pjp.getSignature() + " error ", e); message = e.toString(); log.error(pjp.getSignature() + " error: " + e.toString(), e); } if (type instanceof ParameterizedType) { Type rawType = ((ParameterizedType) type).getRawType(); if (rawType == AppData.class) { return new AppData(message,AppData.FAIL); } else if (rawType == ResultBean.class) { return new ResultBean(message,ResultBean.FAIL); } else if (rawType == Map.class) { Map<String, Object> resultMap = new HashMap<String, Object>(); resultMap.put("msg", message); return resultMap; } type = ((ParameterizedType) type).getRawType(); } return null; if (type == AppData.class) { return new AppData(message, AppData.FAIL); } else if (type == ResultBean.class) { return new ResultBean(e); } else if (type == Map.class) { Map<String, Object> resultMap = new HashMap<String, Object>(); resultMap.put("msg", message); return resultMap; } else if (type == Void.TYPE) { return null; } else { return type.getClass().newInstance(); } } } src/main/java/com/moral/common/bean/ResultBean.java
@@ -17,24 +17,20 @@ private T data; public ResultBean() { super(); this.message = "success"; this.code = SUCCESS; } public ResultBean(Throwable e) { super(); this.message = e.toString(); this.code = FAIL; } public ResultBean(T data) { this(); this.message = "success"; this.code = SUCCESS; this.data = data; } public ResultBean(String message, int code) { super(); this.message = message; this.code = code; } src/main/java/com/moral/controller/AccountController.java
@@ -9,6 +9,7 @@ import javax.servlet.http.HttpServletRequest; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -44,4 +45,10 @@ Integer result = accountService.deleteAccountsByLogic(ids); return new ResultBean<Integer>(result); } @GetMapping("{accountName}") public ResultBean<Integer> getAccountCountByAccountName(@PathVariable("accountName") String accountName) { Integer result = accountService.getAccountCountByAccountName(accountName); return new ResultBean<Integer>(result); } } src/main/java/com/moral/controller/OrganizationController.java
@@ -5,6 +5,9 @@ import com.moral.entity.Organization; import com.moral.service.OrganizationService; import org.springframework.web.bind.annotation.*; import java.util.List; import javax.annotation.Resource; @RestController @@ -28,4 +31,11 @@ ResultBean resultBean = new ResultBean(ResultBean.SUCCESS); return resultBean; } @GetMapping("list/{name}") public ResultBean<List<Organization>> getOrganizationsByName(@PathVariable("name") String name) { List<Organization> organizations = organizationService.getOrganizationsByName(name); return new ResultBean<List<Organization>>(organizations); } } src/main/java/com/moral/entity/Account.java
@@ -68,5 +68,7 @@ * @mbggenerated Thu Dec 07 16:17:21 CST 2017 */ private Date expireTime; private Organization organization; } src/main/java/com/moral/service/AccountService.java
@@ -20,4 +20,6 @@ Integer deleteAccountsByLogic(List<Integer> ids); Integer getAccountCountByAccountName(String accountName); } src/main/java/com/moral/service/OrganizationService.java
@@ -18,4 +18,7 @@ public void addOrModify(Organization organization); public void deleteByIds(Integer... ids); List<Organization> getOrganizationsByName(String name); } src/main/java/com/moral/service/impl/AccountServiceImpl.java
@@ -26,7 +26,9 @@ import com.moral.common.util.Crypto; import com.moral.common.util.ResourceUtil; import com.moral.entity.Account; import com.moral.entity.Organization; import com.moral.mapper.AccountMapper; import com.moral.mapper.OrganizationMapper; import com.moral.service.AccountService; import com.moral.service.OrganizationService; @@ -42,6 +44,9 @@ @Resource private OrganizationService organizationService; @Resource private OrganizationMapper organizationMapper; @Override public Map<String, Object> screenLogin(Map<String, Object> parameters) { Map<String, Object> result = new HashMap<String, Object>(); @@ -109,6 +114,12 @@ } PageHelper.startPage(Integer.valueOf((String) parameters.get("pageIndex")), Integer.valueOf((String) parameters.get("pageSize"))); List<Account> accounts = accountMapper.selectByExample(example); for (Account account : accounts) { if (!ObjectUtils.isEmpty(account.getOrganizationId())) { Organization organization = organizationMapper.selectByPrimaryKey(account.getOrganizationId()); account.setOrganization(organization); } } return new PageBean<Account>(accounts); } @@ -135,4 +146,11 @@ return accountMapper.updateByExampleSelective(account, example); } @Override public Integer getAccountCountByAccountName(String accountName) { Account account = new Account(); account.setAccountName(accountName); return accountMapper.selectCount(account); } } src/main/java/com/moral/service/impl/AuthUserServiceImpl.java
@@ -1,38 +1,58 @@ package com.moral.service.impl; import com.moral.entity.auth.AuthRole; import com.moral.entity.auth.AuthUser; //import com.moral.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import com.moral.common.util.RedisUtil; import com.moral.entity.Account; import com.moral.entity.OperateUser; import com.moral.mapper.AccountMapper; import com.moral.service.AccountService; import com.moral.service.OperateUserService; @Service public class AuthUserServiceImpl implements UserDetailsService { // @Autowired // private UserService userService; @Resource private AccountMapper accountMapper; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { // AuthUser user = userService.findByUsername(username); AuthUser user = null; if(user == null){ throw new UsernameNotFoundException("用户名:"+ username + "不存在!"); } Collection<SimpleGrantedAuthority> collection = new HashSet<SimpleGrantedAuthority>(); Iterator<AuthRole> iterator = user.getList().iterator(); while (iterator.hasNext()){ collection.add(new SimpleGrantedAuthority(iterator.next().getRole_name())); } @Resource private AccountService accountService; return new org.springframework.security.core.userdetails.User(username, user.getPassword(), collection); } @Resource private OperateUserService operateUserService; @Resource private RedisTemplate<String, String> redisTemplate; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { String type = RedisUtil.get(redisTemplate, "token_" + username), password; Collection<SimpleGrantedAuthority> collection = new HashSet<SimpleGrantedAuthority>(); if ("screen".equals(type)) { Account account = accountService.getAccountByAccountName(username); password = account.getPassword(); List<Map<String, Object>> roleNames = accountMapper.getRoleNameByAccountId(account.getId()); for (Map<String, Object> roleName : roleNames) { collection.add(new SimpleGrantedAuthority((String) roleName.get("role_name"))); } } else { OperateUser operateUser = operateUserService.getOperateUserByMobile(username); password = operateUser.getPassword(); collection.add((new SimpleGrantedAuthority("ROLE_MOBILE"))); } return new User(username, password, collection); } } src/main/java/com/moral/service/impl/OrganizationServiceImpl.java
@@ -1,6 +1,7 @@ package com.moral.service.impl; import com.github.pagehelper.PageHelper; import com.moral.common.bean.Constants; import com.moral.common.bean.PageBean; import com.moral.common.util.ExampleUtil; import com.moral.common.util.ValidateUtil; @@ -12,6 +13,7 @@ import com.moral.service.OrganizationService; import org.springframework.stereotype.Service; import tk.mybatis.mapper.entity.Example; import tk.mybatis.mapper.entity.Example.Criteria; import javax.annotation.Resource; import java.util.*; @@ -99,4 +101,15 @@ } } @Override public List<Organization> getOrganizationsByName(String name) { Example example = new Example(Organization.class); Criteria criteria = example.createCriteria(); criteria.andLike("name", "%" + name + "%"); criteria.andEqualTo("isDelete", Constants.IS_DELETE_FALSE); List<Organization> organizations = organizationMapper.selectByExample(example); return organizations; } }