优化代码

--听一首歌,回忆一段往事
This commit is contained in:
meng 2018-12-11 21:20:35 +08:00
parent 8f99e1b247
commit 0d5daac9e8
9 changed files with 223 additions and 285 deletions

View File

@ -57,4 +57,7 @@ public class CurrentMenu implements Serializable {
this.menuType = menuType;
this.num = num;
}
public CurrentMenu() {
}
}

View File

@ -2,6 +2,7 @@ package com.len.base;
import java.io.Serializable;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@ -11,9 +12,7 @@ import lombok.ToString;
* @date 2017/12/30.
* @email 154040976@qq.com
*/
@Getter
@Setter
@ToString
@Data
public class CurrentRole implements Serializable {
/**
@ -32,4 +31,7 @@ public class CurrentRole implements Serializable {
this.roleName = roleName;
this.remark = remark;
}
public CurrentRole() {
}
}

View File

@ -1,13 +0,0 @@
package com.len;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}

View File

@ -15,7 +15,6 @@ import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@ -40,10 +39,10 @@ import java.util.List;
public class LoginController {
@Autowired
private SysUserService userService;
private MenuService menuService;
@Autowired
private MenuService menuService;
SysUserService userService;
@GetMapping(value = "")
public String loginInit() {
@ -66,7 +65,6 @@ public class LoginController {
Subject sub = SecurityUtils.getSubject();
Boolean flag2 = sub.isRemembered();
boolean flag = sub.isAuthenticated() || flag2;
Session session = sub.getSession();
if (flag) {
return "/main/main";
}
@ -84,11 +82,11 @@ public class LoginController {
@ApiOperation(value = "/login", httpMethod = "POST", notes = "登录method")
@PostMapping(value = "/login")
public String login(SysUser user, Model model, String rememberMe, HttpServletRequest request) {
/*String codeMsg = (String) request.getAttribute("shiroLoginFailure");
String codeMsg = (String) request.getAttribute("shiroLoginFailure");
if ("code.error".equals(codeMsg)) {
model.addAttribute("message", "验证码错误");
return "/login";
}*/
}
CustomUsernamePasswordToken token = new CustomUsernamePasswordToken(user.getUsername().trim(),
user.getPassword(), "UserLogin");
Subject subject = ShiroUtil.getSubject();
@ -96,6 +94,8 @@ public class LoginController {
try {
subject.login(token);
if (subject.isAuthenticated()) {
userService.setMenuAndRoles(token.getUsername());
token.getUsername();
return "redirect:/main";
}
} catch (UnknownAccountException | IncorrectCredentialsException e) {

View File

@ -16,9 +16,6 @@ import com.len.util.BeanUtil;
import com.len.util.JsonUtil;
import com.len.util.ReType;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
@ -29,6 +26,8 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
/**
* @author zhuxiaomeng
* @date 2017/12/19.

View File

@ -97,7 +97,7 @@ public class JobTask {
.withMisfireHandlingInstructionDoNothing();
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey)
.withDescription(createTime).withSchedule(schedBuilder).build();
Class clazz = null;
JobDetail jobDetail = scheduler.getJobDetail(jobKey);
HashSet<Trigger> triggerSet = new HashSet<>();
triggerSet.add(trigger);

View File

@ -1,15 +1,9 @@
package com.len.core.shiro;
import com.alibaba.fastjson.JSONArray;
import com.len.base.CurrentMenu;
import com.len.base.CurrentRole;
import com.len.base.CurrentUser;
import com.len.entity.SysMenu;
import com.len.entity.SysRole;
import com.len.entity.SysUser;
import com.len.service.MenuService;
import com.len.service.RoleMenuService;
import com.len.service.RoleUserService;
import com.len.service.SysUserService;
import com.len.util.JWTUtil;
import org.apache.commons.lang3.StringUtils;
@ -17,15 +11,12 @@ import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@ -40,15 +31,6 @@ public class LoginRealm extends AuthorizingRealm {
@Autowired
private SysUserService userService;
@Autowired
private MenuService menuService;
@Autowired
private RoleUserService roleUserService;
@Autowired
private RoleMenuService roleMenuService;
/**
* 获取授权
@ -75,7 +57,7 @@ public class LoginRealm extends AuthorizingRealm {
info.addRole(cRole.getId());
}
for (CurrentMenu cMenu : cUser.getCurrentMenuList()) {
if (!StringUtils.isEmpty(cMenu.getPermission())){
if (!StringUtils.isEmpty(cMenu.getPermission())) {
info.addStringPermission(cMenu.getPermission());
}
}
@ -94,7 +76,6 @@ public class LoginRealm extends AuthorizingRealm {
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken)
throws AuthenticationException {
UsernamePasswordToken upToken = (UsernamePasswordToken) authenticationToken;
String username = (String) authenticationToken.getPrincipal();
SysUser s = null;
try {
@ -104,33 +85,6 @@ public class LoginRealm extends AuthorizingRealm {
}
if (s == null) {
throw new UnknownAccountException("账户密码不正确");
} else {
CurrentUser currentUser = new CurrentUser(s.getId(), s.getUsername(), s.getAge(), s.getEmail(), s.getPhoto(), s.getRealName());
Subject subject = ShiroUtil.getSubject();
/**角色权限封装进去*/
//根据用户获取菜单
List<SysMenu> menuList = new ArrayList<>(new HashSet<>(menuService.getUserMenu(s.getId())));
JSONArray json = menuService.getMenuJsonByUser(menuList);
Session session = subject.getSession();
session.setAttribute("menu", json);
CurrentMenu currentMenu = null;
List<CurrentMenu> currentMenuList = new ArrayList<>();
List<SysRole> roleList = new ArrayList<>();
for (SysMenu m : menuList) {
currentMenu = new CurrentMenu(m.getId(), m.getName(), m.getPId(), m.getUrl(), m.getOrderNum(), m.getIcon(), m.getPermission(), m.getMenuType(), m.getNum());
currentMenuList.add(currentMenu);
roleList.addAll(m.getRoleList());
}
roleList = new ArrayList<>(new HashSet<>(roleList));
List<CurrentRole> currentRoleList = new ArrayList<>();
CurrentRole role = null;
for (SysRole r : roleList) {
role = new CurrentRole(r.getId(), r.getRoleName(), r.getRemark());
currentRoleList.add(role);
}
currentUser.setCurrentRoleList(currentRoleList);
currentUser.setCurrentMenuList(currentMenuList);
session.setAttribute("curentUser", currentUser);
}
ByteSource byteSource = ByteSource.Util.bytes(username);
return new SimpleAuthenticationInfo(username, s.getPassword(), byteSource, getName());

View File

@ -35,12 +35,7 @@ public class RoleServiceImpl extends BaseServiceImpl<SysRole,String> implements
record=super.addValue(record,true);
return roleMapper.insert(record);
}
/* @Override
public int insertSelective(SysRole record) {
return roleMapper.insertSelective(record);
}*/
@Override
public SysRole selectByPrimaryKey(String id) {
return roleMapper.selectByPrimaryKey(id);

View File

@ -1,206 +1,204 @@
package com.len.service.impl;
import com.len.base.BaseMapper;
import com.len.base.impl.BaseServiceImpl;
import com.len.entity.SysRole;
import com.len.entity.SysRoleUser;
import com.len.entity.SysUser;
import com.len.exception.MyException;
import com.len.mapper.SysRoleUserMapper;
import com.len.mapper.SysUserMapper;
import com.len.service.RoleService;
import com.len.service.RoleUserService;
import com.len.service.SysUserService;
import com.len.util.Checkbox;
import com.len.util.JsonUtil;
import com.len.util.Md5Util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author zhuxiaomeng
* @date 2017/12/4.
* @email 154040976@qq.com
*/
@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUser,String> implements SysUserService {
@Autowired
SysUserMapper sysUserMapper;
@Autowired
SysRoleUserMapper sysRoleUserMapper;
@Autowired
RoleService roleService;
@Autowired
RoleUserService roleUserService;
@Override
public BaseMapper<SysUser, String> getMappser() {
return sysUserMapper;
}
@Override
public SysUser login(String username) {
return sysUserMapper.login(username);
}
@Override
public int deleteByPrimaryKey(String id) {
return sysUserMapper.deleteByPrimaryKey(id);
}
@Override
public int insert(SysUser record) {
return sysUserMapper.insert(record);
}
@Override
public int insertSelective(SysUser record) {
String pwd= Md5Util.getMD5(record.getPassword().trim(),record.getUsername().trim());
record.setPassword(pwd);
return super.insertSelective(record);
}
@Override
public SysUser selectByPrimaryKey(String id) {
return sysUserMapper.selectByPrimaryKey(id);
}
@Override
public int updateByPrimaryKeySelective(SysUser record) {
return super.updateByPrimaryKeySelective(record);
}
@Override
public int updateByPrimaryKey(SysUser record) {
return sysUserMapper.updateByPrimaryKey(record);
}
@Override
public List<SysRoleUser> selectByCondition(SysRoleUser sysRoleUser) {
return sysRoleUserMapper.selectByCondition(sysRoleUser);
}
/**
* 分页查询
* @param
* @return
*/
@Override
public List<SysUser> selectListByPage(SysUser sysUser) {
return sysUserMapper.selectListByPage(sysUser);
}
@Override
public int count() {
return sysUserMapper.count();
}
@Override
public int add(SysUser user) {
//密码加密
String pwd= Md5Util.getMD5(user.getPassword().trim(),user.getUsername().trim());
user.setPassword(pwd);
return sysUserMapper.add(user);
}
@Override
public JsonUtil delById(String id,boolean flag) {
if (StringUtils.isEmpty(id)) {
return JsonUtil.error("获取数据失败");
}
JsonUtil j=new JsonUtil();
try {
SysUser sysUser = selectByPrimaryKey(id);
if("admin".equals(sysUser.getUsername())){
return JsonUtil.error("超管无法删除");
}
SysRoleUser roleUser=new SysRoleUser();
roleUser.setUserId(id);
int count=roleUserService.selectCountByCondition(roleUser);
if(count>0){
return JsonUtil.error("账户已经绑定角色,无法删除");
}
if (flag) {
//逻辑
sysUser.setDelFlag(Byte.parseByte("1"));
updateByPrimaryKeySelective(sysUser);
} else {
//物理
sysUserMapper.delById(id);
}
j.setMsg("删除成功");
} catch (MyException e) {
j.setMsg("删除失败");
j.setFlag(false);
e.printStackTrace();
}
return j;
}
@Override
public int checkUser(String username) {
return sysUserMapper.checkUser(username);
}
@Override
public List<Checkbox> getUserRoleByJson(String id){
List<SysRole> roleList=roleService.selectListByPage(new SysRole());
SysRoleUser sysRoleUser =new SysRoleUser();
sysRoleUser.setUserId(id);
List<SysRoleUser> kList= selectByCondition(sysRoleUser);
System.out.println(kList.size());
List<Checkbox> checkboxList=new ArrayList<>();
Checkbox checkbox=null;
for(SysRole sysRole:roleList){
checkbox=new Checkbox();
checkbox.setId(sysRole.getId());
checkbox.setName(sysRole.getRoleName());
for(SysRoleUser sysRoleUser1 :kList){
if(sysRoleUser1.getRoleId().equals(sysRole.getId())){
checkbox.setCheck(true);
}
}
checkboxList.add(checkbox);
}
return checkboxList;
}
@Override
public int rePass(SysUser user) {
return sysUserMapper.rePass(user);
}
@Override
public List<SysUser> getUserByRoleId(String roleId,int page,int limit)
{
Map map = new HashMap<>();
map.put("roleId",roleId);
map.put("page",(page-1)*limit );
map.put("limit",limit);
return sysUserMapper.getUserByRoleId(map);
}
@Override
public int countUserByRoleId(String roleId,int page,int limit)
{
Map map = new HashMap<>();
map.put("roleId",roleId);
map.put("page",(page-1)*limit );
map.put("limit",limit);
return sysUserMapper.countUserByRoleId(map);
}
}
package com.len.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.len.base.BaseMapper;
import com.len.base.CurrentMenu;
import com.len.base.CurrentRole;
import com.len.base.CurrentUser;
import com.len.base.impl.BaseServiceImpl;
import com.len.core.shiro.ShiroUtil;
import com.len.entity.SysMenu;
import com.len.entity.SysRole;
import com.len.entity.SysRoleUser;
import com.len.entity.SysUser;
import com.len.exception.MyException;
import com.len.mapper.SysRoleUserMapper;
import com.len.mapper.SysUserMapper;
import com.len.service.MenuService;
import com.len.service.RoleService;
import com.len.service.RoleUserService;
import com.len.service.SysUserService;
import com.len.util.BeanUtil;
import com.len.util.Checkbox;
import com.len.util.JsonUtil;
import com.len.util.Md5Util;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
/**
* @author zhuxiaomeng
* @date 2017/12/4.
* @email 154040976@qq.com
*/
@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUser, String> implements SysUserService {
@Autowired
SysUserMapper sysUserMapper;
@Autowired
SysRoleUserMapper sysRoleUserMapper;
@Autowired
RoleService roleService;
@Autowired
RoleUserService roleUserService;
@Autowired
MenuService menuService;
private static final String ADMIN = "admin";
@Override
public BaseMapper<SysUser, String> getMappser() {
return sysUserMapper;
}
@Override
public SysUser login(String username) {
return sysUserMapper.login(username);
}
@Override
public int deleteByPrimaryKey(String id) {
return sysUserMapper.deleteByPrimaryKey(id);
}
@Override
public int insert(SysUser record) {
return sysUserMapper.insert(record);
}
@Override
public int insertSelective(SysUser record) {
String pwd = Md5Util.getMD5(record.getPassword().trim(), record.getUsername().trim());
record.setPassword(pwd);
return super.insertSelective(record);
}
@Override
public SysUser selectByPrimaryKey(String id) {
return sysUserMapper.selectByPrimaryKey(id);
}
@Override
public int updateByPrimaryKeySelective(SysUser record) {
return super.updateByPrimaryKeySelective(record);
}
@Override
public int updateByPrimaryKey(SysUser record) {
return sysUserMapper.updateByPrimaryKey(record);
}
@Override
public List<SysRoleUser> selectByCondition(SysRoleUser sysRoleUser) {
return sysRoleUserMapper.selectByCondition(sysRoleUser);
}
/**
* 分页查询
*
* @param
* @return
*/
@Override
public List<SysUser> selectListByPage(SysUser sysUser) {
return sysUserMapper.selectListByPage(sysUser);
}
@Override
public int count() {
return sysUserMapper.count();
}
@Override
public int add(SysUser user) {
//密码加密
String pwd = Md5Util.getMD5(user.getPassword().trim(), user.getUsername().trim());
user.setPassword(pwd);
return sysUserMapper.add(user);
}
@Override
public JsonUtil delById(String id, boolean flag) {
if (StringUtils.isEmpty(id)) {
return JsonUtil.error("获取数据失败");
}
JsonUtil j = new JsonUtil();
try {
SysUser sysUser = selectByPrimaryKey(id);
if (ADMIN.equals(sysUser.getUsername())) {
return JsonUtil.error("超管无法删除");
}
SysRoleUser roleUser = new SysRoleUser();
roleUser.setUserId(id);
int count = roleUserService.selectCountByCondition(roleUser);
if (count > 0) {
return JsonUtil.error("账户已经绑定角色,无法删除");
}
if (flag) {
//逻辑
sysUser.setDelFlag(Byte.parseByte("1"));
updateByPrimaryKeySelective(sysUser);
} else {
//物理
sysUserMapper.delById(id);
}
j.setMsg("删除成功");
} catch (MyException e) {
j.setMsg("删除失败");
j.setFlag(false);
e.printStackTrace();
}
return j;
}
@Override
public int checkUser(String username) {
return sysUserMapper.checkUser(username);
}
@Override
public List<Checkbox> getUserRoleByJson(String id) {
List<SysRole> roleList = roleService.selectListByPage(new SysRole());
SysRoleUser sysRoleUser = new SysRoleUser();
sysRoleUser.setUserId(id);
List<SysRoleUser> kList = selectByCondition(sysRoleUser);
List<Checkbox> checkboxList = new ArrayList<>();
Checkbox checkbox;
for (SysRole sysRole : roleList) {
checkbox = new Checkbox();
checkbox.setId(sysRole.getId());
checkbox.setName(sysRole.getRoleName());
for (SysRoleUser sysRoleUser1 : kList) {
if (sysRoleUser1.getRoleId().equals(sysRole.getId())) {
checkbox.setCheck(true);
break;
}
}
checkboxList.add(checkbox);
}
return checkboxList;
}
@Override
public int rePass(SysUser user) {
return sysUserMapper.rePass(user);
}
}