diff --git a/ms-base/pom.xml b/ms-base/pom.xml index a5dcef6..af71af1 100644 --- a/ms-base/pom.xml +++ b/ms-base/pom.xml @@ -363,26 +363,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-base diff --git a/ms-base/pom.xml b/ms-base/pom.xml index a5dcef6..af71af1 100644 --- a/ms-base/pom.xml +++ b/ms-base/pom.xml @@ -363,26 +363,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-base diff --git a/ms-basic/ms-basic.iml b/ms-basic/ms-basic.iml index 935ebc4..5a2e74f 100644 --- a/ms-basic/ms-basic.iml +++ b/ms-basic/ms-basic.iml @@ -11,8 +11,8 @@ - - + + diff --git a/ms-base/pom.xml b/ms-base/pom.xml index a5dcef6..af71af1 100644 --- a/ms-base/pom.xml +++ b/ms-base/pom.xml @@ -363,26 +363,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-base diff --git a/ms-basic/ms-basic.iml b/ms-basic/ms-basic.iml index 935ebc4..5a2e74f 100644 --- a/ms-basic/ms-basic.iml +++ b/ms-basic/ms-basic.iml @@ -11,8 +11,8 @@ - - + + diff --git a/ms-basic/pom.xml b/ms-basic/pom.xml index e66ec4f..90ba669 100644 --- a/ms-basic/pom.xml +++ b/ms-basic/pom.xml @@ -88,26 +88,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-basic diff --git a/ms-base/pom.xml b/ms-base/pom.xml index a5dcef6..af71af1 100644 --- a/ms-base/pom.xml +++ b/ms-base/pom.xml @@ -363,26 +363,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-base diff --git a/ms-basic/ms-basic.iml b/ms-basic/ms-basic.iml index 935ebc4..5a2e74f 100644 --- a/ms-basic/ms-basic.iml +++ b/ms-basic/ms-basic.iml @@ -11,8 +11,8 @@ - - + + diff --git a/ms-basic/pom.xml b/ms-basic/pom.xml index e66ec4f..90ba669 100644 --- a/ms-basic/pom.xml +++ b/ms-basic/pom.xml @@ -88,26 +88,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-basic diff --git a/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java b/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java index 540c752..30a601c 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java @@ -1,16 +1,16 @@ /** * The MIT License (MIT) * Copyright (c) 2020 铭软科技(mingsoft.net) - + *

* Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: - + *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - + *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR @@ -30,20 +30,25 @@ import io.swagger.annotations.ApiOperation; import net.mingsoft.base.entity.ResultData; import net.mingsoft.basic.action.BaseAction; +import net.mingsoft.basic.bean.ShiroUser; import net.mingsoft.basic.biz.IAppBiz; import net.mingsoft.basic.biz.IManagerBiz; import net.mingsoft.basic.biz.IRoleBiz; import net.mingsoft.basic.constant.Const; +import net.mingsoft.basic.constant.ErrorCodeEnum; import net.mingsoft.basic.constant.e.SessionConstEnum; import net.mingsoft.basic.entity.AppEntity; import net.mingsoft.basic.entity.ManagerEntity; import net.mingsoft.basic.entity.ManagerSessionEntity; import net.mingsoft.basic.entity.RoleEntity; +import net.mingsoft.basic.exception.DcmsLoginException; import net.mingsoft.basic.util.BasicUtil; +import net.mingsoft.basic.util.DcmsUtils; import net.mingsoft.basic.util.StringUtil; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.UsernamePasswordToken; +import org.apache.shiro.session.ExpiredSessionException; import org.apache.shiro.subject.Subject; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -56,14 +61,13 @@ import javax.servlet.http.HttpServletResponse; import java.io.File; import java.util.List; +import java.util.Map; /** - * - * @ClassName: LoginAction + * @ClassName: LoginAction * @Description:TODO(登录的基础应用层) * @author: 铭飞开发团队 - * @date: 2015年1月27日 下午3:21:47 - * + * @date: 2015年1月27日 下午3:21:47 * @Copyright: 2018 www.mingsoft.net Inc. All rights reserved. */ @Api("登录的基础应用层接口") @@ -71,146 +75,228 @@ @RequestMapping("/${ms.manager.path}") public class LoginAction extends BaseAction { - @Value("${ms.manager.path}") - private String managerPath; - /** - * 管理员业务层 - */ - @Autowired - private IManagerBiz managerBiz; + @Value("${ms.manager.path}") + private String managerPath; + @Value("${ms.manager.dcmsHost}") + private String dcmsHost; + @Value("${ms.manager.userInfoPath}") + private String userInfoPath; + @Value("#{${ms.manager.userRoles}}") + private Map userRoles; + @Value("#{'${ms.manager.users}'.split(',')}") + private List users; + /** + * 管理员业务层 + */ + @Autowired + private IManagerBiz managerBiz; - /** - * 角色业务request层 - */ - @Autowired - private IRoleBiz roleBiz; + /** + * 角色业务request层 + */ + @Autowired + private IRoleBiz roleBiz; - /** - * 站点业务层 - */ - @Autowired - private IAppBiz appBiz; + /** + * 站点业务层 + */ + @Autowired + private IAppBiz appBiz; + /** + * 加载管理员登录界面 + * + * @param request 请求对象 + * @return 管理员登录界面地址 + */ + @ApiOperation(value = "加载管理员登录界面") + @SuppressWarnings("resource") + @GetMapping("/dcmsToken") + public String dcmsToken(HttpServletRequest request) { + if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION) != null) { + return "redirect:" + managerPath + "/index.do"; + } + //请求dcms数据token + ShiroUser shiroUser = DcmsUtils.checkToken(dcmsHost + userInfoPath, request.getParameter("token")); + if (shiroUser == null) { + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无效token"); + } + String userName = DcmsUtils.transformCmsUser(shiroUser, userRoles, users); + if (userName == null) { + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无效token"); + } - /** - * 加载管理员登录界面 - * - * @param request - * 请求对象 - * @return 管理员登录界面地址 - */ - @ApiOperation(value = "加载管理员登录界面") - @SuppressWarnings("resource") - @GetMapping("/login") - public String login(HttpServletRequest request) { - if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION)!=null) { - return "redirect:"+managerPath+"/index.do"; - } - // 根据请求地址来显示标题 - AppEntity app = BasicUtil.getApp(); - // 判断应用实体是否存在 - if (app != null) { - // 检测应用是否有自定义界面b - if (!StringUtil.isBlank(app.getAppLoginPage())) { - LOG.debug("跳转自定义登录界面"); - return "redirect:" + app.getAppLoginPage(); - } + AppEntity app = BasicUtil.getApp(); + // 判断应用实体是否存在 + if (app != null) { + // 检测应用是否有自定义界面b + if (!StringUtil.isBlank(app.getAppLoginPage())) { + LOG.debug("跳转自定义登录界面"); + return "redirect:" + app.getAppLoginPage(); + } - } else { - File file = new File(BasicUtil.getRealPath( "WEB-INF/ms.install")); - //存在安装文件 - if (file.exists()) { - String defaultId = FileReader.create(new File(BasicUtil.getRealPath( "WEB-INF/ms.install"))).readString(); - if (!StringUtils.isEmpty(defaultId)) { - app = (AppEntity) appBiz.getEntity(Integer.parseInt(defaultId)); - app.setAppUrl(this.getUrl(request)); - appBiz.updateEntity(app); - FileWriter.create(new File(BasicUtil.getRealPath( "WEB-INF/ms.install.bak"))).write(defaultId); - file.delete(); - } - } + } + AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); + ManagerEntity newManager = new ManagerEntity(); + newManager.setManagerName(userName); + ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + // 创建管理员session对象 + ManagerSessionEntity managerSession = new ManagerSessionEntity(); + AppEntity website = new AppEntity(); + // 获取管理员所在的角色 + RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); + website = (AppEntity) appBiz.getEntity(role.getAppId()); + // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 + if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId() == BasicUtil.getAppId())) { + if (website == null) { + website = BasicUtil.getApp(); + } + List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); + managerSession.setStyle(website.getAppStyle()); + managerSession.setBasicId(website.getAppId()); + managerSession.setManagerParentID(role.getRoleManagerId()); + managerSession.setManagerChildIDs(childManagerList); + // 压入管理员seesion + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } else { + if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { + LOG.debug("roleId: " + _manager.getManagerRoleID()); + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无授权"); + } else { + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } + } + BeanUtils.copyProperties(_manager, managerSession); + Subject subject = SecurityUtils.getSubject(); + UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(), managerSession.getManagerPassword()); + subject.login(upt); + return "redirect:" + managerPath + "/index.do"; + } - } - request.setAttribute("app", app); - return "/login"; - } + @GetMapping("/404") + public String error(HttpServletRequest request) { + return "/404"; + } - /** - * 验证登录 - * - * @param manager - * 管理员实体 - * @param request - * 请求 - * @param response - * 响应 - */ - @ApiOperation(value = "验证登录") - @ApiImplicitParams({ - @ApiImplicitParam(name = "managerName", value = "帐号", required = true,paramType="query"), - @ApiImplicitParam(name = "managerPassword", value = "密码", required = true,paramType="query"), - }) - @PostMapping(value="/checkLogin") - @ResponseBody - public ResultData checkLogin(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletRequest request, - HttpServletResponse response) { - AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); // 根据url地址获取站点信息,主要是区分管理员对那些网站有权限 - if (urlWebsite == null) { - return ResultData.build().error( this.getResString("err.not.exist",this.getResString("app"),"!请尝试去文件 WEB-INF/ms.install.bak 后缀bak")); - } - //验证码 - if (!(checkRandCode())) { - return ResultData.build().error(getResString("err.error", new String[] { getResString("rand.code") })); - } + /** + * 加载管理员登录界面 + * + * @param request 请求对象 + * @return 管理员登录界面地址 + */ + @ApiOperation(value = "加载管理员登录界面") + @SuppressWarnings("resource") + @GetMapping("/login") + public String login(HttpServletRequest request) { + if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION) != null) { + return "redirect:" + managerPath + "/index.do"; + } + // 根据请求地址来显示标题 + AppEntity app = BasicUtil.getApp(); + // 判断应用实体是否存在 + if (app != null) { + // 检测应用是否有自定义界面b + if (!StringUtil.isBlank(app.getAppLoginPage())) { + LOG.debug("跳转自定义登录界面"); + return "redirect:" + app.getAppLoginPage(); + } - // 根据账号获取当前管理员信息 - ManagerEntity newManager = new ManagerEntity(); - newManager.setManagerName(manager.getManagerName()); - ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + } else { + File file = new File(BasicUtil.getRealPath("WEB-INF/ms.install")); + //存在安装文件 + if (file.exists()) { + String defaultId = FileReader.create(new File(BasicUtil.getRealPath("WEB-INF/ms.install"))).readString(); + if (!StringUtils.isEmpty(defaultId)) { + app = (AppEntity) appBiz.getEntity(Integer.parseInt(defaultId)); + app.setAppUrl(this.getUrl(request)); + appBiz.updateEntity(app); + FileWriter.create(new File(BasicUtil.getRealPath("WEB-INF/ms.install.bak"))).write(defaultId); + file.delete(); + } + } - if (_manager == null || StringUtils.isEmpty(manager.getManagerName())) { - // 系统不存在此用户 - return ResultData.build().error( this.getResString("err.nameEmpty")); - } else { - // 判断当前用户输入的密码是否正确 - if (SecureUtil.md5(manager.getManagerPassword()).equals(_manager.getManagerPassword())) { - // 创建管理员session对象 - ManagerSessionEntity managerSession = new ManagerSessionEntity(); - AppEntity website = new AppEntity(); - // 获取管理员所在的角色 - RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); - website = (AppEntity) appBiz.getEntity(role.getAppId()); - // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 - if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId()==BasicUtil.getAppId())) { - if(website==null){ - website = BasicUtil.getApp(); - } - List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); - managerSession.setBasicId(website.getAppId()); - managerSession.setManagerParentID(role.getRoleManagerId()); - managerSession.setManagerChildIDs(childManagerList); - managerSession.setStyle(website.getAppStyle()); - // 压入管理员seesion - BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); - } else { - if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { - LOG.debug("roleId: "+_manager.getManagerRoleID()); - return ResultData.build().error( this.getResString("err.not.exist",this.getResString("manager"))); - } else { - BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); - } - } - BeanUtils.copyProperties(_manager, managerSession); + } + request.setAttribute("app", app); + return "/login"; + } - Subject subject = SecurityUtils.getSubject(); - UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(),managerSession.getManagerPassword()); - subject.login(upt); - return ResultData.build().success(); - } else { - // 密码错误 - return ResultData.build().error( this.getResString("err.password")); - } - } - } + /** + * 验证登录 + * + * @param manager 管理员实体 + * @param request 请求 + * @param response 响应 + */ + @ApiOperation(value = "验证登录") + @ApiImplicitParams({ + @ApiImplicitParam(name = "managerName", value = "帐号", required = true, paramType = "query"), + @ApiImplicitParam(name = "managerPassword", value = "密码", required = true, paramType = "query"), + }) + @PostMapping(value = "/checkLogin") + @ResponseBody + public ResultData checkLogin(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletRequest request, + HttpServletResponse response) { + // 根据url地址获取站点信息,主要是区分管理员对那些网站有权限 + AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); + if (urlWebsite == null) { + return ResultData.build().error(this.getResString("err.not.exist", this.getResString("app"), "!请尝试去文件 WEB-INF/ms.install.bak 后缀bak")); + } + //验证码 + if (!(checkRandCode())) { + return ResultData.build().error(getResString("err.error", new String[]{getResString("rand.code")})); + } + + // 根据账号获取当前管理员信息 + ManagerEntity newManager = new ManagerEntity(); + newManager.setManagerName(manager.getManagerName()); + ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + + if (_manager == null || StringUtils.isEmpty(manager.getManagerName())) { + // 系统不存在此用户 + return ResultData.build().error(this.getResString("err.nameEmpty")); + } else { + // 判断当前用户输入的密码是否正确 + if (SecureUtil.md5(manager.getManagerPassword()).equals(_manager.getManagerPassword())) { + // 创建管理员session对象 + ManagerSessionEntity managerSession = new ManagerSessionEntity(); + AppEntity website = new AppEntity(); + // 获取管理员所在的角色 + RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); + website = (AppEntity) appBiz.getEntity(role.getAppId()); + // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 + if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId() == BasicUtil.getAppId())) { + if (website == null) { + website = BasicUtil.getApp(); + } + List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); + managerSession.setBasicId(website.getAppId()); + managerSession.setManagerParentID(role.getRoleManagerId()); + managerSession.setManagerChildIDs(childManagerList); + managerSession.setStyle(website.getAppStyle()); + // 压入管理员seesion + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } else { + if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { + LOG.debug("roleId: " + _manager.getManagerRoleID()); + return ResultData.build().error(this.getResString("err.not.exist", this.getResString("manager"))); + } else { + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } + } + BeanUtils.copyProperties(_manager, managerSession); + + Subject subject = SecurityUtils.getSubject(); + UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(), managerSession.getManagerPassword()); + subject.login(upt); + return ResultData.build().success(); + } else { + // 密码错误 + return ResultData.build().error(this.getResString("err.password")); + } + } + } } diff --git a/ms-base/pom.xml b/ms-base/pom.xml index a5dcef6..af71af1 100644 --- a/ms-base/pom.xml +++ b/ms-base/pom.xml @@ -363,26 +363,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-base diff --git a/ms-basic/ms-basic.iml b/ms-basic/ms-basic.iml index 935ebc4..5a2e74f 100644 --- a/ms-basic/ms-basic.iml +++ b/ms-basic/ms-basic.iml @@ -11,8 +11,8 @@ - - + + diff --git a/ms-basic/pom.xml b/ms-basic/pom.xml index e66ec4f..90ba669 100644 --- a/ms-basic/pom.xml +++ b/ms-basic/pom.xml @@ -88,26 +88,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-basic diff --git a/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java b/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java index 540c752..30a601c 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java @@ -1,16 +1,16 @@ /** * The MIT License (MIT) * Copyright (c) 2020 铭软科技(mingsoft.net) - + *

* Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: - + *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - + *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR @@ -30,20 +30,25 @@ import io.swagger.annotations.ApiOperation; import net.mingsoft.base.entity.ResultData; import net.mingsoft.basic.action.BaseAction; +import net.mingsoft.basic.bean.ShiroUser; import net.mingsoft.basic.biz.IAppBiz; import net.mingsoft.basic.biz.IManagerBiz; import net.mingsoft.basic.biz.IRoleBiz; import net.mingsoft.basic.constant.Const; +import net.mingsoft.basic.constant.ErrorCodeEnum; import net.mingsoft.basic.constant.e.SessionConstEnum; import net.mingsoft.basic.entity.AppEntity; import net.mingsoft.basic.entity.ManagerEntity; import net.mingsoft.basic.entity.ManagerSessionEntity; import net.mingsoft.basic.entity.RoleEntity; +import net.mingsoft.basic.exception.DcmsLoginException; import net.mingsoft.basic.util.BasicUtil; +import net.mingsoft.basic.util.DcmsUtils; import net.mingsoft.basic.util.StringUtil; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.UsernamePasswordToken; +import org.apache.shiro.session.ExpiredSessionException; import org.apache.shiro.subject.Subject; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -56,14 +61,13 @@ import javax.servlet.http.HttpServletResponse; import java.io.File; import java.util.List; +import java.util.Map; /** - * - * @ClassName: LoginAction + * @ClassName: LoginAction * @Description:TODO(登录的基础应用层) * @author: 铭飞开发团队 - * @date: 2015年1月27日 下午3:21:47 - * + * @date: 2015年1月27日 下午3:21:47 * @Copyright: 2018 www.mingsoft.net Inc. All rights reserved. */ @Api("登录的基础应用层接口") @@ -71,146 +75,228 @@ @RequestMapping("/${ms.manager.path}") public class LoginAction extends BaseAction { - @Value("${ms.manager.path}") - private String managerPath; - /** - * 管理员业务层 - */ - @Autowired - private IManagerBiz managerBiz; + @Value("${ms.manager.path}") + private String managerPath; + @Value("${ms.manager.dcmsHost}") + private String dcmsHost; + @Value("${ms.manager.userInfoPath}") + private String userInfoPath; + @Value("#{${ms.manager.userRoles}}") + private Map userRoles; + @Value("#{'${ms.manager.users}'.split(',')}") + private List users; + /** + * 管理员业务层 + */ + @Autowired + private IManagerBiz managerBiz; - /** - * 角色业务request层 - */ - @Autowired - private IRoleBiz roleBiz; + /** + * 角色业务request层 + */ + @Autowired + private IRoleBiz roleBiz; - /** - * 站点业务层 - */ - @Autowired - private IAppBiz appBiz; + /** + * 站点业务层 + */ + @Autowired + private IAppBiz appBiz; + /** + * 加载管理员登录界面 + * + * @param request 请求对象 + * @return 管理员登录界面地址 + */ + @ApiOperation(value = "加载管理员登录界面") + @SuppressWarnings("resource") + @GetMapping("/dcmsToken") + public String dcmsToken(HttpServletRequest request) { + if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION) != null) { + return "redirect:" + managerPath + "/index.do"; + } + //请求dcms数据token + ShiroUser shiroUser = DcmsUtils.checkToken(dcmsHost + userInfoPath, request.getParameter("token")); + if (shiroUser == null) { + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无效token"); + } + String userName = DcmsUtils.transformCmsUser(shiroUser, userRoles, users); + if (userName == null) { + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无效token"); + } - /** - * 加载管理员登录界面 - * - * @param request - * 请求对象 - * @return 管理员登录界面地址 - */ - @ApiOperation(value = "加载管理员登录界面") - @SuppressWarnings("resource") - @GetMapping("/login") - public String login(HttpServletRequest request) { - if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION)!=null) { - return "redirect:"+managerPath+"/index.do"; - } - // 根据请求地址来显示标题 - AppEntity app = BasicUtil.getApp(); - // 判断应用实体是否存在 - if (app != null) { - // 检测应用是否有自定义界面b - if (!StringUtil.isBlank(app.getAppLoginPage())) { - LOG.debug("跳转自定义登录界面"); - return "redirect:" + app.getAppLoginPage(); - } + AppEntity app = BasicUtil.getApp(); + // 判断应用实体是否存在 + if (app != null) { + // 检测应用是否有自定义界面b + if (!StringUtil.isBlank(app.getAppLoginPage())) { + LOG.debug("跳转自定义登录界面"); + return "redirect:" + app.getAppLoginPage(); + } - } else { - File file = new File(BasicUtil.getRealPath( "WEB-INF/ms.install")); - //存在安装文件 - if (file.exists()) { - String defaultId = FileReader.create(new File(BasicUtil.getRealPath( "WEB-INF/ms.install"))).readString(); - if (!StringUtils.isEmpty(defaultId)) { - app = (AppEntity) appBiz.getEntity(Integer.parseInt(defaultId)); - app.setAppUrl(this.getUrl(request)); - appBiz.updateEntity(app); - FileWriter.create(new File(BasicUtil.getRealPath( "WEB-INF/ms.install.bak"))).write(defaultId); - file.delete(); - } - } + } + AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); + ManagerEntity newManager = new ManagerEntity(); + newManager.setManagerName(userName); + ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + // 创建管理员session对象 + ManagerSessionEntity managerSession = new ManagerSessionEntity(); + AppEntity website = new AppEntity(); + // 获取管理员所在的角色 + RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); + website = (AppEntity) appBiz.getEntity(role.getAppId()); + // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 + if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId() == BasicUtil.getAppId())) { + if (website == null) { + website = BasicUtil.getApp(); + } + List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); + managerSession.setStyle(website.getAppStyle()); + managerSession.setBasicId(website.getAppId()); + managerSession.setManagerParentID(role.getRoleManagerId()); + managerSession.setManagerChildIDs(childManagerList); + // 压入管理员seesion + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } else { + if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { + LOG.debug("roleId: " + _manager.getManagerRoleID()); + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无授权"); + } else { + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } + } + BeanUtils.copyProperties(_manager, managerSession); + Subject subject = SecurityUtils.getSubject(); + UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(), managerSession.getManagerPassword()); + subject.login(upt); + return "redirect:" + managerPath + "/index.do"; + } - } - request.setAttribute("app", app); - return "/login"; - } + @GetMapping("/404") + public String error(HttpServletRequest request) { + return "/404"; + } - /** - * 验证登录 - * - * @param manager - * 管理员实体 - * @param request - * 请求 - * @param response - * 响应 - */ - @ApiOperation(value = "验证登录") - @ApiImplicitParams({ - @ApiImplicitParam(name = "managerName", value = "帐号", required = true,paramType="query"), - @ApiImplicitParam(name = "managerPassword", value = "密码", required = true,paramType="query"), - }) - @PostMapping(value="/checkLogin") - @ResponseBody - public ResultData checkLogin(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletRequest request, - HttpServletResponse response) { - AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); // 根据url地址获取站点信息,主要是区分管理员对那些网站有权限 - if (urlWebsite == null) { - return ResultData.build().error( this.getResString("err.not.exist",this.getResString("app"),"!请尝试去文件 WEB-INF/ms.install.bak 后缀bak")); - } - //验证码 - if (!(checkRandCode())) { - return ResultData.build().error(getResString("err.error", new String[] { getResString("rand.code") })); - } + /** + * 加载管理员登录界面 + * + * @param request 请求对象 + * @return 管理员登录界面地址 + */ + @ApiOperation(value = "加载管理员登录界面") + @SuppressWarnings("resource") + @GetMapping("/login") + public String login(HttpServletRequest request) { + if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION) != null) { + return "redirect:" + managerPath + "/index.do"; + } + // 根据请求地址来显示标题 + AppEntity app = BasicUtil.getApp(); + // 判断应用实体是否存在 + if (app != null) { + // 检测应用是否有自定义界面b + if (!StringUtil.isBlank(app.getAppLoginPage())) { + LOG.debug("跳转自定义登录界面"); + return "redirect:" + app.getAppLoginPage(); + } - // 根据账号获取当前管理员信息 - ManagerEntity newManager = new ManagerEntity(); - newManager.setManagerName(manager.getManagerName()); - ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + } else { + File file = new File(BasicUtil.getRealPath("WEB-INF/ms.install")); + //存在安装文件 + if (file.exists()) { + String defaultId = FileReader.create(new File(BasicUtil.getRealPath("WEB-INF/ms.install"))).readString(); + if (!StringUtils.isEmpty(defaultId)) { + app = (AppEntity) appBiz.getEntity(Integer.parseInt(defaultId)); + app.setAppUrl(this.getUrl(request)); + appBiz.updateEntity(app); + FileWriter.create(new File(BasicUtil.getRealPath("WEB-INF/ms.install.bak"))).write(defaultId); + file.delete(); + } + } - if (_manager == null || StringUtils.isEmpty(manager.getManagerName())) { - // 系统不存在此用户 - return ResultData.build().error( this.getResString("err.nameEmpty")); - } else { - // 判断当前用户输入的密码是否正确 - if (SecureUtil.md5(manager.getManagerPassword()).equals(_manager.getManagerPassword())) { - // 创建管理员session对象 - ManagerSessionEntity managerSession = new ManagerSessionEntity(); - AppEntity website = new AppEntity(); - // 获取管理员所在的角色 - RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); - website = (AppEntity) appBiz.getEntity(role.getAppId()); - // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 - if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId()==BasicUtil.getAppId())) { - if(website==null){ - website = BasicUtil.getApp(); - } - List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); - managerSession.setBasicId(website.getAppId()); - managerSession.setManagerParentID(role.getRoleManagerId()); - managerSession.setManagerChildIDs(childManagerList); - managerSession.setStyle(website.getAppStyle()); - // 压入管理员seesion - BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); - } else { - if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { - LOG.debug("roleId: "+_manager.getManagerRoleID()); - return ResultData.build().error( this.getResString("err.not.exist",this.getResString("manager"))); - } else { - BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); - } - } - BeanUtils.copyProperties(_manager, managerSession); + } + request.setAttribute("app", app); + return "/login"; + } - Subject subject = SecurityUtils.getSubject(); - UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(),managerSession.getManagerPassword()); - subject.login(upt); - return ResultData.build().success(); - } else { - // 密码错误 - return ResultData.build().error( this.getResString("err.password")); - } - } - } + /** + * 验证登录 + * + * @param manager 管理员实体 + * @param request 请求 + * @param response 响应 + */ + @ApiOperation(value = "验证登录") + @ApiImplicitParams({ + @ApiImplicitParam(name = "managerName", value = "帐号", required = true, paramType = "query"), + @ApiImplicitParam(name = "managerPassword", value = "密码", required = true, paramType = "query"), + }) + @PostMapping(value = "/checkLogin") + @ResponseBody + public ResultData checkLogin(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletRequest request, + HttpServletResponse response) { + // 根据url地址获取站点信息,主要是区分管理员对那些网站有权限 + AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); + if (urlWebsite == null) { + return ResultData.build().error(this.getResString("err.not.exist", this.getResString("app"), "!请尝试去文件 WEB-INF/ms.install.bak 后缀bak")); + } + //验证码 + if (!(checkRandCode())) { + return ResultData.build().error(getResString("err.error", new String[]{getResString("rand.code")})); + } + + // 根据账号获取当前管理员信息 + ManagerEntity newManager = new ManagerEntity(); + newManager.setManagerName(manager.getManagerName()); + ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + + if (_manager == null || StringUtils.isEmpty(manager.getManagerName())) { + // 系统不存在此用户 + return ResultData.build().error(this.getResString("err.nameEmpty")); + } else { + // 判断当前用户输入的密码是否正确 + if (SecureUtil.md5(manager.getManagerPassword()).equals(_manager.getManagerPassword())) { + // 创建管理员session对象 + ManagerSessionEntity managerSession = new ManagerSessionEntity(); + AppEntity website = new AppEntity(); + // 获取管理员所在的角色 + RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); + website = (AppEntity) appBiz.getEntity(role.getAppId()); + // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 + if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId() == BasicUtil.getAppId())) { + if (website == null) { + website = BasicUtil.getApp(); + } + List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); + managerSession.setBasicId(website.getAppId()); + managerSession.setManagerParentID(role.getRoleManagerId()); + managerSession.setManagerChildIDs(childManagerList); + managerSession.setStyle(website.getAppStyle()); + // 压入管理员seesion + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } else { + if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { + LOG.debug("roleId: " + _manager.getManagerRoleID()); + return ResultData.build().error(this.getResString("err.not.exist", this.getResString("manager"))); + } else { + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } + } + BeanUtils.copyProperties(_manager, managerSession); + + Subject subject = SecurityUtils.getSubject(); + UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(), managerSession.getManagerPassword()); + subject.login(upt); + return ResultData.build().success(); + } else { + // 密码错误 + return ResultData.build().error(this.getResString("err.password")); + } + } + } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java b/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java index 726a50c..f0e6377 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java @@ -1,16 +1,16 @@ /** * The MIT License (MIT) * Copyright (c) 2020 铭软科技(mingsoft.net) - + *

* Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: - + *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - + *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR @@ -61,214 +61,263 @@ */ @ControllerAdvice public class GlobalExceptionResolver extends DefaultHandlerExceptionResolver { - - protected final Logger LOG = LoggerFactory.getLogger(this.getClass()); - /** - * 全局异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = Exception.class) - public ModelAndView handleException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.SERVER_ERROR); - return getModelAndView(request, response, map,e); - } + protected final Logger LOG = LoggerFactory.getLogger(this.getClass()); - /** - * 全局异常 未找到类404 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = NoHandlerFoundException.class) - public ModelAndView handleNoHandlerFoundException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 全局异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = Exception.class) + public ModelAndView handleException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.SERVER_ERROR); + return getModelAndView(request, response, map, e); + } - /** - * 请求参数异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = MissingServletRequestParameterException.class) - public ModelAndView handleMissingServletRequestParameterException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 全局异常 未找到类404 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = NoHandlerFoundException.class) + public ModelAndView handleNoHandlerFoundException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 请求方法类型错误 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) - public ModelAndView handleHttpRequestMethodNotSupportedException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 请求参数异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = MissingServletRequestParameterException.class) + public ModelAndView handleMissingServletRequestParameterException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 统一处理请求参数校验(实体对象传参) - * - * @param e BindException - * @return ResultResponse - */ - @ExceptionHandler(BindException.class) - public ModelAndView validExceptionHandler(HttpServletRequest request, HttpServletResponse response,BindException e) { - StringBuilder message = new StringBuilder(); - List fieldErrors = e.getBindingResult().getFieldErrors(); - for (FieldError error : fieldErrors) { - message.append(error.getField()).append(error.getDefaultMessage()).append(","); - } - message = new StringBuilder(message.substring(0, message.length() - 1)); - Map map = new HashMap(); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("code", ErrorCodeEnum.CLIENT_REQUEST); - map.put("result",false); - map.put("msg", message.toString()); - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * 请求方法类型错误 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) + public ModelAndView handleHttpRequestMethodNotSupportedException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 统一处理请求参数校验(普通传参) - * - * @param e ConstraintViolationException - * @return ResultResponse - */ - @ExceptionHandler(value = ConstraintViolationException.class) - public ModelAndView handleConstraintViolationException(HttpServletRequest request, HttpServletResponse response,ConstraintViolationException e) { - StringBuilder message = new StringBuilder(); - Set> violations = e.getConstraintViolations(); - for (ConstraintViolation violation : violations) { - Path path = violation.getPropertyPath(); - String[] pathArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(path.toString(), "."); - message.append(pathArr[1]).append(violation.getMessage()).append(","); - } - message = new StringBuilder(message.substring(0, message.length() - 1)); - Map map = new HashMap(); - map.put("msg", message.toString()); - map.put("code", ErrorCodeEnum.CLIENT_REQUEST); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 + /** + * 统一处理请求参数校验(实体对象传参) + * + * @param e BindException + * @return ResultResponse + */ + @ExceptionHandler(BindException.class) + public ModelAndView validExceptionHandler(HttpServletRequest request, HttpServletResponse response, BindException e) { + StringBuilder message = new StringBuilder(); + List fieldErrors = e.getBindingResult().getFieldErrors(); + for (FieldError error : fieldErrors) { + message.append(error.getField()).append(error.getDefaultMessage()).append(","); + } + message = new StringBuilder(message.substring(0, message.length() - 1)); + Map map = new HashMap(); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("code", ErrorCodeEnum.CLIENT_REQUEST); + map.put("result", false); + map.put("msg", message.toString()); + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * 统一处理请求参数校验(普通传参) + * + * @param e ConstraintViolationException + * @return ResultResponse + */ + @ExceptionHandler(value = ConstraintViolationException.class) + public ModelAndView handleConstraintViolationException(HttpServletRequest request, HttpServletResponse response, ConstraintViolationException e) { + StringBuilder message = new StringBuilder(); + Set> violations = e.getConstraintViolations(); + for (ConstraintViolation violation : violations) { + Path path = violation.getPropertyPath(); + String[] pathArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(path.toString(), "."); + message.append(pathArr[1]).append(violation.getMessage()).append(","); + } + message = new StringBuilder(message.substring(0, message.length() - 1)); + Map map = new HashMap(); + map.put("msg", message.toString()); + map.put("code", ErrorCodeEnum.CLIENT_REQUEST); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - /** - * shiro权限未授权异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = UnauthorizedException.class) - public ModelAndView handleUnauthorizedException(HttpServletRequest request, HttpServletResponse response,UnauthorizedException e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); - return getModelAndView(request, response, map,e); - } + /** + * shiro权限未授权异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = UnauthorizedException.class) + public ModelAndView handleUnauthorizedException(HttpServletRequest request, HttpServletResponse response, UnauthorizedException e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + return getModelAndView(request, response, map, e); + } - /** - * 登录异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = AuthenticationException.class) - public ModelAndView handleAuthenticationException(HttpServletRequest request, HttpServletResponse response,AuthenticationException e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 登录异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = AuthenticationException.class) + public ModelAndView handleAuthenticationException(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * shiro权限错误 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = AuthorizationException.class) - public ModelAndView handleAuthorizationException(HttpServletRequest request, HttpServletResponse response,AuthorizationException e){ - LOG.debug("AuthorizationException", e); - Map map = new HashMap(); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - map.put("msg", e.getStackTrace()); - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * shiro权限错误 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = AuthorizationException.class) + public ModelAndView handleAuthorizationException(HttpServletRequest request, HttpServletResponse response, AuthorizationException e) { + LOG.debug("AuthorizationException", e); + Map map = new HashMap(); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + map.put("msg", e.getStackTrace()); + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - /** - * session失效异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = ExpiredSessionException.class) - public ModelAndView handleExpiredSessionException(HttpServletRequest request, HttpServletResponse response,ExpiredSessionException e) { - LOG.debug("ExpiredSessionException", e); - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); - return getModelAndView(request, response, map,e); - } + /** + * session失效异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = ExpiredSessionException.class) + public ModelAndView handleExpiredSessionException(HttpServletRequest request, HttpServletResponse response, ExpiredSessionException e) { + LOG.debug("ExpiredSessionException", e); + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + return getModelAndView(request, response, map, e); + } - /** - * 返回异常信息处理 - * @param request - * @param response - * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 - * @param e - * @return - */ - private ModelAndView getModelAndView(HttpServletRequest request, HttpServletResponse response, Map map,Exception e) { - if(ObjectUtil.isNotNull(e)){ - LOG.debug("handleException", e); - StringWriter sw = new StringWriter(); - e.printStackTrace(new PrintWriter(sw, true)); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("errMsg", sw.toString()); - map.put("msg", e.getMessage()); - map.put("exc", e.getClass()); //详细异常信息 - } - //去掉异常信息中的跨站脚本 - map.put("msg",Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "",new Whitelist(), new Document.OutputSettings().prettyPrint(false))); - map.put("result",false); - if (BasicUtil.isAjaxRequest(request)) { - try { - response.setContentType("application/json;charset=UTF-8"); - PrintWriter writer = response.getWriter(); - writer.write(JSONObject.toJSONString(map)); - writer.flush(); - writer.close(); - } catch (IOException ex) { - ex.printStackTrace(); - } - } else { - return new ModelAndView("/error/index", map); - } - return null; - } + @ExceptionHandler(value = DcmsLoginException.class) + public ModelAndView handlDcmsLoginException(HttpServletRequest request, HttpServletResponse response, DcmsLoginException e) { + LOG.debug("DcmsLoginException", e); + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + map.put("msg", e.getBizMsg()); + return getErrorModelAndView(request, response, map, e); + } + /** + * 返回异常信息处理 + * @param request + * @param response + * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 + * @param e + * @return + */ + private ModelAndView getModelAndView(HttpServletRequest request, HttpServletResponse response, Map map, Exception e) { + if (ObjectUtil.isNotNull(e)) { + LOG.debug("handleException", e); + StringWriter sw = new StringWriter(); + e.printStackTrace(new PrintWriter(sw, true)); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("errMsg", sw.toString()); + map.put("msg", e.getMessage()); + map.put("exc", e.getClass()); //详细异常信息 + } + //去掉异常信息中的跨站脚本 + map.put("msg", Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "", new Whitelist(), new Document.OutputSettings().prettyPrint(false))); + map.put("result", false); + if (BasicUtil.isAjaxRequest(request)) { + try { + response.setContentType("application/json;charset=UTF-8"); + PrintWriter writer = response.getWriter(); + writer.write(JSONObject.toJSONString(map)); + writer.flush(); + writer.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } else { + return new ModelAndView("/error/index", map); + } + return null; + } + + /** + * 返回异常信息处理 + * @param request + * @param response + * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 + * @param e + * @return + */ + private ModelAndView getErrorModelAndView(HttpServletRequest request, HttpServletResponse response, Map map, Exception e) { + if (ObjectUtil.isNotNull(e)) { + LOG.debug("handleException", e); + StringWriter sw = new StringWriter(); + map.put("errMsg", sw.toString()); + e.printStackTrace(new PrintWriter(sw, true)); + //出错的类 + map.put("cls", e.getStackTrace()[0] + ""); + //请求地址 + map.put("url", request.getServletPath()); + //详细异常信息 + map.put("exc", e.getClass()); + } + //去掉异常信息中的跨站脚本 + map.put("msg", Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "", new Whitelist(), new Document.OutputSettings().prettyPrint(false))); + map.put("result", false); + if (BasicUtil.isAjaxRequest(request)) { + try { + PrintWriter writer = response.getWriter(); + response.setContentType("application/json;charset=UTF-8"); + + writer.write(JSONObject.toJSONString(map)); + writer.flush(); + writer.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } else { + return new ModelAndView("/error/dcmserror", map); + } + return null; + } } diff --git a/ms-base/pom.xml b/ms-base/pom.xml index a5dcef6..af71af1 100644 --- a/ms-base/pom.xml +++ b/ms-base/pom.xml @@ -363,26 +363,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-base diff --git a/ms-basic/ms-basic.iml b/ms-basic/ms-basic.iml index 935ebc4..5a2e74f 100644 --- a/ms-basic/ms-basic.iml +++ b/ms-basic/ms-basic.iml @@ -11,8 +11,8 @@ - - + + diff --git a/ms-basic/pom.xml b/ms-basic/pom.xml index e66ec4f..90ba669 100644 --- a/ms-basic/pom.xml +++ b/ms-basic/pom.xml @@ -88,26 +88,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-basic diff --git a/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java b/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java index 540c752..30a601c 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java @@ -1,16 +1,16 @@ /** * The MIT License (MIT) * Copyright (c) 2020 铭软科技(mingsoft.net) - + *

* Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: - + *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - + *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR @@ -30,20 +30,25 @@ import io.swagger.annotations.ApiOperation; import net.mingsoft.base.entity.ResultData; import net.mingsoft.basic.action.BaseAction; +import net.mingsoft.basic.bean.ShiroUser; import net.mingsoft.basic.biz.IAppBiz; import net.mingsoft.basic.biz.IManagerBiz; import net.mingsoft.basic.biz.IRoleBiz; import net.mingsoft.basic.constant.Const; +import net.mingsoft.basic.constant.ErrorCodeEnum; import net.mingsoft.basic.constant.e.SessionConstEnum; import net.mingsoft.basic.entity.AppEntity; import net.mingsoft.basic.entity.ManagerEntity; import net.mingsoft.basic.entity.ManagerSessionEntity; import net.mingsoft.basic.entity.RoleEntity; +import net.mingsoft.basic.exception.DcmsLoginException; import net.mingsoft.basic.util.BasicUtil; +import net.mingsoft.basic.util.DcmsUtils; import net.mingsoft.basic.util.StringUtil; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.UsernamePasswordToken; +import org.apache.shiro.session.ExpiredSessionException; import org.apache.shiro.subject.Subject; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -56,14 +61,13 @@ import javax.servlet.http.HttpServletResponse; import java.io.File; import java.util.List; +import java.util.Map; /** - * - * @ClassName: LoginAction + * @ClassName: LoginAction * @Description:TODO(登录的基础应用层) * @author: 铭飞开发团队 - * @date: 2015年1月27日 下午3:21:47 - * + * @date: 2015年1月27日 下午3:21:47 * @Copyright: 2018 www.mingsoft.net Inc. All rights reserved. */ @Api("登录的基础应用层接口") @@ -71,146 +75,228 @@ @RequestMapping("/${ms.manager.path}") public class LoginAction extends BaseAction { - @Value("${ms.manager.path}") - private String managerPath; - /** - * 管理员业务层 - */ - @Autowired - private IManagerBiz managerBiz; + @Value("${ms.manager.path}") + private String managerPath; + @Value("${ms.manager.dcmsHost}") + private String dcmsHost; + @Value("${ms.manager.userInfoPath}") + private String userInfoPath; + @Value("#{${ms.manager.userRoles}}") + private Map userRoles; + @Value("#{'${ms.manager.users}'.split(',')}") + private List users; + /** + * 管理员业务层 + */ + @Autowired + private IManagerBiz managerBiz; - /** - * 角色业务request层 - */ - @Autowired - private IRoleBiz roleBiz; + /** + * 角色业务request层 + */ + @Autowired + private IRoleBiz roleBiz; - /** - * 站点业务层 - */ - @Autowired - private IAppBiz appBiz; + /** + * 站点业务层 + */ + @Autowired + private IAppBiz appBiz; + /** + * 加载管理员登录界面 + * + * @param request 请求对象 + * @return 管理员登录界面地址 + */ + @ApiOperation(value = "加载管理员登录界面") + @SuppressWarnings("resource") + @GetMapping("/dcmsToken") + public String dcmsToken(HttpServletRequest request) { + if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION) != null) { + return "redirect:" + managerPath + "/index.do"; + } + //请求dcms数据token + ShiroUser shiroUser = DcmsUtils.checkToken(dcmsHost + userInfoPath, request.getParameter("token")); + if (shiroUser == null) { + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无效token"); + } + String userName = DcmsUtils.transformCmsUser(shiroUser, userRoles, users); + if (userName == null) { + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无效token"); + } - /** - * 加载管理员登录界面 - * - * @param request - * 请求对象 - * @return 管理员登录界面地址 - */ - @ApiOperation(value = "加载管理员登录界面") - @SuppressWarnings("resource") - @GetMapping("/login") - public String login(HttpServletRequest request) { - if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION)!=null) { - return "redirect:"+managerPath+"/index.do"; - } - // 根据请求地址来显示标题 - AppEntity app = BasicUtil.getApp(); - // 判断应用实体是否存在 - if (app != null) { - // 检测应用是否有自定义界面b - if (!StringUtil.isBlank(app.getAppLoginPage())) { - LOG.debug("跳转自定义登录界面"); - return "redirect:" + app.getAppLoginPage(); - } + AppEntity app = BasicUtil.getApp(); + // 判断应用实体是否存在 + if (app != null) { + // 检测应用是否有自定义界面b + if (!StringUtil.isBlank(app.getAppLoginPage())) { + LOG.debug("跳转自定义登录界面"); + return "redirect:" + app.getAppLoginPage(); + } - } else { - File file = new File(BasicUtil.getRealPath( "WEB-INF/ms.install")); - //存在安装文件 - if (file.exists()) { - String defaultId = FileReader.create(new File(BasicUtil.getRealPath( "WEB-INF/ms.install"))).readString(); - if (!StringUtils.isEmpty(defaultId)) { - app = (AppEntity) appBiz.getEntity(Integer.parseInt(defaultId)); - app.setAppUrl(this.getUrl(request)); - appBiz.updateEntity(app); - FileWriter.create(new File(BasicUtil.getRealPath( "WEB-INF/ms.install.bak"))).write(defaultId); - file.delete(); - } - } + } + AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); + ManagerEntity newManager = new ManagerEntity(); + newManager.setManagerName(userName); + ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + // 创建管理员session对象 + ManagerSessionEntity managerSession = new ManagerSessionEntity(); + AppEntity website = new AppEntity(); + // 获取管理员所在的角色 + RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); + website = (AppEntity) appBiz.getEntity(role.getAppId()); + // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 + if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId() == BasicUtil.getAppId())) { + if (website == null) { + website = BasicUtil.getApp(); + } + List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); + managerSession.setStyle(website.getAppStyle()); + managerSession.setBasicId(website.getAppId()); + managerSession.setManagerParentID(role.getRoleManagerId()); + managerSession.setManagerChildIDs(childManagerList); + // 压入管理员seesion + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } else { + if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { + LOG.debug("roleId: " + _manager.getManagerRoleID()); + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无授权"); + } else { + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } + } + BeanUtils.copyProperties(_manager, managerSession); + Subject subject = SecurityUtils.getSubject(); + UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(), managerSession.getManagerPassword()); + subject.login(upt); + return "redirect:" + managerPath + "/index.do"; + } - } - request.setAttribute("app", app); - return "/login"; - } + @GetMapping("/404") + public String error(HttpServletRequest request) { + return "/404"; + } - /** - * 验证登录 - * - * @param manager - * 管理员实体 - * @param request - * 请求 - * @param response - * 响应 - */ - @ApiOperation(value = "验证登录") - @ApiImplicitParams({ - @ApiImplicitParam(name = "managerName", value = "帐号", required = true,paramType="query"), - @ApiImplicitParam(name = "managerPassword", value = "密码", required = true,paramType="query"), - }) - @PostMapping(value="/checkLogin") - @ResponseBody - public ResultData checkLogin(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletRequest request, - HttpServletResponse response) { - AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); // 根据url地址获取站点信息,主要是区分管理员对那些网站有权限 - if (urlWebsite == null) { - return ResultData.build().error( this.getResString("err.not.exist",this.getResString("app"),"!请尝试去文件 WEB-INF/ms.install.bak 后缀bak")); - } - //验证码 - if (!(checkRandCode())) { - return ResultData.build().error(getResString("err.error", new String[] { getResString("rand.code") })); - } + /** + * 加载管理员登录界面 + * + * @param request 请求对象 + * @return 管理员登录界面地址 + */ + @ApiOperation(value = "加载管理员登录界面") + @SuppressWarnings("resource") + @GetMapping("/login") + public String login(HttpServletRequest request) { + if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION) != null) { + return "redirect:" + managerPath + "/index.do"; + } + // 根据请求地址来显示标题 + AppEntity app = BasicUtil.getApp(); + // 判断应用实体是否存在 + if (app != null) { + // 检测应用是否有自定义界面b + if (!StringUtil.isBlank(app.getAppLoginPage())) { + LOG.debug("跳转自定义登录界面"); + return "redirect:" + app.getAppLoginPage(); + } - // 根据账号获取当前管理员信息 - ManagerEntity newManager = new ManagerEntity(); - newManager.setManagerName(manager.getManagerName()); - ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + } else { + File file = new File(BasicUtil.getRealPath("WEB-INF/ms.install")); + //存在安装文件 + if (file.exists()) { + String defaultId = FileReader.create(new File(BasicUtil.getRealPath("WEB-INF/ms.install"))).readString(); + if (!StringUtils.isEmpty(defaultId)) { + app = (AppEntity) appBiz.getEntity(Integer.parseInt(defaultId)); + app.setAppUrl(this.getUrl(request)); + appBiz.updateEntity(app); + FileWriter.create(new File(BasicUtil.getRealPath("WEB-INF/ms.install.bak"))).write(defaultId); + file.delete(); + } + } - if (_manager == null || StringUtils.isEmpty(manager.getManagerName())) { - // 系统不存在此用户 - return ResultData.build().error( this.getResString("err.nameEmpty")); - } else { - // 判断当前用户输入的密码是否正确 - if (SecureUtil.md5(manager.getManagerPassword()).equals(_manager.getManagerPassword())) { - // 创建管理员session对象 - ManagerSessionEntity managerSession = new ManagerSessionEntity(); - AppEntity website = new AppEntity(); - // 获取管理员所在的角色 - RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); - website = (AppEntity) appBiz.getEntity(role.getAppId()); - // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 - if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId()==BasicUtil.getAppId())) { - if(website==null){ - website = BasicUtil.getApp(); - } - List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); - managerSession.setBasicId(website.getAppId()); - managerSession.setManagerParentID(role.getRoleManagerId()); - managerSession.setManagerChildIDs(childManagerList); - managerSession.setStyle(website.getAppStyle()); - // 压入管理员seesion - BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); - } else { - if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { - LOG.debug("roleId: "+_manager.getManagerRoleID()); - return ResultData.build().error( this.getResString("err.not.exist",this.getResString("manager"))); - } else { - BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); - } - } - BeanUtils.copyProperties(_manager, managerSession); + } + request.setAttribute("app", app); + return "/login"; + } - Subject subject = SecurityUtils.getSubject(); - UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(),managerSession.getManagerPassword()); - subject.login(upt); - return ResultData.build().success(); - } else { - // 密码错误 - return ResultData.build().error( this.getResString("err.password")); - } - } - } + /** + * 验证登录 + * + * @param manager 管理员实体 + * @param request 请求 + * @param response 响应 + */ + @ApiOperation(value = "验证登录") + @ApiImplicitParams({ + @ApiImplicitParam(name = "managerName", value = "帐号", required = true, paramType = "query"), + @ApiImplicitParam(name = "managerPassword", value = "密码", required = true, paramType = "query"), + }) + @PostMapping(value = "/checkLogin") + @ResponseBody + public ResultData checkLogin(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletRequest request, + HttpServletResponse response) { + // 根据url地址获取站点信息,主要是区分管理员对那些网站有权限 + AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); + if (urlWebsite == null) { + return ResultData.build().error(this.getResString("err.not.exist", this.getResString("app"), "!请尝试去文件 WEB-INF/ms.install.bak 后缀bak")); + } + //验证码 + if (!(checkRandCode())) { + return ResultData.build().error(getResString("err.error", new String[]{getResString("rand.code")})); + } + + // 根据账号获取当前管理员信息 + ManagerEntity newManager = new ManagerEntity(); + newManager.setManagerName(manager.getManagerName()); + ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + + if (_manager == null || StringUtils.isEmpty(manager.getManagerName())) { + // 系统不存在此用户 + return ResultData.build().error(this.getResString("err.nameEmpty")); + } else { + // 判断当前用户输入的密码是否正确 + if (SecureUtil.md5(manager.getManagerPassword()).equals(_manager.getManagerPassword())) { + // 创建管理员session对象 + ManagerSessionEntity managerSession = new ManagerSessionEntity(); + AppEntity website = new AppEntity(); + // 获取管理员所在的角色 + RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); + website = (AppEntity) appBiz.getEntity(role.getAppId()); + // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 + if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId() == BasicUtil.getAppId())) { + if (website == null) { + website = BasicUtil.getApp(); + } + List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); + managerSession.setBasicId(website.getAppId()); + managerSession.setManagerParentID(role.getRoleManagerId()); + managerSession.setManagerChildIDs(childManagerList); + managerSession.setStyle(website.getAppStyle()); + // 压入管理员seesion + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } else { + if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { + LOG.debug("roleId: " + _manager.getManagerRoleID()); + return ResultData.build().error(this.getResString("err.not.exist", this.getResString("manager"))); + } else { + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } + } + BeanUtils.copyProperties(_manager, managerSession); + + Subject subject = SecurityUtils.getSubject(); + UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(), managerSession.getManagerPassword()); + subject.login(upt); + return ResultData.build().success(); + } else { + // 密码错误 + return ResultData.build().error(this.getResString("err.password")); + } + } + } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java b/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java index 726a50c..f0e6377 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java @@ -1,16 +1,16 @@ /** * The MIT License (MIT) * Copyright (c) 2020 铭软科技(mingsoft.net) - + *

* Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: - + *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - + *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR @@ -61,214 +61,263 @@ */ @ControllerAdvice public class GlobalExceptionResolver extends DefaultHandlerExceptionResolver { - - protected final Logger LOG = LoggerFactory.getLogger(this.getClass()); - /** - * 全局异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = Exception.class) - public ModelAndView handleException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.SERVER_ERROR); - return getModelAndView(request, response, map,e); - } + protected final Logger LOG = LoggerFactory.getLogger(this.getClass()); - /** - * 全局异常 未找到类404 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = NoHandlerFoundException.class) - public ModelAndView handleNoHandlerFoundException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 全局异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = Exception.class) + public ModelAndView handleException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.SERVER_ERROR); + return getModelAndView(request, response, map, e); + } - /** - * 请求参数异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = MissingServletRequestParameterException.class) - public ModelAndView handleMissingServletRequestParameterException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 全局异常 未找到类404 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = NoHandlerFoundException.class) + public ModelAndView handleNoHandlerFoundException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 请求方法类型错误 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) - public ModelAndView handleHttpRequestMethodNotSupportedException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 请求参数异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = MissingServletRequestParameterException.class) + public ModelAndView handleMissingServletRequestParameterException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 统一处理请求参数校验(实体对象传参) - * - * @param e BindException - * @return ResultResponse - */ - @ExceptionHandler(BindException.class) - public ModelAndView validExceptionHandler(HttpServletRequest request, HttpServletResponse response,BindException e) { - StringBuilder message = new StringBuilder(); - List fieldErrors = e.getBindingResult().getFieldErrors(); - for (FieldError error : fieldErrors) { - message.append(error.getField()).append(error.getDefaultMessage()).append(","); - } - message = new StringBuilder(message.substring(0, message.length() - 1)); - Map map = new HashMap(); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("code", ErrorCodeEnum.CLIENT_REQUEST); - map.put("result",false); - map.put("msg", message.toString()); - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * 请求方法类型错误 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) + public ModelAndView handleHttpRequestMethodNotSupportedException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 统一处理请求参数校验(普通传参) - * - * @param e ConstraintViolationException - * @return ResultResponse - */ - @ExceptionHandler(value = ConstraintViolationException.class) - public ModelAndView handleConstraintViolationException(HttpServletRequest request, HttpServletResponse response,ConstraintViolationException e) { - StringBuilder message = new StringBuilder(); - Set> violations = e.getConstraintViolations(); - for (ConstraintViolation violation : violations) { - Path path = violation.getPropertyPath(); - String[] pathArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(path.toString(), "."); - message.append(pathArr[1]).append(violation.getMessage()).append(","); - } - message = new StringBuilder(message.substring(0, message.length() - 1)); - Map map = new HashMap(); - map.put("msg", message.toString()); - map.put("code", ErrorCodeEnum.CLIENT_REQUEST); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 + /** + * 统一处理请求参数校验(实体对象传参) + * + * @param e BindException + * @return ResultResponse + */ + @ExceptionHandler(BindException.class) + public ModelAndView validExceptionHandler(HttpServletRequest request, HttpServletResponse response, BindException e) { + StringBuilder message = new StringBuilder(); + List fieldErrors = e.getBindingResult().getFieldErrors(); + for (FieldError error : fieldErrors) { + message.append(error.getField()).append(error.getDefaultMessage()).append(","); + } + message = new StringBuilder(message.substring(0, message.length() - 1)); + Map map = new HashMap(); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("code", ErrorCodeEnum.CLIENT_REQUEST); + map.put("result", false); + map.put("msg", message.toString()); + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * 统一处理请求参数校验(普通传参) + * + * @param e ConstraintViolationException + * @return ResultResponse + */ + @ExceptionHandler(value = ConstraintViolationException.class) + public ModelAndView handleConstraintViolationException(HttpServletRequest request, HttpServletResponse response, ConstraintViolationException e) { + StringBuilder message = new StringBuilder(); + Set> violations = e.getConstraintViolations(); + for (ConstraintViolation violation : violations) { + Path path = violation.getPropertyPath(); + String[] pathArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(path.toString(), "."); + message.append(pathArr[1]).append(violation.getMessage()).append(","); + } + message = new StringBuilder(message.substring(0, message.length() - 1)); + Map map = new HashMap(); + map.put("msg", message.toString()); + map.put("code", ErrorCodeEnum.CLIENT_REQUEST); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - /** - * shiro权限未授权异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = UnauthorizedException.class) - public ModelAndView handleUnauthorizedException(HttpServletRequest request, HttpServletResponse response,UnauthorizedException e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); - return getModelAndView(request, response, map,e); - } + /** + * shiro权限未授权异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = UnauthorizedException.class) + public ModelAndView handleUnauthorizedException(HttpServletRequest request, HttpServletResponse response, UnauthorizedException e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + return getModelAndView(request, response, map, e); + } - /** - * 登录异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = AuthenticationException.class) - public ModelAndView handleAuthenticationException(HttpServletRequest request, HttpServletResponse response,AuthenticationException e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 登录异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = AuthenticationException.class) + public ModelAndView handleAuthenticationException(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * shiro权限错误 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = AuthorizationException.class) - public ModelAndView handleAuthorizationException(HttpServletRequest request, HttpServletResponse response,AuthorizationException e){ - LOG.debug("AuthorizationException", e); - Map map = new HashMap(); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - map.put("msg", e.getStackTrace()); - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * shiro权限错误 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = AuthorizationException.class) + public ModelAndView handleAuthorizationException(HttpServletRequest request, HttpServletResponse response, AuthorizationException e) { + LOG.debug("AuthorizationException", e); + Map map = new HashMap(); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + map.put("msg", e.getStackTrace()); + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - /** - * session失效异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = ExpiredSessionException.class) - public ModelAndView handleExpiredSessionException(HttpServletRequest request, HttpServletResponse response,ExpiredSessionException e) { - LOG.debug("ExpiredSessionException", e); - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); - return getModelAndView(request, response, map,e); - } + /** + * session失效异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = ExpiredSessionException.class) + public ModelAndView handleExpiredSessionException(HttpServletRequest request, HttpServletResponse response, ExpiredSessionException e) { + LOG.debug("ExpiredSessionException", e); + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + return getModelAndView(request, response, map, e); + } - /** - * 返回异常信息处理 - * @param request - * @param response - * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 - * @param e - * @return - */ - private ModelAndView getModelAndView(HttpServletRequest request, HttpServletResponse response, Map map,Exception e) { - if(ObjectUtil.isNotNull(e)){ - LOG.debug("handleException", e); - StringWriter sw = new StringWriter(); - e.printStackTrace(new PrintWriter(sw, true)); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("errMsg", sw.toString()); - map.put("msg", e.getMessage()); - map.put("exc", e.getClass()); //详细异常信息 - } - //去掉异常信息中的跨站脚本 - map.put("msg",Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "",new Whitelist(), new Document.OutputSettings().prettyPrint(false))); - map.put("result",false); - if (BasicUtil.isAjaxRequest(request)) { - try { - response.setContentType("application/json;charset=UTF-8"); - PrintWriter writer = response.getWriter(); - writer.write(JSONObject.toJSONString(map)); - writer.flush(); - writer.close(); - } catch (IOException ex) { - ex.printStackTrace(); - } - } else { - return new ModelAndView("/error/index", map); - } - return null; - } + @ExceptionHandler(value = DcmsLoginException.class) + public ModelAndView handlDcmsLoginException(HttpServletRequest request, HttpServletResponse response, DcmsLoginException e) { + LOG.debug("DcmsLoginException", e); + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + map.put("msg", e.getBizMsg()); + return getErrorModelAndView(request, response, map, e); + } + /** + * 返回异常信息处理 + * @param request + * @param response + * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 + * @param e + * @return + */ + private ModelAndView getModelAndView(HttpServletRequest request, HttpServletResponse response, Map map, Exception e) { + if (ObjectUtil.isNotNull(e)) { + LOG.debug("handleException", e); + StringWriter sw = new StringWriter(); + e.printStackTrace(new PrintWriter(sw, true)); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("errMsg", sw.toString()); + map.put("msg", e.getMessage()); + map.put("exc", e.getClass()); //详细异常信息 + } + //去掉异常信息中的跨站脚本 + map.put("msg", Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "", new Whitelist(), new Document.OutputSettings().prettyPrint(false))); + map.put("result", false); + if (BasicUtil.isAjaxRequest(request)) { + try { + response.setContentType("application/json;charset=UTF-8"); + PrintWriter writer = response.getWriter(); + writer.write(JSONObject.toJSONString(map)); + writer.flush(); + writer.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } else { + return new ModelAndView("/error/index", map); + } + return null; + } + + /** + * 返回异常信息处理 + * @param request + * @param response + * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 + * @param e + * @return + */ + private ModelAndView getErrorModelAndView(HttpServletRequest request, HttpServletResponse response, Map map, Exception e) { + if (ObjectUtil.isNotNull(e)) { + LOG.debug("handleException", e); + StringWriter sw = new StringWriter(); + map.put("errMsg", sw.toString()); + e.printStackTrace(new PrintWriter(sw, true)); + //出错的类 + map.put("cls", e.getStackTrace()[0] + ""); + //请求地址 + map.put("url", request.getServletPath()); + //详细异常信息 + map.put("exc", e.getClass()); + } + //去掉异常信息中的跨站脚本 + map.put("msg", Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "", new Whitelist(), new Document.OutputSettings().prettyPrint(false))); + map.put("result", false); + if (BasicUtil.isAjaxRequest(request)) { + try { + PrintWriter writer = response.getWriter(); + response.setContentType("application/json;charset=UTF-8"); + + writer.write(JSONObject.toJSONString(map)); + writer.flush(); + writer.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } else { + return new ModelAndView("/error/dcmserror", map); + } + return null; + } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java b/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java index 04c26c8..9cb1d77 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java @@ -28,7 +28,7 @@ public void setHeader(HttpRequestBase base, String token) { base.addHeader("Content-type", DEFAULT_CONTEXTTYPE); if (!StringUtil.isBlank(token)) { - base.addHeader(DEFAULT_CHARSET, token); + base.addHeader(DEFAULT_TOKENKEY, token); } } diff --git a/ms-base/pom.xml b/ms-base/pom.xml index a5dcef6..af71af1 100644 --- a/ms-base/pom.xml +++ b/ms-base/pom.xml @@ -363,26 +363,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-base diff --git a/ms-basic/ms-basic.iml b/ms-basic/ms-basic.iml index 935ebc4..5a2e74f 100644 --- a/ms-basic/ms-basic.iml +++ b/ms-basic/ms-basic.iml @@ -11,8 +11,8 @@ - - + + diff --git a/ms-basic/pom.xml b/ms-basic/pom.xml index e66ec4f..90ba669 100644 --- a/ms-basic/pom.xml +++ b/ms-basic/pom.xml @@ -88,26 +88,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-basic diff --git a/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java b/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java index 540c752..30a601c 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java @@ -1,16 +1,16 @@ /** * The MIT License (MIT) * Copyright (c) 2020 铭软科技(mingsoft.net) - + *

* Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: - + *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - + *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR @@ -30,20 +30,25 @@ import io.swagger.annotations.ApiOperation; import net.mingsoft.base.entity.ResultData; import net.mingsoft.basic.action.BaseAction; +import net.mingsoft.basic.bean.ShiroUser; import net.mingsoft.basic.biz.IAppBiz; import net.mingsoft.basic.biz.IManagerBiz; import net.mingsoft.basic.biz.IRoleBiz; import net.mingsoft.basic.constant.Const; +import net.mingsoft.basic.constant.ErrorCodeEnum; import net.mingsoft.basic.constant.e.SessionConstEnum; import net.mingsoft.basic.entity.AppEntity; import net.mingsoft.basic.entity.ManagerEntity; import net.mingsoft.basic.entity.ManagerSessionEntity; import net.mingsoft.basic.entity.RoleEntity; +import net.mingsoft.basic.exception.DcmsLoginException; import net.mingsoft.basic.util.BasicUtil; +import net.mingsoft.basic.util.DcmsUtils; import net.mingsoft.basic.util.StringUtil; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.UsernamePasswordToken; +import org.apache.shiro.session.ExpiredSessionException; import org.apache.shiro.subject.Subject; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -56,14 +61,13 @@ import javax.servlet.http.HttpServletResponse; import java.io.File; import java.util.List; +import java.util.Map; /** - * - * @ClassName: LoginAction + * @ClassName: LoginAction * @Description:TODO(登录的基础应用层) * @author: 铭飞开发团队 - * @date: 2015年1月27日 下午3:21:47 - * + * @date: 2015年1月27日 下午3:21:47 * @Copyright: 2018 www.mingsoft.net Inc. All rights reserved. */ @Api("登录的基础应用层接口") @@ -71,146 +75,228 @@ @RequestMapping("/${ms.manager.path}") public class LoginAction extends BaseAction { - @Value("${ms.manager.path}") - private String managerPath; - /** - * 管理员业务层 - */ - @Autowired - private IManagerBiz managerBiz; + @Value("${ms.manager.path}") + private String managerPath; + @Value("${ms.manager.dcmsHost}") + private String dcmsHost; + @Value("${ms.manager.userInfoPath}") + private String userInfoPath; + @Value("#{${ms.manager.userRoles}}") + private Map userRoles; + @Value("#{'${ms.manager.users}'.split(',')}") + private List users; + /** + * 管理员业务层 + */ + @Autowired + private IManagerBiz managerBiz; - /** - * 角色业务request层 - */ - @Autowired - private IRoleBiz roleBiz; + /** + * 角色业务request层 + */ + @Autowired + private IRoleBiz roleBiz; - /** - * 站点业务层 - */ - @Autowired - private IAppBiz appBiz; + /** + * 站点业务层 + */ + @Autowired + private IAppBiz appBiz; + /** + * 加载管理员登录界面 + * + * @param request 请求对象 + * @return 管理员登录界面地址 + */ + @ApiOperation(value = "加载管理员登录界面") + @SuppressWarnings("resource") + @GetMapping("/dcmsToken") + public String dcmsToken(HttpServletRequest request) { + if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION) != null) { + return "redirect:" + managerPath + "/index.do"; + } + //请求dcms数据token + ShiroUser shiroUser = DcmsUtils.checkToken(dcmsHost + userInfoPath, request.getParameter("token")); + if (shiroUser == null) { + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无效token"); + } + String userName = DcmsUtils.transformCmsUser(shiroUser, userRoles, users); + if (userName == null) { + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无效token"); + } - /** - * 加载管理员登录界面 - * - * @param request - * 请求对象 - * @return 管理员登录界面地址 - */ - @ApiOperation(value = "加载管理员登录界面") - @SuppressWarnings("resource") - @GetMapping("/login") - public String login(HttpServletRequest request) { - if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION)!=null) { - return "redirect:"+managerPath+"/index.do"; - } - // 根据请求地址来显示标题 - AppEntity app = BasicUtil.getApp(); - // 判断应用实体是否存在 - if (app != null) { - // 检测应用是否有自定义界面b - if (!StringUtil.isBlank(app.getAppLoginPage())) { - LOG.debug("跳转自定义登录界面"); - return "redirect:" + app.getAppLoginPage(); - } + AppEntity app = BasicUtil.getApp(); + // 判断应用实体是否存在 + if (app != null) { + // 检测应用是否有自定义界面b + if (!StringUtil.isBlank(app.getAppLoginPage())) { + LOG.debug("跳转自定义登录界面"); + return "redirect:" + app.getAppLoginPage(); + } - } else { - File file = new File(BasicUtil.getRealPath( "WEB-INF/ms.install")); - //存在安装文件 - if (file.exists()) { - String defaultId = FileReader.create(new File(BasicUtil.getRealPath( "WEB-INF/ms.install"))).readString(); - if (!StringUtils.isEmpty(defaultId)) { - app = (AppEntity) appBiz.getEntity(Integer.parseInt(defaultId)); - app.setAppUrl(this.getUrl(request)); - appBiz.updateEntity(app); - FileWriter.create(new File(BasicUtil.getRealPath( "WEB-INF/ms.install.bak"))).write(defaultId); - file.delete(); - } - } + } + AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); + ManagerEntity newManager = new ManagerEntity(); + newManager.setManagerName(userName); + ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + // 创建管理员session对象 + ManagerSessionEntity managerSession = new ManagerSessionEntity(); + AppEntity website = new AppEntity(); + // 获取管理员所在的角色 + RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); + website = (AppEntity) appBiz.getEntity(role.getAppId()); + // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 + if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId() == BasicUtil.getAppId())) { + if (website == null) { + website = BasicUtil.getApp(); + } + List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); + managerSession.setStyle(website.getAppStyle()); + managerSession.setBasicId(website.getAppId()); + managerSession.setManagerParentID(role.getRoleManagerId()); + managerSession.setManagerChildIDs(childManagerList); + // 压入管理员seesion + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } else { + if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { + LOG.debug("roleId: " + _manager.getManagerRoleID()); + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无授权"); + } else { + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } + } + BeanUtils.copyProperties(_manager, managerSession); + Subject subject = SecurityUtils.getSubject(); + UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(), managerSession.getManagerPassword()); + subject.login(upt); + return "redirect:" + managerPath + "/index.do"; + } - } - request.setAttribute("app", app); - return "/login"; - } + @GetMapping("/404") + public String error(HttpServletRequest request) { + return "/404"; + } - /** - * 验证登录 - * - * @param manager - * 管理员实体 - * @param request - * 请求 - * @param response - * 响应 - */ - @ApiOperation(value = "验证登录") - @ApiImplicitParams({ - @ApiImplicitParam(name = "managerName", value = "帐号", required = true,paramType="query"), - @ApiImplicitParam(name = "managerPassword", value = "密码", required = true,paramType="query"), - }) - @PostMapping(value="/checkLogin") - @ResponseBody - public ResultData checkLogin(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletRequest request, - HttpServletResponse response) { - AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); // 根据url地址获取站点信息,主要是区分管理员对那些网站有权限 - if (urlWebsite == null) { - return ResultData.build().error( this.getResString("err.not.exist",this.getResString("app"),"!请尝试去文件 WEB-INF/ms.install.bak 后缀bak")); - } - //验证码 - if (!(checkRandCode())) { - return ResultData.build().error(getResString("err.error", new String[] { getResString("rand.code") })); - } + /** + * 加载管理员登录界面 + * + * @param request 请求对象 + * @return 管理员登录界面地址 + */ + @ApiOperation(value = "加载管理员登录界面") + @SuppressWarnings("resource") + @GetMapping("/login") + public String login(HttpServletRequest request) { + if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION) != null) { + return "redirect:" + managerPath + "/index.do"; + } + // 根据请求地址来显示标题 + AppEntity app = BasicUtil.getApp(); + // 判断应用实体是否存在 + if (app != null) { + // 检测应用是否有自定义界面b + if (!StringUtil.isBlank(app.getAppLoginPage())) { + LOG.debug("跳转自定义登录界面"); + return "redirect:" + app.getAppLoginPage(); + } - // 根据账号获取当前管理员信息 - ManagerEntity newManager = new ManagerEntity(); - newManager.setManagerName(manager.getManagerName()); - ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + } else { + File file = new File(BasicUtil.getRealPath("WEB-INF/ms.install")); + //存在安装文件 + if (file.exists()) { + String defaultId = FileReader.create(new File(BasicUtil.getRealPath("WEB-INF/ms.install"))).readString(); + if (!StringUtils.isEmpty(defaultId)) { + app = (AppEntity) appBiz.getEntity(Integer.parseInt(defaultId)); + app.setAppUrl(this.getUrl(request)); + appBiz.updateEntity(app); + FileWriter.create(new File(BasicUtil.getRealPath("WEB-INF/ms.install.bak"))).write(defaultId); + file.delete(); + } + } - if (_manager == null || StringUtils.isEmpty(manager.getManagerName())) { - // 系统不存在此用户 - return ResultData.build().error( this.getResString("err.nameEmpty")); - } else { - // 判断当前用户输入的密码是否正确 - if (SecureUtil.md5(manager.getManagerPassword()).equals(_manager.getManagerPassword())) { - // 创建管理员session对象 - ManagerSessionEntity managerSession = new ManagerSessionEntity(); - AppEntity website = new AppEntity(); - // 获取管理员所在的角色 - RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); - website = (AppEntity) appBiz.getEntity(role.getAppId()); - // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 - if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId()==BasicUtil.getAppId())) { - if(website==null){ - website = BasicUtil.getApp(); - } - List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); - managerSession.setBasicId(website.getAppId()); - managerSession.setManagerParentID(role.getRoleManagerId()); - managerSession.setManagerChildIDs(childManagerList); - managerSession.setStyle(website.getAppStyle()); - // 压入管理员seesion - BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); - } else { - if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { - LOG.debug("roleId: "+_manager.getManagerRoleID()); - return ResultData.build().error( this.getResString("err.not.exist",this.getResString("manager"))); - } else { - BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); - } - } - BeanUtils.copyProperties(_manager, managerSession); + } + request.setAttribute("app", app); + return "/login"; + } - Subject subject = SecurityUtils.getSubject(); - UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(),managerSession.getManagerPassword()); - subject.login(upt); - return ResultData.build().success(); - } else { - // 密码错误 - return ResultData.build().error( this.getResString("err.password")); - } - } - } + /** + * 验证登录 + * + * @param manager 管理员实体 + * @param request 请求 + * @param response 响应 + */ + @ApiOperation(value = "验证登录") + @ApiImplicitParams({ + @ApiImplicitParam(name = "managerName", value = "帐号", required = true, paramType = "query"), + @ApiImplicitParam(name = "managerPassword", value = "密码", required = true, paramType = "query"), + }) + @PostMapping(value = "/checkLogin") + @ResponseBody + public ResultData checkLogin(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletRequest request, + HttpServletResponse response) { + // 根据url地址获取站点信息,主要是区分管理员对那些网站有权限 + AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); + if (urlWebsite == null) { + return ResultData.build().error(this.getResString("err.not.exist", this.getResString("app"), "!请尝试去文件 WEB-INF/ms.install.bak 后缀bak")); + } + //验证码 + if (!(checkRandCode())) { + return ResultData.build().error(getResString("err.error", new String[]{getResString("rand.code")})); + } + + // 根据账号获取当前管理员信息 + ManagerEntity newManager = new ManagerEntity(); + newManager.setManagerName(manager.getManagerName()); + ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + + if (_manager == null || StringUtils.isEmpty(manager.getManagerName())) { + // 系统不存在此用户 + return ResultData.build().error(this.getResString("err.nameEmpty")); + } else { + // 判断当前用户输入的密码是否正确 + if (SecureUtil.md5(manager.getManagerPassword()).equals(_manager.getManagerPassword())) { + // 创建管理员session对象 + ManagerSessionEntity managerSession = new ManagerSessionEntity(); + AppEntity website = new AppEntity(); + // 获取管理员所在的角色 + RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); + website = (AppEntity) appBiz.getEntity(role.getAppId()); + // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 + if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId() == BasicUtil.getAppId())) { + if (website == null) { + website = BasicUtil.getApp(); + } + List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); + managerSession.setBasicId(website.getAppId()); + managerSession.setManagerParentID(role.getRoleManagerId()); + managerSession.setManagerChildIDs(childManagerList); + managerSession.setStyle(website.getAppStyle()); + // 压入管理员seesion + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } else { + if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { + LOG.debug("roleId: " + _manager.getManagerRoleID()); + return ResultData.build().error(this.getResString("err.not.exist", this.getResString("manager"))); + } else { + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } + } + BeanUtils.copyProperties(_manager, managerSession); + + Subject subject = SecurityUtils.getSubject(); + UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(), managerSession.getManagerPassword()); + subject.login(upt); + return ResultData.build().success(); + } else { + // 密码错误 + return ResultData.build().error(this.getResString("err.password")); + } + } + } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java b/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java index 726a50c..f0e6377 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java @@ -1,16 +1,16 @@ /** * The MIT License (MIT) * Copyright (c) 2020 铭软科技(mingsoft.net) - + *

* Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: - + *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - + *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR @@ -61,214 +61,263 @@ */ @ControllerAdvice public class GlobalExceptionResolver extends DefaultHandlerExceptionResolver { - - protected final Logger LOG = LoggerFactory.getLogger(this.getClass()); - /** - * 全局异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = Exception.class) - public ModelAndView handleException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.SERVER_ERROR); - return getModelAndView(request, response, map,e); - } + protected final Logger LOG = LoggerFactory.getLogger(this.getClass()); - /** - * 全局异常 未找到类404 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = NoHandlerFoundException.class) - public ModelAndView handleNoHandlerFoundException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 全局异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = Exception.class) + public ModelAndView handleException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.SERVER_ERROR); + return getModelAndView(request, response, map, e); + } - /** - * 请求参数异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = MissingServletRequestParameterException.class) - public ModelAndView handleMissingServletRequestParameterException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 全局异常 未找到类404 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = NoHandlerFoundException.class) + public ModelAndView handleNoHandlerFoundException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 请求方法类型错误 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) - public ModelAndView handleHttpRequestMethodNotSupportedException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 请求参数异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = MissingServletRequestParameterException.class) + public ModelAndView handleMissingServletRequestParameterException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 统一处理请求参数校验(实体对象传参) - * - * @param e BindException - * @return ResultResponse - */ - @ExceptionHandler(BindException.class) - public ModelAndView validExceptionHandler(HttpServletRequest request, HttpServletResponse response,BindException e) { - StringBuilder message = new StringBuilder(); - List fieldErrors = e.getBindingResult().getFieldErrors(); - for (FieldError error : fieldErrors) { - message.append(error.getField()).append(error.getDefaultMessage()).append(","); - } - message = new StringBuilder(message.substring(0, message.length() - 1)); - Map map = new HashMap(); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("code", ErrorCodeEnum.CLIENT_REQUEST); - map.put("result",false); - map.put("msg", message.toString()); - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * 请求方法类型错误 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) + public ModelAndView handleHttpRequestMethodNotSupportedException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 统一处理请求参数校验(普通传参) - * - * @param e ConstraintViolationException - * @return ResultResponse - */ - @ExceptionHandler(value = ConstraintViolationException.class) - public ModelAndView handleConstraintViolationException(HttpServletRequest request, HttpServletResponse response,ConstraintViolationException e) { - StringBuilder message = new StringBuilder(); - Set> violations = e.getConstraintViolations(); - for (ConstraintViolation violation : violations) { - Path path = violation.getPropertyPath(); - String[] pathArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(path.toString(), "."); - message.append(pathArr[1]).append(violation.getMessage()).append(","); - } - message = new StringBuilder(message.substring(0, message.length() - 1)); - Map map = new HashMap(); - map.put("msg", message.toString()); - map.put("code", ErrorCodeEnum.CLIENT_REQUEST); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 + /** + * 统一处理请求参数校验(实体对象传参) + * + * @param e BindException + * @return ResultResponse + */ + @ExceptionHandler(BindException.class) + public ModelAndView validExceptionHandler(HttpServletRequest request, HttpServletResponse response, BindException e) { + StringBuilder message = new StringBuilder(); + List fieldErrors = e.getBindingResult().getFieldErrors(); + for (FieldError error : fieldErrors) { + message.append(error.getField()).append(error.getDefaultMessage()).append(","); + } + message = new StringBuilder(message.substring(0, message.length() - 1)); + Map map = new HashMap(); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("code", ErrorCodeEnum.CLIENT_REQUEST); + map.put("result", false); + map.put("msg", message.toString()); + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * 统一处理请求参数校验(普通传参) + * + * @param e ConstraintViolationException + * @return ResultResponse + */ + @ExceptionHandler(value = ConstraintViolationException.class) + public ModelAndView handleConstraintViolationException(HttpServletRequest request, HttpServletResponse response, ConstraintViolationException e) { + StringBuilder message = new StringBuilder(); + Set> violations = e.getConstraintViolations(); + for (ConstraintViolation violation : violations) { + Path path = violation.getPropertyPath(); + String[] pathArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(path.toString(), "."); + message.append(pathArr[1]).append(violation.getMessage()).append(","); + } + message = new StringBuilder(message.substring(0, message.length() - 1)); + Map map = new HashMap(); + map.put("msg", message.toString()); + map.put("code", ErrorCodeEnum.CLIENT_REQUEST); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - /** - * shiro权限未授权异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = UnauthorizedException.class) - public ModelAndView handleUnauthorizedException(HttpServletRequest request, HttpServletResponse response,UnauthorizedException e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); - return getModelAndView(request, response, map,e); - } + /** + * shiro权限未授权异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = UnauthorizedException.class) + public ModelAndView handleUnauthorizedException(HttpServletRequest request, HttpServletResponse response, UnauthorizedException e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + return getModelAndView(request, response, map, e); + } - /** - * 登录异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = AuthenticationException.class) - public ModelAndView handleAuthenticationException(HttpServletRequest request, HttpServletResponse response,AuthenticationException e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 登录异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = AuthenticationException.class) + public ModelAndView handleAuthenticationException(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * shiro权限错误 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = AuthorizationException.class) - public ModelAndView handleAuthorizationException(HttpServletRequest request, HttpServletResponse response,AuthorizationException e){ - LOG.debug("AuthorizationException", e); - Map map = new HashMap(); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - map.put("msg", e.getStackTrace()); - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * shiro权限错误 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = AuthorizationException.class) + public ModelAndView handleAuthorizationException(HttpServletRequest request, HttpServletResponse response, AuthorizationException e) { + LOG.debug("AuthorizationException", e); + Map map = new HashMap(); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + map.put("msg", e.getStackTrace()); + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - /** - * session失效异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = ExpiredSessionException.class) - public ModelAndView handleExpiredSessionException(HttpServletRequest request, HttpServletResponse response,ExpiredSessionException e) { - LOG.debug("ExpiredSessionException", e); - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); - return getModelAndView(request, response, map,e); - } + /** + * session失效异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = ExpiredSessionException.class) + public ModelAndView handleExpiredSessionException(HttpServletRequest request, HttpServletResponse response, ExpiredSessionException e) { + LOG.debug("ExpiredSessionException", e); + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + return getModelAndView(request, response, map, e); + } - /** - * 返回异常信息处理 - * @param request - * @param response - * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 - * @param e - * @return - */ - private ModelAndView getModelAndView(HttpServletRequest request, HttpServletResponse response, Map map,Exception e) { - if(ObjectUtil.isNotNull(e)){ - LOG.debug("handleException", e); - StringWriter sw = new StringWriter(); - e.printStackTrace(new PrintWriter(sw, true)); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("errMsg", sw.toString()); - map.put("msg", e.getMessage()); - map.put("exc", e.getClass()); //详细异常信息 - } - //去掉异常信息中的跨站脚本 - map.put("msg",Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "",new Whitelist(), new Document.OutputSettings().prettyPrint(false))); - map.put("result",false); - if (BasicUtil.isAjaxRequest(request)) { - try { - response.setContentType("application/json;charset=UTF-8"); - PrintWriter writer = response.getWriter(); - writer.write(JSONObject.toJSONString(map)); - writer.flush(); - writer.close(); - } catch (IOException ex) { - ex.printStackTrace(); - } - } else { - return new ModelAndView("/error/index", map); - } - return null; - } + @ExceptionHandler(value = DcmsLoginException.class) + public ModelAndView handlDcmsLoginException(HttpServletRequest request, HttpServletResponse response, DcmsLoginException e) { + LOG.debug("DcmsLoginException", e); + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + map.put("msg", e.getBizMsg()); + return getErrorModelAndView(request, response, map, e); + } + /** + * 返回异常信息处理 + * @param request + * @param response + * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 + * @param e + * @return + */ + private ModelAndView getModelAndView(HttpServletRequest request, HttpServletResponse response, Map map, Exception e) { + if (ObjectUtil.isNotNull(e)) { + LOG.debug("handleException", e); + StringWriter sw = new StringWriter(); + e.printStackTrace(new PrintWriter(sw, true)); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("errMsg", sw.toString()); + map.put("msg", e.getMessage()); + map.put("exc", e.getClass()); //详细异常信息 + } + //去掉异常信息中的跨站脚本 + map.put("msg", Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "", new Whitelist(), new Document.OutputSettings().prettyPrint(false))); + map.put("result", false); + if (BasicUtil.isAjaxRequest(request)) { + try { + response.setContentType("application/json;charset=UTF-8"); + PrintWriter writer = response.getWriter(); + writer.write(JSONObject.toJSONString(map)); + writer.flush(); + writer.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } else { + return new ModelAndView("/error/index", map); + } + return null; + } + + /** + * 返回异常信息处理 + * @param request + * @param response + * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 + * @param e + * @return + */ + private ModelAndView getErrorModelAndView(HttpServletRequest request, HttpServletResponse response, Map map, Exception e) { + if (ObjectUtil.isNotNull(e)) { + LOG.debug("handleException", e); + StringWriter sw = new StringWriter(); + map.put("errMsg", sw.toString()); + e.printStackTrace(new PrintWriter(sw, true)); + //出错的类 + map.put("cls", e.getStackTrace()[0] + ""); + //请求地址 + map.put("url", request.getServletPath()); + //详细异常信息 + map.put("exc", e.getClass()); + } + //去掉异常信息中的跨站脚本 + map.put("msg", Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "", new Whitelist(), new Document.OutputSettings().prettyPrint(false))); + map.put("result", false); + if (BasicUtil.isAjaxRequest(request)) { + try { + PrintWriter writer = response.getWriter(); + response.setContentType("application/json;charset=UTF-8"); + + writer.write(JSONObject.toJSONString(map)); + writer.flush(); + writer.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } else { + return new ModelAndView("/error/dcmserror", map); + } + return null; + } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java b/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java index 04c26c8..9cb1d77 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java @@ -28,7 +28,7 @@ public void setHeader(HttpRequestBase base, String token) { base.addHeader("Content-type", DEFAULT_CONTEXTTYPE); if (!StringUtil.isBlank(token)) { - base.addHeader(DEFAULT_CHARSET, token); + base.addHeader(DEFAULT_TOKENKEY, token); } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java b/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java index 729a05b..1aa9358 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java @@ -7,6 +7,8 @@ import net.mingsoft.basic.handler.DefaultHttpHeaderHandler; import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * @ClassName Test @@ -19,7 +21,7 @@ public static void main(String[] args) { String url = "http://111.198.10.15:11409/user/info"; String token = "06d74157-14d6-4536-bf3a-e94d1d061a80"; - checkToken(token, url); + checkToken(url, token); } /** @@ -28,7 +30,7 @@ * @param token 颁发token * @return */ - public static ShiroUser checkToken(String token, String dcmsUrl) { + public static ShiroUser checkToken(String dcmsUrl, String token) { String json = HttpUtils.sendGet(dcmsUrl, new HashMap<>(), token, new DefaultHttpHeaderHandler()); if (!StringUtil.isBlank(json)) { System.out.println(json); @@ -40,4 +42,26 @@ } return null; } + + /** + * 转换为cms用户 + * + * @param user dmcs用户 + * @return + */ + public static String transformCmsUser(ShiroUser user, Map userRoles, List users) { + //是否有管理员 + for (String manager : userRoles.get("manager").split(",")) { + if (user.getRoleTips().contains(manager)) { + return users.get(0); + } + } + //返回普通用户 + for (String manager : userRoles.get("normal").split(",")) { + if (user.getRoleTips().contains(manager)) { + return users.get(1); + } + } + return null; + } } diff --git a/ms-base/pom.xml b/ms-base/pom.xml index a5dcef6..af71af1 100644 --- a/ms-base/pom.xml +++ b/ms-base/pom.xml @@ -363,26 +363,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-base diff --git a/ms-basic/ms-basic.iml b/ms-basic/ms-basic.iml index 935ebc4..5a2e74f 100644 --- a/ms-basic/ms-basic.iml +++ b/ms-basic/ms-basic.iml @@ -11,8 +11,8 @@ - - + + diff --git a/ms-basic/pom.xml b/ms-basic/pom.xml index e66ec4f..90ba669 100644 --- a/ms-basic/pom.xml +++ b/ms-basic/pom.xml @@ -88,26 +88,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-basic diff --git a/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java b/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java index 540c752..30a601c 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java @@ -1,16 +1,16 @@ /** * The MIT License (MIT) * Copyright (c) 2020 铭软科技(mingsoft.net) - + *

* Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: - + *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - + *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR @@ -30,20 +30,25 @@ import io.swagger.annotations.ApiOperation; import net.mingsoft.base.entity.ResultData; import net.mingsoft.basic.action.BaseAction; +import net.mingsoft.basic.bean.ShiroUser; import net.mingsoft.basic.biz.IAppBiz; import net.mingsoft.basic.biz.IManagerBiz; import net.mingsoft.basic.biz.IRoleBiz; import net.mingsoft.basic.constant.Const; +import net.mingsoft.basic.constant.ErrorCodeEnum; import net.mingsoft.basic.constant.e.SessionConstEnum; import net.mingsoft.basic.entity.AppEntity; import net.mingsoft.basic.entity.ManagerEntity; import net.mingsoft.basic.entity.ManagerSessionEntity; import net.mingsoft.basic.entity.RoleEntity; +import net.mingsoft.basic.exception.DcmsLoginException; import net.mingsoft.basic.util.BasicUtil; +import net.mingsoft.basic.util.DcmsUtils; import net.mingsoft.basic.util.StringUtil; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.UsernamePasswordToken; +import org.apache.shiro.session.ExpiredSessionException; import org.apache.shiro.subject.Subject; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -56,14 +61,13 @@ import javax.servlet.http.HttpServletResponse; import java.io.File; import java.util.List; +import java.util.Map; /** - * - * @ClassName: LoginAction + * @ClassName: LoginAction * @Description:TODO(登录的基础应用层) * @author: 铭飞开发团队 - * @date: 2015年1月27日 下午3:21:47 - * + * @date: 2015年1月27日 下午3:21:47 * @Copyright: 2018 www.mingsoft.net Inc. All rights reserved. */ @Api("登录的基础应用层接口") @@ -71,146 +75,228 @@ @RequestMapping("/${ms.manager.path}") public class LoginAction extends BaseAction { - @Value("${ms.manager.path}") - private String managerPath; - /** - * 管理员业务层 - */ - @Autowired - private IManagerBiz managerBiz; + @Value("${ms.manager.path}") + private String managerPath; + @Value("${ms.manager.dcmsHost}") + private String dcmsHost; + @Value("${ms.manager.userInfoPath}") + private String userInfoPath; + @Value("#{${ms.manager.userRoles}}") + private Map userRoles; + @Value("#{'${ms.manager.users}'.split(',')}") + private List users; + /** + * 管理员业务层 + */ + @Autowired + private IManagerBiz managerBiz; - /** - * 角色业务request层 - */ - @Autowired - private IRoleBiz roleBiz; + /** + * 角色业务request层 + */ + @Autowired + private IRoleBiz roleBiz; - /** - * 站点业务层 - */ - @Autowired - private IAppBiz appBiz; + /** + * 站点业务层 + */ + @Autowired + private IAppBiz appBiz; + /** + * 加载管理员登录界面 + * + * @param request 请求对象 + * @return 管理员登录界面地址 + */ + @ApiOperation(value = "加载管理员登录界面") + @SuppressWarnings("resource") + @GetMapping("/dcmsToken") + public String dcmsToken(HttpServletRequest request) { + if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION) != null) { + return "redirect:" + managerPath + "/index.do"; + } + //请求dcms数据token + ShiroUser shiroUser = DcmsUtils.checkToken(dcmsHost + userInfoPath, request.getParameter("token")); + if (shiroUser == null) { + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无效token"); + } + String userName = DcmsUtils.transformCmsUser(shiroUser, userRoles, users); + if (userName == null) { + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无效token"); + } - /** - * 加载管理员登录界面 - * - * @param request - * 请求对象 - * @return 管理员登录界面地址 - */ - @ApiOperation(value = "加载管理员登录界面") - @SuppressWarnings("resource") - @GetMapping("/login") - public String login(HttpServletRequest request) { - if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION)!=null) { - return "redirect:"+managerPath+"/index.do"; - } - // 根据请求地址来显示标题 - AppEntity app = BasicUtil.getApp(); - // 判断应用实体是否存在 - if (app != null) { - // 检测应用是否有自定义界面b - if (!StringUtil.isBlank(app.getAppLoginPage())) { - LOG.debug("跳转自定义登录界面"); - return "redirect:" + app.getAppLoginPage(); - } + AppEntity app = BasicUtil.getApp(); + // 判断应用实体是否存在 + if (app != null) { + // 检测应用是否有自定义界面b + if (!StringUtil.isBlank(app.getAppLoginPage())) { + LOG.debug("跳转自定义登录界面"); + return "redirect:" + app.getAppLoginPage(); + } - } else { - File file = new File(BasicUtil.getRealPath( "WEB-INF/ms.install")); - //存在安装文件 - if (file.exists()) { - String defaultId = FileReader.create(new File(BasicUtil.getRealPath( "WEB-INF/ms.install"))).readString(); - if (!StringUtils.isEmpty(defaultId)) { - app = (AppEntity) appBiz.getEntity(Integer.parseInt(defaultId)); - app.setAppUrl(this.getUrl(request)); - appBiz.updateEntity(app); - FileWriter.create(new File(BasicUtil.getRealPath( "WEB-INF/ms.install.bak"))).write(defaultId); - file.delete(); - } - } + } + AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); + ManagerEntity newManager = new ManagerEntity(); + newManager.setManagerName(userName); + ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + // 创建管理员session对象 + ManagerSessionEntity managerSession = new ManagerSessionEntity(); + AppEntity website = new AppEntity(); + // 获取管理员所在的角色 + RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); + website = (AppEntity) appBiz.getEntity(role.getAppId()); + // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 + if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId() == BasicUtil.getAppId())) { + if (website == null) { + website = BasicUtil.getApp(); + } + List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); + managerSession.setStyle(website.getAppStyle()); + managerSession.setBasicId(website.getAppId()); + managerSession.setManagerParentID(role.getRoleManagerId()); + managerSession.setManagerChildIDs(childManagerList); + // 压入管理员seesion + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } else { + if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { + LOG.debug("roleId: " + _manager.getManagerRoleID()); + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无授权"); + } else { + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } + } + BeanUtils.copyProperties(_manager, managerSession); + Subject subject = SecurityUtils.getSubject(); + UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(), managerSession.getManagerPassword()); + subject.login(upt); + return "redirect:" + managerPath + "/index.do"; + } - } - request.setAttribute("app", app); - return "/login"; - } + @GetMapping("/404") + public String error(HttpServletRequest request) { + return "/404"; + } - /** - * 验证登录 - * - * @param manager - * 管理员实体 - * @param request - * 请求 - * @param response - * 响应 - */ - @ApiOperation(value = "验证登录") - @ApiImplicitParams({ - @ApiImplicitParam(name = "managerName", value = "帐号", required = true,paramType="query"), - @ApiImplicitParam(name = "managerPassword", value = "密码", required = true,paramType="query"), - }) - @PostMapping(value="/checkLogin") - @ResponseBody - public ResultData checkLogin(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletRequest request, - HttpServletResponse response) { - AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); // 根据url地址获取站点信息,主要是区分管理员对那些网站有权限 - if (urlWebsite == null) { - return ResultData.build().error( this.getResString("err.not.exist",this.getResString("app"),"!请尝试去文件 WEB-INF/ms.install.bak 后缀bak")); - } - //验证码 - if (!(checkRandCode())) { - return ResultData.build().error(getResString("err.error", new String[] { getResString("rand.code") })); - } + /** + * 加载管理员登录界面 + * + * @param request 请求对象 + * @return 管理员登录界面地址 + */ + @ApiOperation(value = "加载管理员登录界面") + @SuppressWarnings("resource") + @GetMapping("/login") + public String login(HttpServletRequest request) { + if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION) != null) { + return "redirect:" + managerPath + "/index.do"; + } + // 根据请求地址来显示标题 + AppEntity app = BasicUtil.getApp(); + // 判断应用实体是否存在 + if (app != null) { + // 检测应用是否有自定义界面b + if (!StringUtil.isBlank(app.getAppLoginPage())) { + LOG.debug("跳转自定义登录界面"); + return "redirect:" + app.getAppLoginPage(); + } - // 根据账号获取当前管理员信息 - ManagerEntity newManager = new ManagerEntity(); - newManager.setManagerName(manager.getManagerName()); - ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + } else { + File file = new File(BasicUtil.getRealPath("WEB-INF/ms.install")); + //存在安装文件 + if (file.exists()) { + String defaultId = FileReader.create(new File(BasicUtil.getRealPath("WEB-INF/ms.install"))).readString(); + if (!StringUtils.isEmpty(defaultId)) { + app = (AppEntity) appBiz.getEntity(Integer.parseInt(defaultId)); + app.setAppUrl(this.getUrl(request)); + appBiz.updateEntity(app); + FileWriter.create(new File(BasicUtil.getRealPath("WEB-INF/ms.install.bak"))).write(defaultId); + file.delete(); + } + } - if (_manager == null || StringUtils.isEmpty(manager.getManagerName())) { - // 系统不存在此用户 - return ResultData.build().error( this.getResString("err.nameEmpty")); - } else { - // 判断当前用户输入的密码是否正确 - if (SecureUtil.md5(manager.getManagerPassword()).equals(_manager.getManagerPassword())) { - // 创建管理员session对象 - ManagerSessionEntity managerSession = new ManagerSessionEntity(); - AppEntity website = new AppEntity(); - // 获取管理员所在的角色 - RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); - website = (AppEntity) appBiz.getEntity(role.getAppId()); - // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 - if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId()==BasicUtil.getAppId())) { - if(website==null){ - website = BasicUtil.getApp(); - } - List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); - managerSession.setBasicId(website.getAppId()); - managerSession.setManagerParentID(role.getRoleManagerId()); - managerSession.setManagerChildIDs(childManagerList); - managerSession.setStyle(website.getAppStyle()); - // 压入管理员seesion - BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); - } else { - if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { - LOG.debug("roleId: "+_manager.getManagerRoleID()); - return ResultData.build().error( this.getResString("err.not.exist",this.getResString("manager"))); - } else { - BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); - } - } - BeanUtils.copyProperties(_manager, managerSession); + } + request.setAttribute("app", app); + return "/login"; + } - Subject subject = SecurityUtils.getSubject(); - UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(),managerSession.getManagerPassword()); - subject.login(upt); - return ResultData.build().success(); - } else { - // 密码错误 - return ResultData.build().error( this.getResString("err.password")); - } - } - } + /** + * 验证登录 + * + * @param manager 管理员实体 + * @param request 请求 + * @param response 响应 + */ + @ApiOperation(value = "验证登录") + @ApiImplicitParams({ + @ApiImplicitParam(name = "managerName", value = "帐号", required = true, paramType = "query"), + @ApiImplicitParam(name = "managerPassword", value = "密码", required = true, paramType = "query"), + }) + @PostMapping(value = "/checkLogin") + @ResponseBody + public ResultData checkLogin(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletRequest request, + HttpServletResponse response) { + // 根据url地址获取站点信息,主要是区分管理员对那些网站有权限 + AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); + if (urlWebsite == null) { + return ResultData.build().error(this.getResString("err.not.exist", this.getResString("app"), "!请尝试去文件 WEB-INF/ms.install.bak 后缀bak")); + } + //验证码 + if (!(checkRandCode())) { + return ResultData.build().error(getResString("err.error", new String[]{getResString("rand.code")})); + } + + // 根据账号获取当前管理员信息 + ManagerEntity newManager = new ManagerEntity(); + newManager.setManagerName(manager.getManagerName()); + ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + + if (_manager == null || StringUtils.isEmpty(manager.getManagerName())) { + // 系统不存在此用户 + return ResultData.build().error(this.getResString("err.nameEmpty")); + } else { + // 判断当前用户输入的密码是否正确 + if (SecureUtil.md5(manager.getManagerPassword()).equals(_manager.getManagerPassword())) { + // 创建管理员session对象 + ManagerSessionEntity managerSession = new ManagerSessionEntity(); + AppEntity website = new AppEntity(); + // 获取管理员所在的角色 + RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); + website = (AppEntity) appBiz.getEntity(role.getAppId()); + // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 + if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId() == BasicUtil.getAppId())) { + if (website == null) { + website = BasicUtil.getApp(); + } + List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); + managerSession.setBasicId(website.getAppId()); + managerSession.setManagerParentID(role.getRoleManagerId()); + managerSession.setManagerChildIDs(childManagerList); + managerSession.setStyle(website.getAppStyle()); + // 压入管理员seesion + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } else { + if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { + LOG.debug("roleId: " + _manager.getManagerRoleID()); + return ResultData.build().error(this.getResString("err.not.exist", this.getResString("manager"))); + } else { + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } + } + BeanUtils.copyProperties(_manager, managerSession); + + Subject subject = SecurityUtils.getSubject(); + UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(), managerSession.getManagerPassword()); + subject.login(upt); + return ResultData.build().success(); + } else { + // 密码错误 + return ResultData.build().error(this.getResString("err.password")); + } + } + } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java b/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java index 726a50c..f0e6377 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java @@ -1,16 +1,16 @@ /** * The MIT License (MIT) * Copyright (c) 2020 铭软科技(mingsoft.net) - + *

* Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: - + *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - + *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR @@ -61,214 +61,263 @@ */ @ControllerAdvice public class GlobalExceptionResolver extends DefaultHandlerExceptionResolver { - - protected final Logger LOG = LoggerFactory.getLogger(this.getClass()); - /** - * 全局异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = Exception.class) - public ModelAndView handleException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.SERVER_ERROR); - return getModelAndView(request, response, map,e); - } + protected final Logger LOG = LoggerFactory.getLogger(this.getClass()); - /** - * 全局异常 未找到类404 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = NoHandlerFoundException.class) - public ModelAndView handleNoHandlerFoundException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 全局异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = Exception.class) + public ModelAndView handleException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.SERVER_ERROR); + return getModelAndView(request, response, map, e); + } - /** - * 请求参数异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = MissingServletRequestParameterException.class) - public ModelAndView handleMissingServletRequestParameterException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 全局异常 未找到类404 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = NoHandlerFoundException.class) + public ModelAndView handleNoHandlerFoundException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 请求方法类型错误 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) - public ModelAndView handleHttpRequestMethodNotSupportedException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 请求参数异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = MissingServletRequestParameterException.class) + public ModelAndView handleMissingServletRequestParameterException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 统一处理请求参数校验(实体对象传参) - * - * @param e BindException - * @return ResultResponse - */ - @ExceptionHandler(BindException.class) - public ModelAndView validExceptionHandler(HttpServletRequest request, HttpServletResponse response,BindException e) { - StringBuilder message = new StringBuilder(); - List fieldErrors = e.getBindingResult().getFieldErrors(); - for (FieldError error : fieldErrors) { - message.append(error.getField()).append(error.getDefaultMessage()).append(","); - } - message = new StringBuilder(message.substring(0, message.length() - 1)); - Map map = new HashMap(); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("code", ErrorCodeEnum.CLIENT_REQUEST); - map.put("result",false); - map.put("msg", message.toString()); - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * 请求方法类型错误 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) + public ModelAndView handleHttpRequestMethodNotSupportedException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 统一处理请求参数校验(普通传参) - * - * @param e ConstraintViolationException - * @return ResultResponse - */ - @ExceptionHandler(value = ConstraintViolationException.class) - public ModelAndView handleConstraintViolationException(HttpServletRequest request, HttpServletResponse response,ConstraintViolationException e) { - StringBuilder message = new StringBuilder(); - Set> violations = e.getConstraintViolations(); - for (ConstraintViolation violation : violations) { - Path path = violation.getPropertyPath(); - String[] pathArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(path.toString(), "."); - message.append(pathArr[1]).append(violation.getMessage()).append(","); - } - message = new StringBuilder(message.substring(0, message.length() - 1)); - Map map = new HashMap(); - map.put("msg", message.toString()); - map.put("code", ErrorCodeEnum.CLIENT_REQUEST); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 + /** + * 统一处理请求参数校验(实体对象传参) + * + * @param e BindException + * @return ResultResponse + */ + @ExceptionHandler(BindException.class) + public ModelAndView validExceptionHandler(HttpServletRequest request, HttpServletResponse response, BindException e) { + StringBuilder message = new StringBuilder(); + List fieldErrors = e.getBindingResult().getFieldErrors(); + for (FieldError error : fieldErrors) { + message.append(error.getField()).append(error.getDefaultMessage()).append(","); + } + message = new StringBuilder(message.substring(0, message.length() - 1)); + Map map = new HashMap(); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("code", ErrorCodeEnum.CLIENT_REQUEST); + map.put("result", false); + map.put("msg", message.toString()); + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * 统一处理请求参数校验(普通传参) + * + * @param e ConstraintViolationException + * @return ResultResponse + */ + @ExceptionHandler(value = ConstraintViolationException.class) + public ModelAndView handleConstraintViolationException(HttpServletRequest request, HttpServletResponse response, ConstraintViolationException e) { + StringBuilder message = new StringBuilder(); + Set> violations = e.getConstraintViolations(); + for (ConstraintViolation violation : violations) { + Path path = violation.getPropertyPath(); + String[] pathArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(path.toString(), "."); + message.append(pathArr[1]).append(violation.getMessage()).append(","); + } + message = new StringBuilder(message.substring(0, message.length() - 1)); + Map map = new HashMap(); + map.put("msg", message.toString()); + map.put("code", ErrorCodeEnum.CLIENT_REQUEST); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - /** - * shiro权限未授权异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = UnauthorizedException.class) - public ModelAndView handleUnauthorizedException(HttpServletRequest request, HttpServletResponse response,UnauthorizedException e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); - return getModelAndView(request, response, map,e); - } + /** + * shiro权限未授权异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = UnauthorizedException.class) + public ModelAndView handleUnauthorizedException(HttpServletRequest request, HttpServletResponse response, UnauthorizedException e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + return getModelAndView(request, response, map, e); + } - /** - * 登录异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = AuthenticationException.class) - public ModelAndView handleAuthenticationException(HttpServletRequest request, HttpServletResponse response,AuthenticationException e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 登录异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = AuthenticationException.class) + public ModelAndView handleAuthenticationException(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * shiro权限错误 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = AuthorizationException.class) - public ModelAndView handleAuthorizationException(HttpServletRequest request, HttpServletResponse response,AuthorizationException e){ - LOG.debug("AuthorizationException", e); - Map map = new HashMap(); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - map.put("msg", e.getStackTrace()); - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * shiro权限错误 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = AuthorizationException.class) + public ModelAndView handleAuthorizationException(HttpServletRequest request, HttpServletResponse response, AuthorizationException e) { + LOG.debug("AuthorizationException", e); + Map map = new HashMap(); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + map.put("msg", e.getStackTrace()); + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - /** - * session失效异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = ExpiredSessionException.class) - public ModelAndView handleExpiredSessionException(HttpServletRequest request, HttpServletResponse response,ExpiredSessionException e) { - LOG.debug("ExpiredSessionException", e); - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); - return getModelAndView(request, response, map,e); - } + /** + * session失效异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = ExpiredSessionException.class) + public ModelAndView handleExpiredSessionException(HttpServletRequest request, HttpServletResponse response, ExpiredSessionException e) { + LOG.debug("ExpiredSessionException", e); + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + return getModelAndView(request, response, map, e); + } - /** - * 返回异常信息处理 - * @param request - * @param response - * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 - * @param e - * @return - */ - private ModelAndView getModelAndView(HttpServletRequest request, HttpServletResponse response, Map map,Exception e) { - if(ObjectUtil.isNotNull(e)){ - LOG.debug("handleException", e); - StringWriter sw = new StringWriter(); - e.printStackTrace(new PrintWriter(sw, true)); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("errMsg", sw.toString()); - map.put("msg", e.getMessage()); - map.put("exc", e.getClass()); //详细异常信息 - } - //去掉异常信息中的跨站脚本 - map.put("msg",Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "",new Whitelist(), new Document.OutputSettings().prettyPrint(false))); - map.put("result",false); - if (BasicUtil.isAjaxRequest(request)) { - try { - response.setContentType("application/json;charset=UTF-8"); - PrintWriter writer = response.getWriter(); - writer.write(JSONObject.toJSONString(map)); - writer.flush(); - writer.close(); - } catch (IOException ex) { - ex.printStackTrace(); - } - } else { - return new ModelAndView("/error/index", map); - } - return null; - } + @ExceptionHandler(value = DcmsLoginException.class) + public ModelAndView handlDcmsLoginException(HttpServletRequest request, HttpServletResponse response, DcmsLoginException e) { + LOG.debug("DcmsLoginException", e); + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + map.put("msg", e.getBizMsg()); + return getErrorModelAndView(request, response, map, e); + } + /** + * 返回异常信息处理 + * @param request + * @param response + * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 + * @param e + * @return + */ + private ModelAndView getModelAndView(HttpServletRequest request, HttpServletResponse response, Map map, Exception e) { + if (ObjectUtil.isNotNull(e)) { + LOG.debug("handleException", e); + StringWriter sw = new StringWriter(); + e.printStackTrace(new PrintWriter(sw, true)); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("errMsg", sw.toString()); + map.put("msg", e.getMessage()); + map.put("exc", e.getClass()); //详细异常信息 + } + //去掉异常信息中的跨站脚本 + map.put("msg", Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "", new Whitelist(), new Document.OutputSettings().prettyPrint(false))); + map.put("result", false); + if (BasicUtil.isAjaxRequest(request)) { + try { + response.setContentType("application/json;charset=UTF-8"); + PrintWriter writer = response.getWriter(); + writer.write(JSONObject.toJSONString(map)); + writer.flush(); + writer.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } else { + return new ModelAndView("/error/index", map); + } + return null; + } + + /** + * 返回异常信息处理 + * @param request + * @param response + * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 + * @param e + * @return + */ + private ModelAndView getErrorModelAndView(HttpServletRequest request, HttpServletResponse response, Map map, Exception e) { + if (ObjectUtil.isNotNull(e)) { + LOG.debug("handleException", e); + StringWriter sw = new StringWriter(); + map.put("errMsg", sw.toString()); + e.printStackTrace(new PrintWriter(sw, true)); + //出错的类 + map.put("cls", e.getStackTrace()[0] + ""); + //请求地址 + map.put("url", request.getServletPath()); + //详细异常信息 + map.put("exc", e.getClass()); + } + //去掉异常信息中的跨站脚本 + map.put("msg", Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "", new Whitelist(), new Document.OutputSettings().prettyPrint(false))); + map.put("result", false); + if (BasicUtil.isAjaxRequest(request)) { + try { + PrintWriter writer = response.getWriter(); + response.setContentType("application/json;charset=UTF-8"); + + writer.write(JSONObject.toJSONString(map)); + writer.flush(); + writer.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } else { + return new ModelAndView("/error/dcmserror", map); + } + return null; + } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java b/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java index 04c26c8..9cb1d77 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java @@ -28,7 +28,7 @@ public void setHeader(HttpRequestBase base, String token) { base.addHeader("Content-type", DEFAULT_CONTEXTTYPE); if (!StringUtil.isBlank(token)) { - base.addHeader(DEFAULT_CHARSET, token); + base.addHeader(DEFAULT_TOKENKEY, token); } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java b/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java index 729a05b..1aa9358 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java @@ -7,6 +7,8 @@ import net.mingsoft.basic.handler.DefaultHttpHeaderHandler; import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * @ClassName Test @@ -19,7 +21,7 @@ public static void main(String[] args) { String url = "http://111.198.10.15:11409/user/info"; String token = "06d74157-14d6-4536-bf3a-e94d1d061a80"; - checkToken(token, url); + checkToken(url, token); } /** @@ -28,7 +30,7 @@ * @param token 颁发token * @return */ - public static ShiroUser checkToken(String token, String dcmsUrl) { + public static ShiroUser checkToken(String dcmsUrl, String token) { String json = HttpUtils.sendGet(dcmsUrl, new HashMap<>(), token, new DefaultHttpHeaderHandler()); if (!StringUtil.isBlank(json)) { System.out.println(json); @@ -40,4 +42,26 @@ } return null; } + + /** + * 转换为cms用户 + * + * @param user dmcs用户 + * @return + */ + public static String transformCmsUser(ShiroUser user, Map userRoles, List users) { + //是否有管理员 + for (String manager : userRoles.get("manager").split(",")) { + if (user.getRoleTips().contains(manager)) { + return users.get(0); + } + } + //返回普通用户 + for (String manager : userRoles.get("normal").split(",")) { + if (user.getRoleTips().contains(manager)) { + return users.get(1); + } + } + return null; + } } diff --git a/ms-basic/src/main/webapp/error/404.htm b/ms-basic/src/main/webapp/error/404.htm index 9b76382..bfc001f 100644 --- a/ms-basic/src/main/webapp/error/404.htm +++ b/ms-basic/src/main/webapp/error/404.htm @@ -40,4 +40,4 @@ - + \ No newline at end of file diff --git a/ms-base/pom.xml b/ms-base/pom.xml index a5dcef6..af71af1 100644 --- a/ms-base/pom.xml +++ b/ms-base/pom.xml @@ -363,26 +363,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-base diff --git a/ms-basic/ms-basic.iml b/ms-basic/ms-basic.iml index 935ebc4..5a2e74f 100644 --- a/ms-basic/ms-basic.iml +++ b/ms-basic/ms-basic.iml @@ -11,8 +11,8 @@ - - + + diff --git a/ms-basic/pom.xml b/ms-basic/pom.xml index e66ec4f..90ba669 100644 --- a/ms-basic/pom.xml +++ b/ms-basic/pom.xml @@ -88,26 +88,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-basic diff --git a/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java b/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java index 540c752..30a601c 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java @@ -1,16 +1,16 @@ /** * The MIT License (MIT) * Copyright (c) 2020 铭软科技(mingsoft.net) - + *

* Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: - + *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - + *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR @@ -30,20 +30,25 @@ import io.swagger.annotations.ApiOperation; import net.mingsoft.base.entity.ResultData; import net.mingsoft.basic.action.BaseAction; +import net.mingsoft.basic.bean.ShiroUser; import net.mingsoft.basic.biz.IAppBiz; import net.mingsoft.basic.biz.IManagerBiz; import net.mingsoft.basic.biz.IRoleBiz; import net.mingsoft.basic.constant.Const; +import net.mingsoft.basic.constant.ErrorCodeEnum; import net.mingsoft.basic.constant.e.SessionConstEnum; import net.mingsoft.basic.entity.AppEntity; import net.mingsoft.basic.entity.ManagerEntity; import net.mingsoft.basic.entity.ManagerSessionEntity; import net.mingsoft.basic.entity.RoleEntity; +import net.mingsoft.basic.exception.DcmsLoginException; import net.mingsoft.basic.util.BasicUtil; +import net.mingsoft.basic.util.DcmsUtils; import net.mingsoft.basic.util.StringUtil; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.UsernamePasswordToken; +import org.apache.shiro.session.ExpiredSessionException; import org.apache.shiro.subject.Subject; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -56,14 +61,13 @@ import javax.servlet.http.HttpServletResponse; import java.io.File; import java.util.List; +import java.util.Map; /** - * - * @ClassName: LoginAction + * @ClassName: LoginAction * @Description:TODO(登录的基础应用层) * @author: 铭飞开发团队 - * @date: 2015年1月27日 下午3:21:47 - * + * @date: 2015年1月27日 下午3:21:47 * @Copyright: 2018 www.mingsoft.net Inc. All rights reserved. */ @Api("登录的基础应用层接口") @@ -71,146 +75,228 @@ @RequestMapping("/${ms.manager.path}") public class LoginAction extends BaseAction { - @Value("${ms.manager.path}") - private String managerPath; - /** - * 管理员业务层 - */ - @Autowired - private IManagerBiz managerBiz; + @Value("${ms.manager.path}") + private String managerPath; + @Value("${ms.manager.dcmsHost}") + private String dcmsHost; + @Value("${ms.manager.userInfoPath}") + private String userInfoPath; + @Value("#{${ms.manager.userRoles}}") + private Map userRoles; + @Value("#{'${ms.manager.users}'.split(',')}") + private List users; + /** + * 管理员业务层 + */ + @Autowired + private IManagerBiz managerBiz; - /** - * 角色业务request层 - */ - @Autowired - private IRoleBiz roleBiz; + /** + * 角色业务request层 + */ + @Autowired + private IRoleBiz roleBiz; - /** - * 站点业务层 - */ - @Autowired - private IAppBiz appBiz; + /** + * 站点业务层 + */ + @Autowired + private IAppBiz appBiz; + /** + * 加载管理员登录界面 + * + * @param request 请求对象 + * @return 管理员登录界面地址 + */ + @ApiOperation(value = "加载管理员登录界面") + @SuppressWarnings("resource") + @GetMapping("/dcmsToken") + public String dcmsToken(HttpServletRequest request) { + if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION) != null) { + return "redirect:" + managerPath + "/index.do"; + } + //请求dcms数据token + ShiroUser shiroUser = DcmsUtils.checkToken(dcmsHost + userInfoPath, request.getParameter("token")); + if (shiroUser == null) { + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无效token"); + } + String userName = DcmsUtils.transformCmsUser(shiroUser, userRoles, users); + if (userName == null) { + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无效token"); + } - /** - * 加载管理员登录界面 - * - * @param request - * 请求对象 - * @return 管理员登录界面地址 - */ - @ApiOperation(value = "加载管理员登录界面") - @SuppressWarnings("resource") - @GetMapping("/login") - public String login(HttpServletRequest request) { - if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION)!=null) { - return "redirect:"+managerPath+"/index.do"; - } - // 根据请求地址来显示标题 - AppEntity app = BasicUtil.getApp(); - // 判断应用实体是否存在 - if (app != null) { - // 检测应用是否有自定义界面b - if (!StringUtil.isBlank(app.getAppLoginPage())) { - LOG.debug("跳转自定义登录界面"); - return "redirect:" + app.getAppLoginPage(); - } + AppEntity app = BasicUtil.getApp(); + // 判断应用实体是否存在 + if (app != null) { + // 检测应用是否有自定义界面b + if (!StringUtil.isBlank(app.getAppLoginPage())) { + LOG.debug("跳转自定义登录界面"); + return "redirect:" + app.getAppLoginPage(); + } - } else { - File file = new File(BasicUtil.getRealPath( "WEB-INF/ms.install")); - //存在安装文件 - if (file.exists()) { - String defaultId = FileReader.create(new File(BasicUtil.getRealPath( "WEB-INF/ms.install"))).readString(); - if (!StringUtils.isEmpty(defaultId)) { - app = (AppEntity) appBiz.getEntity(Integer.parseInt(defaultId)); - app.setAppUrl(this.getUrl(request)); - appBiz.updateEntity(app); - FileWriter.create(new File(BasicUtil.getRealPath( "WEB-INF/ms.install.bak"))).write(defaultId); - file.delete(); - } - } + } + AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); + ManagerEntity newManager = new ManagerEntity(); + newManager.setManagerName(userName); + ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + // 创建管理员session对象 + ManagerSessionEntity managerSession = new ManagerSessionEntity(); + AppEntity website = new AppEntity(); + // 获取管理员所在的角色 + RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); + website = (AppEntity) appBiz.getEntity(role.getAppId()); + // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 + if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId() == BasicUtil.getAppId())) { + if (website == null) { + website = BasicUtil.getApp(); + } + List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); + managerSession.setStyle(website.getAppStyle()); + managerSession.setBasicId(website.getAppId()); + managerSession.setManagerParentID(role.getRoleManagerId()); + managerSession.setManagerChildIDs(childManagerList); + // 压入管理员seesion + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } else { + if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { + LOG.debug("roleId: " + _manager.getManagerRoleID()); + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无授权"); + } else { + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } + } + BeanUtils.copyProperties(_manager, managerSession); + Subject subject = SecurityUtils.getSubject(); + UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(), managerSession.getManagerPassword()); + subject.login(upt); + return "redirect:" + managerPath + "/index.do"; + } - } - request.setAttribute("app", app); - return "/login"; - } + @GetMapping("/404") + public String error(HttpServletRequest request) { + return "/404"; + } - /** - * 验证登录 - * - * @param manager - * 管理员实体 - * @param request - * 请求 - * @param response - * 响应 - */ - @ApiOperation(value = "验证登录") - @ApiImplicitParams({ - @ApiImplicitParam(name = "managerName", value = "帐号", required = true,paramType="query"), - @ApiImplicitParam(name = "managerPassword", value = "密码", required = true,paramType="query"), - }) - @PostMapping(value="/checkLogin") - @ResponseBody - public ResultData checkLogin(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletRequest request, - HttpServletResponse response) { - AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); // 根据url地址获取站点信息,主要是区分管理员对那些网站有权限 - if (urlWebsite == null) { - return ResultData.build().error( this.getResString("err.not.exist",this.getResString("app"),"!请尝试去文件 WEB-INF/ms.install.bak 后缀bak")); - } - //验证码 - if (!(checkRandCode())) { - return ResultData.build().error(getResString("err.error", new String[] { getResString("rand.code") })); - } + /** + * 加载管理员登录界面 + * + * @param request 请求对象 + * @return 管理员登录界面地址 + */ + @ApiOperation(value = "加载管理员登录界面") + @SuppressWarnings("resource") + @GetMapping("/login") + public String login(HttpServletRequest request) { + if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION) != null) { + return "redirect:" + managerPath + "/index.do"; + } + // 根据请求地址来显示标题 + AppEntity app = BasicUtil.getApp(); + // 判断应用实体是否存在 + if (app != null) { + // 检测应用是否有自定义界面b + if (!StringUtil.isBlank(app.getAppLoginPage())) { + LOG.debug("跳转自定义登录界面"); + return "redirect:" + app.getAppLoginPage(); + } - // 根据账号获取当前管理员信息 - ManagerEntity newManager = new ManagerEntity(); - newManager.setManagerName(manager.getManagerName()); - ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + } else { + File file = new File(BasicUtil.getRealPath("WEB-INF/ms.install")); + //存在安装文件 + if (file.exists()) { + String defaultId = FileReader.create(new File(BasicUtil.getRealPath("WEB-INF/ms.install"))).readString(); + if (!StringUtils.isEmpty(defaultId)) { + app = (AppEntity) appBiz.getEntity(Integer.parseInt(defaultId)); + app.setAppUrl(this.getUrl(request)); + appBiz.updateEntity(app); + FileWriter.create(new File(BasicUtil.getRealPath("WEB-INF/ms.install.bak"))).write(defaultId); + file.delete(); + } + } - if (_manager == null || StringUtils.isEmpty(manager.getManagerName())) { - // 系统不存在此用户 - return ResultData.build().error( this.getResString("err.nameEmpty")); - } else { - // 判断当前用户输入的密码是否正确 - if (SecureUtil.md5(manager.getManagerPassword()).equals(_manager.getManagerPassword())) { - // 创建管理员session对象 - ManagerSessionEntity managerSession = new ManagerSessionEntity(); - AppEntity website = new AppEntity(); - // 获取管理员所在的角色 - RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); - website = (AppEntity) appBiz.getEntity(role.getAppId()); - // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 - if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId()==BasicUtil.getAppId())) { - if(website==null){ - website = BasicUtil.getApp(); - } - List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); - managerSession.setBasicId(website.getAppId()); - managerSession.setManagerParentID(role.getRoleManagerId()); - managerSession.setManagerChildIDs(childManagerList); - managerSession.setStyle(website.getAppStyle()); - // 压入管理员seesion - BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); - } else { - if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { - LOG.debug("roleId: "+_manager.getManagerRoleID()); - return ResultData.build().error( this.getResString("err.not.exist",this.getResString("manager"))); - } else { - BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); - } - } - BeanUtils.copyProperties(_manager, managerSession); + } + request.setAttribute("app", app); + return "/login"; + } - Subject subject = SecurityUtils.getSubject(); - UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(),managerSession.getManagerPassword()); - subject.login(upt); - return ResultData.build().success(); - } else { - // 密码错误 - return ResultData.build().error( this.getResString("err.password")); - } - } - } + /** + * 验证登录 + * + * @param manager 管理员实体 + * @param request 请求 + * @param response 响应 + */ + @ApiOperation(value = "验证登录") + @ApiImplicitParams({ + @ApiImplicitParam(name = "managerName", value = "帐号", required = true, paramType = "query"), + @ApiImplicitParam(name = "managerPassword", value = "密码", required = true, paramType = "query"), + }) + @PostMapping(value = "/checkLogin") + @ResponseBody + public ResultData checkLogin(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletRequest request, + HttpServletResponse response) { + // 根据url地址获取站点信息,主要是区分管理员对那些网站有权限 + AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); + if (urlWebsite == null) { + return ResultData.build().error(this.getResString("err.not.exist", this.getResString("app"), "!请尝试去文件 WEB-INF/ms.install.bak 后缀bak")); + } + //验证码 + if (!(checkRandCode())) { + return ResultData.build().error(getResString("err.error", new String[]{getResString("rand.code")})); + } + + // 根据账号获取当前管理员信息 + ManagerEntity newManager = new ManagerEntity(); + newManager.setManagerName(manager.getManagerName()); + ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + + if (_manager == null || StringUtils.isEmpty(manager.getManagerName())) { + // 系统不存在此用户 + return ResultData.build().error(this.getResString("err.nameEmpty")); + } else { + // 判断当前用户输入的密码是否正确 + if (SecureUtil.md5(manager.getManagerPassword()).equals(_manager.getManagerPassword())) { + // 创建管理员session对象 + ManagerSessionEntity managerSession = new ManagerSessionEntity(); + AppEntity website = new AppEntity(); + // 获取管理员所在的角色 + RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); + website = (AppEntity) appBiz.getEntity(role.getAppId()); + // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 + if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId() == BasicUtil.getAppId())) { + if (website == null) { + website = BasicUtil.getApp(); + } + List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); + managerSession.setBasicId(website.getAppId()); + managerSession.setManagerParentID(role.getRoleManagerId()); + managerSession.setManagerChildIDs(childManagerList); + managerSession.setStyle(website.getAppStyle()); + // 压入管理员seesion + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } else { + if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { + LOG.debug("roleId: " + _manager.getManagerRoleID()); + return ResultData.build().error(this.getResString("err.not.exist", this.getResString("manager"))); + } else { + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } + } + BeanUtils.copyProperties(_manager, managerSession); + + Subject subject = SecurityUtils.getSubject(); + UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(), managerSession.getManagerPassword()); + subject.login(upt); + return ResultData.build().success(); + } else { + // 密码错误 + return ResultData.build().error(this.getResString("err.password")); + } + } + } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java b/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java index 726a50c..f0e6377 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java @@ -1,16 +1,16 @@ /** * The MIT License (MIT) * Copyright (c) 2020 铭软科技(mingsoft.net) - + *

* Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: - + *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - + *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR @@ -61,214 +61,263 @@ */ @ControllerAdvice public class GlobalExceptionResolver extends DefaultHandlerExceptionResolver { - - protected final Logger LOG = LoggerFactory.getLogger(this.getClass()); - /** - * 全局异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = Exception.class) - public ModelAndView handleException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.SERVER_ERROR); - return getModelAndView(request, response, map,e); - } + protected final Logger LOG = LoggerFactory.getLogger(this.getClass()); - /** - * 全局异常 未找到类404 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = NoHandlerFoundException.class) - public ModelAndView handleNoHandlerFoundException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 全局异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = Exception.class) + public ModelAndView handleException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.SERVER_ERROR); + return getModelAndView(request, response, map, e); + } - /** - * 请求参数异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = MissingServletRequestParameterException.class) - public ModelAndView handleMissingServletRequestParameterException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 全局异常 未找到类404 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = NoHandlerFoundException.class) + public ModelAndView handleNoHandlerFoundException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 请求方法类型错误 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) - public ModelAndView handleHttpRequestMethodNotSupportedException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 请求参数异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = MissingServletRequestParameterException.class) + public ModelAndView handleMissingServletRequestParameterException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 统一处理请求参数校验(实体对象传参) - * - * @param e BindException - * @return ResultResponse - */ - @ExceptionHandler(BindException.class) - public ModelAndView validExceptionHandler(HttpServletRequest request, HttpServletResponse response,BindException e) { - StringBuilder message = new StringBuilder(); - List fieldErrors = e.getBindingResult().getFieldErrors(); - for (FieldError error : fieldErrors) { - message.append(error.getField()).append(error.getDefaultMessage()).append(","); - } - message = new StringBuilder(message.substring(0, message.length() - 1)); - Map map = new HashMap(); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("code", ErrorCodeEnum.CLIENT_REQUEST); - map.put("result",false); - map.put("msg", message.toString()); - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * 请求方法类型错误 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) + public ModelAndView handleHttpRequestMethodNotSupportedException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 统一处理请求参数校验(普通传参) - * - * @param e ConstraintViolationException - * @return ResultResponse - */ - @ExceptionHandler(value = ConstraintViolationException.class) - public ModelAndView handleConstraintViolationException(HttpServletRequest request, HttpServletResponse response,ConstraintViolationException e) { - StringBuilder message = new StringBuilder(); - Set> violations = e.getConstraintViolations(); - for (ConstraintViolation violation : violations) { - Path path = violation.getPropertyPath(); - String[] pathArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(path.toString(), "."); - message.append(pathArr[1]).append(violation.getMessage()).append(","); - } - message = new StringBuilder(message.substring(0, message.length() - 1)); - Map map = new HashMap(); - map.put("msg", message.toString()); - map.put("code", ErrorCodeEnum.CLIENT_REQUEST); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 + /** + * 统一处理请求参数校验(实体对象传参) + * + * @param e BindException + * @return ResultResponse + */ + @ExceptionHandler(BindException.class) + public ModelAndView validExceptionHandler(HttpServletRequest request, HttpServletResponse response, BindException e) { + StringBuilder message = new StringBuilder(); + List fieldErrors = e.getBindingResult().getFieldErrors(); + for (FieldError error : fieldErrors) { + message.append(error.getField()).append(error.getDefaultMessage()).append(","); + } + message = new StringBuilder(message.substring(0, message.length() - 1)); + Map map = new HashMap(); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("code", ErrorCodeEnum.CLIENT_REQUEST); + map.put("result", false); + map.put("msg", message.toString()); + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * 统一处理请求参数校验(普通传参) + * + * @param e ConstraintViolationException + * @return ResultResponse + */ + @ExceptionHandler(value = ConstraintViolationException.class) + public ModelAndView handleConstraintViolationException(HttpServletRequest request, HttpServletResponse response, ConstraintViolationException e) { + StringBuilder message = new StringBuilder(); + Set> violations = e.getConstraintViolations(); + for (ConstraintViolation violation : violations) { + Path path = violation.getPropertyPath(); + String[] pathArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(path.toString(), "."); + message.append(pathArr[1]).append(violation.getMessage()).append(","); + } + message = new StringBuilder(message.substring(0, message.length() - 1)); + Map map = new HashMap(); + map.put("msg", message.toString()); + map.put("code", ErrorCodeEnum.CLIENT_REQUEST); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - /** - * shiro权限未授权异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = UnauthorizedException.class) - public ModelAndView handleUnauthorizedException(HttpServletRequest request, HttpServletResponse response,UnauthorizedException e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); - return getModelAndView(request, response, map,e); - } + /** + * shiro权限未授权异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = UnauthorizedException.class) + public ModelAndView handleUnauthorizedException(HttpServletRequest request, HttpServletResponse response, UnauthorizedException e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + return getModelAndView(request, response, map, e); + } - /** - * 登录异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = AuthenticationException.class) - public ModelAndView handleAuthenticationException(HttpServletRequest request, HttpServletResponse response,AuthenticationException e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 登录异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = AuthenticationException.class) + public ModelAndView handleAuthenticationException(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * shiro权限错误 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = AuthorizationException.class) - public ModelAndView handleAuthorizationException(HttpServletRequest request, HttpServletResponse response,AuthorizationException e){ - LOG.debug("AuthorizationException", e); - Map map = new HashMap(); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - map.put("msg", e.getStackTrace()); - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * shiro权限错误 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = AuthorizationException.class) + public ModelAndView handleAuthorizationException(HttpServletRequest request, HttpServletResponse response, AuthorizationException e) { + LOG.debug("AuthorizationException", e); + Map map = new HashMap(); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + map.put("msg", e.getStackTrace()); + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - /** - * session失效异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = ExpiredSessionException.class) - public ModelAndView handleExpiredSessionException(HttpServletRequest request, HttpServletResponse response,ExpiredSessionException e) { - LOG.debug("ExpiredSessionException", e); - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); - return getModelAndView(request, response, map,e); - } + /** + * session失效异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = ExpiredSessionException.class) + public ModelAndView handleExpiredSessionException(HttpServletRequest request, HttpServletResponse response, ExpiredSessionException e) { + LOG.debug("ExpiredSessionException", e); + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + return getModelAndView(request, response, map, e); + } - /** - * 返回异常信息处理 - * @param request - * @param response - * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 - * @param e - * @return - */ - private ModelAndView getModelAndView(HttpServletRequest request, HttpServletResponse response, Map map,Exception e) { - if(ObjectUtil.isNotNull(e)){ - LOG.debug("handleException", e); - StringWriter sw = new StringWriter(); - e.printStackTrace(new PrintWriter(sw, true)); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("errMsg", sw.toString()); - map.put("msg", e.getMessage()); - map.put("exc", e.getClass()); //详细异常信息 - } - //去掉异常信息中的跨站脚本 - map.put("msg",Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "",new Whitelist(), new Document.OutputSettings().prettyPrint(false))); - map.put("result",false); - if (BasicUtil.isAjaxRequest(request)) { - try { - response.setContentType("application/json;charset=UTF-8"); - PrintWriter writer = response.getWriter(); - writer.write(JSONObject.toJSONString(map)); - writer.flush(); - writer.close(); - } catch (IOException ex) { - ex.printStackTrace(); - } - } else { - return new ModelAndView("/error/index", map); - } - return null; - } + @ExceptionHandler(value = DcmsLoginException.class) + public ModelAndView handlDcmsLoginException(HttpServletRequest request, HttpServletResponse response, DcmsLoginException e) { + LOG.debug("DcmsLoginException", e); + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + map.put("msg", e.getBizMsg()); + return getErrorModelAndView(request, response, map, e); + } + /** + * 返回异常信息处理 + * @param request + * @param response + * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 + * @param e + * @return + */ + private ModelAndView getModelAndView(HttpServletRequest request, HttpServletResponse response, Map map, Exception e) { + if (ObjectUtil.isNotNull(e)) { + LOG.debug("handleException", e); + StringWriter sw = new StringWriter(); + e.printStackTrace(new PrintWriter(sw, true)); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("errMsg", sw.toString()); + map.put("msg", e.getMessage()); + map.put("exc", e.getClass()); //详细异常信息 + } + //去掉异常信息中的跨站脚本 + map.put("msg", Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "", new Whitelist(), new Document.OutputSettings().prettyPrint(false))); + map.put("result", false); + if (BasicUtil.isAjaxRequest(request)) { + try { + response.setContentType("application/json;charset=UTF-8"); + PrintWriter writer = response.getWriter(); + writer.write(JSONObject.toJSONString(map)); + writer.flush(); + writer.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } else { + return new ModelAndView("/error/index", map); + } + return null; + } + + /** + * 返回异常信息处理 + * @param request + * @param response + * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 + * @param e + * @return + */ + private ModelAndView getErrorModelAndView(HttpServletRequest request, HttpServletResponse response, Map map, Exception e) { + if (ObjectUtil.isNotNull(e)) { + LOG.debug("handleException", e); + StringWriter sw = new StringWriter(); + map.put("errMsg", sw.toString()); + e.printStackTrace(new PrintWriter(sw, true)); + //出错的类 + map.put("cls", e.getStackTrace()[0] + ""); + //请求地址 + map.put("url", request.getServletPath()); + //详细异常信息 + map.put("exc", e.getClass()); + } + //去掉异常信息中的跨站脚本 + map.put("msg", Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "", new Whitelist(), new Document.OutputSettings().prettyPrint(false))); + map.put("result", false); + if (BasicUtil.isAjaxRequest(request)) { + try { + PrintWriter writer = response.getWriter(); + response.setContentType("application/json;charset=UTF-8"); + + writer.write(JSONObject.toJSONString(map)); + writer.flush(); + writer.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } else { + return new ModelAndView("/error/dcmserror", map); + } + return null; + } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java b/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java index 04c26c8..9cb1d77 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java @@ -28,7 +28,7 @@ public void setHeader(HttpRequestBase base, String token) { base.addHeader("Content-type", DEFAULT_CONTEXTTYPE); if (!StringUtil.isBlank(token)) { - base.addHeader(DEFAULT_CHARSET, token); + base.addHeader(DEFAULT_TOKENKEY, token); } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java b/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java index 729a05b..1aa9358 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java @@ -7,6 +7,8 @@ import net.mingsoft.basic.handler.DefaultHttpHeaderHandler; import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * @ClassName Test @@ -19,7 +21,7 @@ public static void main(String[] args) { String url = "http://111.198.10.15:11409/user/info"; String token = "06d74157-14d6-4536-bf3a-e94d1d061a80"; - checkToken(token, url); + checkToken(url, token); } /** @@ -28,7 +30,7 @@ * @param token 颁发token * @return */ - public static ShiroUser checkToken(String token, String dcmsUrl) { + public static ShiroUser checkToken(String dcmsUrl, String token) { String json = HttpUtils.sendGet(dcmsUrl, new HashMap<>(), token, new DefaultHttpHeaderHandler()); if (!StringUtil.isBlank(json)) { System.out.println(json); @@ -40,4 +42,26 @@ } return null; } + + /** + * 转换为cms用户 + * + * @param user dmcs用户 + * @return + */ + public static String transformCmsUser(ShiroUser user, Map userRoles, List users) { + //是否有管理员 + for (String manager : userRoles.get("manager").split(",")) { + if (user.getRoleTips().contains(manager)) { + return users.get(0); + } + } + //返回普通用户 + for (String manager : userRoles.get("normal").split(",")) { + if (user.getRoleTips().contains(manager)) { + return users.get(1); + } + } + return null; + } } diff --git a/ms-basic/src/main/webapp/error/404.htm b/ms-basic/src/main/webapp/error/404.htm index 9b76382..bfc001f 100644 --- a/ms-basic/src/main/webapp/error/404.htm +++ b/ms-basic/src/main/webapp/error/404.htm @@ -40,4 +40,4 @@ - + \ No newline at end of file diff --git a/ms-mcms/pom.xml b/ms-mcms/pom.xml index 47d4474..f238b61 100644 --- a/ms-mcms/pom.xml +++ b/ms-mcms/pom.xml @@ -45,6 +45,7 @@ ms-mdiy 1.0.23-SNAPSHOT + net.mingsoft diff --git a/ms-base/pom.xml b/ms-base/pom.xml index a5dcef6..af71af1 100644 --- a/ms-base/pom.xml +++ b/ms-base/pom.xml @@ -363,26 +363,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-base diff --git a/ms-basic/ms-basic.iml b/ms-basic/ms-basic.iml index 935ebc4..5a2e74f 100644 --- a/ms-basic/ms-basic.iml +++ b/ms-basic/ms-basic.iml @@ -11,8 +11,8 @@ - - + + diff --git a/ms-basic/pom.xml b/ms-basic/pom.xml index e66ec4f..90ba669 100644 --- a/ms-basic/pom.xml +++ b/ms-basic/pom.xml @@ -88,26 +88,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-basic diff --git a/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java b/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java index 540c752..30a601c 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java @@ -1,16 +1,16 @@ /** * The MIT License (MIT) * Copyright (c) 2020 铭软科技(mingsoft.net) - + *

* Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: - + *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - + *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR @@ -30,20 +30,25 @@ import io.swagger.annotations.ApiOperation; import net.mingsoft.base.entity.ResultData; import net.mingsoft.basic.action.BaseAction; +import net.mingsoft.basic.bean.ShiroUser; import net.mingsoft.basic.biz.IAppBiz; import net.mingsoft.basic.biz.IManagerBiz; import net.mingsoft.basic.biz.IRoleBiz; import net.mingsoft.basic.constant.Const; +import net.mingsoft.basic.constant.ErrorCodeEnum; import net.mingsoft.basic.constant.e.SessionConstEnum; import net.mingsoft.basic.entity.AppEntity; import net.mingsoft.basic.entity.ManagerEntity; import net.mingsoft.basic.entity.ManagerSessionEntity; import net.mingsoft.basic.entity.RoleEntity; +import net.mingsoft.basic.exception.DcmsLoginException; import net.mingsoft.basic.util.BasicUtil; +import net.mingsoft.basic.util.DcmsUtils; import net.mingsoft.basic.util.StringUtil; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.UsernamePasswordToken; +import org.apache.shiro.session.ExpiredSessionException; import org.apache.shiro.subject.Subject; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -56,14 +61,13 @@ import javax.servlet.http.HttpServletResponse; import java.io.File; import java.util.List; +import java.util.Map; /** - * - * @ClassName: LoginAction + * @ClassName: LoginAction * @Description:TODO(登录的基础应用层) * @author: 铭飞开发团队 - * @date: 2015年1月27日 下午3:21:47 - * + * @date: 2015年1月27日 下午3:21:47 * @Copyright: 2018 www.mingsoft.net Inc. All rights reserved. */ @Api("登录的基础应用层接口") @@ -71,146 +75,228 @@ @RequestMapping("/${ms.manager.path}") public class LoginAction extends BaseAction { - @Value("${ms.manager.path}") - private String managerPath; - /** - * 管理员业务层 - */ - @Autowired - private IManagerBiz managerBiz; + @Value("${ms.manager.path}") + private String managerPath; + @Value("${ms.manager.dcmsHost}") + private String dcmsHost; + @Value("${ms.manager.userInfoPath}") + private String userInfoPath; + @Value("#{${ms.manager.userRoles}}") + private Map userRoles; + @Value("#{'${ms.manager.users}'.split(',')}") + private List users; + /** + * 管理员业务层 + */ + @Autowired + private IManagerBiz managerBiz; - /** - * 角色业务request层 - */ - @Autowired - private IRoleBiz roleBiz; + /** + * 角色业务request层 + */ + @Autowired + private IRoleBiz roleBiz; - /** - * 站点业务层 - */ - @Autowired - private IAppBiz appBiz; + /** + * 站点业务层 + */ + @Autowired + private IAppBiz appBiz; + /** + * 加载管理员登录界面 + * + * @param request 请求对象 + * @return 管理员登录界面地址 + */ + @ApiOperation(value = "加载管理员登录界面") + @SuppressWarnings("resource") + @GetMapping("/dcmsToken") + public String dcmsToken(HttpServletRequest request) { + if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION) != null) { + return "redirect:" + managerPath + "/index.do"; + } + //请求dcms数据token + ShiroUser shiroUser = DcmsUtils.checkToken(dcmsHost + userInfoPath, request.getParameter("token")); + if (shiroUser == null) { + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无效token"); + } + String userName = DcmsUtils.transformCmsUser(shiroUser, userRoles, users); + if (userName == null) { + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无效token"); + } - /** - * 加载管理员登录界面 - * - * @param request - * 请求对象 - * @return 管理员登录界面地址 - */ - @ApiOperation(value = "加载管理员登录界面") - @SuppressWarnings("resource") - @GetMapping("/login") - public String login(HttpServletRequest request) { - if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION)!=null) { - return "redirect:"+managerPath+"/index.do"; - } - // 根据请求地址来显示标题 - AppEntity app = BasicUtil.getApp(); - // 判断应用实体是否存在 - if (app != null) { - // 检测应用是否有自定义界面b - if (!StringUtil.isBlank(app.getAppLoginPage())) { - LOG.debug("跳转自定义登录界面"); - return "redirect:" + app.getAppLoginPage(); - } + AppEntity app = BasicUtil.getApp(); + // 判断应用实体是否存在 + if (app != null) { + // 检测应用是否有自定义界面b + if (!StringUtil.isBlank(app.getAppLoginPage())) { + LOG.debug("跳转自定义登录界面"); + return "redirect:" + app.getAppLoginPage(); + } - } else { - File file = new File(BasicUtil.getRealPath( "WEB-INF/ms.install")); - //存在安装文件 - if (file.exists()) { - String defaultId = FileReader.create(new File(BasicUtil.getRealPath( "WEB-INF/ms.install"))).readString(); - if (!StringUtils.isEmpty(defaultId)) { - app = (AppEntity) appBiz.getEntity(Integer.parseInt(defaultId)); - app.setAppUrl(this.getUrl(request)); - appBiz.updateEntity(app); - FileWriter.create(new File(BasicUtil.getRealPath( "WEB-INF/ms.install.bak"))).write(defaultId); - file.delete(); - } - } + } + AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); + ManagerEntity newManager = new ManagerEntity(); + newManager.setManagerName(userName); + ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + // 创建管理员session对象 + ManagerSessionEntity managerSession = new ManagerSessionEntity(); + AppEntity website = new AppEntity(); + // 获取管理员所在的角色 + RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); + website = (AppEntity) appBiz.getEntity(role.getAppId()); + // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 + if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId() == BasicUtil.getAppId())) { + if (website == null) { + website = BasicUtil.getApp(); + } + List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); + managerSession.setStyle(website.getAppStyle()); + managerSession.setBasicId(website.getAppId()); + managerSession.setManagerParentID(role.getRoleManagerId()); + managerSession.setManagerChildIDs(childManagerList); + // 压入管理员seesion + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } else { + if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { + LOG.debug("roleId: " + _manager.getManagerRoleID()); + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无授权"); + } else { + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } + } + BeanUtils.copyProperties(_manager, managerSession); + Subject subject = SecurityUtils.getSubject(); + UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(), managerSession.getManagerPassword()); + subject.login(upt); + return "redirect:" + managerPath + "/index.do"; + } - } - request.setAttribute("app", app); - return "/login"; - } + @GetMapping("/404") + public String error(HttpServletRequest request) { + return "/404"; + } - /** - * 验证登录 - * - * @param manager - * 管理员实体 - * @param request - * 请求 - * @param response - * 响应 - */ - @ApiOperation(value = "验证登录") - @ApiImplicitParams({ - @ApiImplicitParam(name = "managerName", value = "帐号", required = true,paramType="query"), - @ApiImplicitParam(name = "managerPassword", value = "密码", required = true,paramType="query"), - }) - @PostMapping(value="/checkLogin") - @ResponseBody - public ResultData checkLogin(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletRequest request, - HttpServletResponse response) { - AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); // 根据url地址获取站点信息,主要是区分管理员对那些网站有权限 - if (urlWebsite == null) { - return ResultData.build().error( this.getResString("err.not.exist",this.getResString("app"),"!请尝试去文件 WEB-INF/ms.install.bak 后缀bak")); - } - //验证码 - if (!(checkRandCode())) { - return ResultData.build().error(getResString("err.error", new String[] { getResString("rand.code") })); - } + /** + * 加载管理员登录界面 + * + * @param request 请求对象 + * @return 管理员登录界面地址 + */ + @ApiOperation(value = "加载管理员登录界面") + @SuppressWarnings("resource") + @GetMapping("/login") + public String login(HttpServletRequest request) { + if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION) != null) { + return "redirect:" + managerPath + "/index.do"; + } + // 根据请求地址来显示标题 + AppEntity app = BasicUtil.getApp(); + // 判断应用实体是否存在 + if (app != null) { + // 检测应用是否有自定义界面b + if (!StringUtil.isBlank(app.getAppLoginPage())) { + LOG.debug("跳转自定义登录界面"); + return "redirect:" + app.getAppLoginPage(); + } - // 根据账号获取当前管理员信息 - ManagerEntity newManager = new ManagerEntity(); - newManager.setManagerName(manager.getManagerName()); - ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + } else { + File file = new File(BasicUtil.getRealPath("WEB-INF/ms.install")); + //存在安装文件 + if (file.exists()) { + String defaultId = FileReader.create(new File(BasicUtil.getRealPath("WEB-INF/ms.install"))).readString(); + if (!StringUtils.isEmpty(defaultId)) { + app = (AppEntity) appBiz.getEntity(Integer.parseInt(defaultId)); + app.setAppUrl(this.getUrl(request)); + appBiz.updateEntity(app); + FileWriter.create(new File(BasicUtil.getRealPath("WEB-INF/ms.install.bak"))).write(defaultId); + file.delete(); + } + } - if (_manager == null || StringUtils.isEmpty(manager.getManagerName())) { - // 系统不存在此用户 - return ResultData.build().error( this.getResString("err.nameEmpty")); - } else { - // 判断当前用户输入的密码是否正确 - if (SecureUtil.md5(manager.getManagerPassword()).equals(_manager.getManagerPassword())) { - // 创建管理员session对象 - ManagerSessionEntity managerSession = new ManagerSessionEntity(); - AppEntity website = new AppEntity(); - // 获取管理员所在的角色 - RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); - website = (AppEntity) appBiz.getEntity(role.getAppId()); - // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 - if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId()==BasicUtil.getAppId())) { - if(website==null){ - website = BasicUtil.getApp(); - } - List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); - managerSession.setBasicId(website.getAppId()); - managerSession.setManagerParentID(role.getRoleManagerId()); - managerSession.setManagerChildIDs(childManagerList); - managerSession.setStyle(website.getAppStyle()); - // 压入管理员seesion - BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); - } else { - if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { - LOG.debug("roleId: "+_manager.getManagerRoleID()); - return ResultData.build().error( this.getResString("err.not.exist",this.getResString("manager"))); - } else { - BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); - } - } - BeanUtils.copyProperties(_manager, managerSession); + } + request.setAttribute("app", app); + return "/login"; + } - Subject subject = SecurityUtils.getSubject(); - UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(),managerSession.getManagerPassword()); - subject.login(upt); - return ResultData.build().success(); - } else { - // 密码错误 - return ResultData.build().error( this.getResString("err.password")); - } - } - } + /** + * 验证登录 + * + * @param manager 管理员实体 + * @param request 请求 + * @param response 响应 + */ + @ApiOperation(value = "验证登录") + @ApiImplicitParams({ + @ApiImplicitParam(name = "managerName", value = "帐号", required = true, paramType = "query"), + @ApiImplicitParam(name = "managerPassword", value = "密码", required = true, paramType = "query"), + }) + @PostMapping(value = "/checkLogin") + @ResponseBody + public ResultData checkLogin(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletRequest request, + HttpServletResponse response) { + // 根据url地址获取站点信息,主要是区分管理员对那些网站有权限 + AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); + if (urlWebsite == null) { + return ResultData.build().error(this.getResString("err.not.exist", this.getResString("app"), "!请尝试去文件 WEB-INF/ms.install.bak 后缀bak")); + } + //验证码 + if (!(checkRandCode())) { + return ResultData.build().error(getResString("err.error", new String[]{getResString("rand.code")})); + } + + // 根据账号获取当前管理员信息 + ManagerEntity newManager = new ManagerEntity(); + newManager.setManagerName(manager.getManagerName()); + ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + + if (_manager == null || StringUtils.isEmpty(manager.getManagerName())) { + // 系统不存在此用户 + return ResultData.build().error(this.getResString("err.nameEmpty")); + } else { + // 判断当前用户输入的密码是否正确 + if (SecureUtil.md5(manager.getManagerPassword()).equals(_manager.getManagerPassword())) { + // 创建管理员session对象 + ManagerSessionEntity managerSession = new ManagerSessionEntity(); + AppEntity website = new AppEntity(); + // 获取管理员所在的角色 + RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); + website = (AppEntity) appBiz.getEntity(role.getAppId()); + // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 + if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId() == BasicUtil.getAppId())) { + if (website == null) { + website = BasicUtil.getApp(); + } + List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); + managerSession.setBasicId(website.getAppId()); + managerSession.setManagerParentID(role.getRoleManagerId()); + managerSession.setManagerChildIDs(childManagerList); + managerSession.setStyle(website.getAppStyle()); + // 压入管理员seesion + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } else { + if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { + LOG.debug("roleId: " + _manager.getManagerRoleID()); + return ResultData.build().error(this.getResString("err.not.exist", this.getResString("manager"))); + } else { + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } + } + BeanUtils.copyProperties(_manager, managerSession); + + Subject subject = SecurityUtils.getSubject(); + UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(), managerSession.getManagerPassword()); + subject.login(upt); + return ResultData.build().success(); + } else { + // 密码错误 + return ResultData.build().error(this.getResString("err.password")); + } + } + } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java b/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java index 726a50c..f0e6377 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java @@ -1,16 +1,16 @@ /** * The MIT License (MIT) * Copyright (c) 2020 铭软科技(mingsoft.net) - + *

* Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: - + *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - + *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR @@ -61,214 +61,263 @@ */ @ControllerAdvice public class GlobalExceptionResolver extends DefaultHandlerExceptionResolver { - - protected final Logger LOG = LoggerFactory.getLogger(this.getClass()); - /** - * 全局异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = Exception.class) - public ModelAndView handleException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.SERVER_ERROR); - return getModelAndView(request, response, map,e); - } + protected final Logger LOG = LoggerFactory.getLogger(this.getClass()); - /** - * 全局异常 未找到类404 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = NoHandlerFoundException.class) - public ModelAndView handleNoHandlerFoundException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 全局异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = Exception.class) + public ModelAndView handleException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.SERVER_ERROR); + return getModelAndView(request, response, map, e); + } - /** - * 请求参数异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = MissingServletRequestParameterException.class) - public ModelAndView handleMissingServletRequestParameterException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 全局异常 未找到类404 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = NoHandlerFoundException.class) + public ModelAndView handleNoHandlerFoundException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 请求方法类型错误 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) - public ModelAndView handleHttpRequestMethodNotSupportedException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 请求参数异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = MissingServletRequestParameterException.class) + public ModelAndView handleMissingServletRequestParameterException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 统一处理请求参数校验(实体对象传参) - * - * @param e BindException - * @return ResultResponse - */ - @ExceptionHandler(BindException.class) - public ModelAndView validExceptionHandler(HttpServletRequest request, HttpServletResponse response,BindException e) { - StringBuilder message = new StringBuilder(); - List fieldErrors = e.getBindingResult().getFieldErrors(); - for (FieldError error : fieldErrors) { - message.append(error.getField()).append(error.getDefaultMessage()).append(","); - } - message = new StringBuilder(message.substring(0, message.length() - 1)); - Map map = new HashMap(); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("code", ErrorCodeEnum.CLIENT_REQUEST); - map.put("result",false); - map.put("msg", message.toString()); - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * 请求方法类型错误 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) + public ModelAndView handleHttpRequestMethodNotSupportedException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 统一处理请求参数校验(普通传参) - * - * @param e ConstraintViolationException - * @return ResultResponse - */ - @ExceptionHandler(value = ConstraintViolationException.class) - public ModelAndView handleConstraintViolationException(HttpServletRequest request, HttpServletResponse response,ConstraintViolationException e) { - StringBuilder message = new StringBuilder(); - Set> violations = e.getConstraintViolations(); - for (ConstraintViolation violation : violations) { - Path path = violation.getPropertyPath(); - String[] pathArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(path.toString(), "."); - message.append(pathArr[1]).append(violation.getMessage()).append(","); - } - message = new StringBuilder(message.substring(0, message.length() - 1)); - Map map = new HashMap(); - map.put("msg", message.toString()); - map.put("code", ErrorCodeEnum.CLIENT_REQUEST); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 + /** + * 统一处理请求参数校验(实体对象传参) + * + * @param e BindException + * @return ResultResponse + */ + @ExceptionHandler(BindException.class) + public ModelAndView validExceptionHandler(HttpServletRequest request, HttpServletResponse response, BindException e) { + StringBuilder message = new StringBuilder(); + List fieldErrors = e.getBindingResult().getFieldErrors(); + for (FieldError error : fieldErrors) { + message.append(error.getField()).append(error.getDefaultMessage()).append(","); + } + message = new StringBuilder(message.substring(0, message.length() - 1)); + Map map = new HashMap(); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("code", ErrorCodeEnum.CLIENT_REQUEST); + map.put("result", false); + map.put("msg", message.toString()); + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * 统一处理请求参数校验(普通传参) + * + * @param e ConstraintViolationException + * @return ResultResponse + */ + @ExceptionHandler(value = ConstraintViolationException.class) + public ModelAndView handleConstraintViolationException(HttpServletRequest request, HttpServletResponse response, ConstraintViolationException e) { + StringBuilder message = new StringBuilder(); + Set> violations = e.getConstraintViolations(); + for (ConstraintViolation violation : violations) { + Path path = violation.getPropertyPath(); + String[] pathArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(path.toString(), "."); + message.append(pathArr[1]).append(violation.getMessage()).append(","); + } + message = new StringBuilder(message.substring(0, message.length() - 1)); + Map map = new HashMap(); + map.put("msg", message.toString()); + map.put("code", ErrorCodeEnum.CLIENT_REQUEST); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - /** - * shiro权限未授权异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = UnauthorizedException.class) - public ModelAndView handleUnauthorizedException(HttpServletRequest request, HttpServletResponse response,UnauthorizedException e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); - return getModelAndView(request, response, map,e); - } + /** + * shiro权限未授权异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = UnauthorizedException.class) + public ModelAndView handleUnauthorizedException(HttpServletRequest request, HttpServletResponse response, UnauthorizedException e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + return getModelAndView(request, response, map, e); + } - /** - * 登录异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = AuthenticationException.class) - public ModelAndView handleAuthenticationException(HttpServletRequest request, HttpServletResponse response,AuthenticationException e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 登录异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = AuthenticationException.class) + public ModelAndView handleAuthenticationException(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * shiro权限错误 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = AuthorizationException.class) - public ModelAndView handleAuthorizationException(HttpServletRequest request, HttpServletResponse response,AuthorizationException e){ - LOG.debug("AuthorizationException", e); - Map map = new HashMap(); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - map.put("msg", e.getStackTrace()); - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * shiro权限错误 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = AuthorizationException.class) + public ModelAndView handleAuthorizationException(HttpServletRequest request, HttpServletResponse response, AuthorizationException e) { + LOG.debug("AuthorizationException", e); + Map map = new HashMap(); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + map.put("msg", e.getStackTrace()); + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - /** - * session失效异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = ExpiredSessionException.class) - public ModelAndView handleExpiredSessionException(HttpServletRequest request, HttpServletResponse response,ExpiredSessionException e) { - LOG.debug("ExpiredSessionException", e); - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); - return getModelAndView(request, response, map,e); - } + /** + * session失效异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = ExpiredSessionException.class) + public ModelAndView handleExpiredSessionException(HttpServletRequest request, HttpServletResponse response, ExpiredSessionException e) { + LOG.debug("ExpiredSessionException", e); + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + return getModelAndView(request, response, map, e); + } - /** - * 返回异常信息处理 - * @param request - * @param response - * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 - * @param e - * @return - */ - private ModelAndView getModelAndView(HttpServletRequest request, HttpServletResponse response, Map map,Exception e) { - if(ObjectUtil.isNotNull(e)){ - LOG.debug("handleException", e); - StringWriter sw = new StringWriter(); - e.printStackTrace(new PrintWriter(sw, true)); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("errMsg", sw.toString()); - map.put("msg", e.getMessage()); - map.put("exc", e.getClass()); //详细异常信息 - } - //去掉异常信息中的跨站脚本 - map.put("msg",Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "",new Whitelist(), new Document.OutputSettings().prettyPrint(false))); - map.put("result",false); - if (BasicUtil.isAjaxRequest(request)) { - try { - response.setContentType("application/json;charset=UTF-8"); - PrintWriter writer = response.getWriter(); - writer.write(JSONObject.toJSONString(map)); - writer.flush(); - writer.close(); - } catch (IOException ex) { - ex.printStackTrace(); - } - } else { - return new ModelAndView("/error/index", map); - } - return null; - } + @ExceptionHandler(value = DcmsLoginException.class) + public ModelAndView handlDcmsLoginException(HttpServletRequest request, HttpServletResponse response, DcmsLoginException e) { + LOG.debug("DcmsLoginException", e); + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + map.put("msg", e.getBizMsg()); + return getErrorModelAndView(request, response, map, e); + } + /** + * 返回异常信息处理 + * @param request + * @param response + * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 + * @param e + * @return + */ + private ModelAndView getModelAndView(HttpServletRequest request, HttpServletResponse response, Map map, Exception e) { + if (ObjectUtil.isNotNull(e)) { + LOG.debug("handleException", e); + StringWriter sw = new StringWriter(); + e.printStackTrace(new PrintWriter(sw, true)); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("errMsg", sw.toString()); + map.put("msg", e.getMessage()); + map.put("exc", e.getClass()); //详细异常信息 + } + //去掉异常信息中的跨站脚本 + map.put("msg", Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "", new Whitelist(), new Document.OutputSettings().prettyPrint(false))); + map.put("result", false); + if (BasicUtil.isAjaxRequest(request)) { + try { + response.setContentType("application/json;charset=UTF-8"); + PrintWriter writer = response.getWriter(); + writer.write(JSONObject.toJSONString(map)); + writer.flush(); + writer.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } else { + return new ModelAndView("/error/index", map); + } + return null; + } + + /** + * 返回异常信息处理 + * @param request + * @param response + * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 + * @param e + * @return + */ + private ModelAndView getErrorModelAndView(HttpServletRequest request, HttpServletResponse response, Map map, Exception e) { + if (ObjectUtil.isNotNull(e)) { + LOG.debug("handleException", e); + StringWriter sw = new StringWriter(); + map.put("errMsg", sw.toString()); + e.printStackTrace(new PrintWriter(sw, true)); + //出错的类 + map.put("cls", e.getStackTrace()[0] + ""); + //请求地址 + map.put("url", request.getServletPath()); + //详细异常信息 + map.put("exc", e.getClass()); + } + //去掉异常信息中的跨站脚本 + map.put("msg", Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "", new Whitelist(), new Document.OutputSettings().prettyPrint(false))); + map.put("result", false); + if (BasicUtil.isAjaxRequest(request)) { + try { + PrintWriter writer = response.getWriter(); + response.setContentType("application/json;charset=UTF-8"); + + writer.write(JSONObject.toJSONString(map)); + writer.flush(); + writer.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } else { + return new ModelAndView("/error/dcmserror", map); + } + return null; + } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java b/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java index 04c26c8..9cb1d77 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java @@ -28,7 +28,7 @@ public void setHeader(HttpRequestBase base, String token) { base.addHeader("Content-type", DEFAULT_CONTEXTTYPE); if (!StringUtil.isBlank(token)) { - base.addHeader(DEFAULT_CHARSET, token); + base.addHeader(DEFAULT_TOKENKEY, token); } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java b/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java index 729a05b..1aa9358 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java @@ -7,6 +7,8 @@ import net.mingsoft.basic.handler.DefaultHttpHeaderHandler; import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * @ClassName Test @@ -19,7 +21,7 @@ public static void main(String[] args) { String url = "http://111.198.10.15:11409/user/info"; String token = "06d74157-14d6-4536-bf3a-e94d1d061a80"; - checkToken(token, url); + checkToken(url, token); } /** @@ -28,7 +30,7 @@ * @param token 颁发token * @return */ - public static ShiroUser checkToken(String token, String dcmsUrl) { + public static ShiroUser checkToken(String dcmsUrl, String token) { String json = HttpUtils.sendGet(dcmsUrl, new HashMap<>(), token, new DefaultHttpHeaderHandler()); if (!StringUtil.isBlank(json)) { System.out.println(json); @@ -40,4 +42,26 @@ } return null; } + + /** + * 转换为cms用户 + * + * @param user dmcs用户 + * @return + */ + public static String transformCmsUser(ShiroUser user, Map userRoles, List users) { + //是否有管理员 + for (String manager : userRoles.get("manager").split(",")) { + if (user.getRoleTips().contains(manager)) { + return users.get(0); + } + } + //返回普通用户 + for (String manager : userRoles.get("normal").split(",")) { + if (user.getRoleTips().contains(manager)) { + return users.get(1); + } + } + return null; + } } diff --git a/ms-basic/src/main/webapp/error/404.htm b/ms-basic/src/main/webapp/error/404.htm index 9b76382..bfc001f 100644 --- a/ms-basic/src/main/webapp/error/404.htm +++ b/ms-basic/src/main/webapp/error/404.htm @@ -40,4 +40,4 @@ - + \ No newline at end of file diff --git a/ms-mcms/pom.xml b/ms-mcms/pom.xml index 47d4474..f238b61 100644 --- a/ms-mcms/pom.xml +++ b/ms-mcms/pom.xml @@ -45,6 +45,7 @@ ms-mdiy 1.0.23-SNAPSHOT + net.mingsoft diff --git a/ms-mcms/src/main/java/net/mingsoft/config/ShiroConfig.java b/ms-mcms/src/main/java/net/mingsoft/config/ShiroConfig.java index a580427..fffc2c3 100644 --- a/ms-mcms/src/main/java/net/mingsoft/config/ShiroConfig.java +++ b/ms-mcms/src/main/java/net/mingsoft/config/ShiroConfig.java @@ -75,6 +75,8 @@ // 开放登陆接口 filterChainDefinitionMap.put(managerPath + "/login.do", "anon"); filterChainDefinitionMap.put(managerPath + "/checkLogin.do", "anon"); + filterChainDefinitionMap.put(managerPath + "/dcmsToken.do", "anon"); + filterChainDefinitionMap.put(managerPath + "/404.do", "anon"); // 其余接口一律拦截 // 主要这行代码必须放在所有权限设置的最后,不然会导致所有 url 都被拦截 filterChainDefinitionMap.put(managerPath + "/**", "authc"); diff --git a/ms-base/pom.xml b/ms-base/pom.xml index a5dcef6..af71af1 100644 --- a/ms-base/pom.xml +++ b/ms-base/pom.xml @@ -363,26 +363,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-base diff --git a/ms-basic/ms-basic.iml b/ms-basic/ms-basic.iml index 935ebc4..5a2e74f 100644 --- a/ms-basic/ms-basic.iml +++ b/ms-basic/ms-basic.iml @@ -11,8 +11,8 @@ - - + + diff --git a/ms-basic/pom.xml b/ms-basic/pom.xml index e66ec4f..90ba669 100644 --- a/ms-basic/pom.xml +++ b/ms-basic/pom.xml @@ -88,26 +88,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-basic diff --git a/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java b/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java index 540c752..30a601c 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java @@ -1,16 +1,16 @@ /** * The MIT License (MIT) * Copyright (c) 2020 铭软科技(mingsoft.net) - + *

* Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: - + *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - + *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR @@ -30,20 +30,25 @@ import io.swagger.annotations.ApiOperation; import net.mingsoft.base.entity.ResultData; import net.mingsoft.basic.action.BaseAction; +import net.mingsoft.basic.bean.ShiroUser; import net.mingsoft.basic.biz.IAppBiz; import net.mingsoft.basic.biz.IManagerBiz; import net.mingsoft.basic.biz.IRoleBiz; import net.mingsoft.basic.constant.Const; +import net.mingsoft.basic.constant.ErrorCodeEnum; import net.mingsoft.basic.constant.e.SessionConstEnum; import net.mingsoft.basic.entity.AppEntity; import net.mingsoft.basic.entity.ManagerEntity; import net.mingsoft.basic.entity.ManagerSessionEntity; import net.mingsoft.basic.entity.RoleEntity; +import net.mingsoft.basic.exception.DcmsLoginException; import net.mingsoft.basic.util.BasicUtil; +import net.mingsoft.basic.util.DcmsUtils; import net.mingsoft.basic.util.StringUtil; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.UsernamePasswordToken; +import org.apache.shiro.session.ExpiredSessionException; import org.apache.shiro.subject.Subject; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -56,14 +61,13 @@ import javax.servlet.http.HttpServletResponse; import java.io.File; import java.util.List; +import java.util.Map; /** - * - * @ClassName: LoginAction + * @ClassName: LoginAction * @Description:TODO(登录的基础应用层) * @author: 铭飞开发团队 - * @date: 2015年1月27日 下午3:21:47 - * + * @date: 2015年1月27日 下午3:21:47 * @Copyright: 2018 www.mingsoft.net Inc. All rights reserved. */ @Api("登录的基础应用层接口") @@ -71,146 +75,228 @@ @RequestMapping("/${ms.manager.path}") public class LoginAction extends BaseAction { - @Value("${ms.manager.path}") - private String managerPath; - /** - * 管理员业务层 - */ - @Autowired - private IManagerBiz managerBiz; + @Value("${ms.manager.path}") + private String managerPath; + @Value("${ms.manager.dcmsHost}") + private String dcmsHost; + @Value("${ms.manager.userInfoPath}") + private String userInfoPath; + @Value("#{${ms.manager.userRoles}}") + private Map userRoles; + @Value("#{'${ms.manager.users}'.split(',')}") + private List users; + /** + * 管理员业务层 + */ + @Autowired + private IManagerBiz managerBiz; - /** - * 角色业务request层 - */ - @Autowired - private IRoleBiz roleBiz; + /** + * 角色业务request层 + */ + @Autowired + private IRoleBiz roleBiz; - /** - * 站点业务层 - */ - @Autowired - private IAppBiz appBiz; + /** + * 站点业务层 + */ + @Autowired + private IAppBiz appBiz; + /** + * 加载管理员登录界面 + * + * @param request 请求对象 + * @return 管理员登录界面地址 + */ + @ApiOperation(value = "加载管理员登录界面") + @SuppressWarnings("resource") + @GetMapping("/dcmsToken") + public String dcmsToken(HttpServletRequest request) { + if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION) != null) { + return "redirect:" + managerPath + "/index.do"; + } + //请求dcms数据token + ShiroUser shiroUser = DcmsUtils.checkToken(dcmsHost + userInfoPath, request.getParameter("token")); + if (shiroUser == null) { + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无效token"); + } + String userName = DcmsUtils.transformCmsUser(shiroUser, userRoles, users); + if (userName == null) { + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无效token"); + } - /** - * 加载管理员登录界面 - * - * @param request - * 请求对象 - * @return 管理员登录界面地址 - */ - @ApiOperation(value = "加载管理员登录界面") - @SuppressWarnings("resource") - @GetMapping("/login") - public String login(HttpServletRequest request) { - if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION)!=null) { - return "redirect:"+managerPath+"/index.do"; - } - // 根据请求地址来显示标题 - AppEntity app = BasicUtil.getApp(); - // 判断应用实体是否存在 - if (app != null) { - // 检测应用是否有自定义界面b - if (!StringUtil.isBlank(app.getAppLoginPage())) { - LOG.debug("跳转自定义登录界面"); - return "redirect:" + app.getAppLoginPage(); - } + AppEntity app = BasicUtil.getApp(); + // 判断应用实体是否存在 + if (app != null) { + // 检测应用是否有自定义界面b + if (!StringUtil.isBlank(app.getAppLoginPage())) { + LOG.debug("跳转自定义登录界面"); + return "redirect:" + app.getAppLoginPage(); + } - } else { - File file = new File(BasicUtil.getRealPath( "WEB-INF/ms.install")); - //存在安装文件 - if (file.exists()) { - String defaultId = FileReader.create(new File(BasicUtil.getRealPath( "WEB-INF/ms.install"))).readString(); - if (!StringUtils.isEmpty(defaultId)) { - app = (AppEntity) appBiz.getEntity(Integer.parseInt(defaultId)); - app.setAppUrl(this.getUrl(request)); - appBiz.updateEntity(app); - FileWriter.create(new File(BasicUtil.getRealPath( "WEB-INF/ms.install.bak"))).write(defaultId); - file.delete(); - } - } + } + AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); + ManagerEntity newManager = new ManagerEntity(); + newManager.setManagerName(userName); + ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + // 创建管理员session对象 + ManagerSessionEntity managerSession = new ManagerSessionEntity(); + AppEntity website = new AppEntity(); + // 获取管理员所在的角色 + RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); + website = (AppEntity) appBiz.getEntity(role.getAppId()); + // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 + if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId() == BasicUtil.getAppId())) { + if (website == null) { + website = BasicUtil.getApp(); + } + List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); + managerSession.setStyle(website.getAppStyle()); + managerSession.setBasicId(website.getAppId()); + managerSession.setManagerParentID(role.getRoleManagerId()); + managerSession.setManagerChildIDs(childManagerList); + // 压入管理员seesion + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } else { + if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { + LOG.debug("roleId: " + _manager.getManagerRoleID()); + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无授权"); + } else { + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } + } + BeanUtils.copyProperties(_manager, managerSession); + Subject subject = SecurityUtils.getSubject(); + UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(), managerSession.getManagerPassword()); + subject.login(upt); + return "redirect:" + managerPath + "/index.do"; + } - } - request.setAttribute("app", app); - return "/login"; - } + @GetMapping("/404") + public String error(HttpServletRequest request) { + return "/404"; + } - /** - * 验证登录 - * - * @param manager - * 管理员实体 - * @param request - * 请求 - * @param response - * 响应 - */ - @ApiOperation(value = "验证登录") - @ApiImplicitParams({ - @ApiImplicitParam(name = "managerName", value = "帐号", required = true,paramType="query"), - @ApiImplicitParam(name = "managerPassword", value = "密码", required = true,paramType="query"), - }) - @PostMapping(value="/checkLogin") - @ResponseBody - public ResultData checkLogin(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletRequest request, - HttpServletResponse response) { - AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); // 根据url地址获取站点信息,主要是区分管理员对那些网站有权限 - if (urlWebsite == null) { - return ResultData.build().error( this.getResString("err.not.exist",this.getResString("app"),"!请尝试去文件 WEB-INF/ms.install.bak 后缀bak")); - } - //验证码 - if (!(checkRandCode())) { - return ResultData.build().error(getResString("err.error", new String[] { getResString("rand.code") })); - } + /** + * 加载管理员登录界面 + * + * @param request 请求对象 + * @return 管理员登录界面地址 + */ + @ApiOperation(value = "加载管理员登录界面") + @SuppressWarnings("resource") + @GetMapping("/login") + public String login(HttpServletRequest request) { + if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION) != null) { + return "redirect:" + managerPath + "/index.do"; + } + // 根据请求地址来显示标题 + AppEntity app = BasicUtil.getApp(); + // 判断应用实体是否存在 + if (app != null) { + // 检测应用是否有自定义界面b + if (!StringUtil.isBlank(app.getAppLoginPage())) { + LOG.debug("跳转自定义登录界面"); + return "redirect:" + app.getAppLoginPage(); + } - // 根据账号获取当前管理员信息 - ManagerEntity newManager = new ManagerEntity(); - newManager.setManagerName(manager.getManagerName()); - ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + } else { + File file = new File(BasicUtil.getRealPath("WEB-INF/ms.install")); + //存在安装文件 + if (file.exists()) { + String defaultId = FileReader.create(new File(BasicUtil.getRealPath("WEB-INF/ms.install"))).readString(); + if (!StringUtils.isEmpty(defaultId)) { + app = (AppEntity) appBiz.getEntity(Integer.parseInt(defaultId)); + app.setAppUrl(this.getUrl(request)); + appBiz.updateEntity(app); + FileWriter.create(new File(BasicUtil.getRealPath("WEB-INF/ms.install.bak"))).write(defaultId); + file.delete(); + } + } - if (_manager == null || StringUtils.isEmpty(manager.getManagerName())) { - // 系统不存在此用户 - return ResultData.build().error( this.getResString("err.nameEmpty")); - } else { - // 判断当前用户输入的密码是否正确 - if (SecureUtil.md5(manager.getManagerPassword()).equals(_manager.getManagerPassword())) { - // 创建管理员session对象 - ManagerSessionEntity managerSession = new ManagerSessionEntity(); - AppEntity website = new AppEntity(); - // 获取管理员所在的角色 - RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); - website = (AppEntity) appBiz.getEntity(role.getAppId()); - // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 - if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId()==BasicUtil.getAppId())) { - if(website==null){ - website = BasicUtil.getApp(); - } - List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); - managerSession.setBasicId(website.getAppId()); - managerSession.setManagerParentID(role.getRoleManagerId()); - managerSession.setManagerChildIDs(childManagerList); - managerSession.setStyle(website.getAppStyle()); - // 压入管理员seesion - BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); - } else { - if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { - LOG.debug("roleId: "+_manager.getManagerRoleID()); - return ResultData.build().error( this.getResString("err.not.exist",this.getResString("manager"))); - } else { - BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); - } - } - BeanUtils.copyProperties(_manager, managerSession); + } + request.setAttribute("app", app); + return "/login"; + } - Subject subject = SecurityUtils.getSubject(); - UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(),managerSession.getManagerPassword()); - subject.login(upt); - return ResultData.build().success(); - } else { - // 密码错误 - return ResultData.build().error( this.getResString("err.password")); - } - } - } + /** + * 验证登录 + * + * @param manager 管理员实体 + * @param request 请求 + * @param response 响应 + */ + @ApiOperation(value = "验证登录") + @ApiImplicitParams({ + @ApiImplicitParam(name = "managerName", value = "帐号", required = true, paramType = "query"), + @ApiImplicitParam(name = "managerPassword", value = "密码", required = true, paramType = "query"), + }) + @PostMapping(value = "/checkLogin") + @ResponseBody + public ResultData checkLogin(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletRequest request, + HttpServletResponse response) { + // 根据url地址获取站点信息,主要是区分管理员对那些网站有权限 + AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); + if (urlWebsite == null) { + return ResultData.build().error(this.getResString("err.not.exist", this.getResString("app"), "!请尝试去文件 WEB-INF/ms.install.bak 后缀bak")); + } + //验证码 + if (!(checkRandCode())) { + return ResultData.build().error(getResString("err.error", new String[]{getResString("rand.code")})); + } + + // 根据账号获取当前管理员信息 + ManagerEntity newManager = new ManagerEntity(); + newManager.setManagerName(manager.getManagerName()); + ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + + if (_manager == null || StringUtils.isEmpty(manager.getManagerName())) { + // 系统不存在此用户 + return ResultData.build().error(this.getResString("err.nameEmpty")); + } else { + // 判断当前用户输入的密码是否正确 + if (SecureUtil.md5(manager.getManagerPassword()).equals(_manager.getManagerPassword())) { + // 创建管理员session对象 + ManagerSessionEntity managerSession = new ManagerSessionEntity(); + AppEntity website = new AppEntity(); + // 获取管理员所在的角色 + RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); + website = (AppEntity) appBiz.getEntity(role.getAppId()); + // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 + if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId() == BasicUtil.getAppId())) { + if (website == null) { + website = BasicUtil.getApp(); + } + List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); + managerSession.setBasicId(website.getAppId()); + managerSession.setManagerParentID(role.getRoleManagerId()); + managerSession.setManagerChildIDs(childManagerList); + managerSession.setStyle(website.getAppStyle()); + // 压入管理员seesion + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } else { + if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { + LOG.debug("roleId: " + _manager.getManagerRoleID()); + return ResultData.build().error(this.getResString("err.not.exist", this.getResString("manager"))); + } else { + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } + } + BeanUtils.copyProperties(_manager, managerSession); + + Subject subject = SecurityUtils.getSubject(); + UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(), managerSession.getManagerPassword()); + subject.login(upt); + return ResultData.build().success(); + } else { + // 密码错误 + return ResultData.build().error(this.getResString("err.password")); + } + } + } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java b/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java index 726a50c..f0e6377 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java @@ -1,16 +1,16 @@ /** * The MIT License (MIT) * Copyright (c) 2020 铭软科技(mingsoft.net) - + *

* Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: - + *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - + *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR @@ -61,214 +61,263 @@ */ @ControllerAdvice public class GlobalExceptionResolver extends DefaultHandlerExceptionResolver { - - protected final Logger LOG = LoggerFactory.getLogger(this.getClass()); - /** - * 全局异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = Exception.class) - public ModelAndView handleException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.SERVER_ERROR); - return getModelAndView(request, response, map,e); - } + protected final Logger LOG = LoggerFactory.getLogger(this.getClass()); - /** - * 全局异常 未找到类404 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = NoHandlerFoundException.class) - public ModelAndView handleNoHandlerFoundException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 全局异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = Exception.class) + public ModelAndView handleException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.SERVER_ERROR); + return getModelAndView(request, response, map, e); + } - /** - * 请求参数异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = MissingServletRequestParameterException.class) - public ModelAndView handleMissingServletRequestParameterException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 全局异常 未找到类404 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = NoHandlerFoundException.class) + public ModelAndView handleNoHandlerFoundException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 请求方法类型错误 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) - public ModelAndView handleHttpRequestMethodNotSupportedException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 请求参数异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = MissingServletRequestParameterException.class) + public ModelAndView handleMissingServletRequestParameterException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 统一处理请求参数校验(实体对象传参) - * - * @param e BindException - * @return ResultResponse - */ - @ExceptionHandler(BindException.class) - public ModelAndView validExceptionHandler(HttpServletRequest request, HttpServletResponse response,BindException e) { - StringBuilder message = new StringBuilder(); - List fieldErrors = e.getBindingResult().getFieldErrors(); - for (FieldError error : fieldErrors) { - message.append(error.getField()).append(error.getDefaultMessage()).append(","); - } - message = new StringBuilder(message.substring(0, message.length() - 1)); - Map map = new HashMap(); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("code", ErrorCodeEnum.CLIENT_REQUEST); - map.put("result",false); - map.put("msg", message.toString()); - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * 请求方法类型错误 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) + public ModelAndView handleHttpRequestMethodNotSupportedException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 统一处理请求参数校验(普通传参) - * - * @param e ConstraintViolationException - * @return ResultResponse - */ - @ExceptionHandler(value = ConstraintViolationException.class) - public ModelAndView handleConstraintViolationException(HttpServletRequest request, HttpServletResponse response,ConstraintViolationException e) { - StringBuilder message = new StringBuilder(); - Set> violations = e.getConstraintViolations(); - for (ConstraintViolation violation : violations) { - Path path = violation.getPropertyPath(); - String[] pathArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(path.toString(), "."); - message.append(pathArr[1]).append(violation.getMessage()).append(","); - } - message = new StringBuilder(message.substring(0, message.length() - 1)); - Map map = new HashMap(); - map.put("msg", message.toString()); - map.put("code", ErrorCodeEnum.CLIENT_REQUEST); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 + /** + * 统一处理请求参数校验(实体对象传参) + * + * @param e BindException + * @return ResultResponse + */ + @ExceptionHandler(BindException.class) + public ModelAndView validExceptionHandler(HttpServletRequest request, HttpServletResponse response, BindException e) { + StringBuilder message = new StringBuilder(); + List fieldErrors = e.getBindingResult().getFieldErrors(); + for (FieldError error : fieldErrors) { + message.append(error.getField()).append(error.getDefaultMessage()).append(","); + } + message = new StringBuilder(message.substring(0, message.length() - 1)); + Map map = new HashMap(); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("code", ErrorCodeEnum.CLIENT_REQUEST); + map.put("result", false); + map.put("msg", message.toString()); + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * 统一处理请求参数校验(普通传参) + * + * @param e ConstraintViolationException + * @return ResultResponse + */ + @ExceptionHandler(value = ConstraintViolationException.class) + public ModelAndView handleConstraintViolationException(HttpServletRequest request, HttpServletResponse response, ConstraintViolationException e) { + StringBuilder message = new StringBuilder(); + Set> violations = e.getConstraintViolations(); + for (ConstraintViolation violation : violations) { + Path path = violation.getPropertyPath(); + String[] pathArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(path.toString(), "."); + message.append(pathArr[1]).append(violation.getMessage()).append(","); + } + message = new StringBuilder(message.substring(0, message.length() - 1)); + Map map = new HashMap(); + map.put("msg", message.toString()); + map.put("code", ErrorCodeEnum.CLIENT_REQUEST); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - /** - * shiro权限未授权异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = UnauthorizedException.class) - public ModelAndView handleUnauthorizedException(HttpServletRequest request, HttpServletResponse response,UnauthorizedException e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); - return getModelAndView(request, response, map,e); - } + /** + * shiro权限未授权异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = UnauthorizedException.class) + public ModelAndView handleUnauthorizedException(HttpServletRequest request, HttpServletResponse response, UnauthorizedException e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + return getModelAndView(request, response, map, e); + } - /** - * 登录异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = AuthenticationException.class) - public ModelAndView handleAuthenticationException(HttpServletRequest request, HttpServletResponse response,AuthenticationException e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 登录异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = AuthenticationException.class) + public ModelAndView handleAuthenticationException(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * shiro权限错误 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = AuthorizationException.class) - public ModelAndView handleAuthorizationException(HttpServletRequest request, HttpServletResponse response,AuthorizationException e){ - LOG.debug("AuthorizationException", e); - Map map = new HashMap(); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - map.put("msg", e.getStackTrace()); - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * shiro权限错误 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = AuthorizationException.class) + public ModelAndView handleAuthorizationException(HttpServletRequest request, HttpServletResponse response, AuthorizationException e) { + LOG.debug("AuthorizationException", e); + Map map = new HashMap(); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + map.put("msg", e.getStackTrace()); + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - /** - * session失效异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = ExpiredSessionException.class) - public ModelAndView handleExpiredSessionException(HttpServletRequest request, HttpServletResponse response,ExpiredSessionException e) { - LOG.debug("ExpiredSessionException", e); - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); - return getModelAndView(request, response, map,e); - } + /** + * session失效异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = ExpiredSessionException.class) + public ModelAndView handleExpiredSessionException(HttpServletRequest request, HttpServletResponse response, ExpiredSessionException e) { + LOG.debug("ExpiredSessionException", e); + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + return getModelAndView(request, response, map, e); + } - /** - * 返回异常信息处理 - * @param request - * @param response - * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 - * @param e - * @return - */ - private ModelAndView getModelAndView(HttpServletRequest request, HttpServletResponse response, Map map,Exception e) { - if(ObjectUtil.isNotNull(e)){ - LOG.debug("handleException", e); - StringWriter sw = new StringWriter(); - e.printStackTrace(new PrintWriter(sw, true)); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("errMsg", sw.toString()); - map.put("msg", e.getMessage()); - map.put("exc", e.getClass()); //详细异常信息 - } - //去掉异常信息中的跨站脚本 - map.put("msg",Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "",new Whitelist(), new Document.OutputSettings().prettyPrint(false))); - map.put("result",false); - if (BasicUtil.isAjaxRequest(request)) { - try { - response.setContentType("application/json;charset=UTF-8"); - PrintWriter writer = response.getWriter(); - writer.write(JSONObject.toJSONString(map)); - writer.flush(); - writer.close(); - } catch (IOException ex) { - ex.printStackTrace(); - } - } else { - return new ModelAndView("/error/index", map); - } - return null; - } + @ExceptionHandler(value = DcmsLoginException.class) + public ModelAndView handlDcmsLoginException(HttpServletRequest request, HttpServletResponse response, DcmsLoginException e) { + LOG.debug("DcmsLoginException", e); + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + map.put("msg", e.getBizMsg()); + return getErrorModelAndView(request, response, map, e); + } + /** + * 返回异常信息处理 + * @param request + * @param response + * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 + * @param e + * @return + */ + private ModelAndView getModelAndView(HttpServletRequest request, HttpServletResponse response, Map map, Exception e) { + if (ObjectUtil.isNotNull(e)) { + LOG.debug("handleException", e); + StringWriter sw = new StringWriter(); + e.printStackTrace(new PrintWriter(sw, true)); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("errMsg", sw.toString()); + map.put("msg", e.getMessage()); + map.put("exc", e.getClass()); //详细异常信息 + } + //去掉异常信息中的跨站脚本 + map.put("msg", Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "", new Whitelist(), new Document.OutputSettings().prettyPrint(false))); + map.put("result", false); + if (BasicUtil.isAjaxRequest(request)) { + try { + response.setContentType("application/json;charset=UTF-8"); + PrintWriter writer = response.getWriter(); + writer.write(JSONObject.toJSONString(map)); + writer.flush(); + writer.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } else { + return new ModelAndView("/error/index", map); + } + return null; + } + + /** + * 返回异常信息处理 + * @param request + * @param response + * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 + * @param e + * @return + */ + private ModelAndView getErrorModelAndView(HttpServletRequest request, HttpServletResponse response, Map map, Exception e) { + if (ObjectUtil.isNotNull(e)) { + LOG.debug("handleException", e); + StringWriter sw = new StringWriter(); + map.put("errMsg", sw.toString()); + e.printStackTrace(new PrintWriter(sw, true)); + //出错的类 + map.put("cls", e.getStackTrace()[0] + ""); + //请求地址 + map.put("url", request.getServletPath()); + //详细异常信息 + map.put("exc", e.getClass()); + } + //去掉异常信息中的跨站脚本 + map.put("msg", Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "", new Whitelist(), new Document.OutputSettings().prettyPrint(false))); + map.put("result", false); + if (BasicUtil.isAjaxRequest(request)) { + try { + PrintWriter writer = response.getWriter(); + response.setContentType("application/json;charset=UTF-8"); + + writer.write(JSONObject.toJSONString(map)); + writer.flush(); + writer.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } else { + return new ModelAndView("/error/dcmserror", map); + } + return null; + } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java b/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java index 04c26c8..9cb1d77 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java @@ -28,7 +28,7 @@ public void setHeader(HttpRequestBase base, String token) { base.addHeader("Content-type", DEFAULT_CONTEXTTYPE); if (!StringUtil.isBlank(token)) { - base.addHeader(DEFAULT_CHARSET, token); + base.addHeader(DEFAULT_TOKENKEY, token); } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java b/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java index 729a05b..1aa9358 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java @@ -7,6 +7,8 @@ import net.mingsoft.basic.handler.DefaultHttpHeaderHandler; import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * @ClassName Test @@ -19,7 +21,7 @@ public static void main(String[] args) { String url = "http://111.198.10.15:11409/user/info"; String token = "06d74157-14d6-4536-bf3a-e94d1d061a80"; - checkToken(token, url); + checkToken(url, token); } /** @@ -28,7 +30,7 @@ * @param token 颁发token * @return */ - public static ShiroUser checkToken(String token, String dcmsUrl) { + public static ShiroUser checkToken(String dcmsUrl, String token) { String json = HttpUtils.sendGet(dcmsUrl, new HashMap<>(), token, new DefaultHttpHeaderHandler()); if (!StringUtil.isBlank(json)) { System.out.println(json); @@ -40,4 +42,26 @@ } return null; } + + /** + * 转换为cms用户 + * + * @param user dmcs用户 + * @return + */ + public static String transformCmsUser(ShiroUser user, Map userRoles, List users) { + //是否有管理员 + for (String manager : userRoles.get("manager").split(",")) { + if (user.getRoleTips().contains(manager)) { + return users.get(0); + } + } + //返回普通用户 + for (String manager : userRoles.get("normal").split(",")) { + if (user.getRoleTips().contains(manager)) { + return users.get(1); + } + } + return null; + } } diff --git a/ms-basic/src/main/webapp/error/404.htm b/ms-basic/src/main/webapp/error/404.htm index 9b76382..bfc001f 100644 --- a/ms-basic/src/main/webapp/error/404.htm +++ b/ms-basic/src/main/webapp/error/404.htm @@ -40,4 +40,4 @@ - + \ No newline at end of file diff --git a/ms-mcms/pom.xml b/ms-mcms/pom.xml index 47d4474..f238b61 100644 --- a/ms-mcms/pom.xml +++ b/ms-mcms/pom.xml @@ -45,6 +45,7 @@ ms-mdiy 1.0.23-SNAPSHOT + net.mingsoft diff --git a/ms-mcms/src/main/java/net/mingsoft/config/ShiroConfig.java b/ms-mcms/src/main/java/net/mingsoft/config/ShiroConfig.java index a580427..fffc2c3 100644 --- a/ms-mcms/src/main/java/net/mingsoft/config/ShiroConfig.java +++ b/ms-mcms/src/main/java/net/mingsoft/config/ShiroConfig.java @@ -75,6 +75,8 @@ // 开放登陆接口 filterChainDefinitionMap.put(managerPath + "/login.do", "anon"); filterChainDefinitionMap.put(managerPath + "/checkLogin.do", "anon"); + filterChainDefinitionMap.put(managerPath + "/dcmsToken.do", "anon"); + filterChainDefinitionMap.put(managerPath + "/404.do", "anon"); // 其余接口一律拦截 // 主要这行代码必须放在所有权限设置的最后,不然会导致所有 url 都被拦截 filterChainDefinitionMap.put(managerPath + "/**", "authc"); diff --git a/ms-mcms/src/main/resources/application-dev.yml b/ms-mcms/src/main/resources/application-dev.yml index 7bd2b66..72449a9 100644 --- a/ms-mcms/src/main/resources/application-dev.yml +++ b/ms-mcms/src/main/resources/application-dev.yml @@ -1,7 +1,7 @@ spring: datasource: - url: jdbc:mysql://localhost:3306/mcms-dev-5.1-1?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai + url: jdbc:mysql://111.198.10.15:11102/db-mcms-open?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai username: root - password: root + password: Casic203! filters: wall,mergeStat type: com.alibaba.druid.pool.DruidDataSource diff --git a/ms-base/pom.xml b/ms-base/pom.xml index a5dcef6..af71af1 100644 --- a/ms-base/pom.xml +++ b/ms-base/pom.xml @@ -363,26 +363,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-base diff --git a/ms-basic/ms-basic.iml b/ms-basic/ms-basic.iml index 935ebc4..5a2e74f 100644 --- a/ms-basic/ms-basic.iml +++ b/ms-basic/ms-basic.iml @@ -11,8 +11,8 @@ - - + + diff --git a/ms-basic/pom.xml b/ms-basic/pom.xml index e66ec4f..90ba669 100644 --- a/ms-basic/pom.xml +++ b/ms-basic/pom.xml @@ -88,26 +88,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-basic diff --git a/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java b/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java index 540c752..30a601c 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java @@ -1,16 +1,16 @@ /** * The MIT License (MIT) * Copyright (c) 2020 铭软科技(mingsoft.net) - + *

* Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: - + *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - + *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR @@ -30,20 +30,25 @@ import io.swagger.annotations.ApiOperation; import net.mingsoft.base.entity.ResultData; import net.mingsoft.basic.action.BaseAction; +import net.mingsoft.basic.bean.ShiroUser; import net.mingsoft.basic.biz.IAppBiz; import net.mingsoft.basic.biz.IManagerBiz; import net.mingsoft.basic.biz.IRoleBiz; import net.mingsoft.basic.constant.Const; +import net.mingsoft.basic.constant.ErrorCodeEnum; import net.mingsoft.basic.constant.e.SessionConstEnum; import net.mingsoft.basic.entity.AppEntity; import net.mingsoft.basic.entity.ManagerEntity; import net.mingsoft.basic.entity.ManagerSessionEntity; import net.mingsoft.basic.entity.RoleEntity; +import net.mingsoft.basic.exception.DcmsLoginException; import net.mingsoft.basic.util.BasicUtil; +import net.mingsoft.basic.util.DcmsUtils; import net.mingsoft.basic.util.StringUtil; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.UsernamePasswordToken; +import org.apache.shiro.session.ExpiredSessionException; import org.apache.shiro.subject.Subject; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -56,14 +61,13 @@ import javax.servlet.http.HttpServletResponse; import java.io.File; import java.util.List; +import java.util.Map; /** - * - * @ClassName: LoginAction + * @ClassName: LoginAction * @Description:TODO(登录的基础应用层) * @author: 铭飞开发团队 - * @date: 2015年1月27日 下午3:21:47 - * + * @date: 2015年1月27日 下午3:21:47 * @Copyright: 2018 www.mingsoft.net Inc. All rights reserved. */ @Api("登录的基础应用层接口") @@ -71,146 +75,228 @@ @RequestMapping("/${ms.manager.path}") public class LoginAction extends BaseAction { - @Value("${ms.manager.path}") - private String managerPath; - /** - * 管理员业务层 - */ - @Autowired - private IManagerBiz managerBiz; + @Value("${ms.manager.path}") + private String managerPath; + @Value("${ms.manager.dcmsHost}") + private String dcmsHost; + @Value("${ms.manager.userInfoPath}") + private String userInfoPath; + @Value("#{${ms.manager.userRoles}}") + private Map userRoles; + @Value("#{'${ms.manager.users}'.split(',')}") + private List users; + /** + * 管理员业务层 + */ + @Autowired + private IManagerBiz managerBiz; - /** - * 角色业务request层 - */ - @Autowired - private IRoleBiz roleBiz; + /** + * 角色业务request层 + */ + @Autowired + private IRoleBiz roleBiz; - /** - * 站点业务层 - */ - @Autowired - private IAppBiz appBiz; + /** + * 站点业务层 + */ + @Autowired + private IAppBiz appBiz; + /** + * 加载管理员登录界面 + * + * @param request 请求对象 + * @return 管理员登录界面地址 + */ + @ApiOperation(value = "加载管理员登录界面") + @SuppressWarnings("resource") + @GetMapping("/dcmsToken") + public String dcmsToken(HttpServletRequest request) { + if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION) != null) { + return "redirect:" + managerPath + "/index.do"; + } + //请求dcms数据token + ShiroUser shiroUser = DcmsUtils.checkToken(dcmsHost + userInfoPath, request.getParameter("token")); + if (shiroUser == null) { + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无效token"); + } + String userName = DcmsUtils.transformCmsUser(shiroUser, userRoles, users); + if (userName == null) { + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无效token"); + } - /** - * 加载管理员登录界面 - * - * @param request - * 请求对象 - * @return 管理员登录界面地址 - */ - @ApiOperation(value = "加载管理员登录界面") - @SuppressWarnings("resource") - @GetMapping("/login") - public String login(HttpServletRequest request) { - if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION)!=null) { - return "redirect:"+managerPath+"/index.do"; - } - // 根据请求地址来显示标题 - AppEntity app = BasicUtil.getApp(); - // 判断应用实体是否存在 - if (app != null) { - // 检测应用是否有自定义界面b - if (!StringUtil.isBlank(app.getAppLoginPage())) { - LOG.debug("跳转自定义登录界面"); - return "redirect:" + app.getAppLoginPage(); - } + AppEntity app = BasicUtil.getApp(); + // 判断应用实体是否存在 + if (app != null) { + // 检测应用是否有自定义界面b + if (!StringUtil.isBlank(app.getAppLoginPage())) { + LOG.debug("跳转自定义登录界面"); + return "redirect:" + app.getAppLoginPage(); + } - } else { - File file = new File(BasicUtil.getRealPath( "WEB-INF/ms.install")); - //存在安装文件 - if (file.exists()) { - String defaultId = FileReader.create(new File(BasicUtil.getRealPath( "WEB-INF/ms.install"))).readString(); - if (!StringUtils.isEmpty(defaultId)) { - app = (AppEntity) appBiz.getEntity(Integer.parseInt(defaultId)); - app.setAppUrl(this.getUrl(request)); - appBiz.updateEntity(app); - FileWriter.create(new File(BasicUtil.getRealPath( "WEB-INF/ms.install.bak"))).write(defaultId); - file.delete(); - } - } + } + AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); + ManagerEntity newManager = new ManagerEntity(); + newManager.setManagerName(userName); + ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + // 创建管理员session对象 + ManagerSessionEntity managerSession = new ManagerSessionEntity(); + AppEntity website = new AppEntity(); + // 获取管理员所在的角色 + RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); + website = (AppEntity) appBiz.getEntity(role.getAppId()); + // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 + if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId() == BasicUtil.getAppId())) { + if (website == null) { + website = BasicUtil.getApp(); + } + List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); + managerSession.setStyle(website.getAppStyle()); + managerSession.setBasicId(website.getAppId()); + managerSession.setManagerParentID(role.getRoleManagerId()); + managerSession.setManagerChildIDs(childManagerList); + // 压入管理员seesion + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } else { + if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { + LOG.debug("roleId: " + _manager.getManagerRoleID()); + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无授权"); + } else { + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } + } + BeanUtils.copyProperties(_manager, managerSession); + Subject subject = SecurityUtils.getSubject(); + UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(), managerSession.getManagerPassword()); + subject.login(upt); + return "redirect:" + managerPath + "/index.do"; + } - } - request.setAttribute("app", app); - return "/login"; - } + @GetMapping("/404") + public String error(HttpServletRequest request) { + return "/404"; + } - /** - * 验证登录 - * - * @param manager - * 管理员实体 - * @param request - * 请求 - * @param response - * 响应 - */ - @ApiOperation(value = "验证登录") - @ApiImplicitParams({ - @ApiImplicitParam(name = "managerName", value = "帐号", required = true,paramType="query"), - @ApiImplicitParam(name = "managerPassword", value = "密码", required = true,paramType="query"), - }) - @PostMapping(value="/checkLogin") - @ResponseBody - public ResultData checkLogin(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletRequest request, - HttpServletResponse response) { - AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); // 根据url地址获取站点信息,主要是区分管理员对那些网站有权限 - if (urlWebsite == null) { - return ResultData.build().error( this.getResString("err.not.exist",this.getResString("app"),"!请尝试去文件 WEB-INF/ms.install.bak 后缀bak")); - } - //验证码 - if (!(checkRandCode())) { - return ResultData.build().error(getResString("err.error", new String[] { getResString("rand.code") })); - } + /** + * 加载管理员登录界面 + * + * @param request 请求对象 + * @return 管理员登录界面地址 + */ + @ApiOperation(value = "加载管理员登录界面") + @SuppressWarnings("resource") + @GetMapping("/login") + public String login(HttpServletRequest request) { + if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION) != null) { + return "redirect:" + managerPath + "/index.do"; + } + // 根据请求地址来显示标题 + AppEntity app = BasicUtil.getApp(); + // 判断应用实体是否存在 + if (app != null) { + // 检测应用是否有自定义界面b + if (!StringUtil.isBlank(app.getAppLoginPage())) { + LOG.debug("跳转自定义登录界面"); + return "redirect:" + app.getAppLoginPage(); + } - // 根据账号获取当前管理员信息 - ManagerEntity newManager = new ManagerEntity(); - newManager.setManagerName(manager.getManagerName()); - ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + } else { + File file = new File(BasicUtil.getRealPath("WEB-INF/ms.install")); + //存在安装文件 + if (file.exists()) { + String defaultId = FileReader.create(new File(BasicUtil.getRealPath("WEB-INF/ms.install"))).readString(); + if (!StringUtils.isEmpty(defaultId)) { + app = (AppEntity) appBiz.getEntity(Integer.parseInt(defaultId)); + app.setAppUrl(this.getUrl(request)); + appBiz.updateEntity(app); + FileWriter.create(new File(BasicUtil.getRealPath("WEB-INF/ms.install.bak"))).write(defaultId); + file.delete(); + } + } - if (_manager == null || StringUtils.isEmpty(manager.getManagerName())) { - // 系统不存在此用户 - return ResultData.build().error( this.getResString("err.nameEmpty")); - } else { - // 判断当前用户输入的密码是否正确 - if (SecureUtil.md5(manager.getManagerPassword()).equals(_manager.getManagerPassword())) { - // 创建管理员session对象 - ManagerSessionEntity managerSession = new ManagerSessionEntity(); - AppEntity website = new AppEntity(); - // 获取管理员所在的角色 - RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); - website = (AppEntity) appBiz.getEntity(role.getAppId()); - // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 - if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId()==BasicUtil.getAppId())) { - if(website==null){ - website = BasicUtil.getApp(); - } - List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); - managerSession.setBasicId(website.getAppId()); - managerSession.setManagerParentID(role.getRoleManagerId()); - managerSession.setManagerChildIDs(childManagerList); - managerSession.setStyle(website.getAppStyle()); - // 压入管理员seesion - BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); - } else { - if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { - LOG.debug("roleId: "+_manager.getManagerRoleID()); - return ResultData.build().error( this.getResString("err.not.exist",this.getResString("manager"))); - } else { - BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); - } - } - BeanUtils.copyProperties(_manager, managerSession); + } + request.setAttribute("app", app); + return "/login"; + } - Subject subject = SecurityUtils.getSubject(); - UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(),managerSession.getManagerPassword()); - subject.login(upt); - return ResultData.build().success(); - } else { - // 密码错误 - return ResultData.build().error( this.getResString("err.password")); - } - } - } + /** + * 验证登录 + * + * @param manager 管理员实体 + * @param request 请求 + * @param response 响应 + */ + @ApiOperation(value = "验证登录") + @ApiImplicitParams({ + @ApiImplicitParam(name = "managerName", value = "帐号", required = true, paramType = "query"), + @ApiImplicitParam(name = "managerPassword", value = "密码", required = true, paramType = "query"), + }) + @PostMapping(value = "/checkLogin") + @ResponseBody + public ResultData checkLogin(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletRequest request, + HttpServletResponse response) { + // 根据url地址获取站点信息,主要是区分管理员对那些网站有权限 + AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); + if (urlWebsite == null) { + return ResultData.build().error(this.getResString("err.not.exist", this.getResString("app"), "!请尝试去文件 WEB-INF/ms.install.bak 后缀bak")); + } + //验证码 + if (!(checkRandCode())) { + return ResultData.build().error(getResString("err.error", new String[]{getResString("rand.code")})); + } + + // 根据账号获取当前管理员信息 + ManagerEntity newManager = new ManagerEntity(); + newManager.setManagerName(manager.getManagerName()); + ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + + if (_manager == null || StringUtils.isEmpty(manager.getManagerName())) { + // 系统不存在此用户 + return ResultData.build().error(this.getResString("err.nameEmpty")); + } else { + // 判断当前用户输入的密码是否正确 + if (SecureUtil.md5(manager.getManagerPassword()).equals(_manager.getManagerPassword())) { + // 创建管理员session对象 + ManagerSessionEntity managerSession = new ManagerSessionEntity(); + AppEntity website = new AppEntity(); + // 获取管理员所在的角色 + RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); + website = (AppEntity) appBiz.getEntity(role.getAppId()); + // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 + if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId() == BasicUtil.getAppId())) { + if (website == null) { + website = BasicUtil.getApp(); + } + List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); + managerSession.setBasicId(website.getAppId()); + managerSession.setManagerParentID(role.getRoleManagerId()); + managerSession.setManagerChildIDs(childManagerList); + managerSession.setStyle(website.getAppStyle()); + // 压入管理员seesion + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } else { + if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { + LOG.debug("roleId: " + _manager.getManagerRoleID()); + return ResultData.build().error(this.getResString("err.not.exist", this.getResString("manager"))); + } else { + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } + } + BeanUtils.copyProperties(_manager, managerSession); + + Subject subject = SecurityUtils.getSubject(); + UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(), managerSession.getManagerPassword()); + subject.login(upt); + return ResultData.build().success(); + } else { + // 密码错误 + return ResultData.build().error(this.getResString("err.password")); + } + } + } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java b/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java index 726a50c..f0e6377 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java @@ -1,16 +1,16 @@ /** * The MIT License (MIT) * Copyright (c) 2020 铭软科技(mingsoft.net) - + *

* Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: - + *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - + *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR @@ -61,214 +61,263 @@ */ @ControllerAdvice public class GlobalExceptionResolver extends DefaultHandlerExceptionResolver { - - protected final Logger LOG = LoggerFactory.getLogger(this.getClass()); - /** - * 全局异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = Exception.class) - public ModelAndView handleException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.SERVER_ERROR); - return getModelAndView(request, response, map,e); - } + protected final Logger LOG = LoggerFactory.getLogger(this.getClass()); - /** - * 全局异常 未找到类404 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = NoHandlerFoundException.class) - public ModelAndView handleNoHandlerFoundException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 全局异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = Exception.class) + public ModelAndView handleException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.SERVER_ERROR); + return getModelAndView(request, response, map, e); + } - /** - * 请求参数异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = MissingServletRequestParameterException.class) - public ModelAndView handleMissingServletRequestParameterException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 全局异常 未找到类404 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = NoHandlerFoundException.class) + public ModelAndView handleNoHandlerFoundException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 请求方法类型错误 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) - public ModelAndView handleHttpRequestMethodNotSupportedException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 请求参数异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = MissingServletRequestParameterException.class) + public ModelAndView handleMissingServletRequestParameterException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 统一处理请求参数校验(实体对象传参) - * - * @param e BindException - * @return ResultResponse - */ - @ExceptionHandler(BindException.class) - public ModelAndView validExceptionHandler(HttpServletRequest request, HttpServletResponse response,BindException e) { - StringBuilder message = new StringBuilder(); - List fieldErrors = e.getBindingResult().getFieldErrors(); - for (FieldError error : fieldErrors) { - message.append(error.getField()).append(error.getDefaultMessage()).append(","); - } - message = new StringBuilder(message.substring(0, message.length() - 1)); - Map map = new HashMap(); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("code", ErrorCodeEnum.CLIENT_REQUEST); - map.put("result",false); - map.put("msg", message.toString()); - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * 请求方法类型错误 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) + public ModelAndView handleHttpRequestMethodNotSupportedException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 统一处理请求参数校验(普通传参) - * - * @param e ConstraintViolationException - * @return ResultResponse - */ - @ExceptionHandler(value = ConstraintViolationException.class) - public ModelAndView handleConstraintViolationException(HttpServletRequest request, HttpServletResponse response,ConstraintViolationException e) { - StringBuilder message = new StringBuilder(); - Set> violations = e.getConstraintViolations(); - for (ConstraintViolation violation : violations) { - Path path = violation.getPropertyPath(); - String[] pathArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(path.toString(), "."); - message.append(pathArr[1]).append(violation.getMessage()).append(","); - } - message = new StringBuilder(message.substring(0, message.length() - 1)); - Map map = new HashMap(); - map.put("msg", message.toString()); - map.put("code", ErrorCodeEnum.CLIENT_REQUEST); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 + /** + * 统一处理请求参数校验(实体对象传参) + * + * @param e BindException + * @return ResultResponse + */ + @ExceptionHandler(BindException.class) + public ModelAndView validExceptionHandler(HttpServletRequest request, HttpServletResponse response, BindException e) { + StringBuilder message = new StringBuilder(); + List fieldErrors = e.getBindingResult().getFieldErrors(); + for (FieldError error : fieldErrors) { + message.append(error.getField()).append(error.getDefaultMessage()).append(","); + } + message = new StringBuilder(message.substring(0, message.length() - 1)); + Map map = new HashMap(); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("code", ErrorCodeEnum.CLIENT_REQUEST); + map.put("result", false); + map.put("msg", message.toString()); + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * 统一处理请求参数校验(普通传参) + * + * @param e ConstraintViolationException + * @return ResultResponse + */ + @ExceptionHandler(value = ConstraintViolationException.class) + public ModelAndView handleConstraintViolationException(HttpServletRequest request, HttpServletResponse response, ConstraintViolationException e) { + StringBuilder message = new StringBuilder(); + Set> violations = e.getConstraintViolations(); + for (ConstraintViolation violation : violations) { + Path path = violation.getPropertyPath(); + String[] pathArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(path.toString(), "."); + message.append(pathArr[1]).append(violation.getMessage()).append(","); + } + message = new StringBuilder(message.substring(0, message.length() - 1)); + Map map = new HashMap(); + map.put("msg", message.toString()); + map.put("code", ErrorCodeEnum.CLIENT_REQUEST); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - /** - * shiro权限未授权异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = UnauthorizedException.class) - public ModelAndView handleUnauthorizedException(HttpServletRequest request, HttpServletResponse response,UnauthorizedException e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); - return getModelAndView(request, response, map,e); - } + /** + * shiro权限未授权异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = UnauthorizedException.class) + public ModelAndView handleUnauthorizedException(HttpServletRequest request, HttpServletResponse response, UnauthorizedException e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + return getModelAndView(request, response, map, e); + } - /** - * 登录异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = AuthenticationException.class) - public ModelAndView handleAuthenticationException(HttpServletRequest request, HttpServletResponse response,AuthenticationException e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 登录异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = AuthenticationException.class) + public ModelAndView handleAuthenticationException(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * shiro权限错误 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = AuthorizationException.class) - public ModelAndView handleAuthorizationException(HttpServletRequest request, HttpServletResponse response,AuthorizationException e){ - LOG.debug("AuthorizationException", e); - Map map = new HashMap(); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - map.put("msg", e.getStackTrace()); - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * shiro权限错误 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = AuthorizationException.class) + public ModelAndView handleAuthorizationException(HttpServletRequest request, HttpServletResponse response, AuthorizationException e) { + LOG.debug("AuthorizationException", e); + Map map = new HashMap(); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + map.put("msg", e.getStackTrace()); + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - /** - * session失效异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = ExpiredSessionException.class) - public ModelAndView handleExpiredSessionException(HttpServletRequest request, HttpServletResponse response,ExpiredSessionException e) { - LOG.debug("ExpiredSessionException", e); - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); - return getModelAndView(request, response, map,e); - } + /** + * session失效异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = ExpiredSessionException.class) + public ModelAndView handleExpiredSessionException(HttpServletRequest request, HttpServletResponse response, ExpiredSessionException e) { + LOG.debug("ExpiredSessionException", e); + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + return getModelAndView(request, response, map, e); + } - /** - * 返回异常信息处理 - * @param request - * @param response - * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 - * @param e - * @return - */ - private ModelAndView getModelAndView(HttpServletRequest request, HttpServletResponse response, Map map,Exception e) { - if(ObjectUtil.isNotNull(e)){ - LOG.debug("handleException", e); - StringWriter sw = new StringWriter(); - e.printStackTrace(new PrintWriter(sw, true)); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("errMsg", sw.toString()); - map.put("msg", e.getMessage()); - map.put("exc", e.getClass()); //详细异常信息 - } - //去掉异常信息中的跨站脚本 - map.put("msg",Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "",new Whitelist(), new Document.OutputSettings().prettyPrint(false))); - map.put("result",false); - if (BasicUtil.isAjaxRequest(request)) { - try { - response.setContentType("application/json;charset=UTF-8"); - PrintWriter writer = response.getWriter(); - writer.write(JSONObject.toJSONString(map)); - writer.flush(); - writer.close(); - } catch (IOException ex) { - ex.printStackTrace(); - } - } else { - return new ModelAndView("/error/index", map); - } - return null; - } + @ExceptionHandler(value = DcmsLoginException.class) + public ModelAndView handlDcmsLoginException(HttpServletRequest request, HttpServletResponse response, DcmsLoginException e) { + LOG.debug("DcmsLoginException", e); + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + map.put("msg", e.getBizMsg()); + return getErrorModelAndView(request, response, map, e); + } + /** + * 返回异常信息处理 + * @param request + * @param response + * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 + * @param e + * @return + */ + private ModelAndView getModelAndView(HttpServletRequest request, HttpServletResponse response, Map map, Exception e) { + if (ObjectUtil.isNotNull(e)) { + LOG.debug("handleException", e); + StringWriter sw = new StringWriter(); + e.printStackTrace(new PrintWriter(sw, true)); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("errMsg", sw.toString()); + map.put("msg", e.getMessage()); + map.put("exc", e.getClass()); //详细异常信息 + } + //去掉异常信息中的跨站脚本 + map.put("msg", Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "", new Whitelist(), new Document.OutputSettings().prettyPrint(false))); + map.put("result", false); + if (BasicUtil.isAjaxRequest(request)) { + try { + response.setContentType("application/json;charset=UTF-8"); + PrintWriter writer = response.getWriter(); + writer.write(JSONObject.toJSONString(map)); + writer.flush(); + writer.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } else { + return new ModelAndView("/error/index", map); + } + return null; + } + + /** + * 返回异常信息处理 + * @param request + * @param response + * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 + * @param e + * @return + */ + private ModelAndView getErrorModelAndView(HttpServletRequest request, HttpServletResponse response, Map map, Exception e) { + if (ObjectUtil.isNotNull(e)) { + LOG.debug("handleException", e); + StringWriter sw = new StringWriter(); + map.put("errMsg", sw.toString()); + e.printStackTrace(new PrintWriter(sw, true)); + //出错的类 + map.put("cls", e.getStackTrace()[0] + ""); + //请求地址 + map.put("url", request.getServletPath()); + //详细异常信息 + map.put("exc", e.getClass()); + } + //去掉异常信息中的跨站脚本 + map.put("msg", Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "", new Whitelist(), new Document.OutputSettings().prettyPrint(false))); + map.put("result", false); + if (BasicUtil.isAjaxRequest(request)) { + try { + PrintWriter writer = response.getWriter(); + response.setContentType("application/json;charset=UTF-8"); + + writer.write(JSONObject.toJSONString(map)); + writer.flush(); + writer.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } else { + return new ModelAndView("/error/dcmserror", map); + } + return null; + } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java b/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java index 04c26c8..9cb1d77 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java @@ -28,7 +28,7 @@ public void setHeader(HttpRequestBase base, String token) { base.addHeader("Content-type", DEFAULT_CONTEXTTYPE); if (!StringUtil.isBlank(token)) { - base.addHeader(DEFAULT_CHARSET, token); + base.addHeader(DEFAULT_TOKENKEY, token); } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java b/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java index 729a05b..1aa9358 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java @@ -7,6 +7,8 @@ import net.mingsoft.basic.handler.DefaultHttpHeaderHandler; import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * @ClassName Test @@ -19,7 +21,7 @@ public static void main(String[] args) { String url = "http://111.198.10.15:11409/user/info"; String token = "06d74157-14d6-4536-bf3a-e94d1d061a80"; - checkToken(token, url); + checkToken(url, token); } /** @@ -28,7 +30,7 @@ * @param token 颁发token * @return */ - public static ShiroUser checkToken(String token, String dcmsUrl) { + public static ShiroUser checkToken(String dcmsUrl, String token) { String json = HttpUtils.sendGet(dcmsUrl, new HashMap<>(), token, new DefaultHttpHeaderHandler()); if (!StringUtil.isBlank(json)) { System.out.println(json); @@ -40,4 +42,26 @@ } return null; } + + /** + * 转换为cms用户 + * + * @param user dmcs用户 + * @return + */ + public static String transformCmsUser(ShiroUser user, Map userRoles, List users) { + //是否有管理员 + for (String manager : userRoles.get("manager").split(",")) { + if (user.getRoleTips().contains(manager)) { + return users.get(0); + } + } + //返回普通用户 + for (String manager : userRoles.get("normal").split(",")) { + if (user.getRoleTips().contains(manager)) { + return users.get(1); + } + } + return null; + } } diff --git a/ms-basic/src/main/webapp/error/404.htm b/ms-basic/src/main/webapp/error/404.htm index 9b76382..bfc001f 100644 --- a/ms-basic/src/main/webapp/error/404.htm +++ b/ms-basic/src/main/webapp/error/404.htm @@ -40,4 +40,4 @@ - + \ No newline at end of file diff --git a/ms-mcms/pom.xml b/ms-mcms/pom.xml index 47d4474..f238b61 100644 --- a/ms-mcms/pom.xml +++ b/ms-mcms/pom.xml @@ -45,6 +45,7 @@ ms-mdiy 1.0.23-SNAPSHOT + net.mingsoft diff --git a/ms-mcms/src/main/java/net/mingsoft/config/ShiroConfig.java b/ms-mcms/src/main/java/net/mingsoft/config/ShiroConfig.java index a580427..fffc2c3 100644 --- a/ms-mcms/src/main/java/net/mingsoft/config/ShiroConfig.java +++ b/ms-mcms/src/main/java/net/mingsoft/config/ShiroConfig.java @@ -75,6 +75,8 @@ // 开放登陆接口 filterChainDefinitionMap.put(managerPath + "/login.do", "anon"); filterChainDefinitionMap.put(managerPath + "/checkLogin.do", "anon"); + filterChainDefinitionMap.put(managerPath + "/dcmsToken.do", "anon"); + filterChainDefinitionMap.put(managerPath + "/404.do", "anon"); // 其余接口一律拦截 // 主要这行代码必须放在所有权限设置的最后,不然会导致所有 url 都被拦截 filterChainDefinitionMap.put(managerPath + "/**", "authc"); diff --git a/ms-mcms/src/main/resources/application-dev.yml b/ms-mcms/src/main/resources/application-dev.yml index 7bd2b66..72449a9 100644 --- a/ms-mcms/src/main/resources/application-dev.yml +++ b/ms-mcms/src/main/resources/application-dev.yml @@ -1,7 +1,7 @@ spring: datasource: - url: jdbc:mysql://localhost:3306/mcms-dev-5.1-1?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai + url: jdbc:mysql://111.198.10.15:11102/db-mcms-open?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai username: root - password: root + password: Casic203! filters: wall,mergeStat type: com.alibaba.druid.pool.DruidDataSource diff --git a/ms-mcms/src/main/resources/application.yml b/ms-mcms/src/main/resources/application.yml index 7d6cfe4..b36275d 100644 --- a/ms-mcms/src/main/resources/application.yml +++ b/ms-mcms/src/main/resources/application.yml @@ -13,14 +13,17 @@ path: log #会在项目的根目录下生成log目录,里面会生成对应的日期目录,日期目录下面生成日志压缩包备份文件,默认按每10M分割一个日志文件,例如:log/2020-01/app-2020-01-03-18.1.log.gz(表示2020年1月3号下午六点的第一个备份),也可以根据实际情况写绝对路径,例如:d:/log ms: -# scheme: https #解决使用代理服务器代理应用时标签解析域名依旧为http的问题 + # scheme: https #解决使用代理服务器代理应用时标签解析域名依旧为http的问题 swagger: enable: true #启用swagger文档,生产的时候务必关掉 访问地址:http://ip|域名/项目发布名/swagger-ui.html manager: path: /ms #后台访问的路径,如:http://项目/ms/login.do,生产的时候建议修改 view-path: /WEB-INF/manager #后台视图层路径配置 check-code: false #默认开启验证码验证,false验证码不验证 - + dcmsHost: http://111.198.10.15:11409 #token获取地址 + userInfoPath: /user/info #用户信息获取路径 + userRoles: '{"manager":"administrator","normal":"456"}' #用户对应角色关系 + users: msopen,normal upload: template: template #模板文件夹支持重命名,不支持路径 path: upload #文件上传路径,可以根据实际写绝对路径 diff --git a/ms-base/pom.xml b/ms-base/pom.xml index a5dcef6..af71af1 100644 --- a/ms-base/pom.xml +++ b/ms-base/pom.xml @@ -363,26 +363,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-base diff --git a/ms-basic/ms-basic.iml b/ms-basic/ms-basic.iml index 935ebc4..5a2e74f 100644 --- a/ms-basic/ms-basic.iml +++ b/ms-basic/ms-basic.iml @@ -11,8 +11,8 @@ - - + + diff --git a/ms-basic/pom.xml b/ms-basic/pom.xml index e66ec4f..90ba669 100644 --- a/ms-basic/pom.xml +++ b/ms-basic/pom.xml @@ -88,26 +88,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-basic diff --git a/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java b/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java index 540c752..30a601c 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java @@ -1,16 +1,16 @@ /** * The MIT License (MIT) * Copyright (c) 2020 铭软科技(mingsoft.net) - + *

* Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: - + *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - + *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR @@ -30,20 +30,25 @@ import io.swagger.annotations.ApiOperation; import net.mingsoft.base.entity.ResultData; import net.mingsoft.basic.action.BaseAction; +import net.mingsoft.basic.bean.ShiroUser; import net.mingsoft.basic.biz.IAppBiz; import net.mingsoft.basic.biz.IManagerBiz; import net.mingsoft.basic.biz.IRoleBiz; import net.mingsoft.basic.constant.Const; +import net.mingsoft.basic.constant.ErrorCodeEnum; import net.mingsoft.basic.constant.e.SessionConstEnum; import net.mingsoft.basic.entity.AppEntity; import net.mingsoft.basic.entity.ManagerEntity; import net.mingsoft.basic.entity.ManagerSessionEntity; import net.mingsoft.basic.entity.RoleEntity; +import net.mingsoft.basic.exception.DcmsLoginException; import net.mingsoft.basic.util.BasicUtil; +import net.mingsoft.basic.util.DcmsUtils; import net.mingsoft.basic.util.StringUtil; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.UsernamePasswordToken; +import org.apache.shiro.session.ExpiredSessionException; import org.apache.shiro.subject.Subject; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -56,14 +61,13 @@ import javax.servlet.http.HttpServletResponse; import java.io.File; import java.util.List; +import java.util.Map; /** - * - * @ClassName: LoginAction + * @ClassName: LoginAction * @Description:TODO(登录的基础应用层) * @author: 铭飞开发团队 - * @date: 2015年1月27日 下午3:21:47 - * + * @date: 2015年1月27日 下午3:21:47 * @Copyright: 2018 www.mingsoft.net Inc. All rights reserved. */ @Api("登录的基础应用层接口") @@ -71,146 +75,228 @@ @RequestMapping("/${ms.manager.path}") public class LoginAction extends BaseAction { - @Value("${ms.manager.path}") - private String managerPath; - /** - * 管理员业务层 - */ - @Autowired - private IManagerBiz managerBiz; + @Value("${ms.manager.path}") + private String managerPath; + @Value("${ms.manager.dcmsHost}") + private String dcmsHost; + @Value("${ms.manager.userInfoPath}") + private String userInfoPath; + @Value("#{${ms.manager.userRoles}}") + private Map userRoles; + @Value("#{'${ms.manager.users}'.split(',')}") + private List users; + /** + * 管理员业务层 + */ + @Autowired + private IManagerBiz managerBiz; - /** - * 角色业务request层 - */ - @Autowired - private IRoleBiz roleBiz; + /** + * 角色业务request层 + */ + @Autowired + private IRoleBiz roleBiz; - /** - * 站点业务层 - */ - @Autowired - private IAppBiz appBiz; + /** + * 站点业务层 + */ + @Autowired + private IAppBiz appBiz; + /** + * 加载管理员登录界面 + * + * @param request 请求对象 + * @return 管理员登录界面地址 + */ + @ApiOperation(value = "加载管理员登录界面") + @SuppressWarnings("resource") + @GetMapping("/dcmsToken") + public String dcmsToken(HttpServletRequest request) { + if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION) != null) { + return "redirect:" + managerPath + "/index.do"; + } + //请求dcms数据token + ShiroUser shiroUser = DcmsUtils.checkToken(dcmsHost + userInfoPath, request.getParameter("token")); + if (shiroUser == null) { + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无效token"); + } + String userName = DcmsUtils.transformCmsUser(shiroUser, userRoles, users); + if (userName == null) { + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无效token"); + } - /** - * 加载管理员登录界面 - * - * @param request - * 请求对象 - * @return 管理员登录界面地址 - */ - @ApiOperation(value = "加载管理员登录界面") - @SuppressWarnings("resource") - @GetMapping("/login") - public String login(HttpServletRequest request) { - if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION)!=null) { - return "redirect:"+managerPath+"/index.do"; - } - // 根据请求地址来显示标题 - AppEntity app = BasicUtil.getApp(); - // 判断应用实体是否存在 - if (app != null) { - // 检测应用是否有自定义界面b - if (!StringUtil.isBlank(app.getAppLoginPage())) { - LOG.debug("跳转自定义登录界面"); - return "redirect:" + app.getAppLoginPage(); - } + AppEntity app = BasicUtil.getApp(); + // 判断应用实体是否存在 + if (app != null) { + // 检测应用是否有自定义界面b + if (!StringUtil.isBlank(app.getAppLoginPage())) { + LOG.debug("跳转自定义登录界面"); + return "redirect:" + app.getAppLoginPage(); + } - } else { - File file = new File(BasicUtil.getRealPath( "WEB-INF/ms.install")); - //存在安装文件 - if (file.exists()) { - String defaultId = FileReader.create(new File(BasicUtil.getRealPath( "WEB-INF/ms.install"))).readString(); - if (!StringUtils.isEmpty(defaultId)) { - app = (AppEntity) appBiz.getEntity(Integer.parseInt(defaultId)); - app.setAppUrl(this.getUrl(request)); - appBiz.updateEntity(app); - FileWriter.create(new File(BasicUtil.getRealPath( "WEB-INF/ms.install.bak"))).write(defaultId); - file.delete(); - } - } + } + AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); + ManagerEntity newManager = new ManagerEntity(); + newManager.setManagerName(userName); + ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + // 创建管理员session对象 + ManagerSessionEntity managerSession = new ManagerSessionEntity(); + AppEntity website = new AppEntity(); + // 获取管理员所在的角色 + RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); + website = (AppEntity) appBiz.getEntity(role.getAppId()); + // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 + if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId() == BasicUtil.getAppId())) { + if (website == null) { + website = BasicUtil.getApp(); + } + List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); + managerSession.setStyle(website.getAppStyle()); + managerSession.setBasicId(website.getAppId()); + managerSession.setManagerParentID(role.getRoleManagerId()); + managerSession.setManagerChildIDs(childManagerList); + // 压入管理员seesion + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } else { + if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { + LOG.debug("roleId: " + _manager.getManagerRoleID()); + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无授权"); + } else { + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } + } + BeanUtils.copyProperties(_manager, managerSession); + Subject subject = SecurityUtils.getSubject(); + UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(), managerSession.getManagerPassword()); + subject.login(upt); + return "redirect:" + managerPath + "/index.do"; + } - } - request.setAttribute("app", app); - return "/login"; - } + @GetMapping("/404") + public String error(HttpServletRequest request) { + return "/404"; + } - /** - * 验证登录 - * - * @param manager - * 管理员实体 - * @param request - * 请求 - * @param response - * 响应 - */ - @ApiOperation(value = "验证登录") - @ApiImplicitParams({ - @ApiImplicitParam(name = "managerName", value = "帐号", required = true,paramType="query"), - @ApiImplicitParam(name = "managerPassword", value = "密码", required = true,paramType="query"), - }) - @PostMapping(value="/checkLogin") - @ResponseBody - public ResultData checkLogin(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletRequest request, - HttpServletResponse response) { - AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); // 根据url地址获取站点信息,主要是区分管理员对那些网站有权限 - if (urlWebsite == null) { - return ResultData.build().error( this.getResString("err.not.exist",this.getResString("app"),"!请尝试去文件 WEB-INF/ms.install.bak 后缀bak")); - } - //验证码 - if (!(checkRandCode())) { - return ResultData.build().error(getResString("err.error", new String[] { getResString("rand.code") })); - } + /** + * 加载管理员登录界面 + * + * @param request 请求对象 + * @return 管理员登录界面地址 + */ + @ApiOperation(value = "加载管理员登录界面") + @SuppressWarnings("resource") + @GetMapping("/login") + public String login(HttpServletRequest request) { + if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION) != null) { + return "redirect:" + managerPath + "/index.do"; + } + // 根据请求地址来显示标题 + AppEntity app = BasicUtil.getApp(); + // 判断应用实体是否存在 + if (app != null) { + // 检测应用是否有自定义界面b + if (!StringUtil.isBlank(app.getAppLoginPage())) { + LOG.debug("跳转自定义登录界面"); + return "redirect:" + app.getAppLoginPage(); + } - // 根据账号获取当前管理员信息 - ManagerEntity newManager = new ManagerEntity(); - newManager.setManagerName(manager.getManagerName()); - ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + } else { + File file = new File(BasicUtil.getRealPath("WEB-INF/ms.install")); + //存在安装文件 + if (file.exists()) { + String defaultId = FileReader.create(new File(BasicUtil.getRealPath("WEB-INF/ms.install"))).readString(); + if (!StringUtils.isEmpty(defaultId)) { + app = (AppEntity) appBiz.getEntity(Integer.parseInt(defaultId)); + app.setAppUrl(this.getUrl(request)); + appBiz.updateEntity(app); + FileWriter.create(new File(BasicUtil.getRealPath("WEB-INF/ms.install.bak"))).write(defaultId); + file.delete(); + } + } - if (_manager == null || StringUtils.isEmpty(manager.getManagerName())) { - // 系统不存在此用户 - return ResultData.build().error( this.getResString("err.nameEmpty")); - } else { - // 判断当前用户输入的密码是否正确 - if (SecureUtil.md5(manager.getManagerPassword()).equals(_manager.getManagerPassword())) { - // 创建管理员session对象 - ManagerSessionEntity managerSession = new ManagerSessionEntity(); - AppEntity website = new AppEntity(); - // 获取管理员所在的角色 - RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); - website = (AppEntity) appBiz.getEntity(role.getAppId()); - // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 - if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId()==BasicUtil.getAppId())) { - if(website==null){ - website = BasicUtil.getApp(); - } - List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); - managerSession.setBasicId(website.getAppId()); - managerSession.setManagerParentID(role.getRoleManagerId()); - managerSession.setManagerChildIDs(childManagerList); - managerSession.setStyle(website.getAppStyle()); - // 压入管理员seesion - BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); - } else { - if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { - LOG.debug("roleId: "+_manager.getManagerRoleID()); - return ResultData.build().error( this.getResString("err.not.exist",this.getResString("manager"))); - } else { - BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); - } - } - BeanUtils.copyProperties(_manager, managerSession); + } + request.setAttribute("app", app); + return "/login"; + } - Subject subject = SecurityUtils.getSubject(); - UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(),managerSession.getManagerPassword()); - subject.login(upt); - return ResultData.build().success(); - } else { - // 密码错误 - return ResultData.build().error( this.getResString("err.password")); - } - } - } + /** + * 验证登录 + * + * @param manager 管理员实体 + * @param request 请求 + * @param response 响应 + */ + @ApiOperation(value = "验证登录") + @ApiImplicitParams({ + @ApiImplicitParam(name = "managerName", value = "帐号", required = true, paramType = "query"), + @ApiImplicitParam(name = "managerPassword", value = "密码", required = true, paramType = "query"), + }) + @PostMapping(value = "/checkLogin") + @ResponseBody + public ResultData checkLogin(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletRequest request, + HttpServletResponse response) { + // 根据url地址获取站点信息,主要是区分管理员对那些网站有权限 + AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); + if (urlWebsite == null) { + return ResultData.build().error(this.getResString("err.not.exist", this.getResString("app"), "!请尝试去文件 WEB-INF/ms.install.bak 后缀bak")); + } + //验证码 + if (!(checkRandCode())) { + return ResultData.build().error(getResString("err.error", new String[]{getResString("rand.code")})); + } + + // 根据账号获取当前管理员信息 + ManagerEntity newManager = new ManagerEntity(); + newManager.setManagerName(manager.getManagerName()); + ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + + if (_manager == null || StringUtils.isEmpty(manager.getManagerName())) { + // 系统不存在此用户 + return ResultData.build().error(this.getResString("err.nameEmpty")); + } else { + // 判断当前用户输入的密码是否正确 + if (SecureUtil.md5(manager.getManagerPassword()).equals(_manager.getManagerPassword())) { + // 创建管理员session对象 + ManagerSessionEntity managerSession = new ManagerSessionEntity(); + AppEntity website = new AppEntity(); + // 获取管理员所在的角色 + RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); + website = (AppEntity) appBiz.getEntity(role.getAppId()); + // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 + if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId() == BasicUtil.getAppId())) { + if (website == null) { + website = BasicUtil.getApp(); + } + List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); + managerSession.setBasicId(website.getAppId()); + managerSession.setManagerParentID(role.getRoleManagerId()); + managerSession.setManagerChildIDs(childManagerList); + managerSession.setStyle(website.getAppStyle()); + // 压入管理员seesion + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } else { + if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { + LOG.debug("roleId: " + _manager.getManagerRoleID()); + return ResultData.build().error(this.getResString("err.not.exist", this.getResString("manager"))); + } else { + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } + } + BeanUtils.copyProperties(_manager, managerSession); + + Subject subject = SecurityUtils.getSubject(); + UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(), managerSession.getManagerPassword()); + subject.login(upt); + return ResultData.build().success(); + } else { + // 密码错误 + return ResultData.build().error(this.getResString("err.password")); + } + } + } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java b/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java index 726a50c..f0e6377 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java @@ -1,16 +1,16 @@ /** * The MIT License (MIT) * Copyright (c) 2020 铭软科技(mingsoft.net) - + *

* Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: - + *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - + *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR @@ -61,214 +61,263 @@ */ @ControllerAdvice public class GlobalExceptionResolver extends DefaultHandlerExceptionResolver { - - protected final Logger LOG = LoggerFactory.getLogger(this.getClass()); - /** - * 全局异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = Exception.class) - public ModelAndView handleException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.SERVER_ERROR); - return getModelAndView(request, response, map,e); - } + protected final Logger LOG = LoggerFactory.getLogger(this.getClass()); - /** - * 全局异常 未找到类404 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = NoHandlerFoundException.class) - public ModelAndView handleNoHandlerFoundException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 全局异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = Exception.class) + public ModelAndView handleException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.SERVER_ERROR); + return getModelAndView(request, response, map, e); + } - /** - * 请求参数异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = MissingServletRequestParameterException.class) - public ModelAndView handleMissingServletRequestParameterException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 全局异常 未找到类404 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = NoHandlerFoundException.class) + public ModelAndView handleNoHandlerFoundException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 请求方法类型错误 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) - public ModelAndView handleHttpRequestMethodNotSupportedException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 请求参数异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = MissingServletRequestParameterException.class) + public ModelAndView handleMissingServletRequestParameterException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 统一处理请求参数校验(实体对象传参) - * - * @param e BindException - * @return ResultResponse - */ - @ExceptionHandler(BindException.class) - public ModelAndView validExceptionHandler(HttpServletRequest request, HttpServletResponse response,BindException e) { - StringBuilder message = new StringBuilder(); - List fieldErrors = e.getBindingResult().getFieldErrors(); - for (FieldError error : fieldErrors) { - message.append(error.getField()).append(error.getDefaultMessage()).append(","); - } - message = new StringBuilder(message.substring(0, message.length() - 1)); - Map map = new HashMap(); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("code", ErrorCodeEnum.CLIENT_REQUEST); - map.put("result",false); - map.put("msg", message.toString()); - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * 请求方法类型错误 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) + public ModelAndView handleHttpRequestMethodNotSupportedException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 统一处理请求参数校验(普通传参) - * - * @param e ConstraintViolationException - * @return ResultResponse - */ - @ExceptionHandler(value = ConstraintViolationException.class) - public ModelAndView handleConstraintViolationException(HttpServletRequest request, HttpServletResponse response,ConstraintViolationException e) { - StringBuilder message = new StringBuilder(); - Set> violations = e.getConstraintViolations(); - for (ConstraintViolation violation : violations) { - Path path = violation.getPropertyPath(); - String[] pathArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(path.toString(), "."); - message.append(pathArr[1]).append(violation.getMessage()).append(","); - } - message = new StringBuilder(message.substring(0, message.length() - 1)); - Map map = new HashMap(); - map.put("msg", message.toString()); - map.put("code", ErrorCodeEnum.CLIENT_REQUEST); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 + /** + * 统一处理请求参数校验(实体对象传参) + * + * @param e BindException + * @return ResultResponse + */ + @ExceptionHandler(BindException.class) + public ModelAndView validExceptionHandler(HttpServletRequest request, HttpServletResponse response, BindException e) { + StringBuilder message = new StringBuilder(); + List fieldErrors = e.getBindingResult().getFieldErrors(); + for (FieldError error : fieldErrors) { + message.append(error.getField()).append(error.getDefaultMessage()).append(","); + } + message = new StringBuilder(message.substring(0, message.length() - 1)); + Map map = new HashMap(); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("code", ErrorCodeEnum.CLIENT_REQUEST); + map.put("result", false); + map.put("msg", message.toString()); + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * 统一处理请求参数校验(普通传参) + * + * @param e ConstraintViolationException + * @return ResultResponse + */ + @ExceptionHandler(value = ConstraintViolationException.class) + public ModelAndView handleConstraintViolationException(HttpServletRequest request, HttpServletResponse response, ConstraintViolationException e) { + StringBuilder message = new StringBuilder(); + Set> violations = e.getConstraintViolations(); + for (ConstraintViolation violation : violations) { + Path path = violation.getPropertyPath(); + String[] pathArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(path.toString(), "."); + message.append(pathArr[1]).append(violation.getMessage()).append(","); + } + message = new StringBuilder(message.substring(0, message.length() - 1)); + Map map = new HashMap(); + map.put("msg", message.toString()); + map.put("code", ErrorCodeEnum.CLIENT_REQUEST); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - /** - * shiro权限未授权异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = UnauthorizedException.class) - public ModelAndView handleUnauthorizedException(HttpServletRequest request, HttpServletResponse response,UnauthorizedException e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); - return getModelAndView(request, response, map,e); - } + /** + * shiro权限未授权异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = UnauthorizedException.class) + public ModelAndView handleUnauthorizedException(HttpServletRequest request, HttpServletResponse response, UnauthorizedException e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + return getModelAndView(request, response, map, e); + } - /** - * 登录异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = AuthenticationException.class) - public ModelAndView handleAuthenticationException(HttpServletRequest request, HttpServletResponse response,AuthenticationException e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 登录异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = AuthenticationException.class) + public ModelAndView handleAuthenticationException(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * shiro权限错误 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = AuthorizationException.class) - public ModelAndView handleAuthorizationException(HttpServletRequest request, HttpServletResponse response,AuthorizationException e){ - LOG.debug("AuthorizationException", e); - Map map = new HashMap(); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - map.put("msg", e.getStackTrace()); - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * shiro权限错误 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = AuthorizationException.class) + public ModelAndView handleAuthorizationException(HttpServletRequest request, HttpServletResponse response, AuthorizationException e) { + LOG.debug("AuthorizationException", e); + Map map = new HashMap(); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + map.put("msg", e.getStackTrace()); + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - /** - * session失效异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = ExpiredSessionException.class) - public ModelAndView handleExpiredSessionException(HttpServletRequest request, HttpServletResponse response,ExpiredSessionException e) { - LOG.debug("ExpiredSessionException", e); - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); - return getModelAndView(request, response, map,e); - } + /** + * session失效异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = ExpiredSessionException.class) + public ModelAndView handleExpiredSessionException(HttpServletRequest request, HttpServletResponse response, ExpiredSessionException e) { + LOG.debug("ExpiredSessionException", e); + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + return getModelAndView(request, response, map, e); + } - /** - * 返回异常信息处理 - * @param request - * @param response - * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 - * @param e - * @return - */ - private ModelAndView getModelAndView(HttpServletRequest request, HttpServletResponse response, Map map,Exception e) { - if(ObjectUtil.isNotNull(e)){ - LOG.debug("handleException", e); - StringWriter sw = new StringWriter(); - e.printStackTrace(new PrintWriter(sw, true)); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("errMsg", sw.toString()); - map.put("msg", e.getMessage()); - map.put("exc", e.getClass()); //详细异常信息 - } - //去掉异常信息中的跨站脚本 - map.put("msg",Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "",new Whitelist(), new Document.OutputSettings().prettyPrint(false))); - map.put("result",false); - if (BasicUtil.isAjaxRequest(request)) { - try { - response.setContentType("application/json;charset=UTF-8"); - PrintWriter writer = response.getWriter(); - writer.write(JSONObject.toJSONString(map)); - writer.flush(); - writer.close(); - } catch (IOException ex) { - ex.printStackTrace(); - } - } else { - return new ModelAndView("/error/index", map); - } - return null; - } + @ExceptionHandler(value = DcmsLoginException.class) + public ModelAndView handlDcmsLoginException(HttpServletRequest request, HttpServletResponse response, DcmsLoginException e) { + LOG.debug("DcmsLoginException", e); + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + map.put("msg", e.getBizMsg()); + return getErrorModelAndView(request, response, map, e); + } + /** + * 返回异常信息处理 + * @param request + * @param response + * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 + * @param e + * @return + */ + private ModelAndView getModelAndView(HttpServletRequest request, HttpServletResponse response, Map map, Exception e) { + if (ObjectUtil.isNotNull(e)) { + LOG.debug("handleException", e); + StringWriter sw = new StringWriter(); + e.printStackTrace(new PrintWriter(sw, true)); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("errMsg", sw.toString()); + map.put("msg", e.getMessage()); + map.put("exc", e.getClass()); //详细异常信息 + } + //去掉异常信息中的跨站脚本 + map.put("msg", Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "", new Whitelist(), new Document.OutputSettings().prettyPrint(false))); + map.put("result", false); + if (BasicUtil.isAjaxRequest(request)) { + try { + response.setContentType("application/json;charset=UTF-8"); + PrintWriter writer = response.getWriter(); + writer.write(JSONObject.toJSONString(map)); + writer.flush(); + writer.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } else { + return new ModelAndView("/error/index", map); + } + return null; + } + + /** + * 返回异常信息处理 + * @param request + * @param response + * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 + * @param e + * @return + */ + private ModelAndView getErrorModelAndView(HttpServletRequest request, HttpServletResponse response, Map map, Exception e) { + if (ObjectUtil.isNotNull(e)) { + LOG.debug("handleException", e); + StringWriter sw = new StringWriter(); + map.put("errMsg", sw.toString()); + e.printStackTrace(new PrintWriter(sw, true)); + //出错的类 + map.put("cls", e.getStackTrace()[0] + ""); + //请求地址 + map.put("url", request.getServletPath()); + //详细异常信息 + map.put("exc", e.getClass()); + } + //去掉异常信息中的跨站脚本 + map.put("msg", Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "", new Whitelist(), new Document.OutputSettings().prettyPrint(false))); + map.put("result", false); + if (BasicUtil.isAjaxRequest(request)) { + try { + PrintWriter writer = response.getWriter(); + response.setContentType("application/json;charset=UTF-8"); + + writer.write(JSONObject.toJSONString(map)); + writer.flush(); + writer.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } else { + return new ModelAndView("/error/dcmserror", map); + } + return null; + } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java b/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java index 04c26c8..9cb1d77 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java @@ -28,7 +28,7 @@ public void setHeader(HttpRequestBase base, String token) { base.addHeader("Content-type", DEFAULT_CONTEXTTYPE); if (!StringUtil.isBlank(token)) { - base.addHeader(DEFAULT_CHARSET, token); + base.addHeader(DEFAULT_TOKENKEY, token); } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java b/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java index 729a05b..1aa9358 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java @@ -7,6 +7,8 @@ import net.mingsoft.basic.handler.DefaultHttpHeaderHandler; import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * @ClassName Test @@ -19,7 +21,7 @@ public static void main(String[] args) { String url = "http://111.198.10.15:11409/user/info"; String token = "06d74157-14d6-4536-bf3a-e94d1d061a80"; - checkToken(token, url); + checkToken(url, token); } /** @@ -28,7 +30,7 @@ * @param token 颁发token * @return */ - public static ShiroUser checkToken(String token, String dcmsUrl) { + public static ShiroUser checkToken(String dcmsUrl, String token) { String json = HttpUtils.sendGet(dcmsUrl, new HashMap<>(), token, new DefaultHttpHeaderHandler()); if (!StringUtil.isBlank(json)) { System.out.println(json); @@ -40,4 +42,26 @@ } return null; } + + /** + * 转换为cms用户 + * + * @param user dmcs用户 + * @return + */ + public static String transformCmsUser(ShiroUser user, Map userRoles, List users) { + //是否有管理员 + for (String manager : userRoles.get("manager").split(",")) { + if (user.getRoleTips().contains(manager)) { + return users.get(0); + } + } + //返回普通用户 + for (String manager : userRoles.get("normal").split(",")) { + if (user.getRoleTips().contains(manager)) { + return users.get(1); + } + } + return null; + } } diff --git a/ms-basic/src/main/webapp/error/404.htm b/ms-basic/src/main/webapp/error/404.htm index 9b76382..bfc001f 100644 --- a/ms-basic/src/main/webapp/error/404.htm +++ b/ms-basic/src/main/webapp/error/404.htm @@ -40,4 +40,4 @@ - + \ No newline at end of file diff --git a/ms-mcms/pom.xml b/ms-mcms/pom.xml index 47d4474..f238b61 100644 --- a/ms-mcms/pom.xml +++ b/ms-mcms/pom.xml @@ -45,6 +45,7 @@ ms-mdiy 1.0.23-SNAPSHOT + net.mingsoft diff --git a/ms-mcms/src/main/java/net/mingsoft/config/ShiroConfig.java b/ms-mcms/src/main/java/net/mingsoft/config/ShiroConfig.java index a580427..fffc2c3 100644 --- a/ms-mcms/src/main/java/net/mingsoft/config/ShiroConfig.java +++ b/ms-mcms/src/main/java/net/mingsoft/config/ShiroConfig.java @@ -75,6 +75,8 @@ // 开放登陆接口 filterChainDefinitionMap.put(managerPath + "/login.do", "anon"); filterChainDefinitionMap.put(managerPath + "/checkLogin.do", "anon"); + filterChainDefinitionMap.put(managerPath + "/dcmsToken.do", "anon"); + filterChainDefinitionMap.put(managerPath + "/404.do", "anon"); // 其余接口一律拦截 // 主要这行代码必须放在所有权限设置的最后,不然会导致所有 url 都被拦截 filterChainDefinitionMap.put(managerPath + "/**", "authc"); diff --git a/ms-mcms/src/main/resources/application-dev.yml b/ms-mcms/src/main/resources/application-dev.yml index 7bd2b66..72449a9 100644 --- a/ms-mcms/src/main/resources/application-dev.yml +++ b/ms-mcms/src/main/resources/application-dev.yml @@ -1,7 +1,7 @@ spring: datasource: - url: jdbc:mysql://localhost:3306/mcms-dev-5.1-1?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai + url: jdbc:mysql://111.198.10.15:11102/db-mcms-open?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai username: root - password: root + password: Casic203! filters: wall,mergeStat type: com.alibaba.druid.pool.DruidDataSource diff --git a/ms-mcms/src/main/resources/application.yml b/ms-mcms/src/main/resources/application.yml index 7d6cfe4..b36275d 100644 --- a/ms-mcms/src/main/resources/application.yml +++ b/ms-mcms/src/main/resources/application.yml @@ -13,14 +13,17 @@ path: log #会在项目的根目录下生成log目录,里面会生成对应的日期目录,日期目录下面生成日志压缩包备份文件,默认按每10M分割一个日志文件,例如:log/2020-01/app-2020-01-03-18.1.log.gz(表示2020年1月3号下午六点的第一个备份),也可以根据实际情况写绝对路径,例如:d:/log ms: -# scheme: https #解决使用代理服务器代理应用时标签解析域名依旧为http的问题 + # scheme: https #解决使用代理服务器代理应用时标签解析域名依旧为http的问题 swagger: enable: true #启用swagger文档,生产的时候务必关掉 访问地址:http://ip|域名/项目发布名/swagger-ui.html manager: path: /ms #后台访问的路径,如:http://项目/ms/login.do,生产的时候建议修改 view-path: /WEB-INF/manager #后台视图层路径配置 check-code: false #默认开启验证码验证,false验证码不验证 - + dcmsHost: http://111.198.10.15:11409 #token获取地址 + userInfoPath: /user/info #用户信息获取路径 + userRoles: '{"manager":"administrator","normal":"456"}' #用户对应角色关系 + users: msopen,normal upload: template: template #模板文件夹支持重命名,不支持路径 path: upload #文件上传路径,可以根据实际写绝对路径 diff --git a/ms-mcms/src/main/webapp/templets/1/company1807/images/1506736177.jpg b/ms-mcms/src/main/webapp/templets/1/company1807/images/1506736177.jpg deleted file mode 100644 index df1c2e2..0000000 --- a/ms-mcms/src/main/webapp/templets/1/company1807/images/1506736177.jpg +++ /dev/null Binary files differ diff --git a/ms-base/pom.xml b/ms-base/pom.xml index a5dcef6..af71af1 100644 --- a/ms-base/pom.xml +++ b/ms-base/pom.xml @@ -363,26 +363,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-base diff --git a/ms-basic/ms-basic.iml b/ms-basic/ms-basic.iml index 935ebc4..5a2e74f 100644 --- a/ms-basic/ms-basic.iml +++ b/ms-basic/ms-basic.iml @@ -11,8 +11,8 @@ - - + + diff --git a/ms-basic/pom.xml b/ms-basic/pom.xml index e66ec4f..90ba669 100644 --- a/ms-basic/pom.xml +++ b/ms-basic/pom.xml @@ -88,26 +88,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-basic diff --git a/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java b/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java index 540c752..30a601c 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java @@ -1,16 +1,16 @@ /** * The MIT License (MIT) * Copyright (c) 2020 铭软科技(mingsoft.net) - + *

* Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: - + *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - + *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR @@ -30,20 +30,25 @@ import io.swagger.annotations.ApiOperation; import net.mingsoft.base.entity.ResultData; import net.mingsoft.basic.action.BaseAction; +import net.mingsoft.basic.bean.ShiroUser; import net.mingsoft.basic.biz.IAppBiz; import net.mingsoft.basic.biz.IManagerBiz; import net.mingsoft.basic.biz.IRoleBiz; import net.mingsoft.basic.constant.Const; +import net.mingsoft.basic.constant.ErrorCodeEnum; import net.mingsoft.basic.constant.e.SessionConstEnum; import net.mingsoft.basic.entity.AppEntity; import net.mingsoft.basic.entity.ManagerEntity; import net.mingsoft.basic.entity.ManagerSessionEntity; import net.mingsoft.basic.entity.RoleEntity; +import net.mingsoft.basic.exception.DcmsLoginException; import net.mingsoft.basic.util.BasicUtil; +import net.mingsoft.basic.util.DcmsUtils; import net.mingsoft.basic.util.StringUtil; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.UsernamePasswordToken; +import org.apache.shiro.session.ExpiredSessionException; import org.apache.shiro.subject.Subject; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -56,14 +61,13 @@ import javax.servlet.http.HttpServletResponse; import java.io.File; import java.util.List; +import java.util.Map; /** - * - * @ClassName: LoginAction + * @ClassName: LoginAction * @Description:TODO(登录的基础应用层) * @author: 铭飞开发团队 - * @date: 2015年1月27日 下午3:21:47 - * + * @date: 2015年1月27日 下午3:21:47 * @Copyright: 2018 www.mingsoft.net Inc. All rights reserved. */ @Api("登录的基础应用层接口") @@ -71,146 +75,228 @@ @RequestMapping("/${ms.manager.path}") public class LoginAction extends BaseAction { - @Value("${ms.manager.path}") - private String managerPath; - /** - * 管理员业务层 - */ - @Autowired - private IManagerBiz managerBiz; + @Value("${ms.manager.path}") + private String managerPath; + @Value("${ms.manager.dcmsHost}") + private String dcmsHost; + @Value("${ms.manager.userInfoPath}") + private String userInfoPath; + @Value("#{${ms.manager.userRoles}}") + private Map userRoles; + @Value("#{'${ms.manager.users}'.split(',')}") + private List users; + /** + * 管理员业务层 + */ + @Autowired + private IManagerBiz managerBiz; - /** - * 角色业务request层 - */ - @Autowired - private IRoleBiz roleBiz; + /** + * 角色业务request层 + */ + @Autowired + private IRoleBiz roleBiz; - /** - * 站点业务层 - */ - @Autowired - private IAppBiz appBiz; + /** + * 站点业务层 + */ + @Autowired + private IAppBiz appBiz; + /** + * 加载管理员登录界面 + * + * @param request 请求对象 + * @return 管理员登录界面地址 + */ + @ApiOperation(value = "加载管理员登录界面") + @SuppressWarnings("resource") + @GetMapping("/dcmsToken") + public String dcmsToken(HttpServletRequest request) { + if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION) != null) { + return "redirect:" + managerPath + "/index.do"; + } + //请求dcms数据token + ShiroUser shiroUser = DcmsUtils.checkToken(dcmsHost + userInfoPath, request.getParameter("token")); + if (shiroUser == null) { + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无效token"); + } + String userName = DcmsUtils.transformCmsUser(shiroUser, userRoles, users); + if (userName == null) { + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无效token"); + } - /** - * 加载管理员登录界面 - * - * @param request - * 请求对象 - * @return 管理员登录界面地址 - */ - @ApiOperation(value = "加载管理员登录界面") - @SuppressWarnings("resource") - @GetMapping("/login") - public String login(HttpServletRequest request) { - if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION)!=null) { - return "redirect:"+managerPath+"/index.do"; - } - // 根据请求地址来显示标题 - AppEntity app = BasicUtil.getApp(); - // 判断应用实体是否存在 - if (app != null) { - // 检测应用是否有自定义界面b - if (!StringUtil.isBlank(app.getAppLoginPage())) { - LOG.debug("跳转自定义登录界面"); - return "redirect:" + app.getAppLoginPage(); - } + AppEntity app = BasicUtil.getApp(); + // 判断应用实体是否存在 + if (app != null) { + // 检测应用是否有自定义界面b + if (!StringUtil.isBlank(app.getAppLoginPage())) { + LOG.debug("跳转自定义登录界面"); + return "redirect:" + app.getAppLoginPage(); + } - } else { - File file = new File(BasicUtil.getRealPath( "WEB-INF/ms.install")); - //存在安装文件 - if (file.exists()) { - String defaultId = FileReader.create(new File(BasicUtil.getRealPath( "WEB-INF/ms.install"))).readString(); - if (!StringUtils.isEmpty(defaultId)) { - app = (AppEntity) appBiz.getEntity(Integer.parseInt(defaultId)); - app.setAppUrl(this.getUrl(request)); - appBiz.updateEntity(app); - FileWriter.create(new File(BasicUtil.getRealPath( "WEB-INF/ms.install.bak"))).write(defaultId); - file.delete(); - } - } + } + AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); + ManagerEntity newManager = new ManagerEntity(); + newManager.setManagerName(userName); + ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + // 创建管理员session对象 + ManagerSessionEntity managerSession = new ManagerSessionEntity(); + AppEntity website = new AppEntity(); + // 获取管理员所在的角色 + RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); + website = (AppEntity) appBiz.getEntity(role.getAppId()); + // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 + if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId() == BasicUtil.getAppId())) { + if (website == null) { + website = BasicUtil.getApp(); + } + List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); + managerSession.setStyle(website.getAppStyle()); + managerSession.setBasicId(website.getAppId()); + managerSession.setManagerParentID(role.getRoleManagerId()); + managerSession.setManagerChildIDs(childManagerList); + // 压入管理员seesion + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } else { + if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { + LOG.debug("roleId: " + _manager.getManagerRoleID()); + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无授权"); + } else { + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } + } + BeanUtils.copyProperties(_manager, managerSession); + Subject subject = SecurityUtils.getSubject(); + UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(), managerSession.getManagerPassword()); + subject.login(upt); + return "redirect:" + managerPath + "/index.do"; + } - } - request.setAttribute("app", app); - return "/login"; - } + @GetMapping("/404") + public String error(HttpServletRequest request) { + return "/404"; + } - /** - * 验证登录 - * - * @param manager - * 管理员实体 - * @param request - * 请求 - * @param response - * 响应 - */ - @ApiOperation(value = "验证登录") - @ApiImplicitParams({ - @ApiImplicitParam(name = "managerName", value = "帐号", required = true,paramType="query"), - @ApiImplicitParam(name = "managerPassword", value = "密码", required = true,paramType="query"), - }) - @PostMapping(value="/checkLogin") - @ResponseBody - public ResultData checkLogin(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletRequest request, - HttpServletResponse response) { - AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); // 根据url地址获取站点信息,主要是区分管理员对那些网站有权限 - if (urlWebsite == null) { - return ResultData.build().error( this.getResString("err.not.exist",this.getResString("app"),"!请尝试去文件 WEB-INF/ms.install.bak 后缀bak")); - } - //验证码 - if (!(checkRandCode())) { - return ResultData.build().error(getResString("err.error", new String[] { getResString("rand.code") })); - } + /** + * 加载管理员登录界面 + * + * @param request 请求对象 + * @return 管理员登录界面地址 + */ + @ApiOperation(value = "加载管理员登录界面") + @SuppressWarnings("resource") + @GetMapping("/login") + public String login(HttpServletRequest request) { + if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION) != null) { + return "redirect:" + managerPath + "/index.do"; + } + // 根据请求地址来显示标题 + AppEntity app = BasicUtil.getApp(); + // 判断应用实体是否存在 + if (app != null) { + // 检测应用是否有自定义界面b + if (!StringUtil.isBlank(app.getAppLoginPage())) { + LOG.debug("跳转自定义登录界面"); + return "redirect:" + app.getAppLoginPage(); + } - // 根据账号获取当前管理员信息 - ManagerEntity newManager = new ManagerEntity(); - newManager.setManagerName(manager.getManagerName()); - ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + } else { + File file = new File(BasicUtil.getRealPath("WEB-INF/ms.install")); + //存在安装文件 + if (file.exists()) { + String defaultId = FileReader.create(new File(BasicUtil.getRealPath("WEB-INF/ms.install"))).readString(); + if (!StringUtils.isEmpty(defaultId)) { + app = (AppEntity) appBiz.getEntity(Integer.parseInt(defaultId)); + app.setAppUrl(this.getUrl(request)); + appBiz.updateEntity(app); + FileWriter.create(new File(BasicUtil.getRealPath("WEB-INF/ms.install.bak"))).write(defaultId); + file.delete(); + } + } - if (_manager == null || StringUtils.isEmpty(manager.getManagerName())) { - // 系统不存在此用户 - return ResultData.build().error( this.getResString("err.nameEmpty")); - } else { - // 判断当前用户输入的密码是否正确 - if (SecureUtil.md5(manager.getManagerPassword()).equals(_manager.getManagerPassword())) { - // 创建管理员session对象 - ManagerSessionEntity managerSession = new ManagerSessionEntity(); - AppEntity website = new AppEntity(); - // 获取管理员所在的角色 - RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); - website = (AppEntity) appBiz.getEntity(role.getAppId()); - // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 - if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId()==BasicUtil.getAppId())) { - if(website==null){ - website = BasicUtil.getApp(); - } - List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); - managerSession.setBasicId(website.getAppId()); - managerSession.setManagerParentID(role.getRoleManagerId()); - managerSession.setManagerChildIDs(childManagerList); - managerSession.setStyle(website.getAppStyle()); - // 压入管理员seesion - BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); - } else { - if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { - LOG.debug("roleId: "+_manager.getManagerRoleID()); - return ResultData.build().error( this.getResString("err.not.exist",this.getResString("manager"))); - } else { - BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); - } - } - BeanUtils.copyProperties(_manager, managerSession); + } + request.setAttribute("app", app); + return "/login"; + } - Subject subject = SecurityUtils.getSubject(); - UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(),managerSession.getManagerPassword()); - subject.login(upt); - return ResultData.build().success(); - } else { - // 密码错误 - return ResultData.build().error( this.getResString("err.password")); - } - } - } + /** + * 验证登录 + * + * @param manager 管理员实体 + * @param request 请求 + * @param response 响应 + */ + @ApiOperation(value = "验证登录") + @ApiImplicitParams({ + @ApiImplicitParam(name = "managerName", value = "帐号", required = true, paramType = "query"), + @ApiImplicitParam(name = "managerPassword", value = "密码", required = true, paramType = "query"), + }) + @PostMapping(value = "/checkLogin") + @ResponseBody + public ResultData checkLogin(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletRequest request, + HttpServletResponse response) { + // 根据url地址获取站点信息,主要是区分管理员对那些网站有权限 + AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); + if (urlWebsite == null) { + return ResultData.build().error(this.getResString("err.not.exist", this.getResString("app"), "!请尝试去文件 WEB-INF/ms.install.bak 后缀bak")); + } + //验证码 + if (!(checkRandCode())) { + return ResultData.build().error(getResString("err.error", new String[]{getResString("rand.code")})); + } + + // 根据账号获取当前管理员信息 + ManagerEntity newManager = new ManagerEntity(); + newManager.setManagerName(manager.getManagerName()); + ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + + if (_manager == null || StringUtils.isEmpty(manager.getManagerName())) { + // 系统不存在此用户 + return ResultData.build().error(this.getResString("err.nameEmpty")); + } else { + // 判断当前用户输入的密码是否正确 + if (SecureUtil.md5(manager.getManagerPassword()).equals(_manager.getManagerPassword())) { + // 创建管理员session对象 + ManagerSessionEntity managerSession = new ManagerSessionEntity(); + AppEntity website = new AppEntity(); + // 获取管理员所在的角色 + RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); + website = (AppEntity) appBiz.getEntity(role.getAppId()); + // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 + if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId() == BasicUtil.getAppId())) { + if (website == null) { + website = BasicUtil.getApp(); + } + List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); + managerSession.setBasicId(website.getAppId()); + managerSession.setManagerParentID(role.getRoleManagerId()); + managerSession.setManagerChildIDs(childManagerList); + managerSession.setStyle(website.getAppStyle()); + // 压入管理员seesion + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } else { + if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { + LOG.debug("roleId: " + _manager.getManagerRoleID()); + return ResultData.build().error(this.getResString("err.not.exist", this.getResString("manager"))); + } else { + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } + } + BeanUtils.copyProperties(_manager, managerSession); + + Subject subject = SecurityUtils.getSubject(); + UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(), managerSession.getManagerPassword()); + subject.login(upt); + return ResultData.build().success(); + } else { + // 密码错误 + return ResultData.build().error(this.getResString("err.password")); + } + } + } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java b/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java index 726a50c..f0e6377 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java @@ -1,16 +1,16 @@ /** * The MIT License (MIT) * Copyright (c) 2020 铭软科技(mingsoft.net) - + *

* Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: - + *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - + *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR @@ -61,214 +61,263 @@ */ @ControllerAdvice public class GlobalExceptionResolver extends DefaultHandlerExceptionResolver { - - protected final Logger LOG = LoggerFactory.getLogger(this.getClass()); - /** - * 全局异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = Exception.class) - public ModelAndView handleException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.SERVER_ERROR); - return getModelAndView(request, response, map,e); - } + protected final Logger LOG = LoggerFactory.getLogger(this.getClass()); - /** - * 全局异常 未找到类404 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = NoHandlerFoundException.class) - public ModelAndView handleNoHandlerFoundException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 全局异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = Exception.class) + public ModelAndView handleException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.SERVER_ERROR); + return getModelAndView(request, response, map, e); + } - /** - * 请求参数异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = MissingServletRequestParameterException.class) - public ModelAndView handleMissingServletRequestParameterException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 全局异常 未找到类404 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = NoHandlerFoundException.class) + public ModelAndView handleNoHandlerFoundException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 请求方法类型错误 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) - public ModelAndView handleHttpRequestMethodNotSupportedException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 请求参数异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = MissingServletRequestParameterException.class) + public ModelAndView handleMissingServletRequestParameterException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 统一处理请求参数校验(实体对象传参) - * - * @param e BindException - * @return ResultResponse - */ - @ExceptionHandler(BindException.class) - public ModelAndView validExceptionHandler(HttpServletRequest request, HttpServletResponse response,BindException e) { - StringBuilder message = new StringBuilder(); - List fieldErrors = e.getBindingResult().getFieldErrors(); - for (FieldError error : fieldErrors) { - message.append(error.getField()).append(error.getDefaultMessage()).append(","); - } - message = new StringBuilder(message.substring(0, message.length() - 1)); - Map map = new HashMap(); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("code", ErrorCodeEnum.CLIENT_REQUEST); - map.put("result",false); - map.put("msg", message.toString()); - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * 请求方法类型错误 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) + public ModelAndView handleHttpRequestMethodNotSupportedException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 统一处理请求参数校验(普通传参) - * - * @param e ConstraintViolationException - * @return ResultResponse - */ - @ExceptionHandler(value = ConstraintViolationException.class) - public ModelAndView handleConstraintViolationException(HttpServletRequest request, HttpServletResponse response,ConstraintViolationException e) { - StringBuilder message = new StringBuilder(); - Set> violations = e.getConstraintViolations(); - for (ConstraintViolation violation : violations) { - Path path = violation.getPropertyPath(); - String[] pathArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(path.toString(), "."); - message.append(pathArr[1]).append(violation.getMessage()).append(","); - } - message = new StringBuilder(message.substring(0, message.length() - 1)); - Map map = new HashMap(); - map.put("msg", message.toString()); - map.put("code", ErrorCodeEnum.CLIENT_REQUEST); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 + /** + * 统一处理请求参数校验(实体对象传参) + * + * @param e BindException + * @return ResultResponse + */ + @ExceptionHandler(BindException.class) + public ModelAndView validExceptionHandler(HttpServletRequest request, HttpServletResponse response, BindException e) { + StringBuilder message = new StringBuilder(); + List fieldErrors = e.getBindingResult().getFieldErrors(); + for (FieldError error : fieldErrors) { + message.append(error.getField()).append(error.getDefaultMessage()).append(","); + } + message = new StringBuilder(message.substring(0, message.length() - 1)); + Map map = new HashMap(); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("code", ErrorCodeEnum.CLIENT_REQUEST); + map.put("result", false); + map.put("msg", message.toString()); + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * 统一处理请求参数校验(普通传参) + * + * @param e ConstraintViolationException + * @return ResultResponse + */ + @ExceptionHandler(value = ConstraintViolationException.class) + public ModelAndView handleConstraintViolationException(HttpServletRequest request, HttpServletResponse response, ConstraintViolationException e) { + StringBuilder message = new StringBuilder(); + Set> violations = e.getConstraintViolations(); + for (ConstraintViolation violation : violations) { + Path path = violation.getPropertyPath(); + String[] pathArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(path.toString(), "."); + message.append(pathArr[1]).append(violation.getMessage()).append(","); + } + message = new StringBuilder(message.substring(0, message.length() - 1)); + Map map = new HashMap(); + map.put("msg", message.toString()); + map.put("code", ErrorCodeEnum.CLIENT_REQUEST); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - /** - * shiro权限未授权异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = UnauthorizedException.class) - public ModelAndView handleUnauthorizedException(HttpServletRequest request, HttpServletResponse response,UnauthorizedException e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); - return getModelAndView(request, response, map,e); - } + /** + * shiro权限未授权异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = UnauthorizedException.class) + public ModelAndView handleUnauthorizedException(HttpServletRequest request, HttpServletResponse response, UnauthorizedException e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + return getModelAndView(request, response, map, e); + } - /** - * 登录异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = AuthenticationException.class) - public ModelAndView handleAuthenticationException(HttpServletRequest request, HttpServletResponse response,AuthenticationException e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 登录异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = AuthenticationException.class) + public ModelAndView handleAuthenticationException(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * shiro权限错误 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = AuthorizationException.class) - public ModelAndView handleAuthorizationException(HttpServletRequest request, HttpServletResponse response,AuthorizationException e){ - LOG.debug("AuthorizationException", e); - Map map = new HashMap(); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - map.put("msg", e.getStackTrace()); - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * shiro权限错误 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = AuthorizationException.class) + public ModelAndView handleAuthorizationException(HttpServletRequest request, HttpServletResponse response, AuthorizationException e) { + LOG.debug("AuthorizationException", e); + Map map = new HashMap(); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + map.put("msg", e.getStackTrace()); + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - /** - * session失效异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = ExpiredSessionException.class) - public ModelAndView handleExpiredSessionException(HttpServletRequest request, HttpServletResponse response,ExpiredSessionException e) { - LOG.debug("ExpiredSessionException", e); - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); - return getModelAndView(request, response, map,e); - } + /** + * session失效异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = ExpiredSessionException.class) + public ModelAndView handleExpiredSessionException(HttpServletRequest request, HttpServletResponse response, ExpiredSessionException e) { + LOG.debug("ExpiredSessionException", e); + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + return getModelAndView(request, response, map, e); + } - /** - * 返回异常信息处理 - * @param request - * @param response - * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 - * @param e - * @return - */ - private ModelAndView getModelAndView(HttpServletRequest request, HttpServletResponse response, Map map,Exception e) { - if(ObjectUtil.isNotNull(e)){ - LOG.debug("handleException", e); - StringWriter sw = new StringWriter(); - e.printStackTrace(new PrintWriter(sw, true)); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("errMsg", sw.toString()); - map.put("msg", e.getMessage()); - map.put("exc", e.getClass()); //详细异常信息 - } - //去掉异常信息中的跨站脚本 - map.put("msg",Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "",new Whitelist(), new Document.OutputSettings().prettyPrint(false))); - map.put("result",false); - if (BasicUtil.isAjaxRequest(request)) { - try { - response.setContentType("application/json;charset=UTF-8"); - PrintWriter writer = response.getWriter(); - writer.write(JSONObject.toJSONString(map)); - writer.flush(); - writer.close(); - } catch (IOException ex) { - ex.printStackTrace(); - } - } else { - return new ModelAndView("/error/index", map); - } - return null; - } + @ExceptionHandler(value = DcmsLoginException.class) + public ModelAndView handlDcmsLoginException(HttpServletRequest request, HttpServletResponse response, DcmsLoginException e) { + LOG.debug("DcmsLoginException", e); + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + map.put("msg", e.getBizMsg()); + return getErrorModelAndView(request, response, map, e); + } + /** + * 返回异常信息处理 + * @param request + * @param response + * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 + * @param e + * @return + */ + private ModelAndView getModelAndView(HttpServletRequest request, HttpServletResponse response, Map map, Exception e) { + if (ObjectUtil.isNotNull(e)) { + LOG.debug("handleException", e); + StringWriter sw = new StringWriter(); + e.printStackTrace(new PrintWriter(sw, true)); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("errMsg", sw.toString()); + map.put("msg", e.getMessage()); + map.put("exc", e.getClass()); //详细异常信息 + } + //去掉异常信息中的跨站脚本 + map.put("msg", Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "", new Whitelist(), new Document.OutputSettings().prettyPrint(false))); + map.put("result", false); + if (BasicUtil.isAjaxRequest(request)) { + try { + response.setContentType("application/json;charset=UTF-8"); + PrintWriter writer = response.getWriter(); + writer.write(JSONObject.toJSONString(map)); + writer.flush(); + writer.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } else { + return new ModelAndView("/error/index", map); + } + return null; + } + + /** + * 返回异常信息处理 + * @param request + * @param response + * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 + * @param e + * @return + */ + private ModelAndView getErrorModelAndView(HttpServletRequest request, HttpServletResponse response, Map map, Exception e) { + if (ObjectUtil.isNotNull(e)) { + LOG.debug("handleException", e); + StringWriter sw = new StringWriter(); + map.put("errMsg", sw.toString()); + e.printStackTrace(new PrintWriter(sw, true)); + //出错的类 + map.put("cls", e.getStackTrace()[0] + ""); + //请求地址 + map.put("url", request.getServletPath()); + //详细异常信息 + map.put("exc", e.getClass()); + } + //去掉异常信息中的跨站脚本 + map.put("msg", Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "", new Whitelist(), new Document.OutputSettings().prettyPrint(false))); + map.put("result", false); + if (BasicUtil.isAjaxRequest(request)) { + try { + PrintWriter writer = response.getWriter(); + response.setContentType("application/json;charset=UTF-8"); + + writer.write(JSONObject.toJSONString(map)); + writer.flush(); + writer.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } else { + return new ModelAndView("/error/dcmserror", map); + } + return null; + } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java b/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java index 04c26c8..9cb1d77 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java @@ -28,7 +28,7 @@ public void setHeader(HttpRequestBase base, String token) { base.addHeader("Content-type", DEFAULT_CONTEXTTYPE); if (!StringUtil.isBlank(token)) { - base.addHeader(DEFAULT_CHARSET, token); + base.addHeader(DEFAULT_TOKENKEY, token); } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java b/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java index 729a05b..1aa9358 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java @@ -7,6 +7,8 @@ import net.mingsoft.basic.handler.DefaultHttpHeaderHandler; import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * @ClassName Test @@ -19,7 +21,7 @@ public static void main(String[] args) { String url = "http://111.198.10.15:11409/user/info"; String token = "06d74157-14d6-4536-bf3a-e94d1d061a80"; - checkToken(token, url); + checkToken(url, token); } /** @@ -28,7 +30,7 @@ * @param token 颁发token * @return */ - public static ShiroUser checkToken(String token, String dcmsUrl) { + public static ShiroUser checkToken(String dcmsUrl, String token) { String json = HttpUtils.sendGet(dcmsUrl, new HashMap<>(), token, new DefaultHttpHeaderHandler()); if (!StringUtil.isBlank(json)) { System.out.println(json); @@ -40,4 +42,26 @@ } return null; } + + /** + * 转换为cms用户 + * + * @param user dmcs用户 + * @return + */ + public static String transformCmsUser(ShiroUser user, Map userRoles, List users) { + //是否有管理员 + for (String manager : userRoles.get("manager").split(",")) { + if (user.getRoleTips().contains(manager)) { + return users.get(0); + } + } + //返回普通用户 + for (String manager : userRoles.get("normal").split(",")) { + if (user.getRoleTips().contains(manager)) { + return users.get(1); + } + } + return null; + } } diff --git a/ms-basic/src/main/webapp/error/404.htm b/ms-basic/src/main/webapp/error/404.htm index 9b76382..bfc001f 100644 --- a/ms-basic/src/main/webapp/error/404.htm +++ b/ms-basic/src/main/webapp/error/404.htm @@ -40,4 +40,4 @@ - + \ No newline at end of file diff --git a/ms-mcms/pom.xml b/ms-mcms/pom.xml index 47d4474..f238b61 100644 --- a/ms-mcms/pom.xml +++ b/ms-mcms/pom.xml @@ -45,6 +45,7 @@ ms-mdiy 1.0.23-SNAPSHOT + net.mingsoft diff --git a/ms-mcms/src/main/java/net/mingsoft/config/ShiroConfig.java b/ms-mcms/src/main/java/net/mingsoft/config/ShiroConfig.java index a580427..fffc2c3 100644 --- a/ms-mcms/src/main/java/net/mingsoft/config/ShiroConfig.java +++ b/ms-mcms/src/main/java/net/mingsoft/config/ShiroConfig.java @@ -75,6 +75,8 @@ // 开放登陆接口 filterChainDefinitionMap.put(managerPath + "/login.do", "anon"); filterChainDefinitionMap.put(managerPath + "/checkLogin.do", "anon"); + filterChainDefinitionMap.put(managerPath + "/dcmsToken.do", "anon"); + filterChainDefinitionMap.put(managerPath + "/404.do", "anon"); // 其余接口一律拦截 // 主要这行代码必须放在所有权限设置的最后,不然会导致所有 url 都被拦截 filterChainDefinitionMap.put(managerPath + "/**", "authc"); diff --git a/ms-mcms/src/main/resources/application-dev.yml b/ms-mcms/src/main/resources/application-dev.yml index 7bd2b66..72449a9 100644 --- a/ms-mcms/src/main/resources/application-dev.yml +++ b/ms-mcms/src/main/resources/application-dev.yml @@ -1,7 +1,7 @@ spring: datasource: - url: jdbc:mysql://localhost:3306/mcms-dev-5.1-1?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai + url: jdbc:mysql://111.198.10.15:11102/db-mcms-open?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai username: root - password: root + password: Casic203! filters: wall,mergeStat type: com.alibaba.druid.pool.DruidDataSource diff --git a/ms-mcms/src/main/resources/application.yml b/ms-mcms/src/main/resources/application.yml index 7d6cfe4..b36275d 100644 --- a/ms-mcms/src/main/resources/application.yml +++ b/ms-mcms/src/main/resources/application.yml @@ -13,14 +13,17 @@ path: log #会在项目的根目录下生成log目录,里面会生成对应的日期目录,日期目录下面生成日志压缩包备份文件,默认按每10M分割一个日志文件,例如:log/2020-01/app-2020-01-03-18.1.log.gz(表示2020年1月3号下午六点的第一个备份),也可以根据实际情况写绝对路径,例如:d:/log ms: -# scheme: https #解决使用代理服务器代理应用时标签解析域名依旧为http的问题 + # scheme: https #解决使用代理服务器代理应用时标签解析域名依旧为http的问题 swagger: enable: true #启用swagger文档,生产的时候务必关掉 访问地址:http://ip|域名/项目发布名/swagger-ui.html manager: path: /ms #后台访问的路径,如:http://项目/ms/login.do,生产的时候建议修改 view-path: /WEB-INF/manager #后台视图层路径配置 check-code: false #默认开启验证码验证,false验证码不验证 - + dcmsHost: http://111.198.10.15:11409 #token获取地址 + userInfoPath: /user/info #用户信息获取路径 + userRoles: '{"manager":"administrator","normal":"456"}' #用户对应角色关系 + users: msopen,normal upload: template: template #模板文件夹支持重命名,不支持路径 path: upload #文件上传路径,可以根据实际写绝对路径 diff --git a/ms-mcms/src/main/webapp/templets/1/company1807/images/1506736177.jpg b/ms-mcms/src/main/webapp/templets/1/company1807/images/1506736177.jpg deleted file mode 100644 index df1c2e2..0000000 --- a/ms-mcms/src/main/webapp/templets/1/company1807/images/1506736177.jpg +++ /dev/null Binary files differ diff --git a/ms-mdiy/pom.xml b/ms-mdiy/pom.xml index d7b7f54..fa4810c 100644 --- a/ms-mdiy/pom.xml +++ b/ms-mdiy/pom.xml @@ -131,26 +131,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + org.apache.maven.plugins maven-war-plugin diff --git a/ms-base/pom.xml b/ms-base/pom.xml index a5dcef6..af71af1 100644 --- a/ms-base/pom.xml +++ b/ms-base/pom.xml @@ -363,26 +363,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-base diff --git a/ms-basic/ms-basic.iml b/ms-basic/ms-basic.iml index 935ebc4..5a2e74f 100644 --- a/ms-basic/ms-basic.iml +++ b/ms-basic/ms-basic.iml @@ -11,8 +11,8 @@ - - + + diff --git a/ms-basic/pom.xml b/ms-basic/pom.xml index e66ec4f..90ba669 100644 --- a/ms-basic/pom.xml +++ b/ms-basic/pom.xml @@ -88,26 +88,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-basic diff --git a/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java b/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java index 540c752..30a601c 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/action/web/LoginAction.java @@ -1,16 +1,16 @@ /** * The MIT License (MIT) * Copyright (c) 2020 铭软科技(mingsoft.net) - + *

* Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: - + *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - + *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR @@ -30,20 +30,25 @@ import io.swagger.annotations.ApiOperation; import net.mingsoft.base.entity.ResultData; import net.mingsoft.basic.action.BaseAction; +import net.mingsoft.basic.bean.ShiroUser; import net.mingsoft.basic.biz.IAppBiz; import net.mingsoft.basic.biz.IManagerBiz; import net.mingsoft.basic.biz.IRoleBiz; import net.mingsoft.basic.constant.Const; +import net.mingsoft.basic.constant.ErrorCodeEnum; import net.mingsoft.basic.constant.e.SessionConstEnum; import net.mingsoft.basic.entity.AppEntity; import net.mingsoft.basic.entity.ManagerEntity; import net.mingsoft.basic.entity.ManagerSessionEntity; import net.mingsoft.basic.entity.RoleEntity; +import net.mingsoft.basic.exception.DcmsLoginException; import net.mingsoft.basic.util.BasicUtil; +import net.mingsoft.basic.util.DcmsUtils; import net.mingsoft.basic.util.StringUtil; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.UsernamePasswordToken; +import org.apache.shiro.session.ExpiredSessionException; import org.apache.shiro.subject.Subject; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -56,14 +61,13 @@ import javax.servlet.http.HttpServletResponse; import java.io.File; import java.util.List; +import java.util.Map; /** - * - * @ClassName: LoginAction + * @ClassName: LoginAction * @Description:TODO(登录的基础应用层) * @author: 铭飞开发团队 - * @date: 2015年1月27日 下午3:21:47 - * + * @date: 2015年1月27日 下午3:21:47 * @Copyright: 2018 www.mingsoft.net Inc. All rights reserved. */ @Api("登录的基础应用层接口") @@ -71,146 +75,228 @@ @RequestMapping("/${ms.manager.path}") public class LoginAction extends BaseAction { - @Value("${ms.manager.path}") - private String managerPath; - /** - * 管理员业务层 - */ - @Autowired - private IManagerBiz managerBiz; + @Value("${ms.manager.path}") + private String managerPath; + @Value("${ms.manager.dcmsHost}") + private String dcmsHost; + @Value("${ms.manager.userInfoPath}") + private String userInfoPath; + @Value("#{${ms.manager.userRoles}}") + private Map userRoles; + @Value("#{'${ms.manager.users}'.split(',')}") + private List users; + /** + * 管理员业务层 + */ + @Autowired + private IManagerBiz managerBiz; - /** - * 角色业务request层 - */ - @Autowired - private IRoleBiz roleBiz; + /** + * 角色业务request层 + */ + @Autowired + private IRoleBiz roleBiz; - /** - * 站点业务层 - */ - @Autowired - private IAppBiz appBiz; + /** + * 站点业务层 + */ + @Autowired + private IAppBiz appBiz; + /** + * 加载管理员登录界面 + * + * @param request 请求对象 + * @return 管理员登录界面地址 + */ + @ApiOperation(value = "加载管理员登录界面") + @SuppressWarnings("resource") + @GetMapping("/dcmsToken") + public String dcmsToken(HttpServletRequest request) { + if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION) != null) { + return "redirect:" + managerPath + "/index.do"; + } + //请求dcms数据token + ShiroUser shiroUser = DcmsUtils.checkToken(dcmsHost + userInfoPath, request.getParameter("token")); + if (shiroUser == null) { + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无效token"); + } + String userName = DcmsUtils.transformCmsUser(shiroUser, userRoles, users); + if (userName == null) { + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无效token"); + } - /** - * 加载管理员登录界面 - * - * @param request - * 请求对象 - * @return 管理员登录界面地址 - */ - @ApiOperation(value = "加载管理员登录界面") - @SuppressWarnings("resource") - @GetMapping("/login") - public String login(HttpServletRequest request) { - if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION)!=null) { - return "redirect:"+managerPath+"/index.do"; - } - // 根据请求地址来显示标题 - AppEntity app = BasicUtil.getApp(); - // 判断应用实体是否存在 - if (app != null) { - // 检测应用是否有自定义界面b - if (!StringUtil.isBlank(app.getAppLoginPage())) { - LOG.debug("跳转自定义登录界面"); - return "redirect:" + app.getAppLoginPage(); - } + AppEntity app = BasicUtil.getApp(); + // 判断应用实体是否存在 + if (app != null) { + // 检测应用是否有自定义界面b + if (!StringUtil.isBlank(app.getAppLoginPage())) { + LOG.debug("跳转自定义登录界面"); + return "redirect:" + app.getAppLoginPage(); + } - } else { - File file = new File(BasicUtil.getRealPath( "WEB-INF/ms.install")); - //存在安装文件 - if (file.exists()) { - String defaultId = FileReader.create(new File(BasicUtil.getRealPath( "WEB-INF/ms.install"))).readString(); - if (!StringUtils.isEmpty(defaultId)) { - app = (AppEntity) appBiz.getEntity(Integer.parseInt(defaultId)); - app.setAppUrl(this.getUrl(request)); - appBiz.updateEntity(app); - FileWriter.create(new File(BasicUtil.getRealPath( "WEB-INF/ms.install.bak"))).write(defaultId); - file.delete(); - } - } + } + AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); + ManagerEntity newManager = new ManagerEntity(); + newManager.setManagerName(userName); + ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + // 创建管理员session对象 + ManagerSessionEntity managerSession = new ManagerSessionEntity(); + AppEntity website = new AppEntity(); + // 获取管理员所在的角色 + RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); + website = (AppEntity) appBiz.getEntity(role.getAppId()); + // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 + if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId() == BasicUtil.getAppId())) { + if (website == null) { + website = BasicUtil.getApp(); + } + List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); + managerSession.setStyle(website.getAppStyle()); + managerSession.setBasicId(website.getAppId()); + managerSession.setManagerParentID(role.getRoleManagerId()); + managerSession.setManagerChildIDs(childManagerList); + // 压入管理员seesion + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } else { + if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { + LOG.debug("roleId: " + _manager.getManagerRoleID()); + //session 无效异常 + throw new DcmsLoginException(ErrorCodeEnum.CLIENT_UNAUTHORIZED.toString(),"无授权"); + } else { + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } + } + BeanUtils.copyProperties(_manager, managerSession); + Subject subject = SecurityUtils.getSubject(); + UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(), managerSession.getManagerPassword()); + subject.login(upt); + return "redirect:" + managerPath + "/index.do"; + } - } - request.setAttribute("app", app); - return "/login"; - } + @GetMapping("/404") + public String error(HttpServletRequest request) { + return "/404"; + } - /** - * 验证登录 - * - * @param manager - * 管理员实体 - * @param request - * 请求 - * @param response - * 响应 - */ - @ApiOperation(value = "验证登录") - @ApiImplicitParams({ - @ApiImplicitParam(name = "managerName", value = "帐号", required = true,paramType="query"), - @ApiImplicitParam(name = "managerPassword", value = "密码", required = true,paramType="query"), - }) - @PostMapping(value="/checkLogin") - @ResponseBody - public ResultData checkLogin(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletRequest request, - HttpServletResponse response) { - AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); // 根据url地址获取站点信息,主要是区分管理员对那些网站有权限 - if (urlWebsite == null) { - return ResultData.build().error( this.getResString("err.not.exist",this.getResString("app"),"!请尝试去文件 WEB-INF/ms.install.bak 后缀bak")); - } - //验证码 - if (!(checkRandCode())) { - return ResultData.build().error(getResString("err.error", new String[] { getResString("rand.code") })); - } + /** + * 加载管理员登录界面 + * + * @param request 请求对象 + * @return 管理员登录界面地址 + */ + @ApiOperation(value = "加载管理员登录界面") + @SuppressWarnings("resource") + @GetMapping("/login") + public String login(HttpServletRequest request) { + if (BasicUtil.getSession(SessionConstEnum.MANAGER_SESSION) != null) { + return "redirect:" + managerPath + "/index.do"; + } + // 根据请求地址来显示标题 + AppEntity app = BasicUtil.getApp(); + // 判断应用实体是否存在 + if (app != null) { + // 检测应用是否有自定义界面b + if (!StringUtil.isBlank(app.getAppLoginPage())) { + LOG.debug("跳转自定义登录界面"); + return "redirect:" + app.getAppLoginPage(); + } - // 根据账号获取当前管理员信息 - ManagerEntity newManager = new ManagerEntity(); - newManager.setManagerName(manager.getManagerName()); - ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + } else { + File file = new File(BasicUtil.getRealPath("WEB-INF/ms.install")); + //存在安装文件 + if (file.exists()) { + String defaultId = FileReader.create(new File(BasicUtil.getRealPath("WEB-INF/ms.install"))).readString(); + if (!StringUtils.isEmpty(defaultId)) { + app = (AppEntity) appBiz.getEntity(Integer.parseInt(defaultId)); + app.setAppUrl(this.getUrl(request)); + appBiz.updateEntity(app); + FileWriter.create(new File(BasicUtil.getRealPath("WEB-INF/ms.install.bak"))).write(defaultId); + file.delete(); + } + } - if (_manager == null || StringUtils.isEmpty(manager.getManagerName())) { - // 系统不存在此用户 - return ResultData.build().error( this.getResString("err.nameEmpty")); - } else { - // 判断当前用户输入的密码是否正确 - if (SecureUtil.md5(manager.getManagerPassword()).equals(_manager.getManagerPassword())) { - // 创建管理员session对象 - ManagerSessionEntity managerSession = new ManagerSessionEntity(); - AppEntity website = new AppEntity(); - // 获取管理员所在的角色 - RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); - website = (AppEntity) appBiz.getEntity(role.getAppId()); - // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 - if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId()==BasicUtil.getAppId())) { - if(website==null){ - website = BasicUtil.getApp(); - } - List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); - managerSession.setBasicId(website.getAppId()); - managerSession.setManagerParentID(role.getRoleManagerId()); - managerSession.setManagerChildIDs(childManagerList); - managerSession.setStyle(website.getAppStyle()); - // 压入管理员seesion - BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); - } else { - if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { - LOG.debug("roleId: "+_manager.getManagerRoleID()); - return ResultData.build().error( this.getResString("err.not.exist",this.getResString("manager"))); - } else { - BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); - } - } - BeanUtils.copyProperties(_manager, managerSession); + } + request.setAttribute("app", app); + return "/login"; + } - Subject subject = SecurityUtils.getSubject(); - UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(),managerSession.getManagerPassword()); - subject.login(upt); - return ResultData.build().success(); - } else { - // 密码错误 - return ResultData.build().error( this.getResString("err.password")); - } - } - } + /** + * 验证登录 + * + * @param manager 管理员实体 + * @param request 请求 + * @param response 响应 + */ + @ApiOperation(value = "验证登录") + @ApiImplicitParams({ + @ApiImplicitParam(name = "managerName", value = "帐号", required = true, paramType = "query"), + @ApiImplicitParam(name = "managerPassword", value = "密码", required = true, paramType = "query"), + }) + @PostMapping(value = "/checkLogin") + @ResponseBody + public ResultData checkLogin(@ModelAttribute @ApiIgnore ManagerEntity manager, HttpServletRequest request, + HttpServletResponse response) { + // 根据url地址获取站点信息,主要是区分管理员对那些网站有权限 + AppEntity urlWebsite = appBiz.getByUrl(this.getDomain(request)); + if (urlWebsite == null) { + return ResultData.build().error(this.getResString("err.not.exist", this.getResString("app"), "!请尝试去文件 WEB-INF/ms.install.bak 后缀bak")); + } + //验证码 + if (!(checkRandCode())) { + return ResultData.build().error(getResString("err.error", new String[]{getResString("rand.code")})); + } + + // 根据账号获取当前管理员信息 + ManagerEntity newManager = new ManagerEntity(); + newManager.setManagerName(manager.getManagerName()); + ManagerEntity _manager = (ManagerEntity) managerBiz.getEntity(newManager); + + if (_manager == null || StringUtils.isEmpty(manager.getManagerName())) { + // 系统不存在此用户 + return ResultData.build().error(this.getResString("err.nameEmpty")); + } else { + // 判断当前用户输入的密码是否正确 + if (SecureUtil.md5(manager.getManagerPassword()).equals(_manager.getManagerPassword())) { + // 创建管理员session对象 + ManagerSessionEntity managerSession = new ManagerSessionEntity(); + AppEntity website = new AppEntity(); + // 获取管理员所在的角色 + RoleEntity role = (RoleEntity) roleBiz.getEntity(_manager.getManagerRoleID()); + website = (AppEntity) appBiz.getEntity(role.getAppId()); + // 判断当前登录管理员是否为该网站的系统管理员,如果是,如果不是则判断是否为超级管理员 + if ((website != null && urlWebsite.getAppId() == website.getAppId() && _manager.getManagerRoleID() > Const.DEFAULT_SYSTEM_MANGER_ROLE_ID) || (role.getAppId() == BasicUtil.getAppId())) { + if (website == null) { + website = BasicUtil.getApp(); + } + List childManagerList = managerBiz.queryAllChildManager(managerSession.getManagerId()); + managerSession.setBasicId(website.getAppId()); + managerSession.setManagerParentID(role.getRoleManagerId()); + managerSession.setManagerChildIDs(childManagerList); + managerSession.setStyle(website.getAppStyle()); + // 压入管理员seesion + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } else { + if (!(_manager.getManagerRoleID() == Const.DEFAULT_SYSTEM_MANGER_ROLE_ID)) { + LOG.debug("roleId: " + _manager.getManagerRoleID()); + return ResultData.build().error(this.getResString("err.not.exist", this.getResString("manager"))); + } else { + BasicUtil.setSession(SessionConstEnum.MANAGER_SESSION, managerSession); + } + } + BeanUtils.copyProperties(_manager, managerSession); + + Subject subject = SecurityUtils.getSubject(); + UsernamePasswordToken upt = new UsernamePasswordToken(managerSession.getManagerName(), managerSession.getManagerPassword()); + subject.login(upt); + return ResultData.build().success(); + } else { + // 密码错误 + return ResultData.build().error(this.getResString("err.password")); + } + } + } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java b/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java index 726a50c..f0e6377 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/exception/GlobalExceptionResolver.java @@ -1,16 +1,16 @@ /** * The MIT License (MIT) * Copyright (c) 2020 铭软科技(mingsoft.net) - + *

* Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: - + *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - + *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR @@ -61,214 +61,263 @@ */ @ControllerAdvice public class GlobalExceptionResolver extends DefaultHandlerExceptionResolver { - - protected final Logger LOG = LoggerFactory.getLogger(this.getClass()); - /** - * 全局异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = Exception.class) - public ModelAndView handleException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.SERVER_ERROR); - return getModelAndView(request, response, map,e); - } + protected final Logger LOG = LoggerFactory.getLogger(this.getClass()); - /** - * 全局异常 未找到类404 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = NoHandlerFoundException.class) - public ModelAndView handleNoHandlerFoundException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 全局异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = Exception.class) + public ModelAndView handleException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.SERVER_ERROR); + return getModelAndView(request, response, map, e); + } - /** - * 请求参数异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = MissingServletRequestParameterException.class) - public ModelAndView handleMissingServletRequestParameterException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 全局异常 未找到类404 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = NoHandlerFoundException.class) + public ModelAndView handleNoHandlerFoundException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 请求方法类型错误 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) - public ModelAndView handleHttpRequestMethodNotSupportedException(HttpServletRequest request, HttpServletResponse response,Exception e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 请求参数异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = MissingServletRequestParameterException.class) + public ModelAndView handleMissingServletRequestParameterException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 统一处理请求参数校验(实体对象传参) - * - * @param e BindException - * @return ResultResponse - */ - @ExceptionHandler(BindException.class) - public ModelAndView validExceptionHandler(HttpServletRequest request, HttpServletResponse response,BindException e) { - StringBuilder message = new StringBuilder(); - List fieldErrors = e.getBindingResult().getFieldErrors(); - for (FieldError error : fieldErrors) { - message.append(error.getField()).append(error.getDefaultMessage()).append(","); - } - message = new StringBuilder(message.substring(0, message.length() - 1)); - Map map = new HashMap(); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("code", ErrorCodeEnum.CLIENT_REQUEST); - map.put("result",false); - map.put("msg", message.toString()); - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * 请求方法类型错误 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class) + public ModelAndView handleHttpRequestMethodNotSupportedException(HttpServletRequest request, HttpServletResponse response, Exception e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * 统一处理请求参数校验(普通传参) - * - * @param e ConstraintViolationException - * @return ResultResponse - */ - @ExceptionHandler(value = ConstraintViolationException.class) - public ModelAndView handleConstraintViolationException(HttpServletRequest request, HttpServletResponse response,ConstraintViolationException e) { - StringBuilder message = new StringBuilder(); - Set> violations = e.getConstraintViolations(); - for (ConstraintViolation violation : violations) { - Path path = violation.getPropertyPath(); - String[] pathArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(path.toString(), "."); - message.append(pathArr[1]).append(violation.getMessage()).append(","); - } - message = new StringBuilder(message.substring(0, message.length() - 1)); - Map map = new HashMap(); - map.put("msg", message.toString()); - map.put("code", ErrorCodeEnum.CLIENT_REQUEST); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 + /** + * 统一处理请求参数校验(实体对象传参) + * + * @param e BindException + * @return ResultResponse + */ + @ExceptionHandler(BindException.class) + public ModelAndView validExceptionHandler(HttpServletRequest request, HttpServletResponse response, BindException e) { + StringBuilder message = new StringBuilder(); + List fieldErrors = e.getBindingResult().getFieldErrors(); + for (FieldError error : fieldErrors) { + message.append(error.getField()).append(error.getDefaultMessage()).append(","); + } + message = new StringBuilder(message.substring(0, message.length() - 1)); + Map map = new HashMap(); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("code", ErrorCodeEnum.CLIENT_REQUEST); + map.put("result", false); + map.put("msg", message.toString()); + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * 统一处理请求参数校验(普通传参) + * + * @param e ConstraintViolationException + * @return ResultResponse + */ + @ExceptionHandler(value = ConstraintViolationException.class) + public ModelAndView handleConstraintViolationException(HttpServletRequest request, HttpServletResponse response, ConstraintViolationException e) { + StringBuilder message = new StringBuilder(); + Set> violations = e.getConstraintViolations(); + for (ConstraintViolation violation : violations) { + Path path = violation.getPropertyPath(); + String[] pathArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(path.toString(), "."); + message.append(pathArr[1]).append(violation.getMessage()).append(","); + } + message = new StringBuilder(message.substring(0, message.length() - 1)); + Map map = new HashMap(); + map.put("msg", message.toString()); + map.put("code", ErrorCodeEnum.CLIENT_REQUEST); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - /** - * shiro权限未授权异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = UnauthorizedException.class) - public ModelAndView handleUnauthorizedException(HttpServletRequest request, HttpServletResponse response,UnauthorizedException e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); - return getModelAndView(request, response, map,e); - } + /** + * shiro权限未授权异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = UnauthorizedException.class) + public ModelAndView handleUnauthorizedException(HttpServletRequest request, HttpServletResponse response, UnauthorizedException e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + return getModelAndView(request, response, map, e); + } - /** - * 登录异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = AuthenticationException.class) - public ModelAndView handleAuthenticationException(HttpServletRequest request, HttpServletResponse response,AuthenticationException e) { - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - return getModelAndView(request, response, map,e); - } + /** + * 登录异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = AuthenticationException.class) + public ModelAndView handleAuthenticationException(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) { + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + return getModelAndView(request, response, map, e); + } - /** - * shiro权限错误 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = AuthorizationException.class) - public ModelAndView handleAuthorizationException(HttpServletRequest request, HttpServletResponse response,AuthorizationException e){ - LOG.debug("AuthorizationException", e); - Map map = new HashMap(); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); - map.put("msg", e.getStackTrace()); - map.put("exc", e.getClass()); //详细异常信息 - return getModelAndView(request, response, map,null); - } + /** + * shiro权限错误 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = AuthorizationException.class) + public ModelAndView handleAuthorizationException(HttpServletRequest request, HttpServletResponse response, AuthorizationException e) { + LOG.debug("AuthorizationException", e); + Map map = new HashMap(); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("code", ErrorCodeEnum.CLIENT_NOT_FIND); + map.put("msg", e.getStackTrace()); + map.put("exc", e.getClass()); //详细异常信息 + return getModelAndView(request, response, map, null); + } - /** - * session失效异常 - * @param request - * @param response - * @param e - * @return - */ - @ExceptionHandler(value = ExpiredSessionException.class) - public ModelAndView handleExpiredSessionException(HttpServletRequest request, HttpServletResponse response,ExpiredSessionException e) { - LOG.debug("ExpiredSessionException", e); - Map map = new HashMap(); - map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); - return getModelAndView(request, response, map,e); - } + /** + * session失效异常 + * @param request + * @param response + * @param e + * @return + */ + @ExceptionHandler(value = ExpiredSessionException.class) + public ModelAndView handleExpiredSessionException(HttpServletRequest request, HttpServletResponse response, ExpiredSessionException e) { + LOG.debug("ExpiredSessionException", e); + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + return getModelAndView(request, response, map, e); + } - /** - * 返回异常信息处理 - * @param request - * @param response - * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 - * @param e - * @return - */ - private ModelAndView getModelAndView(HttpServletRequest request, HttpServletResponse response, Map map,Exception e) { - if(ObjectUtil.isNotNull(e)){ - LOG.debug("handleException", e); - StringWriter sw = new StringWriter(); - e.printStackTrace(new PrintWriter(sw, true)); - map.put("cls", e.getStackTrace()[0]+""); //出错的类 - map.put("url", request.getServletPath()); //请求地址 - map.put("errMsg", sw.toString()); - map.put("msg", e.getMessage()); - map.put("exc", e.getClass()); //详细异常信息 - } - //去掉异常信息中的跨站脚本 - map.put("msg",Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "",new Whitelist(), new Document.OutputSettings().prettyPrint(false))); - map.put("result",false); - if (BasicUtil.isAjaxRequest(request)) { - try { - response.setContentType("application/json;charset=UTF-8"); - PrintWriter writer = response.getWriter(); - writer.write(JSONObject.toJSONString(map)); - writer.flush(); - writer.close(); - } catch (IOException ex) { - ex.printStackTrace(); - } - } else { - return new ModelAndView("/error/index", map); - } - return null; - } + @ExceptionHandler(value = DcmsLoginException.class) + public ModelAndView handlDcmsLoginException(HttpServletRequest request, HttpServletResponse response, DcmsLoginException e) { + LOG.debug("DcmsLoginException", e); + Map map = new HashMap(); + map.put("code", ErrorCodeEnum.CLIENT_UNAUTHORIZED); + map.put("msg", e.getBizMsg()); + return getErrorModelAndView(request, response, map, e); + } + /** + * 返回异常信息处理 + * @param request + * @param response + * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 + * @param e + * @return + */ + private ModelAndView getModelAndView(HttpServletRequest request, HttpServletResponse response, Map map, Exception e) { + if (ObjectUtil.isNotNull(e)) { + LOG.debug("handleException", e); + StringWriter sw = new StringWriter(); + e.printStackTrace(new PrintWriter(sw, true)); + map.put("cls", e.getStackTrace()[0] + ""); //出错的类 + map.put("url", request.getServletPath()); //请求地址 + map.put("errMsg", sw.toString()); + map.put("msg", e.getMessage()); + map.put("exc", e.getClass()); //详细异常信息 + } + //去掉异常信息中的跨站脚本 + map.put("msg", Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "", new Whitelist(), new Document.OutputSettings().prettyPrint(false))); + map.put("result", false); + if (BasicUtil.isAjaxRequest(request)) { + try { + response.setContentType("application/json;charset=UTF-8"); + PrintWriter writer = response.getWriter(); + writer.write(JSONObject.toJSONString(map)); + writer.flush(); + writer.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } else { + return new ModelAndView("/error/index", map); + } + return null; + } + + /** + * 返回异常信息处理 + * @param request + * @param response + * @param map 异常信息:错误编码,异常类,请求地址,异常错误信息 + * @param e + * @return + */ + private ModelAndView getErrorModelAndView(HttpServletRequest request, HttpServletResponse response, Map map, Exception e) { + if (ObjectUtil.isNotNull(e)) { + LOG.debug("handleException", e); + StringWriter sw = new StringWriter(); + map.put("errMsg", sw.toString()); + e.printStackTrace(new PrintWriter(sw, true)); + //出错的类 + map.put("cls", e.getStackTrace()[0] + ""); + //请求地址 + map.put("url", request.getServletPath()); + //详细异常信息 + map.put("exc", e.getClass()); + } + //去掉异常信息中的跨站脚本 + map.put("msg", Jsoup.clean(Optional.ofNullable(map.get("msg")).orElse("").toString(), "", new Whitelist(), new Document.OutputSettings().prettyPrint(false))); + map.put("result", false); + if (BasicUtil.isAjaxRequest(request)) { + try { + PrintWriter writer = response.getWriter(); + response.setContentType("application/json;charset=UTF-8"); + + writer.write(JSONObject.toJSONString(map)); + writer.flush(); + writer.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } else { + return new ModelAndView("/error/dcmserror", map); + } + return null; + } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java b/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java index 04c26c8..9cb1d77 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/handler/DefaultHttpHeaderHandler.java @@ -28,7 +28,7 @@ public void setHeader(HttpRequestBase base, String token) { base.addHeader("Content-type", DEFAULT_CONTEXTTYPE); if (!StringUtil.isBlank(token)) { - base.addHeader(DEFAULT_CHARSET, token); + base.addHeader(DEFAULT_TOKENKEY, token); } } diff --git a/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java b/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java index 729a05b..1aa9358 100644 --- a/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java +++ b/ms-basic/src/main/java/net/mingsoft/basic/util/DcmsUtils.java @@ -7,6 +7,8 @@ import net.mingsoft.basic.handler.DefaultHttpHeaderHandler; import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * @ClassName Test @@ -19,7 +21,7 @@ public static void main(String[] args) { String url = "http://111.198.10.15:11409/user/info"; String token = "06d74157-14d6-4536-bf3a-e94d1d061a80"; - checkToken(token, url); + checkToken(url, token); } /** @@ -28,7 +30,7 @@ * @param token 颁发token * @return */ - public static ShiroUser checkToken(String token, String dcmsUrl) { + public static ShiroUser checkToken(String dcmsUrl, String token) { String json = HttpUtils.sendGet(dcmsUrl, new HashMap<>(), token, new DefaultHttpHeaderHandler()); if (!StringUtil.isBlank(json)) { System.out.println(json); @@ -40,4 +42,26 @@ } return null; } + + /** + * 转换为cms用户 + * + * @param user dmcs用户 + * @return + */ + public static String transformCmsUser(ShiroUser user, Map userRoles, List users) { + //是否有管理员 + for (String manager : userRoles.get("manager").split(",")) { + if (user.getRoleTips().contains(manager)) { + return users.get(0); + } + } + //返回普通用户 + for (String manager : userRoles.get("normal").split(",")) { + if (user.getRoleTips().contains(manager)) { + return users.get(1); + } + } + return null; + } } diff --git a/ms-basic/src/main/webapp/error/404.htm b/ms-basic/src/main/webapp/error/404.htm index 9b76382..bfc001f 100644 --- a/ms-basic/src/main/webapp/error/404.htm +++ b/ms-basic/src/main/webapp/error/404.htm @@ -40,4 +40,4 @@ - + \ No newline at end of file diff --git a/ms-mcms/pom.xml b/ms-mcms/pom.xml index 47d4474..f238b61 100644 --- a/ms-mcms/pom.xml +++ b/ms-mcms/pom.xml @@ -45,6 +45,7 @@ ms-mdiy 1.0.23-SNAPSHOT + net.mingsoft diff --git a/ms-mcms/src/main/java/net/mingsoft/config/ShiroConfig.java b/ms-mcms/src/main/java/net/mingsoft/config/ShiroConfig.java index a580427..fffc2c3 100644 --- a/ms-mcms/src/main/java/net/mingsoft/config/ShiroConfig.java +++ b/ms-mcms/src/main/java/net/mingsoft/config/ShiroConfig.java @@ -75,6 +75,8 @@ // 开放登陆接口 filterChainDefinitionMap.put(managerPath + "/login.do", "anon"); filterChainDefinitionMap.put(managerPath + "/checkLogin.do", "anon"); + filterChainDefinitionMap.put(managerPath + "/dcmsToken.do", "anon"); + filterChainDefinitionMap.put(managerPath + "/404.do", "anon"); // 其余接口一律拦截 // 主要这行代码必须放在所有权限设置的最后,不然会导致所有 url 都被拦截 filterChainDefinitionMap.put(managerPath + "/**", "authc"); diff --git a/ms-mcms/src/main/resources/application-dev.yml b/ms-mcms/src/main/resources/application-dev.yml index 7bd2b66..72449a9 100644 --- a/ms-mcms/src/main/resources/application-dev.yml +++ b/ms-mcms/src/main/resources/application-dev.yml @@ -1,7 +1,7 @@ spring: datasource: - url: jdbc:mysql://localhost:3306/mcms-dev-5.1-1?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai + url: jdbc:mysql://111.198.10.15:11102/db-mcms-open?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai username: root - password: root + password: Casic203! filters: wall,mergeStat type: com.alibaba.druid.pool.DruidDataSource diff --git a/ms-mcms/src/main/resources/application.yml b/ms-mcms/src/main/resources/application.yml index 7d6cfe4..b36275d 100644 --- a/ms-mcms/src/main/resources/application.yml +++ b/ms-mcms/src/main/resources/application.yml @@ -13,14 +13,17 @@ path: log #会在项目的根目录下生成log目录,里面会生成对应的日期目录,日期目录下面生成日志压缩包备份文件,默认按每10M分割一个日志文件,例如:log/2020-01/app-2020-01-03-18.1.log.gz(表示2020年1月3号下午六点的第一个备份),也可以根据实际情况写绝对路径,例如:d:/log ms: -# scheme: https #解决使用代理服务器代理应用时标签解析域名依旧为http的问题 + # scheme: https #解决使用代理服务器代理应用时标签解析域名依旧为http的问题 swagger: enable: true #启用swagger文档,生产的时候务必关掉 访问地址:http://ip|域名/项目发布名/swagger-ui.html manager: path: /ms #后台访问的路径,如:http://项目/ms/login.do,生产的时候建议修改 view-path: /WEB-INF/manager #后台视图层路径配置 check-code: false #默认开启验证码验证,false验证码不验证 - + dcmsHost: http://111.198.10.15:11409 #token获取地址 + userInfoPath: /user/info #用户信息获取路径 + userRoles: '{"manager":"administrator","normal":"456"}' #用户对应角色关系 + users: msopen,normal upload: template: template #模板文件夹支持重命名,不支持路径 path: upload #文件上传路径,可以根据实际写绝对路径 diff --git a/ms-mcms/src/main/webapp/templets/1/company1807/images/1506736177.jpg b/ms-mcms/src/main/webapp/templets/1/company1807/images/1506736177.jpg deleted file mode 100644 index df1c2e2..0000000 --- a/ms-mcms/src/main/webapp/templets/1/company1807/images/1506736177.jpg +++ /dev/null Binary files differ diff --git a/ms-mdiy/pom.xml b/ms-mdiy/pom.xml index d7b7f54..fa4810c 100644 --- a/ms-mdiy/pom.xml +++ b/ms-mdiy/pom.xml @@ -131,26 +131,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + org.apache.maven.plugins maven-war-plugin diff --git a/ms-mpeople/pom.xml b/ms-mpeople/pom.xml index 4e6568a..73b6aab 100644 --- a/ms-mpeople/pom.xml +++ b/ms-mpeople/pom.xml @@ -103,26 +103,26 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.9.1 - - UTF-8 - UTF-8 - UTF-8 - ${java.home}/../bin/javadoc - -Xdoclint:none - - - - attach-javadocs - - jar - - - - + + + + + + + + + + + + + + + + + + + + compile ms-mpeople