├── .gitignore ├── LICENSE ├── README.md ├── pom.xml ├── zhcc-common ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── hu │ └── zhcc │ └── common │ ├── cache │ └── CacheManager.java │ ├── entity │ ├── Page.java │ ├── PageRequest.java │ └── Result.java │ ├── exception │ ├── CustomException.java │ ├── DAOException.java │ ├── ExceptionEnum.java │ ├── GlobalCustomRestExceptionHandler.java │ └── ServiceException.java │ ├── filter │ └── CORSFilter.java │ └── utils │ ├── DateTimeUtils.java │ ├── PropertiesFileUtil.java │ ├── RedisUtil.java │ └── clearMaven.py ├── zhcc-shiro ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── hu │ │ │ └── zhcc │ │ │ └── shiro │ │ │ ├── controller │ │ │ ├── AuthController.java │ │ │ ├── BaseController.java │ │ │ ├── HomeController.java │ │ │ ├── ResourceController.java │ │ │ ├── RoleController.java │ │ │ ├── RouterController.java │ │ │ └── UserController.java │ │ │ ├── dao │ │ │ ├── ResourceDAO.java │ │ │ ├── ResourcePermissionDAO.java │ │ │ ├── RoleDAO.java │ │ │ ├── RouterDAO.java │ │ │ ├── RouterPermissionDAO.java │ │ │ ├── UserDAO.java │ │ │ └── UserRoleDAO.java │ │ │ ├── entity │ │ │ ├── bo │ │ │ │ ├── Resource.java │ │ │ │ ├── Role.java │ │ │ │ ├── Router.java │ │ │ │ └── User.java │ │ │ ├── dto │ │ │ │ ├── AvailableResourceDTO.java │ │ │ │ ├── PermissionDTO.java │ │ │ │ ├── ResourceDTO.java │ │ │ │ ├── ResourcePermissionDTO.java │ │ │ │ ├── RoleDTO.java │ │ │ │ ├── RouterDTO.java │ │ │ │ └── UserDTO.java │ │ │ └── vo │ │ │ │ ├── AvailableResourceListVO.java │ │ │ │ ├── ResourceDetailVO.java │ │ │ │ ├── ResourceListVO.java │ │ │ │ ├── ResourcePermissionVO.java │ │ │ │ ├── RoleDetailVO.java │ │ │ │ ├── RoleListVO.java │ │ │ │ ├── RouterDetailVO.java │ │ │ │ ├── RouterListVO.java │ │ │ │ ├── RouterNavVO.java │ │ │ │ ├── UserDetailVO.java │ │ │ │ └── UserListVO.java │ │ │ ├── security │ │ │ ├── ChainDefinitionSectionMetaSource.java │ │ │ ├── JwtAuthcFilter.java │ │ │ ├── JwtDefaultSubjectFactory.java │ │ │ ├── JwtRealm.java │ │ │ ├── JwtToken.java │ │ │ └── RestfulPermissionFilter.java │ │ │ ├── service │ │ │ ├── ResourceService.java │ │ │ ├── ResourceServiceImpl.java │ │ │ ├── RoleService.java │ │ │ ├── RoleServiceImpl.java │ │ │ ├── RouterService.java │ │ │ ├── RouterServiceImpl.java │ │ │ ├── UserService.java │ │ │ └── UserServiceImpl.java │ │ │ └── utils │ │ │ └── JwtUtils.java │ └── resources │ │ └── mapper.com.hu.zhcc.shiro.dao │ │ ├── ResourceDAO.xml │ │ ├── ResourcePermissionDAO.xml │ │ ├── RoleDAO.xml │ │ ├── RouterDAO.xml │ │ ├── RouterPermissionDAO.xml │ │ ├── UserDAO.xml │ │ └── UserRoleDAO.xml │ └── test │ ├── java │ └── com │ │ └── hu │ │ └── TestParameter8.java │ └── resources │ └── spring-test-mybatis.xml ├── zhcc-test ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── hu │ │ └── zhcc │ │ └── BaseJunit4Test.java │ └── resources │ ├── db.properties │ ├── logback.xml │ ├── mybatis │ └── mybatis-config.xml │ └── spring │ ├── applicationContext-jedis.xml │ ├── applicationContext.xml │ └── spring-mybatis.xml └── zhcc-web ├── .gitignore ├── Dockerfile ├── pom.xml └── src └── main ├── resources ├── db.properties ├── logback.xml ├── mybatis │ └── mybatis-config.xml └── spring │ ├── applicationContext-jedis.xml │ ├── applicationContext.xml │ ├── spring-beans.xml │ ├── spring-mvc.xml │ ├── spring-mybatis.xml │ └── spring-shiro.xml └── webapp ├── WEB-INF └── web.xml ├── index.html ├── sql └── zhcc.sql └── static ├── css ├── app.bba252ef599858dbecf587e65afe3e37.css └── app.bba252ef599858dbecf587e65afe3e37.css.map ├── fonts └── element-icons.6f0a763.ttf ├── img └── personal_avatar.67a7159.png └── js ├── 0.db58244402cde7c6d910.js ├── 0.db58244402cde7c6d910.js.map ├── 1.d291e6bff81b0d67d3f3.js ├── 1.d291e6bff81b0d67d3f3.js.map ├── app.3d68ad0c82fb017b0ed3.js ├── app.3d68ad0c82fb017b0ed3.js.map ├── manifest.bb527afcf3012cd9ef8e.js ├── manifest.bb527afcf3012cd9ef8e.js.map ├── vendor.9585d0402147dbec02e5.js └── vendor.9585d0402147dbec02e5.js.map /.gitignore: -------------------------------------------------------------------------------- 1 | # DIY 2 | target/ 3 | 4 | # svn 5 | .svn/ 6 | # Linux System 7 | *~ 8 | 9 | # KDE directory preferences 10 | .directory 11 | 12 | # Linux trash folder which might appear on any partition or disk 13 | .Trash-* 14 | 15 | # Windows System 16 | # Windows image file caches 17 | Thumbs.db 18 | ehthumbs.db 19 | 20 | # Folder config file 21 | Desktop.ini 22 | 23 | # Recycle Bin used on file shares 24 | $RECYCLE.BIN/ 25 | 26 | # Windows Installer files 27 | *.cab 28 | *.msi 29 | *.msm 30 | *.msp 31 | 32 | # Windows shortcuts 33 | *.lnk 34 | 35 | # OSX System 36 | .DS_Store 37 | .AppleDouble 38 | .LSOverride 39 | 40 | # Icon must end with two \r 41 | Icon 42 | 43 | 44 | # Thumbnails 45 | ._* 46 | 47 | # Files that might appear in the root of a volume 48 | .DocumentRevisions-V100 49 | .fseventsd 50 | .Spotlight-V100 51 | .TemporaryItems 52 | .Trashes 53 | .VolumeIcon.icns 54 | 55 | # Directories potentially created on remote AFP share 56 | .AppleDB 57 | .AppleDesktop 58 | Network Trash Folder 59 | Temporary Items 60 | .apdisk 61 | 62 | # Eclipse 63 | *.pydevproject 64 | .metadata 65 | .gradle 66 | bin/ 67 | tmp/ 68 | *.tmp 69 | *.bak 70 | *.swp 71 | *~.nib 72 | local.properties 73 | .settings/ 74 | .loadpath 75 | 76 | # Eclipse Core 77 | .project 78 | 79 | # External tool builders 80 | .externalToolBuilders/ 81 | 82 | # Locally stored "Eclipse launch configurations" 83 | *.launch 84 | 85 | # CDT-specific 86 | .cproject 87 | 88 | # JDT-specific (Eclipse Java Development Tools) 89 | .classpath 90 | 91 | # Java annotation processor (APT) 92 | .factorypath 93 | # PDT-specific 94 | .buildpath 95 | # sbteclipse plugin 96 | .target 97 | # TeXlipse plugin 98 | .texlipse 99 | # JetBrains 100 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio 101 | *.iml 102 | ## Directory-based project format: 103 | .idea/ 104 | # if you remove the above rule, at least ignore the following: 105 | # User-specific stuff: 106 | # .idea/workspace.xml 107 | # .idea/tasks.xml 108 | # .idea/dictionaries 109 | # Sensitive or high-churn files: 110 | # .idea/dataSources.ids 111 | # .idea/dataSources.xml 112 | # .idea/sqlDataSources.xml 113 | # .idea/dynamic.xml 114 | # .idea/uiDesigner.xml 115 | # Gradle: 116 | # .idea/gradle.xml 117 | # .idea/libraries 118 | # Mongo Explorer plugin: 119 | # .idea/mongoSettings.xml 120 | ## File-based project format: 121 | *.ipr 122 | *.iws 123 | ## Plugin-specific files: 124 | # IntelliJ 125 | /out/ 126 | # mpeltonen/sbt-idea plugin 127 | .idea_modules/ 128 | # JIRA plugin 129 | atlassian-ide-plugin.xml 130 | # Crashlytics plugin (for Android Studio and IntelliJ) 131 | com_crashlytics_export_strings.xml 132 | crashlytics.properties 133 | crashlytics-build.properties 134 | # JAVA 135 | 136 | *.class 137 | # Mobile Tools for Java (J2ME) 138 | .mtj.tmp/ 139 | # Package Files # 140 | *.jar 141 | *.war 142 | *.ear 143 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 144 | hs_err_pid* 145 | #Maven 146 | target/ 147 | **/target/ 148 | pom.xml.tag 149 | pom.xml.releaseBackup 150 | pom.xml.versionsBackup 151 | pom.xml.next 152 | release.properties 153 | dependency-reduced-pom.xml 154 | buildNumber.properties 155 | .mvn/timing.properties 156 | logs/ 157 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.hu.zhcc 7 | zhcc-server 8 | 1.0 9 | pom 10 | 11 | zhcc-server 12 | http://hulichao.top 13 | 14 | UTF-8 15 | 1.8 16 | 3.7.0 17 | 4.3.14.RELEASE 18 | 1.6.0 19 | 20 | 21 | 22 | 23 | org.slf4j 24 | slf4j-api 25 | 1.7.25 26 | 27 | 28 | ch.qos.logback 29 | logback-classic 30 | 1.2.3 31 | 32 | 33 | org.apache.commons 34 | commons-pool2 35 | 2.5.0 36 | 37 | 38 | org.apache.commons 39 | commons-lang3 40 | 3.7 41 | 42 | 43 | 44 | org.springframework 45 | spring-test 46 | ${spring.version} 47 | provided 48 | 49 | 50 | org.mybatis 51 | mybatis 52 | 3.5.6 53 | 54 | 55 | org.mybatis 56 | mybatis-spring 57 | 1.3.1 58 | 59 | 60 | mysql 61 | mysql-connector-java 62 | 8.0.16 63 | 64 | 65 | com.alibaba 66 | druid 67 | 1.1.7 68 | 69 | 70 | 71 | org.springframework 72 | spring-context 73 | ${spring.version} 74 | 75 | 76 | org.springframework 77 | spring-context-support 78 | ${spring.version} 79 | 80 | 81 | org.springframework 82 | spring-tx 83 | ${spring.version} 84 | 85 | 86 | org.springframework 87 | spring-jdbc 88 | ${spring.version} 89 | 90 | 91 | 92 | 93 | 94 | javax.servlet 95 | javax.servlet-api 96 | 4.0.0 97 | provided 98 | 99 | 100 | 101 | org.springframework 102 | spring-webmvc 103 | ${spring.version} 104 | 105 | 106 | 107 | io.jsonwebtoken 108 | jjwt 109 | 0.9.0 110 | 111 | 112 | 113 | com.alibaba 114 | fastjson 115 | 1.2.46 116 | 117 | 118 | 119 | com.fasterxml.jackson.core 120 | jackson-databind 121 | 2.9.10.7 122 | 123 | 124 | 125 | com.baidu.unbiz 126 | fluent-validator-jsr303 127 | 1.0.2 128 | 129 | 130 | 131 | 132 | 133 | org.apache.shiro 134 | shiro-core 135 | ${shiro.version} 136 | 137 | 138 | org.apache.shiro 139 | shiro-web 140 | ${shiro.version} 141 | 142 | 143 | org.apache.shiro 144 | shiro-spring 145 | ${shiro.version} 146 | 147 | 148 | 149 | 150 | redis.clients 151 | jedis 152 | 2.8.2 153 | 154 | 155 | 156 | 157 | io.springfox 158 | springfox-swagger2 159 | 2.2.2 160 | 161 | 162 | io.springfox 163 | springfox-swagger-ui 164 | 2.2.2 165 | 166 | 167 | 168 | 169 | 170 | 171 | org.apache.maven.plugins 172 | maven-compiler-plugin 173 | ${maven-compiler-plugin.version} 174 | 175 | ${java.version} 176 | ${java.version} 177 | UTF-8 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | src/main/java 186 | 187 | **/*.properties 188 | **/*.xml 189 | 190 | false 191 | 192 | 193 | src/main/resources 194 | 195 | **/*.properties 196 | **/*.xml 197 | 198 | false 199 | 200 | 201 | 202 | 203 | zhcc-web 204 | zhcc-common 205 | zhcc-shiro 206 | zhcc-test 207 | 208 | 209 | -------------------------------------------------------------------------------- /zhcc-common/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | zhcc-server 7 | com.hu.zhcc 8 | 1.0 9 | 10 | 4.0.0 11 | 12 | zhcc-common 13 | 14 | zhcc-common 15 | http://www.hulichao.top 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /zhcc-common/src/main/java/com/hu/zhcc/common/cache/CacheManager.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.common.cache; 2 | 3 | /** 4 | * 缓存管理器接口,暂时还未实现 5 | * 6 | * @author hulichao 7 | * @date 2018/3/18 8 | */ 9 | public interface CacheManager { 10 | 11 | } 12 | -------------------------------------------------------------------------------- /zhcc-common/src/main/java/com/hu/zhcc/common/entity/Page.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.common.entity; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * 分页查询结果类 7 | * 8 | * Created by hulichao 9 | * @param 10 | */ 11 | public final class Page implements java.io.Serializable { 12 | 13 | private static final long serialVersionUID = 1L; 14 | 15 | private long total; 16 | 17 | private List rows; 18 | 19 | public Page(long total, List rows) { 20 | this.total = total; 21 | this.rows = rows; 22 | } 23 | 24 | public long getTotal() { 25 | return this.total; 26 | } 27 | 28 | public List getRows() { 29 | return this.rows; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /zhcc-common/src/main/java/com/hu/zhcc/common/entity/PageRequest.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.common.entity; 2 | 3 | import java.util.Map; 4 | 5 | /** 6 | * 分页请求类 7 | * 8 | * Created by hulichao 9 | */ 10 | public final class PageRequest implements java.io.Serializable { 11 | 12 | private static final long serialVersionUID = 1L; 13 | 14 | private int offset; 15 | 16 | private int limit; 17 | 18 | private String order; 19 | 20 | private Map parameters; 21 | 22 | public PageRequest(int offset, int limit) { 23 | this(offset, limit, null); 24 | } 25 | 26 | public PageRequest(int offset, int limit, Map parameters) { 27 | this(offset, limit, parameters, null); 28 | } 29 | 30 | public PageRequest(int offset, int limit, Map parameters, String order) { 31 | if (offset < 0) { 32 | throw new IllegalArgumentException("Offset must not be less than zero!"); 33 | } 34 | 35 | if (limit < 1) { 36 | throw new IllegalArgumentException("Limit must not be less than one!"); 37 | } 38 | 39 | this.offset = offset; 40 | this.limit = limit; 41 | this.parameters = parameters; 42 | this.order = order; 43 | } 44 | 45 | public int getPage() { 46 | return this.offset; 47 | } 48 | 49 | public int getSize() { 50 | return this.limit; 51 | } 52 | 53 | public String getOrder() { 54 | return this.order; 55 | } 56 | 57 | public Map getSearch() { 58 | return this.parameters; 59 | } 60 | 61 | public int getOffset() { 62 | return this.offset; 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /zhcc-common/src/main/java/com/hu/zhcc/common/entity/Result.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.common.entity; 2 | 3 | import com.baidu.unbiz.fluentvalidator.ValidationError; 4 | 5 | import java.util.HashMap; 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | /** 10 | * 页面响应通用entity,在本项目中封装到ResponseEntity中返回。 11 | * 12 | * Created by hulichao on 2018/6/20 13 | */ 14 | public class Result extends HashMap { 15 | 16 | private static final long serialVersionUID = 1L; 17 | 18 | public Result() { 19 | put("code", 20000); 20 | } 21 | 22 | public static Result fail() { 23 | return fail(50000, "未知异常,请联系管理员"); 24 | } 25 | 26 | public static Result fail(String msg) { 27 | return fail(50000, msg); 28 | } 29 | 30 | public static Result fail(List fails) { 31 | return fail(fails.get(0).getErrorMsg()).put("data",fails); 32 | } 33 | 34 | public static Result fail(int code, String msg) { 35 | Result result = new Result(); 36 | result.put("code", code); 37 | result.put("msg", msg); 38 | return result; 39 | } 40 | 41 | public static Result success(String msg) { 42 | Result result = new Result(); 43 | result.put("msg", msg); 44 | return result; 45 | } 46 | 47 | public static Result success(Map map) { 48 | Result result = new Result(); 49 | result.putAll(map); 50 | return result; 51 | } 52 | 53 | public static Result success() { 54 | return new Result(); 55 | } 56 | 57 | @Override 58 | public Result put(String key, Object value) { 59 | super.put(key, value); 60 | return this; 61 | } 62 | } -------------------------------------------------------------------------------- /zhcc-common/src/main/java/com/hu/zhcc/common/exception/CustomException.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.common.exception; 2 | 3 | import org.springframework.http.HttpStatus; 4 | 5 | /** 6 | * 客户端异常类, CustomException 7 | * 8 | * @author hulichao 9 | * @date 2018/7/20 10 | */ 11 | public class CustomException extends RuntimeException { 12 | 13 | private static final long serialVersionUID = -2499344353634158686L; 14 | 15 | private HttpStatus httpStatus; 16 | 17 | private int code; 18 | 19 | public CustomException(HttpStatus httpStatus, int code, String message) { 20 | this(message); 21 | this.httpStatus = httpStatus; 22 | this.code = code; 23 | } 24 | 25 | public CustomException(String message) { 26 | super(message); 27 | this.httpStatus = HttpStatus.INTERNAL_SERVER_ERROR; 28 | this.code = 50000; 29 | } 30 | 31 | public CustomException(String message, int code, Exception e) { 32 | 33 | super(message, e.getCause()); 34 | this.code = code; 35 | } 36 | 37 | public HttpStatus getHttpStatus() { 38 | return httpStatus; 39 | } 40 | 41 | public void setHttpStatus(HttpStatus httpStatus) { 42 | this.httpStatus = httpStatus; 43 | } 44 | 45 | public int getCode() { 46 | return code; 47 | } 48 | 49 | public void setCode(int state) { 50 | this.code = code; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /zhcc-common/src/main/java/com/hu/zhcc/common/exception/DAOException.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.common.exception; 2 | 3 | import org.springframework.http.HttpStatus; 4 | 5 | /** 6 | * dao层异常 7 | * 8 | * Created by hulichao 9 | */ 10 | public class DAOException extends RuntimeException { 11 | 12 | private static final long serialVersionUID = -4537960897528403282L; 13 | 14 | private HttpStatus httpStatus; 15 | 16 | private int code; 17 | 18 | public DAOException(HttpStatus httpStatus, int code, String message) { 19 | this(message); 20 | this.httpStatus = httpStatus; 21 | this.code = code; 22 | } 23 | 24 | public DAOException(String message) { 25 | super(message); 26 | this.httpStatus = HttpStatus.INTERNAL_SERVER_ERROR; 27 | this.code = 50000; 28 | } 29 | 30 | public DAOException(String message, int code, Exception e) { 31 | 32 | super(message, e.getCause()); 33 | this.code = code; 34 | } 35 | 36 | public HttpStatus getHttpStatus() { 37 | return httpStatus; 38 | } 39 | 40 | public void setHttpStatus(HttpStatus httpStatus) { 41 | this.httpStatus = httpStatus; 42 | } 43 | 44 | public int getCode() { 45 | return code; 46 | } 47 | 48 | public void setCode(int state) { 49 | this.code = code; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /zhcc-common/src/main/java/com/hu/zhcc/common/exception/ExceptionEnum.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.common.exception; 2 | import org.springframework.http.HttpStatus; 3 | 4 | /** 5 | * 异常枚举,设置目的,异常格式化调用 6 | * 7 | * @author hulichao 8 | * @date 2018/7/20 9 | */ 10 | public enum ExceptionEnum { 11 | LOGIN_USERNAME_ERROR(40001, "登录失败,用户名不存在", HttpStatus.BAD_REQUEST), 12 | 13 | LOGIN_PASSWORD_ERROR(40002, "登录失败,密码错误", HttpStatus.BAD_REQUEST), 14 | 15 | RESOURCE_PERMISSSION_ERROR(40101, "没有权限,请联系管理员", HttpStatus.UNAUTHORIZED); 16 | 17 | private int code; 18 | 19 | private String msg; 20 | 21 | private HttpStatus httpStatus; 22 | 23 | ExceptionEnum(int code, String msg, HttpStatus status) { 24 | this.code = code; 25 | this.msg = msg; 26 | this.httpStatus = status; 27 | } 28 | 29 | public int getCode() { 30 | return code; 31 | } 32 | 33 | public void setCode(int code) { 34 | this.code = code; 35 | } 36 | 37 | public String getMsg() { 38 | return msg; 39 | } 40 | 41 | public void setMsg(String msg) { 42 | this.msg = msg; 43 | } 44 | 45 | public HttpStatus getHttpStatus() { 46 | return httpStatus; 47 | } 48 | 49 | public void setHttpStatus(HttpStatus httpStatus) { 50 | this.httpStatus = httpStatus; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /zhcc-common/src/main/java/com/hu/zhcc/common/exception/GlobalCustomRestExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.common.exception; 2 | 3 | import com.hu.zhcc.common.entity.Result; 4 | import org.springframework.core.Ordered; 5 | import org.springframework.core.annotation.Order; 6 | import org.springframework.http.HttpHeaders; 7 | import org.springframework.http.HttpStatus; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.web.HttpRequestMethodNotSupportedException; 10 | import org.springframework.web.bind.annotation.ControllerAdvice; 11 | import org.springframework.web.bind.annotation.ExceptionHandler; 12 | import org.springframework.web.context.request.WebRequest; 13 | import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; 14 | 15 | /** 16 | * 全局Rest异常处理 17 | * 18 | * @author hulichao 19 | * @date 2018/7/20 20 | */ 21 | @ControllerAdvice 22 | @Order(Ordered.HIGHEST_PRECEDENCE) 23 | public class GlobalCustomRestExceptionHandler extends ResponseEntityExceptionHandler { 24 | //处理客户端自定义异常 25 | @ExceptionHandler(CustomException.class) 26 | public ResponseEntity handleCustomerException(CustomException ex) { 27 | 28 | final Result error = Result.fail(ex.getCode(), ex.getLocalizedMessage()); 29 | 30 | return new ResponseEntity(error, new HttpHeaders(), ex.getHttpStatus()); 31 | 32 | } 33 | 34 | //处理Service层自定义异常 35 | @ExceptionHandler(ServiceException.class) 36 | public ResponseEntity handleServiceException(ServiceException ex) { 37 | 38 | final Result error = Result.fail(ex.getCode(), ex.getLocalizedMessage()); 39 | 40 | return new ResponseEntity(error, new HttpHeaders(), ex.getHttpStatus()); 41 | 42 | } 43 | 44 | //处理Dao层自定义异常 45 | @ExceptionHandler(DAOException.class) 46 | public ResponseEntity handleDAOException(DAOException ex) { 47 | 48 | final Result error = Result.fail(ex.getCode(), ex.getLocalizedMessage()); 49 | 50 | return new ResponseEntity(error, new HttpHeaders(), ex.getHttpStatus()); 51 | 52 | } 53 | 54 | //这里请求方法不支持的异常 55 | @Override 56 | protected ResponseEntity handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, 57 | HttpHeaders headers, HttpStatus status, WebRequest request) { 58 | final Result error = Result.fail(status.value(), "HttpRequestMethodNotSupported"); 59 | return new ResponseEntity(error, new HttpHeaders(), status); 60 | } 61 | // 处理通用(上面异常处理不了的异常)异常 62 | @ExceptionHandler(Exception.class) 63 | protected ResponseEntity handleFutureException(Exception ex, 64 | HttpHeaders headers, HttpStatus status, WebRequest request) { 65 | final Result error = Result.fail(status.value(), "未知异常,请联系管理员"); 66 | return new ResponseEntity(error, new HttpHeaders(), status); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /zhcc-common/src/main/java/com/hu/zhcc/common/exception/ServiceException.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.common.exception; 2 | 3 | import org.springframework.http.HttpStatus; 4 | 5 | /** 6 | * service层异常 7 | * 8 | * @author hulichao 9 | * @date 2018/3/15 10 | */ 11 | public class ServiceException extends RuntimeException { 12 | 13 | 14 | private HttpStatus httpStatus; 15 | 16 | private int code; 17 | 18 | public ServiceException(HttpStatus httpStatus, int code, String message) { 19 | this(message); 20 | this.httpStatus = httpStatus; 21 | this.code = code; 22 | } 23 | 24 | public ServiceException(String message) { 25 | super(message); 26 | this.httpStatus = HttpStatus.INTERNAL_SERVER_ERROR; 27 | this.code = 50000; 28 | } 29 | 30 | public ServiceException(String message, int code, Exception e) { 31 | 32 | super(message, e.getCause()); 33 | this.code = code; 34 | } 35 | 36 | public HttpStatus getHttpStatus() { 37 | return httpStatus; 38 | } 39 | 40 | public void setHttpStatus(HttpStatus httpStatus) { 41 | this.httpStatus = httpStatus; 42 | } 43 | 44 | public int getCode() { 45 | return code; 46 | } 47 | 48 | public void setCode(int state) { 49 | this.code = code; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /zhcc-common/src/main/java/com/hu/zhcc/common/filter/CORSFilter.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.common.filter; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.Filter; 6 | import javax.servlet.FilterChain; 7 | import javax.servlet.FilterConfig; 8 | import javax.servlet.ServletException; 9 | import javax.servlet.ServletRequest; 10 | import javax.servlet.ServletResponse; 11 | import javax.servlet.http.HttpServletResponse; 12 | 13 | /** 14 | * 跨域过滤器 15 | * 16 | * @author hulichao 17 | * @date 2018/3/12 18 | */ 19 | public class CORSFilter implements Filter { 20 | 21 | public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) 22 | throws IOException, ServletException { 23 | HttpServletResponse response = (HttpServletResponse) res; 24 | response.setHeader("Access-Control-Allow-Origin", "*"); 25 | response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE"); 26 | response.setHeader("Access-Control-Max-Age", "3600"); 27 | response.setHeader("Access-Control-Allow-Headers", "Content-Type, X-Token"); 28 | chain.doFilter(req, res); 29 | } 30 | 31 | public void init(FilterConfig filterConfig) { 32 | } 33 | 34 | public void destroy() { 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /zhcc-common/src/main/java/com/hu/zhcc/common/utils/DateTimeUtils.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.common.utils; 2 | 3 | import java.text.ParseException; 4 | import java.util.Date; 5 | 6 | /** 7 | * 日期时间工具类 8 | * @author Administrator 9 | * 10 | */ 11 | public final class DateTimeUtils { 12 | 13 | /** 14 | * 得到UTC日期时间 15 | * @param date 16 | * @param format 17 | * @return 18 | */ 19 | public static Date getUTCDate(String date, String format) { 20 | java.text.SimpleDateFormat simpleDateFormat = new java.text.SimpleDateFormat(format); 21 | java.util.TimeZone utcZone = java.util.TimeZone.getTimeZone("UTC"); 22 | simpleDateFormat.setTimeZone(utcZone); 23 | try { 24 | Date utcDate = simpleDateFormat.parse(date); 25 | return utcDate; 26 | } catch (ParseException e) { 27 | return null; 28 | } 29 | } 30 | 31 | /** 32 | * 得到UTC日期时间 33 | * @param date 34 | * @return 35 | */ 36 | public static Date getUTCDate(String date) { 37 | return getUTCDate(date, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /zhcc-common/src/main/java/com/hu/zhcc/common/utils/PropertiesFileUtil.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.common.utils; 2 | 3 | import java.util.Date; 4 | import java.util.HashMap; 5 | import java.util.MissingResourceException; 6 | import java.util.ResourceBundle; 7 | 8 | /** 9 | * 资源文件读取工具 10 | * @author hulichao 11 | * @date 2018/8/20 12 | */ 13 | public class PropertiesFileUtil { 14 | 15 | // 当打开多个资源文件时,缓存资源文件 16 | private static HashMap configMap = new HashMap(); 17 | // 打开文件时间,判断超时使用 18 | private Date loadTime = null; 19 | // 资源文件 20 | private ResourceBundle resourceBundle = null; 21 | // 默认资源文件名称 22 | private static final String NAME = "config"; 23 | // 缓存时间 24 | private static final Integer TIME_OUT = 60 * 1000; 25 | 26 | // 私有构造方法,创建单例 27 | private PropertiesFileUtil(String name) { 28 | this.loadTime = new Date(); 29 | this.resourceBundle = ResourceBundle.getBundle(name); 30 | } 31 | 32 | public static synchronized PropertiesFileUtil getInstance() { 33 | return getInstance(NAME); 34 | } 35 | 36 | public static synchronized PropertiesFileUtil getInstance(String name) { 37 | PropertiesFileUtil conf = configMap.get(name); 38 | if (null == conf) { 39 | conf = new PropertiesFileUtil(name); 40 | configMap.put(name, conf); 41 | } 42 | // 判断是否打开的资源文件是否超时1分钟 43 | if ((System.currentTimeMillis() - conf.getLoadTime().getTime()) > TIME_OUT) { 44 | conf = new PropertiesFileUtil(name); 45 | configMap.put(name, conf); 46 | } 47 | return conf; 48 | } 49 | 50 | // 根据key读取value 51 | public String get(String key) { 52 | try { 53 | String value = resourceBundle.getString(key); 54 | return value; 55 | } catch (MissingResourceException e) { 56 | return ""; 57 | } 58 | } 59 | 60 | // 根据key读取value(整形) 61 | public Integer getInt(String key) { 62 | try { 63 | String value = resourceBundle.getString(key); 64 | return Integer.parseInt(value); 65 | } catch (MissingResourceException e) { 66 | return null; 67 | } 68 | } 69 | 70 | // 根据key读取value(布尔) 71 | public boolean getBool(String key) { 72 | try { 73 | String value = resourceBundle.getString(key); 74 | if ("true".equals(value)) { 75 | return true; 76 | } 77 | return false; 78 | } catch (MissingResourceException e) { 79 | return false; 80 | } 81 | } 82 | 83 | public Date getLoadTime() { 84 | return loadTime; 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /zhcc-common/src/main/java/com/hu/zhcc/common/utils/clearMaven.py: -------------------------------------------------------------------------------- 1 | import os 2 | import shutil 3 | from datetime import datetime 4 | 5 | # maven仓库地址 6 | mvnHome = "/Users/hulichao/.m2/repository" 7 | # 删除该日期前的文件以及文件夹 8 | deleteDateBefore = datetime(2018,8,20,0,0,0) 9 | 10 | 11 | def listPathAndClean(pathContext): 12 | pathDir = os.listdir(pathContext) 13 | for filename in pathDir: 14 | filepath = os.path.join(pathContext, filename) 15 | currentTimeFile = datetime.fromtimestamp(os.path.getmtime(filepath)) 16 | 17 | # 对比时间 18 | if deleteDateBefore > currentTimeFile: 19 | print("filePath:"+filepath+"-----updatetime:"+str(currentTimeFile)) 20 | print('delete this') 21 | if (os.path.isdir(filepath)): 22 | shutil.rmtree(filepath) 23 | else: 24 | os.remove(filepath) 25 | continue 26 | 27 | # 不到期的则深入遍历 28 | if os.path.isdir(filepath): 29 | listPathAndClean(filepath) 30 | 31 | 32 | 33 | if __name__ == '__main__': 34 | print(deleteDateBefore) 35 | print('start list should delete path') 36 | listPathAndClean(mvnHome) -------------------------------------------------------------------------------- /zhcc-shiro/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | zhcc-server 7 | com.hu.zhcc 8 | 1.0 9 | 10 | 4.0.0 11 | 12 | zhcc-shiro 13 | 14 | zhcc-shiro 15 | http://www.hulichao.top 16 | 17 | 18 | 19 | org.pegdown 20 | pegdown 21 | 1.6.0 22 | 23 | 24 | com.hu.zhcc 25 | zhcc-common 26 | 1.0 27 | 28 | 29 | com.hu.zhcc 30 | zhcc-test 31 | 1.0 32 | test 33 | 34 | 35 | junit 36 | junit 37 | 4.13.1 38 | test 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/controller/AuthController.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.controller; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | import com.hu.zhcc.shiro.entity.dto.RouterDTO; 5 | import com.hu.zhcc.shiro.entity.dto.UserDTO; 6 | import com.hu.zhcc.shiro.entity.vo.RouterNavVO; 7 | import com.hu.zhcc.shiro.service.RouterService; 8 | import com.hu.zhcc.shiro.service.UserService; 9 | import com.hu.zhcc.shiro.utils.JwtUtils; 10 | import org.apache.shiro.crypto.hash.Md5Hash; 11 | import org.springframework.beans.BeanUtils; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.http.HttpStatus; 14 | import org.springframework.http.ResponseEntity; 15 | import org.springframework.web.bind.annotation.PostMapping; 16 | import org.springframework.web.bind.annotation.RequestMapping; 17 | import org.springframework.web.bind.annotation.RestController; 18 | 19 | import javax.servlet.http.HttpServletRequest; 20 | import java.util.ArrayList; 21 | import java.util.HashMap; 22 | import java.util.List; 23 | import java.util.Map; 24 | 25 | /** 26 | * 登陆系统第一步:获取token 27 | * 28 | * 29 | */ 30 | @RestController 31 | @RequestMapping("/auth") 32 | public class AuthController { 33 | 34 | @Autowired 35 | private UserService userService; 36 | 37 | @Autowired 38 | private RouterService routerService; 39 | 40 | @Autowired 41 | private JwtUtils jwtUtils; 42 | 43 | @PostMapping("/token") 44 | public ResponseEntity getToken(HttpServletRequest request, String loginName, String password) { 45 | // 验证用户信息 46 | UserDTO user = userService.getByLoginName(loginName); 47 | if(user == null) { 48 | return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("用户名不存在!"); 49 | } 50 | if(user.getLocked()) { 51 | return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("该账户被锁定!"); 52 | } 53 | Md5Hash md5 = new Md5Hash(password, user.getSalt(), 6); 54 | String md5Password = md5.toHex(); 55 | if(!md5Password.equals(user.getPassword())) { 56 | return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("登录失败,用户名或密码错误!"); 57 | } 58 | 59 | // 得到可访问的router 60 | List dtoList = routerService.listAuthorizedRouter(user.getId()); 61 | List voList = new ArrayList(dtoList.size()); 62 | for(RouterDTO dto : dtoList) { 63 | RouterNavVO vo = new RouterNavVO(); 64 | BeanUtils.copyProperties(dto, vo); 65 | voList.add(vo); 66 | } 67 | 68 | Map response = new HashMap(); 69 | response.put("userId", user.getId().toString()); 70 | response.put("username", user.getName()); 71 | response.put("routers", voList); 72 | JSONObject json = new JSONObject(); 73 | json.put("userId", user.getId()); 74 | json.put("username", user.getName()); 75 | json.put("host", request.getRemoteHost()); 76 | response.put("token", jwtUtils.createJWT(json.toJSONString())); 77 | return ResponseEntity.ok(response); 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/controller/BaseController.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.controller; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | import com.hu.zhcc.shiro.utils.JwtUtils; 5 | import io.jsonwebtoken.Claims; 6 | 7 | /** 8 | * Controllerg公用基类 9 | * 包含全局异常和jwt--> subject 10 | * 处理全局异常两种方式 11 | * 1.如本类中,继承一个Controller(已注释) 12 | * 2.使用全局异常bean来处理 13 | * 14 | * 15 | * @author hulichao 16 | * @date 2018/3/15 17 | */ 18 | public class BaseController { 19 | 20 | /* @ExceptionHandler 21 | public ResponseEntity exceptionHandler(HttpServletRequest request, Exception e) { 22 | //FIXME 23 | return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("系统管理员跑路拉,您再自己瞅瞅吧"); 24 | }*/ 25 | 26 | /** 27 | * 从jwt中获取subject信息 28 | * @param jwtUtils 29 | * @param jwt 30 | * @param key 31 | * @return 32 | */ 33 | protected String getSubjectFromJwt(JwtUtils jwtUtils, String jwt, String key) { 34 | Claims claims = jwtUtils.parseJWT(jwt); 35 | String subject = claims.getSubject(); 36 | if(key != null && !key.trim().equals("")) { 37 | JSONObject json = JSONObject.parseObject(subject); 38 | return json.getString(key); 39 | } else { 40 | return subject; 41 | } 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/controller/HomeController.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.controller; 2 | 3 | 4 | import com.hu.zhcc.shiro.entity.dto.ResourceDTO; 5 | import com.hu.zhcc.shiro.service.ResourceService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Controller; 8 | import org.springframework.web.bind.annotation.CookieValue; 9 | import org.springframework.web.bind.annotation.GetMapping; 10 | import org.springframework.web.servlet.ModelAndView; 11 | 12 | import com.hu.zhcc.shiro.utils.JwtUtils; 13 | 14 | import java.util.List; 15 | 16 | /** 17 | * 在认证后执行的 18 | * 获取首页资源 19 | * 20 | */ 21 | @Controller 22 | public class HomeController extends BaseController { 23 | 24 | @Autowired 25 | private ResourceService resourceService; 26 | 27 | @Autowired 28 | private JwtUtils jwtUtils; 29 | 30 | @GetMapping("/index") 31 | public ModelAndView index(@CookieValue(value="jwt", required = false) String jwt) { 32 | if(jwt == null){ 33 | ModelAndView result = new ModelAndView("login.html"); 34 | return result; 35 | } 36 | //得到用户id 37 | String currentUserId = this.getSubjectFromJwt(jwtUtils, jwt, "userId"); 38 | // 得到所有已授权的菜单 39 | List list = this.resourceService.listAuthorizedByUserId(Integer.parseInt(currentUserId)); 40 | ModelAndView result = new ModelAndView("index"); 41 | result.addObject("menuList", list); 42 | return result; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/controller/ResourceController.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.controller; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.hu.zhcc.shiro.entity.dto.AvailableResourceDTO; 7 | import com.hu.zhcc.shiro.entity.dto.ResourceDTO; 8 | import com.hu.zhcc.shiro.entity.dto.ResourcePermissionDTO; 9 | import com.hu.zhcc.shiro.service.ResourceService; 10 | import com.hu.zhcc.shiro.entity.vo.ResourceDetailVO; 11 | import com.hu.zhcc.shiro.entity.vo.ResourceListVO; 12 | import org.springframework.beans.BeanUtils; 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.http.HttpStatus; 15 | import org.springframework.http.ResponseEntity; 16 | import org.springframework.web.bind.annotation.DeleteMapping; 17 | import org.springframework.web.bind.annotation.GetMapping; 18 | import org.springframework.web.bind.annotation.PathVariable; 19 | import org.springframework.web.bind.annotation.PostMapping; 20 | import org.springframework.web.bind.annotation.PutMapping; 21 | import org.springframework.web.bind.annotation.RequestBody; 22 | import org.springframework.web.bind.annotation.RequestHeader; 23 | import org.springframework.web.bind.annotation.RequestMapping; 24 | import org.springframework.web.bind.annotation.RequestParam; 25 | import org.springframework.web.bind.annotation.RestController; 26 | 27 | import com.hu.zhcc.shiro.utils.JwtUtils; 28 | import com.hu.zhcc.shiro.entity.vo.AvailableResourceListVO; 29 | import com.hu.zhcc.shiro.entity.vo.ResourcePermissionVO; 30 | 31 | /** 32 | * 资源控制器 33 | * 34 | * @author hulichao 35 | * @date 2018/4/11 36 | */ 37 | @RestController 38 | @RequestMapping("/resources") 39 | public class ResourceController extends BaseController { 40 | 41 | @Autowired 42 | private ResourceService resourceService; 43 | 44 | @Autowired 45 | private JwtUtils jwtUtils; 46 | 47 | @GetMapping("") 48 | public ResponseEntity> listResource(@RequestParam() int routerId) { 49 | List dtoList = this.resourceService.listByRouterId(routerId); 50 | List voList = new ArrayList(dtoList.size()); 51 | for(ResourceDTO dto : dtoList) { 52 | ResourceListVO vo = new ResourceListVO(); 53 | BeanUtils.copyProperties(dto, vo); 54 | voList.add(vo); 55 | } 56 | return ResponseEntity.ok(voList); 57 | } 58 | 59 | @GetMapping("/{id}") 60 | public ResponseEntity getById(@PathVariable("id") int id) { 61 | ResourceDTO dto = this.resourceService.getById(id); 62 | if(dto == null) { 63 | return ResponseEntity.notFound().build(); 64 | } 65 | ResourceDetailVO vo = new ResourceDetailVO(); 66 | BeanUtils.copyProperties(dto, vo); 67 | return ResponseEntity.ok(vo); 68 | } 69 | 70 | @PutMapping("/{id}") 71 | public ResponseEntity updateResource(@RequestBody ResourceDetailVO vo) { 72 | ResourceDTO dto = new ResourceDTO(); 73 | BeanUtils.copyProperties(vo, dto); 74 | int rows = this.resourceService.updateResource(dto); 75 | return rows == 0 ? ResponseEntity.notFound().build() : 76 | ResponseEntity.status(HttpStatus.CREATED).body(rows); 77 | } 78 | 79 | @PostMapping("") 80 | public ResponseEntity saveResource(@RequestBody ResourceDetailVO vo) { 81 | ResourceDTO dto = new ResourceDTO(); 82 | BeanUtils.copyProperties(vo, dto); 83 | int id = this.resourceService.saveResource(dto); 84 | vo.setId(id); 85 | return ResponseEntity.ok(vo); 86 | } 87 | 88 | @DeleteMapping("/{id}") 89 | public ResponseEntity removeResource(@PathVariable("id") int id) { 90 | int rows = this.resourceService.removeResource(id); 91 | return rows == 0 ? ResponseEntity.notFound().build() : 92 | ResponseEntity.noContent().build(); 93 | } 94 | 95 | @GetMapping("/permissions") 96 | public ResponseEntity> listResourcePermission( 97 | @RequestHeader(value="X-Token") String token, @RequestParam(required = false) int routerId) { 98 | String currentUserId = this.getSubjectFromJwt(jwtUtils, token, "userId"); 99 | List dtoList = resourceService.listPermission(Integer.parseInt(currentUserId), routerId); 100 | List voList = new ArrayList(); 101 | for(ResourcePermissionDTO dto : dtoList) { 102 | ResourcePermissionVO vo = new ResourcePermissionVO(); 103 | BeanUtils.copyProperties(dto, vo); 104 | voList.add(vo); 105 | } 106 | return ResponseEntity.ok(voList); 107 | } 108 | 109 | @GetMapping("/available") 110 | public ResponseEntity> listAvailableResource() { 111 | List dtoList = this.resourceService.listAllAvailable(); 112 | List voList = new ArrayList(); 113 | for(AvailableResourceDTO dto : dtoList) { 114 | AvailableResourceListVO vo = new AvailableResourceListVO(); 115 | BeanUtils.copyProperties(dto, vo); 116 | voList.add(vo); 117 | } 118 | return ResponseEntity.ok(voList); 119 | } 120 | 121 | } 122 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/controller/RoleController.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.controller; 2 | 3 | import com.hu.zhcc.common.entity.Page; 4 | import com.hu.zhcc.shiro.entity.dto.PermissionDTO; 5 | import com.hu.zhcc.shiro.entity.dto.RoleDTO; 6 | import com.hu.zhcc.shiro.entity.vo.RoleDetailVO; 7 | import com.hu.zhcc.shiro.entity.vo.RoleListVO; 8 | import com.hu.zhcc.shiro.service.RoleService; 9 | import org.apache.commons.lang3.StringUtils; 10 | import org.springframework.beans.BeanUtils; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.http.HttpStatus; 13 | import org.springframework.http.ResponseEntity; 14 | import org.springframework.web.bind.annotation.*; 15 | 16 | import java.util.ArrayList; 17 | import java.util.HashMap; 18 | import java.util.List; 19 | import java.util.Map; 20 | 21 | 22 | /** 23 | * 角色控制器 24 | */ 25 | @RestController 26 | @RequestMapping("/roles") 27 | public class RoleController extends BaseController { 28 | 29 | @Autowired 30 | private RoleService roleService; 31 | 32 | @GetMapping("") 33 | public ResponseEntity> getRoleList(@RequestParam(required = false) String name, 34 | int offset, int limit) { 35 | Map paremeters = new HashMap(); 36 | if (!StringUtils.isBlank(name)) { 37 | paremeters.put("name", name); 38 | } 39 | Page result = this.roleService.listRole(paremeters, offset, limit); 40 | List voList = new ArrayList(); 41 | for(RoleDTO dto : result.getRows()) { 42 | RoleListVO vo = new RoleListVO(); 43 | BeanUtils.copyProperties(dto, vo); 44 | voList.add(vo); 45 | } 46 | return ResponseEntity.ok(new Page(result.getTotal(), voList)); 47 | } 48 | 49 | @PostMapping("") 50 | public ResponseEntity saveRole(@RequestBody RoleDetailVO vo) { 51 | RoleDTO dto = new RoleDTO(); 52 | BeanUtils.copyProperties(vo, dto); 53 | this.roleService.saveRole(dto); 54 | vo.setId(dto.getId()); 55 | return ResponseEntity.status(HttpStatus.CREATED).body(vo); 56 | } 57 | 58 | @GetMapping("/{id}") 59 | public ResponseEntity getById(@PathVariable("id") int id) { 60 | RoleDTO dto = this.roleService.getById(id); 61 | if(dto == null) { 62 | return ResponseEntity.notFound().build(); 63 | } 64 | 65 | RoleDetailVO vo = new RoleDetailVO(); 66 | BeanUtils.copyProperties(dto, vo); 67 | return ResponseEntity.ok(vo); 68 | } 69 | 70 | @PutMapping("/{id}") 71 | public ResponseEntity updateRole(@PathVariable("id") int id, @RequestBody RoleDetailVO vo) { 72 | RoleDTO dto = new RoleDTO(); 73 | BeanUtils.copyProperties(vo, dto); 74 | int rows = this.roleService.updateRole(dto); 75 | return rows == 0 ? ResponseEntity.notFound().build() : 76 | ResponseEntity.status(HttpStatus.CREATED).body(rows); 77 | } 78 | 79 | @GetMapping("/all") 80 | public ResponseEntity> listAllRole() { 81 | List dtoList = this.roleService.listAllRole(); 82 | if(dtoList.size() == 0) { 83 | return ResponseEntity.notFound().build(); 84 | } 85 | List voList = new ArrayList(dtoList.size()); 86 | for(RoleDTO dto : dtoList) { 87 | RoleListVO vo = new RoleListVO(); 88 | BeanUtils.copyProperties(dto, vo); 89 | voList.add(vo); 90 | } 91 | return ResponseEntity.ok(voList); 92 | } 93 | 94 | @DeleteMapping("/{id}") 95 | public ResponseEntity removeRole(@PathVariable("id") int id) { 96 | return this.roleService.removeRole(id) > 0 ? 97 | ResponseEntity.noContent().build() : ResponseEntity.notFound().build(); 98 | } 99 | 100 | @GetMapping("/{id}/resourcePermissions") 101 | public ResponseEntity> listResourcePermissionByRoleId(@PathVariable("id") int id) { 102 | return ResponseEntity.ok(this.roleService.listResourcePermission(id)); 103 | } 104 | 105 | @PostMapping("/{id}/permissions") 106 | public ResponseEntity savePermission(@PathVariable("id") int id, @RequestBody PermissionDTO permissionDTO) { 107 | int rows = this.roleService.savePermission(id, permissionDTO.getRouterIds(), permissionDTO.getResourceIds()); 108 | return ResponseEntity.status(HttpStatus.CREATED).body(rows); 109 | } 110 | 111 | } -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/controller/RouterController.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.controller; 2 | 3 | import com.hu.zhcc.common.entity.Page; 4 | import com.hu.zhcc.shiro.entity.dto.RouterDTO; 5 | import com.hu.zhcc.shiro.entity.vo.RouterDetailVO; 6 | import com.hu.zhcc.shiro.entity.vo.RouterListVO; 7 | import com.hu.zhcc.shiro.entity.vo.RouterNavVO; 8 | import com.hu.zhcc.shiro.service.RouterService; 9 | import com.hu.zhcc.shiro.utils.JwtUtils; 10 | import org.apache.commons.lang3.StringUtils; 11 | import org.springframework.beans.BeanUtils; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.http.HttpStatus; 14 | import org.springframework.http.ResponseEntity; 15 | import org.springframework.web.bind.annotation.*; 16 | 17 | import java.util.ArrayList; 18 | import java.util.HashMap; 19 | import java.util.List; 20 | import java.util.Map; 21 | 22 | 23 | /** 24 | * 路由控制器 25 | */ 26 | @RestController 27 | @RequestMapping("/routers") 28 | public class RouterController extends BaseController { 29 | 30 | @Autowired 31 | private JwtUtils jwtUtils; 32 | 33 | @Autowired 34 | private RouterService routerService; 35 | 36 | @GetMapping("") 37 | public ResponseEntity> listRouter(@RequestParam(required = false) String name, int offset, int limit) { 38 | Map paremeters = new HashMap(); 39 | if (!StringUtils.isBlank(name)) { 40 | paremeters.put("name", name); 41 | } 42 | Page result = routerService.listRouter(paremeters, offset, limit); 43 | List voList = new ArrayList(result.getRows().size()); 44 | for(RouterDTO dto : result.getRows()) { 45 | RouterListVO vo = new RouterListVO(); 46 | BeanUtils.copyProperties(dto, vo); 47 | voList.add(vo); 48 | } 49 | return ResponseEntity.ok(new Page(result.getTotal(), voList)); 50 | } 51 | 52 | @GetMapping("/authorized") 53 | public ResponseEntity> listAuthorizedRouter(@RequestHeader(value="X-Token") String token) { 54 | String currentUserId = this.getSubjectFromJwt(jwtUtils, token, "userId"); 55 | List dtoList = routerService.listAuthorizedRouter(Integer.parseInt(currentUserId)); 56 | List voList = new ArrayList(dtoList.size()); 57 | for(RouterDTO dto : dtoList) { 58 | RouterNavVO vo = new RouterNavVO(); 59 | BeanUtils.copyProperties(dto, vo); 60 | voList.add(vo); 61 | } 62 | return ResponseEntity.ok(voList); 63 | } 64 | 65 | @GetMapping("/{id}") 66 | public ResponseEntity getById(@PathVariable("id") int id) { 67 | RouterDTO dto = this.routerService.getById(id); 68 | if(dto == null) { 69 | return ResponseEntity.notFound().build(); 70 | } 71 | RouterDetailVO vo = new RouterDetailVO(); 72 | BeanUtils.copyProperties(dto, vo); 73 | return ResponseEntity.ok(vo); 74 | } 75 | 76 | @GetMapping("/search") 77 | public ResponseEntity> listByParentId(@RequestParam() int parentId) { 78 | List dtoList = this.routerService.listByParentId(parentId); 79 | List voList = new ArrayList(dtoList.size()); 80 | for(RouterDTO dto : dtoList) { 81 | RouterListVO vo = new RouterListVO(); 82 | BeanUtils.copyProperties(dto, vo); 83 | voList.add(vo); 84 | } 85 | return ResponseEntity.ok(voList); 86 | } 87 | 88 | @PutMapping("/{id}") 89 | public ResponseEntity updateRouter(@PathVariable("id") int id, @RequestBody RouterDetailVO vo) { 90 | RouterDTO dto = new RouterDTO(); 91 | BeanUtils.copyProperties(vo, dto); 92 | int rows = this.routerService.updateRouter(dto); 93 | return rows == 0 ? ResponseEntity.notFound().build() : ResponseEntity.status(HttpStatus.CREATED).body(rows); 94 | } 95 | 96 | @PostMapping("") 97 | public ResponseEntity saveRouter(@RequestBody RouterDetailVO vo) { 98 | RouterDTO dto = new RouterDTO(); 99 | BeanUtils.copyProperties(vo, dto); 100 | vo.setId(this.routerService.saveRouter(dto).getId()); 101 | return ResponseEntity.status(HttpStatus.CREATED).body(vo); 102 | } 103 | 104 | @DeleteMapping("/{id}") 105 | public ResponseEntity removeRouter(@PathVariable("id") int id) { 106 | return this.routerService.removeRouter(id) > 0 ? 107 | ResponseEntity.noContent().build() : ResponseEntity.notFound().build(); 108 | } 109 | 110 | @GetMapping("/all") 111 | public ResponseEntity> listAll(@RequestParam(required = false) Boolean includeLocked) { 112 | List dtoList = this.routerService.listAll(includeLocked == null ? false : includeLocked); 113 | List voList = new ArrayList(); 114 | for(RouterDTO dto : dtoList) { 115 | RouterListVO vo = new RouterListVO(); 116 | BeanUtils.copyProperties(dto, vo); 117 | voList.add(vo); 118 | } 119 | return ResponseEntity.ok(voList); 120 | } 121 | 122 | } 123 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.controller; 2 | 3 | import com.hu.zhcc.common.entity.Page; 4 | import com.hu.zhcc.shiro.entity.dto.UserDTO; 5 | import com.hu.zhcc.shiro.entity.vo.UserDetailVO; 6 | import com.hu.zhcc.shiro.entity.vo.UserListVO; 7 | import com.hu.zhcc.shiro.service.UserService; 8 | import com.hu.zhcc.shiro.utils.JwtUtils; 9 | import org.apache.commons.lang3.StringUtils; 10 | import org.apache.shiro.crypto.SecureRandomNumberGenerator; 11 | import org.apache.shiro.crypto.hash.Md5Hash; 12 | import org.springframework.beans.BeanUtils; 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.http.HttpStatus; 15 | import org.springframework.http.ResponseEntity; 16 | import org.springframework.web.bind.annotation.*; 17 | 18 | import java.text.SimpleDateFormat; 19 | import java.util.ArrayList; 20 | import java.util.HashMap; 21 | import java.util.List; 22 | import java.util.Map; 23 | 24 | 25 | /** 26 | * 用户控制器 27 | */ 28 | @RestController 29 | @RequestMapping("/users") 30 | public class UserController extends BaseController { 31 | 32 | @Autowired 33 | private UserService userService; 34 | 35 | @Autowired 36 | private JwtUtils jwtUtils; 37 | 38 | @PostMapping("") 39 | public ResponseEntity saveUser(@RequestBody UserDetailVO vo, @RequestHeader(value="X-Token") String token) { 40 | String currentUserId = this.getSubjectFromJwt(jwtUtils, token, "userId"); 41 | 42 | UserDTO dto = new UserDTO(); 43 | dto.setName(vo.getName()); 44 | dto.setLoginName(vo.getLoginName()); 45 | dto.setCreatorId(Integer.parseInt(currentUserId)); 46 | dto.setRoleIds(vo.getRoleIds()); 47 | 48 | // 随机生成salt 49 | SecureRandomNumberGenerator secureRandomNumberGenerator = new SecureRandomNumberGenerator(); 50 | String salt = secureRandomNumberGenerator.nextBytes().toHex(); 51 | Md5Hash md5 = new Md5Hash(vo.getPassword(), salt, 6); 52 | // 设置盐 53 | dto.setSalt(salt); 54 | // 设置新密码 55 | String md5Password = md5.toHex(); 56 | dto.setPassword(md5Password); 57 | 58 | UserDTO user = userService.saveUser(dto); 59 | vo.setId(user.getId()); 60 | return ResponseEntity.status(HttpStatus.CREATED).body(vo); 61 | } 62 | 63 | @GetMapping("") 64 | public ResponseEntity> getUserList(@RequestParam(required = false) String name, @RequestParam(required = false) Boolean locked, 65 | @RequestParam(required = false) String sort, int offset, int limit) { 66 | Map paremeters = new HashMap(); 67 | if (!StringUtils.isBlank(name)) { 68 | paremeters.put("name", name); 69 | } 70 | if (locked != null) { 71 | paremeters.put("locked", locked); 72 | } 73 | Page result = userService.listUser(paremeters, sort, offset, limit); 74 | List voList = new ArrayList(); 75 | for(UserDTO dto : result.getRows()) { 76 | UserListVO vo = new UserListVO(); 77 | BeanUtils.copyProperties(dto, vo); 78 | vo.setCreatedAt(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dto.getCreatedAt())); 79 | voList.add(vo); 80 | } 81 | return ResponseEntity.ok(new Page(result.getTotal(), voList)); 82 | } 83 | 84 | @GetMapping("/{id}") 85 | public ResponseEntity getUserById(@PathVariable("id") int id) { 86 | UserDTO user = userService.getById(id); 87 | if (user != null) { 88 | UserDetailVO vo = new UserDetailVO(); 89 | BeanUtils.copyProperties(user, vo); 90 | vo.setCreatedAt(user.getCreatedAt().toInstant().toString()); 91 | return ResponseEntity.ok(vo); 92 | } 93 | return ResponseEntity.notFound().build(); 94 | } 95 | 96 | @PutMapping("/{id}") 97 | public ResponseEntity updateUser(@PathVariable("id") int id, @RequestBody UserDetailVO vo) { 98 | UserDTO dto = new UserDTO(); 99 | BeanUtils.copyProperties(vo, dto); 100 | int rows = userService.updateUser(dto); 101 | if (rows == 0) { 102 | return ResponseEntity.notFound().build(); 103 | } 104 | return ResponseEntity.status(HttpStatus.CREATED).body(rows); 105 | } 106 | 107 | @DeleteMapping("/{id}") 108 | public ResponseEntity removeUser(@PathVariable("id") int id) { 109 | return userService.removeUser(id) > 0 ? ResponseEntity.noContent().build() : 110 | ResponseEntity.notFound().build(); 111 | } 112 | 113 | @PutMapping("/{id}/password") 114 | public ResponseEntity changePassword(@PathVariable("id") int userId, String password) { 115 | // 随机生成salt 116 | SecureRandomNumberGenerator secureRandomNumberGenerator = new SecureRandomNumberGenerator(); 117 | String salt = secureRandomNumberGenerator.nextBytes().toHex(); 118 | 119 | // Md5密码 120 | Md5Hash md5 = new Md5Hash(password, salt, 6); 121 | String md5Password = md5.toHex(); 122 | 123 | int rows = userService.changePassword(userId, salt, md5Password); 124 | if(rows > 0) { 125 | return ResponseEntity.status(HttpStatus.CREATED).body(rows); 126 | } 127 | return ResponseEntity.notFound().build(); 128 | } 129 | 130 | @GetMapping("/me") 131 | public ResponseEntity getCurrentUser(@RequestHeader(value="X-Token") String token) { 132 | String currentUserId = this.getSubjectFromJwt(jwtUtils, token, "userId"); 133 | UserDTO dto = this.userService.getById(Integer.parseInt(currentUserId)); 134 | if(dto == null) { 135 | return ResponseEntity.notFound().build(); 136 | } 137 | UserDetailVO vo = new UserDetailVO(); 138 | BeanUtils.copyProperties(dto, vo); 139 | return ResponseEntity.ok(vo); 140 | } 141 | 142 | @PutMapping("/me") 143 | public ResponseEntity updateCurrentUser(@RequestHeader(value="X-Token") String token, @RequestBody UserDetailVO userDetail) { 144 | String currentUserId = this.getSubjectFromJwt(jwtUtils, token, "userId"); 145 | UserDTO dto = new UserDTO(); 146 | dto.setId(Integer.parseInt(currentUserId)); 147 | dto.setName(userDetail.getName()); 148 | dto.setLoginName(userDetail.getLoginName()); 149 | if(!StringUtils.isBlank(userDetail.getPassword())) { 150 | // 随机生成salt 151 | SecureRandomNumberGenerator secureRandomNumberGenerator = new SecureRandomNumberGenerator(); 152 | String salt = secureRandomNumberGenerator.nextBytes().toHex(); 153 | 154 | // Md5密码 155 | Md5Hash md5 = new Md5Hash(userDetail.getPassword(), salt, 6); 156 | String md5Password = md5.toHex(); 157 | dto.setSalt(salt); 158 | dto.setPassword(md5Password); 159 | } 160 | int rows = this.userService.updateCurrentUser(dto); 161 | return rows > 0 ? ResponseEntity.status(HttpStatus.CREATED).body(rows) : 162 | ResponseEntity.notFound().build(); 163 | } 164 | 165 | } 166 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/dao/ResourceDAO.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.dao; 2 | 3 | import com.hu.zhcc.shiro.entity.bo.Resource; 4 | import org.mybatis.spring.annotation.MapperScan; 5 | 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | /** 10 | * 资源管理DAO接口 11 | * 12 | * @author hulichao 13 | * @date 2018/3/25 14 | */ 15 | @MapperScan 16 | public interface ResourceDAO { 17 | 18 | /** 19 | * 得到资源列表 20 | * 21 | * @param params 22 | * 包含查询参数的Map 23 | * @return 24 | */ 25 | List listResource(Map params); 26 | 27 | /** 28 | * 得到用户授权的资源 29 | * 30 | * @param userId 31 | * 用户Id 32 | * @return 33 | */ 34 | List listAuthorized(int userId); 35 | 36 | /** 37 | * 添加资源 38 | * @param resource 39 | * @return 40 | */ 41 | int saveResource(Resource resource); 42 | 43 | /** 44 | * 修改资源 45 | * @param resource 46 | * @return 47 | */ 48 | int updateResource(Resource resource); 49 | 50 | /** 51 | * 删除资源 52 | * @param id 53 | * @return 54 | */ 55 | int removeResource(int id); 56 | 57 | /** 58 | * 根据id得到资源 59 | * @param id 60 | * @return 61 | */ 62 | Resource getById(int id); 63 | 64 | /** 65 | * 根据路由id得到资源列表 66 | * @param routerId 67 | * @return 68 | */ 69 | List listByRouterId(int routerId); 70 | 71 | } 72 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/dao/ResourcePermissionDAO.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.dao; 2 | 3 | 4 | import com.hu.zhcc.shiro.entity.bo.Resource; 5 | import org.mybatis.spring.annotation.MapperScan; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * 权限管理DAO接口 11 | * 12 | * @author hulichao 13 | * @date 2018/3/30 14 | */ 15 | @MapperScan 16 | public interface ResourcePermissionDAO { 17 | 18 | /** 19 | * 根据用户id得到资源权限 20 | * 21 | * @param userId 22 | * 用户Id 23 | * @return 24 | */ 25 | List listByUserId(int userId); 26 | 27 | /** 28 | * 根据路由id得到资源权限 29 | * @param userId 30 | * @param routerId 31 | * @return 32 | */ 33 | List listPermission(int userId, int routerId); 34 | 35 | /** 36 | * 根据用户id得到资源权限代码 37 | * @param userId 38 | * @return 39 | */ 40 | List listCodeByUserId(int userId); 41 | 42 | /** 43 | * 根据角色id删除资源权限 44 | * @param roleId 45 | * @return 46 | */ 47 | int removeByRoleId(int roleId); 48 | 49 | /** 50 | * 根据角色id得到资源权限 51 | * @param roleId 52 | * @return 53 | */ 54 | List listByRoleId(int roleId); 55 | 56 | /** 57 | * 保存权限 58 | * @param roleId 59 | * @param resourceIds 60 | * @return 61 | */ 62 | int savePermission(int roleId, int[] resourceIds); 63 | 64 | } 65 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/dao/RoleDAO.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.dao; 2 | 3 | import com.hu.zhcc.shiro.entity.bo.Role; 4 | import com.hu.zhcc.common.entity.PageRequest; 5 | import org.mybatis.spring.annotation.MapperScan; 6 | 7 | import java.util.List; 8 | import java.util.Map; 9 | 10 | /** 11 | * 角色DAO接口 12 | * 13 | * @author hulichao 14 | * @date 2018/3/27 15 | */ 16 | @MapperScan 17 | public interface RoleDAO { 18 | 19 | /** 20 | * 统计角色数 21 | * 22 | * @param parameters 23 | * 查询条件 24 | * @return 25 | */ 26 | int countRole(Map parameters); 27 | 28 | /** 29 | * 分页查询角色信息 30 | * 31 | * @param pageRequest 32 | * 分页请求参数 33 | * @return 34 | */ 35 | List listRole(PageRequest pageRequest); 36 | 37 | /** 38 | * 添加角色 39 | * @param role 40 | */ 41 | void saveRole(Role role); 42 | 43 | /** 44 | * 根据id得到角色 45 | * @param id 46 | * @return 47 | */ 48 | Role getById(int id); 49 | 50 | /** 51 | * 更新角色 52 | * @param role 53 | * @return 54 | */ 55 | int updateRole(Role role); 56 | 57 | /** 58 | * 删除角色 59 | * @param id 60 | * @return 61 | */ 62 | int removeRole(int id); 63 | 64 | /** 65 | * 得到所有角色 66 | * @return 67 | */ 68 | List listAllRole(); 69 | 70 | } 71 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/dao/RouterDAO.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.dao; 2 | 3 | import com.hu.zhcc.shiro.entity.bo.Router; 4 | import com.hu.zhcc.common.entity.PageRequest; 5 | import org.mybatis.spring.annotation.MapperScan; 6 | 7 | import java.util.List; 8 | import java.util.Map; 9 | 10 | /** 11 | * 前端路由DAO类 12 | * 13 | * @author hulichao 14 | * @date 2018/3/19 15 | */ 16 | @MapperScan 17 | public interface RouterDAO { 18 | 19 | /** 20 | * 统计路由数 21 | * @param parameters 22 | * @return 23 | */ 24 | int countRouter(Map parameters); 25 | 26 | /** 27 | * 得到路由列表 28 | * @param pageRequest 29 | * @return 30 | */ 31 | List listRouter(PageRequest pageRequest); 32 | 33 | /** 34 | * 根据id得到路由信息 35 | * @param id 36 | * @return 37 | */ 38 | Router getById(int id); 39 | 40 | /** 41 | * 根据上级路由id得到路由列表 42 | * @param parentId 43 | * @return 44 | */ 45 | List listByParentId(int parentId); 46 | 47 | /** 48 | * 添加路由 49 | * @param router 50 | */ 51 | void saveRouter(Router router); 52 | 53 | /** 54 | * 更新路由 55 | * @param router 56 | * @return 57 | */ 58 | int updateRouter(Router router); 59 | 60 | /** 61 | * 删除路由 62 | * @param id 63 | * @return 64 | */ 65 | int removeRouter(int id); 66 | 67 | /** 68 | * 得到所有路由 69 | * @param includeLocked 70 | * @return 71 | */ 72 | List listAll(boolean includeLocked); 73 | 74 | } 75 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/dao/RouterPermissionDAO.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.dao; 2 | 3 | 4 | import com.hu.zhcc.shiro.entity.bo.Router; 5 | import org.mybatis.spring.annotation.MapperScan; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * 路由权限DAO接口 11 | * 12 | * @author hulichao 13 | * @date 2018/3/22 14 | */ 15 | @MapperScan 16 | public interface RouterPermissionDAO { 17 | 18 | /** 19 | * 根据用户id得到路由权限列表 20 | * @param userId 21 | * @return 22 | */ 23 | List listByUserId(int userId); 24 | 25 | /** 26 | * 根据角色id删除路由权限 27 | * @param roleId 28 | */ 29 | void removeByRoleId(int roleId); 30 | 31 | /** 32 | * 保存路由权限 33 | * @param roleId 34 | * @param routerIds 35 | * @return 36 | */ 37 | int savePermission(int roleId, int[] routerIds); 38 | 39 | } 40 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/dao/UserDAO.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.dao; 2 | 3 | import com.hu.zhcc.shiro.entity.bo.User; 4 | import com.hu.zhcc.common.entity.PageRequest; 5 | import org.mybatis.spring.annotation.MapperScan; 6 | 7 | import java.util.List; 8 | import java.util.Map; 9 | 10 | /** 11 | * 用户管理dao接口 12 | * 13 | * @author hulichao 14 | * @date 2018/3/2 15 | */ 16 | @MapperScan 17 | public interface UserDAO { 18 | 19 | /** 20 | * 根据登录名得到用户 21 | * 22 | * @param loginName 23 | * 登录名 24 | * @return 25 | */ 26 | User getByLoginName(String loginName); 27 | 28 | /** 29 | * 创建系统用户 30 | * 31 | * @param user 32 | */ 33 | void saveUser(User user); 34 | 35 | /** 36 | * 修改用户口令 37 | * 38 | * @param userId 39 | * 用户Id 40 | * @param salt 41 | * 盐 42 | * @param password 43 | * 口令 44 | * @return 45 | */ 46 | int updatePassword(int userId, String salt, String password); 47 | 48 | /** 49 | * 统计用户数 50 | * 51 | * @param condition 52 | * 查询条件 53 | * @return 54 | */ 55 | int countUser(Map parameters); 56 | 57 | /** 58 | * 分页查询用户信息 59 | * 60 | * @param pageRequest 61 | * 分页请求参数 62 | * @return 63 | */ 64 | List listUser(PageRequest pageRequest); 65 | 66 | /** 67 | * 根据用户Id查询用户信息 68 | * @param id 用户id 69 | * @return 70 | */ 71 | User getById(int id); 72 | 73 | /** 74 | * 更新用户 75 | * @param user 76 | * @return 77 | */ 78 | int updateUser(User user); 79 | 80 | /** 81 | * 删除用户 82 | * @param id 83 | * @param deletedAt 删除时间 84 | * @return 85 | */ 86 | int removeUser(int id, long deletedAt); 87 | 88 | } 89 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/dao/UserRoleDAO.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.dao; 2 | 3 | 4 | import com.hu.zhcc.shiro.entity.bo.Role; 5 | import org.mybatis.spring.annotation.MapperScan; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * 用户角色dao接口 11 | * 12 | * @author hulichao 13 | * @date 2018/4/9 14 | */ 15 | @MapperScan 16 | public interface UserRoleDAO { 17 | 18 | /** 19 | * 根据用户id得到角色列表 20 | * @param userId 21 | * @return 22 | */ 23 | List listUserRole(int userId); 24 | 25 | /** 26 | * 添加用户角色 27 | * @param userId 28 | * @param roleIds 29 | * @return 30 | */ 31 | int saveUserRole(int userId, int[] roleIds); 32 | 33 | /** 34 | * 根据用户id删除用户角色 35 | * @param userId 36 | * @return 37 | */ 38 | int removeByUserId(int userId); 39 | 40 | /** 41 | * 根据角色id删除用户角色 42 | * @param roleId 43 | * @return 44 | */ 45 | int removeByRoleId(int roleId); 46 | 47 | } 48 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/entity/bo/Resource.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.entity.bo; 2 | 3 | /** 4 | * 资源实体类 5 | * 6 | * @author hulichao 7 | * @date 2018/3/25 8 | */ 9 | public class Resource implements java.io.Serializable { 10 | 11 | private static final long serialVersionUID = 8286484183880343900L; 12 | 13 | private Integer id; 14 | 15 | private String name; 16 | 17 | private String code; 18 | 19 | private String url; 20 | 21 | private String permission; 22 | 23 | private boolean locked; 24 | 25 | private Router router; 26 | 27 | public Integer getId() { 28 | return this.id; 29 | } 30 | public void setId(Integer id) { 31 | this.id = id; 32 | } 33 | 34 | public String getName() { 35 | return this.name; 36 | } 37 | public void setName(String name) { 38 | this.name = name; 39 | } 40 | 41 | public String getCode() { 42 | return this.code; 43 | } 44 | public void setCode(String code) { 45 | this.code = code; 46 | } 47 | 48 | public String getUrl() { 49 | return this.url; 50 | } 51 | public void setUrl(String url) { 52 | this.url = url; 53 | } 54 | 55 | public String getPermission() { 56 | return this.permission; 57 | } 58 | public void setPermission(String permission) { 59 | this.permission = permission; 60 | } 61 | 62 | public Boolean getLocked() { 63 | return this.locked; 64 | } 65 | public void setLocked(Boolean locked) { 66 | this.locked = locked; 67 | } 68 | 69 | public Router getRouter() { 70 | return this.router; 71 | } 72 | public void setRouter(Router router) { 73 | this.router = router; 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/entity/bo/Role.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.entity.bo; 2 | 3 | /** 4 | * 系统角色实体类 5 | * 6 | * @author hulichao 7 | * @date 2018/3/27 8 | */ 9 | public class Role implements java.io.Serializable { 10 | 11 | private static final long serialVersionUID = 3631536115032816649L; 12 | 13 | private Integer id; 14 | 15 | private String code; 16 | 17 | private String name; 18 | 19 | private boolean locked; 20 | 21 | public Integer getId() { 22 | return this.id; 23 | } 24 | public void setId(Integer id) { 25 | this.id = id; 26 | } 27 | 28 | public String getCode() { 29 | return this.code; 30 | } 31 | public void setCode(String code) { 32 | this.code = code; 33 | } 34 | 35 | public String getName() { 36 | return this.name; 37 | } 38 | public void setName(String name) { 39 | this.name = name; 40 | } 41 | 42 | public Boolean getLocked() { 43 | return this.locked; 44 | } 45 | public void setLocked(Boolean locked) { 46 | this.locked = locked; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/entity/bo/Router.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.entity.bo; 2 | 3 | /** 4 | * 前端路由实体类 5 | * 6 | * @author hulichao 7 | * @date 2018/3/19 8 | */ 9 | public class Router implements java.io.Serializable { 10 | 11 | private static final long serialVersionUID = 7627998429341237040L; 12 | 13 | private int id; 14 | 15 | private String name; 16 | 17 | private String code; 18 | 19 | private String url; 20 | 21 | private Router parent; 22 | 23 | private short level; 24 | 25 | private boolean locked; 26 | 27 | private int displayOrder; 28 | 29 | private String properties; 30 | 31 | public void setId(int id) { 32 | this.id = id; 33 | } 34 | public int getId() { 35 | return this.id; 36 | } 37 | 38 | public void setName(String name) { 39 | this.name = name; 40 | } 41 | public String getName() { 42 | return this.name; 43 | } 44 | 45 | public String getCode() { 46 | return this.code; 47 | } 48 | public void setCode(String code) { 49 | this.code = code; 50 | } 51 | 52 | public String getUrl() { 53 | return this.url; 54 | } 55 | public void setUrl(String url) { 56 | this.url = url; 57 | } 58 | 59 | public Router getParent() { 60 | return this.parent; 61 | } 62 | public void setParent(Router parent) { 63 | this.parent = parent; 64 | } 65 | 66 | public short getLevel() { 67 | return this.level; 68 | } 69 | public void setLevel(short level) { 70 | this.level = level; 71 | } 72 | 73 | public boolean getLocked() { 74 | return this.locked; 75 | } 76 | public void setLocked(boolean locked) { 77 | this.locked = locked; 78 | } 79 | 80 | public int getDisplayOrder() { 81 | return this.displayOrder; 82 | } 83 | public void setDisplayOrder(int displayOrder) { 84 | this.displayOrder = displayOrder; 85 | } 86 | 87 | public String getProperties() { 88 | return this.properties; 89 | } 90 | public void setProperties(String properties) { 91 | this.properties = properties; 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/entity/bo/User.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.entity.bo; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * 用户实体类 7 | * 8 | * @author hulichao 9 | * @date 2018/3/28 10 | */ 11 | public class User implements java.io.Serializable { 12 | 13 | private static final long serialVersionUID = -654281045807461943L; 14 | 15 | private Integer id; 16 | 17 | private String name; 18 | 19 | private String loginName; 20 | 21 | private String salt; 22 | 23 | private String password; 24 | 25 | private boolean locked; 26 | 27 | private Date createdAt; 28 | 29 | private User createdBy; 30 | 31 | public Integer getId() { 32 | return this.id; 33 | } 34 | public void setId(Integer id) { 35 | this.id = id; 36 | } 37 | 38 | public String getName() { 39 | return this.name; 40 | } 41 | public void setName(String name) { 42 | this.name = name; 43 | } 44 | 45 | public String getLoginName() { 46 | return this.loginName; 47 | } 48 | public void setLoginName(String loginName) { 49 | this.loginName = loginName; 50 | } 51 | 52 | public String getSalt() { 53 | return this.salt; 54 | } 55 | public void setSalt(String salt) { 56 | this.salt = salt; 57 | } 58 | 59 | public String getPassword() { 60 | return this.password; 61 | } 62 | public void setPassword(String password) { 63 | this.password = password; 64 | } 65 | 66 | public boolean getLocked() { 67 | return this.locked; 68 | } 69 | public void setLocked(boolean locked) { 70 | this.locked = locked; 71 | } 72 | 73 | public Date getCreatedAt() { 74 | return this.createdAt; 75 | } 76 | public void setCreatedAt(Date createdAt) { 77 | this.createdAt = createdAt; 78 | } 79 | 80 | public void setCreatedBy(User createdBy) { 81 | this.createdBy = createdBy; 82 | } 83 | public User getCreatedBy() { 84 | return this.createdBy; 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/entity/dto/AvailableResourceDTO.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.entity.dto; 2 | 3 | /** 4 | * 可用资源dto类(Shiro动态FilterChainDefinitions dto类) 5 | * 6 | * @author hulichao 7 | * @date 2018/4/11 8 | */ 9 | public final class AvailableResourceDTO implements java.io.Serializable { 10 | 11 | private static final long serialVersionUID = -5316506151248386322L; 12 | 13 | /* 14 | * 该类的数据来自于Resource(sys_resource) 15 | */ 16 | 17 | private int id; 18 | 19 | private String name; 20 | 21 | private String code; 22 | 23 | private String url; 24 | 25 | private int routerId; 26 | 27 | public int getId() { 28 | return this.id; 29 | } 30 | 31 | public void setId(int id) { 32 | this.id = id; 33 | } 34 | 35 | public String getName() { 36 | return this.name; 37 | } 38 | 39 | public void setName(String name) { 40 | this.name = name; 41 | } 42 | 43 | public String getCode() { 44 | return this.code; 45 | } 46 | 47 | public void setCode(String code) { 48 | this.code = code; 49 | } 50 | 51 | public String getUrl() { 52 | return this.url; 53 | } 54 | 55 | public void setUrl(String url) { 56 | this.url = url; 57 | } 58 | 59 | public int getRouterId() { 60 | return this.routerId; 61 | } 62 | public void setRouterId(int routerId) { 63 | this.routerId = routerId; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/entity/dto/PermissionDTO.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.entity.dto; 2 | 3 | /** 4 | * 权限dto类 5 | * 6 | * @author hulichao 7 | * @date 2018/4/27 8 | */ 9 | public final class PermissionDTO implements java.io.Serializable { 10 | 11 | private static final long serialVersionUID = 1318329930069664403L; 12 | 13 | private int[] routerIds; 14 | 15 | private int[] resourceIds; 16 | 17 | public int[] getRouterIds() { 18 | return this.routerIds; 19 | } 20 | public void setRouterIds(int[] routerIds) { 21 | this.routerIds = routerIds; 22 | } 23 | 24 | public int[] getResourceIds() { 25 | return this.resourceIds; 26 | } 27 | public void setResourceIds(int[] resourceIds) { 28 | this.resourceIds = resourceIds; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/entity/dto/ResourceDTO.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.entity.dto; 2 | 3 | /** 4 | * 资源dto类 5 | * 6 | * @author hulichao 7 | * @date 2018/4/11 8 | */ 9 | public final class ResourceDTO implements java.io.Serializable { 10 | 11 | private static final long serialVersionUID = 4368450080812978404L; 12 | 13 | private int id; 14 | 15 | private String name; 16 | 17 | private String code; 18 | 19 | private String url; 20 | 21 | private String permission; 22 | 23 | private boolean locked; 24 | 25 | private int routerId; 26 | 27 | private String routerName; 28 | 29 | public int getId() { 30 | return this.id; 31 | } 32 | 33 | public void setId(int id) { 34 | this.id = id; 35 | } 36 | 37 | public String getName() { 38 | return this.name; 39 | } 40 | 41 | public void setName(String name) { 42 | this.name = name; 43 | } 44 | 45 | public String getCode() { 46 | return this.code; 47 | } 48 | 49 | public void setCode(String code) { 50 | this.code = code; 51 | } 52 | 53 | public String getUrl() { 54 | return this.url; 55 | } 56 | 57 | public void setUrl(String url) { 58 | this.url = url; 59 | } 60 | 61 | public String getPermission() { 62 | return this.permission; 63 | } 64 | 65 | public void setPermission(String permission) { 66 | this.permission = permission; 67 | } 68 | 69 | public boolean getLocked() { 70 | return this.locked; 71 | } 72 | 73 | public void setLocked(boolean locked) { 74 | this.locked = locked; 75 | } 76 | 77 | public int getRouterId() { 78 | return this.routerId; 79 | } 80 | 81 | public void setRouterId(int routerId) { 82 | this.routerId = routerId; 83 | } 84 | 85 | public String getRouterName() { 86 | return this.routerName; 87 | } 88 | 89 | public void setRouterName(String routerName) { 90 | this.routerName = routerName; 91 | } 92 | 93 | } 94 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/entity/dto/ResourcePermissionDTO.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.entity.dto; 2 | 3 | /** 4 | * 资源权限dto类 5 | * 6 | * @author hulichao 7 | * @date 2018/4/26 8 | */ 9 | public final class ResourcePermissionDTO implements java.io.Serializable { 10 | 11 | private static final long serialVersionUID = -8134140320624062359L; 12 | 13 | private int resourceId; 14 | 15 | private String resourcePermission; 16 | 17 | public int getResourceId() { 18 | return this.resourceId; 19 | } 20 | 21 | public void setResourceId(int resourceId) { 22 | this.resourceId = resourceId; 23 | } 24 | 25 | public String getResourcePermission() { 26 | return this.resourcePermission; 27 | } 28 | 29 | public void setResourcePermission(String resourcePermission) { 30 | this.resourcePermission = resourcePermission; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/entity/dto/RoleDTO.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.entity.dto; 2 | 3 | /** 4 | * 角色DTO类 5 | * 6 | * @author hulichao 7 | * @date 2018/4/4 8 | */ 9 | public final class RoleDTO implements java.io.Serializable { 10 | 11 | private static final long serialVersionUID = 7358013142957971759L; 12 | 13 | private Integer id; 14 | 15 | private String code; 16 | 17 | private String name; 18 | 19 | private Boolean locked; 20 | 21 | public Integer getId() { 22 | return this.id; 23 | } 24 | 25 | public void setId(Integer id) { 26 | this.id = id; 27 | } 28 | 29 | public String getCode() { 30 | return this.code; 31 | } 32 | 33 | public void setCode(String code) { 34 | this.code = code; 35 | } 36 | 37 | public String getName() { 38 | return this.name; 39 | } 40 | 41 | public void setName(String name) { 42 | this.name = name; 43 | } 44 | 45 | public Boolean getLocked() { 46 | return this.locked; 47 | } 48 | 49 | public void setLocked(Boolean locked) { 50 | this.locked = locked; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/entity/dto/RouterDTO.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.entity.dto; 2 | 3 | /** 4 | * 路由dto类 5 | * 6 | * @author hulichao 7 | * @date 2018/3/31 8 | */ 9 | public final class RouterDTO implements java.io.Serializable { 10 | 11 | private static final long serialVersionUID = -7736347925179797588L; 12 | 13 | private int id; 14 | 15 | private String name; 16 | 17 | private String code; 18 | 19 | private String url; 20 | 21 | private int parentId; 22 | 23 | private String parentName; 24 | 25 | private short level; 26 | 27 | private boolean locked; 28 | 29 | private int displayOrder; 30 | 31 | private String properties; 32 | 33 | public void setId(int id) { 34 | this.id = id; 35 | } 36 | 37 | public int getId() { 38 | return this.id; 39 | } 40 | 41 | public void setName(String name) { 42 | this.name = name; 43 | } 44 | 45 | public String getName() { 46 | return this.name; 47 | } 48 | 49 | public String getCode() { 50 | return this.code; 51 | } 52 | 53 | public void setCode(String code) { 54 | this.code = code; 55 | } 56 | 57 | public String getUrl() { 58 | return this.url; 59 | } 60 | 61 | public void setUrl(String url) { 62 | this.url = url; 63 | } 64 | 65 | public int getParentId() { 66 | return this.parentId; 67 | } 68 | 69 | public void setParentId(int parentId) { 70 | this.parentId = parentId; 71 | } 72 | 73 | public String getParentName() { 74 | return this.parentName; 75 | } 76 | public void setParentName(String parentName) { 77 | this.parentName = parentName; 78 | } 79 | 80 | public short getLevel() { 81 | return this.level; 82 | } 83 | 84 | public void setLevel(short level) { 85 | this.level = level; 86 | } 87 | 88 | public boolean getLocked() { 89 | return this.locked; 90 | } 91 | 92 | public void setLocked(boolean locked) { 93 | this.locked = locked; 94 | } 95 | 96 | public int getDisplayOrder() { 97 | return this.displayOrder; 98 | } 99 | 100 | public void setDisplayOrder(int displayOrder) { 101 | this.displayOrder = displayOrder; 102 | } 103 | 104 | public String getProperties() { 105 | return this.properties; 106 | } 107 | 108 | public void setProperties(String properties) { 109 | this.properties = properties; 110 | } 111 | 112 | } 113 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/entity/dto/UserDTO.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.entity.dto; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * 用户DTO类 7 | * @author hulichao 8 | * @date 2018/3/15 9 | */ 10 | public final class UserDTO implements java.io.Serializable { 11 | 12 | private static final long serialVersionUID = 1L; 13 | 14 | private int id; 15 | 16 | private String name; 17 | 18 | private String loginName; 19 | 20 | private String salt; 21 | 22 | private String password; 23 | 24 | private boolean locked; 25 | 26 | private Date createdAt; 27 | 28 | private int creatorId; 29 | 30 | private String creatorName; 31 | 32 | private int[] roleIds; 33 | 34 | 35 | public Integer getId() { 36 | return this.id; 37 | } 38 | public void setId(Integer id) { 39 | this.id = id; 40 | } 41 | 42 | public String getName() { 43 | return this.name; 44 | } 45 | public void setName(String name) { 46 | this.name = name; 47 | } 48 | 49 | public String getLoginName() { 50 | return this.loginName; 51 | } 52 | public void setLoginName(String loginName) { 53 | this.loginName = loginName; 54 | } 55 | 56 | public String getSalt() { 57 | return this.salt; 58 | } 59 | public void setSalt(String salt) { 60 | this.salt = salt; 61 | } 62 | 63 | public String getPassword() { 64 | return this.password; 65 | } 66 | public void setPassword(String password) { 67 | this.password = password; 68 | } 69 | 70 | public boolean getLocked() { 71 | return this.locked; 72 | } 73 | public void setLocked(boolean locked) { 74 | this.locked = locked; 75 | } 76 | 77 | public Date getCreatedAt() { 78 | return this.createdAt; 79 | } 80 | public void setCreatedAt(Date createdAt) { 81 | this.createdAt = createdAt; 82 | } 83 | 84 | public int getCreatorId() { 85 | return this.creatorId; 86 | } 87 | public void setCreatorId(int creatorId) { 88 | this.creatorId = creatorId; 89 | } 90 | 91 | public String getCreatorName() { 92 | return this.creatorName; 93 | } 94 | public void setCreatorName(String creatorName) { 95 | this.creatorName = creatorName; 96 | } 97 | 98 | public int[] getRoleIds() { 99 | return this.roleIds; 100 | } 101 | public void setRoleIds(int[] roleIds) { 102 | this.roleIds = roleIds; 103 | } 104 | 105 | } 106 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/entity/vo/AvailableResourceListVO.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.entity.vo; 2 | 3 | /** 4 | * 可用资源列表vo类 5 | * 6 | * @author hulichao 7 | * @date 2018/4/27 8 | */ 9 | public final class AvailableResourceListVO { 10 | 11 | private int id; 12 | 13 | private String name; 14 | 15 | private int routerId; 16 | 17 | public int getId() { 18 | return this.id; 19 | } 20 | 21 | public void setId(int id) { 22 | this.id = id; 23 | } 24 | 25 | public String getName() { 26 | return this.name; 27 | } 28 | 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | public int getRouterId() { 34 | return this.routerId; 35 | } 36 | 37 | public void setRouterId(int routerId) { 38 | this.routerId = routerId; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/entity/vo/ResourceDetailVO.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.entity.vo; 2 | 3 | /** 4 | * 资源详情vo类 5 | * 6 | * @author hulichao 7 | * @date 2018/4/24 8 | */ 9 | public final class ResourceDetailVO { 10 | 11 | private int id; 12 | 13 | private String name; 14 | 15 | private String code; 16 | 17 | private String url; 18 | 19 | private String permission; 20 | 21 | private boolean locked; 22 | 23 | private int routerId; 24 | 25 | private String routerName; 26 | 27 | public int getId() { 28 | return this.id; 29 | } 30 | 31 | public void setId(int id) { 32 | this.id = id; 33 | } 34 | 35 | public String getName() { 36 | return this.name; 37 | } 38 | 39 | public void setName(String name) { 40 | this.name = name; 41 | } 42 | 43 | public String getCode() { 44 | return this.code; 45 | } 46 | 47 | public void setCode(String code) { 48 | this.code = code; 49 | } 50 | 51 | public String getUrl() { 52 | return this.url; 53 | } 54 | 55 | public void setUrl(String url) { 56 | this.url = url; 57 | } 58 | 59 | public String getPermission() { 60 | return this.permission; 61 | } 62 | 63 | public void setPermission(String permission) { 64 | this.permission = permission; 65 | } 66 | 67 | public boolean getLocked() { 68 | return this.locked; 69 | } 70 | 71 | public void setLocked(boolean locked) { 72 | this.locked = locked; 73 | } 74 | 75 | public int getRouterId() { 76 | return this.routerId; 77 | } 78 | 79 | public void setRouterId(int routerId) { 80 | this.routerId = routerId; 81 | } 82 | 83 | public String getRouterName() { 84 | return this.routerName; 85 | } 86 | 87 | public void setRouterName(String routerName) { 88 | this.routerName = routerName; 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/entity/vo/ResourceListVO.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.entity.vo; 2 | 3 | /** 4 | * 资源列表vo类 5 | * 6 | * @author hulichao 7 | * @date 2018/4/11 8 | */ 9 | public final class ResourceListVO { 10 | 11 | private int id; 12 | 13 | private String name; 14 | 15 | private String code; 16 | 17 | private String url; 18 | 19 | private String permission; 20 | 21 | private boolean locked; 22 | 23 | private int routerId; 24 | 25 | public int getId() { 26 | return this.id; 27 | } 28 | 29 | public void setId(int id) { 30 | this.id = id; 31 | } 32 | 33 | public String getName() { 34 | return this.name; 35 | } 36 | 37 | public void setName(String name) { 38 | this.name = name; 39 | } 40 | 41 | public String getCode() { 42 | return this.code; 43 | } 44 | 45 | public void setCode(String code) { 46 | this.code = code; 47 | } 48 | 49 | public String getUrl() { 50 | return this.url; 51 | } 52 | 53 | public void setUrl(String url) { 54 | this.url = url; 55 | } 56 | 57 | public String getPermission() { 58 | return this.permission; 59 | } 60 | 61 | public void setPermission(String permission) { 62 | this.permission = permission; 63 | } 64 | 65 | public boolean getLocked() { 66 | return this.locked; 67 | } 68 | 69 | public void setLocked(boolean locked) { 70 | this.locked = locked; 71 | } 72 | 73 | public int getRouterId() { 74 | return this.routerId; 75 | } 76 | 77 | public void setRouterId(int routerId) { 78 | this.routerId = routerId; 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/entity/vo/ResourcePermissionVO.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.entity.vo; 2 | 3 | /** 4 | * 资源权限vo类 5 | * 6 | * @author hulichao 7 | * @date 2018/4/26 8 | */ 9 | public final class ResourcePermissionVO { 10 | 11 | private int roleId; 12 | 13 | private int resourceId; 14 | 15 | private String resourcePermission; 16 | 17 | public int getRoleId() { 18 | return roleId; 19 | } 20 | 21 | public void setRoleId(int roleId) { 22 | this.roleId = roleId; 23 | } 24 | 25 | public int getResourceId() { 26 | return this.resourceId; 27 | } 28 | 29 | public void setResourceId(int resourceId) { 30 | this.resourceId = resourceId; 31 | } 32 | 33 | public String getResourcePermission() { 34 | return this.resourcePermission; 35 | } 36 | 37 | public void setResourcePermission(String resourcePermission) { 38 | this.resourcePermission = resourcePermission; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/entity/vo/RoleDetailVO.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.entity.vo; 2 | 3 | /** 4 | * 角色详情vo类 5 | * 6 | * @author hulichao 7 | * @date 2018/4/6 8 | */ 9 | public final class RoleDetailVO { 10 | 11 | private Integer id; 12 | 13 | private String code; 14 | 15 | private String name; 16 | 17 | private Boolean locked; 18 | 19 | public Integer getId() { 20 | return this.id; 21 | } 22 | 23 | public void setId(Integer id) { 24 | this.id = id; 25 | } 26 | 27 | public String getCode() { 28 | return this.code; 29 | } 30 | 31 | public void setCode(String code) { 32 | this.code = code; 33 | } 34 | 35 | public String getName() { 36 | return this.name; 37 | } 38 | 39 | public void setName(String name) { 40 | this.name = name; 41 | } 42 | 43 | public Boolean getLocked() { 44 | return this.locked; 45 | } 46 | 47 | public void setLocked(Boolean locked) { 48 | this.locked = locked; 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/entity/vo/RoleListVO.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.entity.vo; 2 | 3 | /** 4 | * 角色列表vo类 5 | * 6 | * @author hulichao 7 | * @date 2018/4/5 8 | */ 9 | public final class RoleListVO { 10 | 11 | private Integer id; 12 | 13 | private String code; 14 | 15 | private String name; 16 | 17 | private Boolean locked; 18 | 19 | public Integer getId() { 20 | return this.id; 21 | } 22 | 23 | public void setId(Integer id) { 24 | this.id = id; 25 | } 26 | 27 | public String getCode() { 28 | return this.code; 29 | } 30 | 31 | public void setCode(String code) { 32 | this.code = code; 33 | } 34 | 35 | public String getName() { 36 | return this.name; 37 | } 38 | 39 | public void setName(String name) { 40 | this.name = name; 41 | } 42 | 43 | public Boolean getLocked() { 44 | return this.locked; 45 | } 46 | 47 | public void setLocked(Boolean locked) { 48 | this.locked = locked; 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/entity/vo/RouterDetailVO.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.entity.vo; 2 | 3 | /** 4 | * 路由详情vo类 5 | * 6 | * @author hulichao 7 | * @date 2018/3/28 8 | */ 9 | public final class RouterDetailVO { 10 | 11 | private int id; 12 | 13 | private String name; 14 | 15 | private String code; 16 | 17 | private String url; 18 | 19 | private int parentId; 20 | 21 | private String parentName; 22 | 23 | private short level; 24 | 25 | private boolean locked; 26 | 27 | private int displayOrder; 28 | 29 | private String properties; 30 | 31 | public void setId(int id) { 32 | this.id = id; 33 | } 34 | public int getId() { 35 | return this.id; 36 | } 37 | 38 | public void setName(String name) { 39 | this.name = name; 40 | } 41 | public String getName() { 42 | return this.name; 43 | } 44 | 45 | public String getCode() { 46 | return this.code; 47 | } 48 | public void setCode(String code) { 49 | this.code = code; 50 | } 51 | 52 | public String getUrl() { 53 | return this.url; 54 | } 55 | public void setUrl(String url) { 56 | this.url = url; 57 | } 58 | 59 | public int getParentId() { 60 | return this.parentId; 61 | } 62 | public void setParentId(int parentId) { 63 | this.parentId = parentId; 64 | } 65 | 66 | public String getParentName() { 67 | return this.parentName; 68 | } 69 | public void setParentName(String parentName) { 70 | this.parentName = parentName; 71 | } 72 | 73 | public short getLevel() { 74 | return this.level; 75 | } 76 | public void setLevel(short level) { 77 | this.level = level; 78 | } 79 | 80 | public boolean getLocked() { 81 | return this.locked; 82 | } 83 | public void setLocked(boolean locked) { 84 | this.locked = locked; 85 | } 86 | 87 | public int getDisplayOrder() { 88 | return this.displayOrder; 89 | } 90 | public void setDisplayOrder(int displayOrder) { 91 | this.displayOrder = displayOrder; 92 | } 93 | 94 | public String getProperties() { 95 | return this.properties; 96 | } 97 | public void setProperties(String properties) { 98 | this.properties = properties; 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/entity/vo/RouterListVO.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.entity.vo; 2 | 3 | /** 4 | * 路由列表vo类 5 | * 6 | * @author hulichao 7 | * @date 2018/3/26 8 | */ 9 | public final class RouterListVO { 10 | 11 | public int id; 12 | 13 | public String name; 14 | 15 | private String code; 16 | 17 | private String url; 18 | 19 | private short level; 20 | 21 | private boolean locked; 22 | 23 | private int displayOrder; 24 | 25 | private int parentId; 26 | 27 | public int getId() { 28 | return this.id; 29 | } 30 | public void setId(int id) { 31 | this.id = id; 32 | } 33 | 34 | public String getName() { 35 | return this.name; 36 | } 37 | public void setName(String name) { 38 | this.name = name; 39 | } 40 | 41 | public String getCode() { 42 | return this.code; 43 | } 44 | public void setCode(String code) { 45 | this.code = code; 46 | } 47 | 48 | public String getUrl() { 49 | return this.url; 50 | } 51 | public void setUrl(String url) { 52 | this.url = url; 53 | } 54 | 55 | public short getLevel() { 56 | return this.level; 57 | } 58 | public void setLevel(short level) { 59 | this.level = level; 60 | } 61 | 62 | public boolean getLocked() { 63 | return this.locked; 64 | } 65 | public void setLocked(boolean locked) { 66 | this.locked = locked; 67 | } 68 | 69 | public int getDisplayOrder() { 70 | return this.displayOrder; 71 | } 72 | public void setDisplayOrder(int displayOrder) { 73 | this.displayOrder = displayOrder; 74 | } 75 | 76 | public int getParentId() { 77 | return this.parentId; 78 | } 79 | public void setParentId(int parentId) { 80 | this.parentId = parentId; 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/entity/vo/RouterNavVO.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.entity.vo; 2 | 3 | /** 4 | * 路由导航VO类 5 | * 6 | * @author hulichao 7 | * @date 2018/3/19 8 | */ 9 | public final class RouterNavVO { 10 | 11 | private int id; 12 | 13 | private String code; 14 | 15 | private String name; 16 | 17 | private String url; 18 | 19 | private String properties; 20 | 21 | private int parentId; 22 | 23 | private short level; 24 | 25 | public int getId() { 26 | return this.id; 27 | } 28 | public void setId(int id) { 29 | this.id = id; 30 | } 31 | 32 | public String getCode() { 33 | return this.code; 34 | } 35 | public void setCode(String code) { 36 | this.code = code; 37 | } 38 | 39 | public String getName() { 40 | return this.name; 41 | } 42 | public void setName(String name) { 43 | this.name = name; 44 | } 45 | 46 | public String getUrl() { 47 | return this.url; 48 | } 49 | public void setUrl(String url) { 50 | this.url = url; 51 | } 52 | 53 | public String getProperties() { 54 | return this.properties; 55 | } 56 | public void setProperties(String properties) { 57 | this.properties = properties; 58 | } 59 | 60 | public int getParentId() { 61 | return this.parentId; 62 | } 63 | public void setParentId(int parentId) { 64 | this.parentId = parentId; 65 | } 66 | 67 | public short getLevel() { 68 | return this.level; 69 | } 70 | public void setLevel(short level) { 71 | this.level = level; 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/entity/vo/UserDetailVO.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.entity.vo; 2 | 3 | /** 4 | * 用户详情VO对象 5 | * 6 | * @author hulichao 7 | * @date 2018/3/28 8 | */ 9 | public final class UserDetailVO { 10 | 11 | private Integer id; 12 | 13 | private String name; 14 | 15 | private String loginName; 16 | 17 | private String password; 18 | 19 | private boolean locked; 20 | 21 | private String createdAt; 22 | 23 | private int creatorId; 24 | 25 | private String creatorName; 26 | 27 | private int[] roleIds; 28 | 29 | public Integer getId() { 30 | return this.id; 31 | } 32 | public void setId(Integer id) { 33 | this.id = id; 34 | } 35 | 36 | public String getName() { 37 | return this.name; 38 | } 39 | public void setName(String name) { 40 | this.name = name; 41 | } 42 | 43 | public String getLoginName() { 44 | return this.loginName; 45 | } 46 | public void setLoginName(String loginName) { 47 | this.loginName = loginName; 48 | } 49 | 50 | public String getPassword() { 51 | return this.password; 52 | } 53 | public void setPassword(String password) { 54 | this.password = password; 55 | } 56 | 57 | public boolean getLocked() { 58 | return this.locked; 59 | } 60 | public void setLocked(boolean locked) { 61 | this.locked = locked; 62 | } 63 | 64 | public String getCreatedAt() { 65 | return this.createdAt; 66 | } 67 | public void setCreatedAt(String createdAt) { 68 | this.createdAt = createdAt; 69 | } 70 | 71 | public void setCreatorId(int creatorId) { 72 | this.creatorId = creatorId; 73 | } 74 | public int getCreatedBy() { 75 | return this.creatorId; 76 | } 77 | 78 | public void setCreatorName(String creatorName) { 79 | this.creatorName = creatorName; 80 | } 81 | public String getCreatorName() { 82 | return this.creatorName; 83 | } 84 | 85 | public int[] getRoleIds() { 86 | return this.roleIds; 87 | } 88 | public void setRoleIds(int[] roleIds) { 89 | this.roleIds = roleIds; 90 | } 91 | 92 | } 93 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/entity/vo/UserListVO.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.entity.vo; 2 | 3 | /** 4 | * 用户列表对象VO类 5 | * 6 | * @author hulichao 7 | * @date 2018/3/28 8 | */ 9 | public final class UserListVO { 10 | 11 | private int id; 12 | 13 | private String name; 14 | 15 | private String loginName; 16 | 17 | private boolean locked; 18 | 19 | private String createdAt; 20 | 21 | public int getId() { 22 | return this.id; 23 | } 24 | public void setId(int id) { 25 | this.id = id; 26 | } 27 | 28 | public String getName() { 29 | return this.name; 30 | } 31 | public void setName(String name) { 32 | this.name = name; 33 | } 34 | 35 | public String getLoginName() { 36 | return this.loginName; 37 | } 38 | public void setLoginName(String loginName) { 39 | this.loginName = loginName; 40 | } 41 | 42 | public boolean getLocked() { 43 | return this.locked; 44 | } 45 | public void setLocked(boolean locked) { 46 | this.locked = locked; 47 | } 48 | 49 | public String getCreatedAt() { 50 | return this.createdAt; 51 | } 52 | public void setCreatedAt(String createdAt) { 53 | this.createdAt = createdAt; 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/security/ChainDefinitionSectionMetaSource.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.security; 2 | 3 | import java.text.MessageFormat; 4 | import java.util.List; 5 | 6 | import com.hu.zhcc.shiro.entity.dto.AvailableResourceDTO; 7 | import com.hu.zhcc.shiro.service.ResourceService; 8 | import org.apache.shiro.config.Ini; 9 | import org.apache.shiro.config.Ini.Section; 10 | import org.springframework.beans.factory.FactoryBean; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | 13 | 14 | /** 15 | * Shiro动态FilterChainDefinitions 16 | * 17 | * @author hulichao 18 | * @date 2018/3/18 19 | */ 20 | public class ChainDefinitionSectionMetaSource implements FactoryBean { 21 | 22 | @Autowired 23 | private ResourceService resourceService; 24 | 25 | /** 26 | * 默认url过滤定义(shiro过滤器的filterChainDefinitions属性) 27 | */ 28 | private String filterChainDefinitions; 29 | 30 | /** 31 | * 设置默认url过滤定义 32 | * 33 | * @param filterChainDefinitions 34 | */ 35 | public void setFilterChainDefinitions(String filterChainDefinitions) { 36 | this.filterChainDefinitions = filterChainDefinitions; 37 | } 38 | 39 | @Override 40 | public Section getObject() throws Exception { 41 | /******************************************* 42 | * rest:例子/admins/user/**=rest[user],根据请求的方法,相当于/admins/user/**=perms[user:method] 43 | * ,其中method为post,get,delete等。 44 | * port:例子/admins/user/**=port[8081],当请求的url的端口不是8081是跳转到schemal://serverName:8081?queryString, 45 | * 其中schmal是协议http或https等,serverName是你访问的host,8081是url配置里port的端口,queryString是你访问的url里的?后面的参数。 46 | * perms:例子/admins/user/**=perms[user:add:*],perms参数可以写多个,多个时必须加上引号,并且参数之间用逗号分割, 47 | * 例如/admins/user/**=perms["user:add:*,user:modify:*"],当有多个参数时必须每个参数都通过才通过,想当于isPermitedAll()方法。 48 | * roles:例子/admins/user/**=roles[admin],参数可以写多个,多个时必须加上引号,并且参数之间用逗号分割,当有多个参数时, 49 | * 例如/admins/user/**=roles["admin,guest"],每个参数通过才算通过,相当于hasAllRoles()方法。 50 | * anon:例子/admins/**=anon 没有参数,表示可以匿名使用。 51 | * authc:例如/admins/user/**=authc表示需要认证才能使用,没有参数。 52 | * authcBasic:例如/admins/user/**=authcBasic没有参数表示httpBasic认证。 53 | * ssl:例子/admins/user/**=ssl没有参数,表示安全的url请求,协议为https 54 | * user:例如/admins/user/**=user没有参数表示必须存在用户,当登入操作时不做检查。 55 | *******************************************/ 56 | //jwtAuthcFilter控制器用来做身份认证,用restfulPermissionFilter做restful权限验证。 57 | String restPermissionString = "jwtAuthcFilter,restfulPermissionFilter[{0}]"; 58 | 59 | // 加载默认的url过滤定义 60 | Ini ini = new Ini(); 61 | ini.load(this.filterChainDefinitions); 62 | Ini.Section section = ini.getSection(Ini.DEFAULT_SECTION_NAME); 63 | 64 | List dtoList = this.resourceService.listAllAvailable(); 65 | // 将自定义url过滤添加到section中 66 | for (AvailableResourceDTO dto : dtoList) { 67 | if(dto.getCode() != null && !dto.getCode().trim().equals("")) { 68 | if(!section.containsKey(dto.getCode())) { 69 | section.put(dto.getUrl(), MessageFormat.format(restPermissionString, dto.getCode())); 70 | } 71 | } 72 | } 73 | section.put("/**", "jwtAuthcFilter"); 74 | return section; 75 | } 76 | 77 | @Override 78 | public Class getObjectType() { 79 | return this.getClass(); 80 | } 81 | 82 | @Override 83 | public boolean isSingleton() { 84 | return false; 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/security/JwtAuthcFilter.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.security; 2 | 3 | import javax.servlet.ServletRequest; 4 | import javax.servlet.ServletResponse; 5 | import javax.servlet.http.HttpServletRequest; 6 | 7 | import org.apache.commons.lang3.StringUtils; 8 | import org.apache.shiro.web.filter.AccessControlFilter; 9 | import org.apache.shiro.web.servlet.ShiroHttpServletRequest; 10 | 11 | public class JwtAuthcFilter extends AccessControlFilter { 12 | 13 | @Override 14 | protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) 15 | throws Exception { 16 | //处理跨域请求 17 | if(request instanceof ShiroHttpServletRequest) { 18 | if(StringUtils.equalsIgnoreCase("OPTIONS", ((ShiroHttpServletRequest) request).getMethod())) { 19 | return true; 20 | } 21 | } 22 | 23 | // 拦截后先进入该方法。直接返回false,交由onAccessDenied处理鉴权与登录逻辑 24 | return false; 25 | } 26 | 27 | @Override 28 | protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception { 29 | //从header中得到token 30 | String token = ((HttpServletRequest)request).getHeader("X-Token"); 31 | String host = request.getRemoteHost(); 32 | 33 | JwtToken jwtToken = new JwtToken(token, host); 34 | try { 35 | //委托给Realm进行登录 36 | getSubject(request, response).login(jwtToken); 37 | } catch (Exception e) { 38 | return false; 39 | } 40 | return true; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/security/JwtDefaultSubjectFactory.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.security; 2 | 3 | import org.apache.shiro.subject.Subject; 4 | import org.apache.shiro.subject.SubjectContext; 5 | import org.apache.shiro.web.mgt.DefaultWebSubjectFactory; 6 | 7 | /** 8 | * Shiro Subject工厂类 9 | * 10 | * @author hulichao 11 | * @date 2018/3/15 12 | */ 13 | public class JwtDefaultSubjectFactory extends DefaultWebSubjectFactory { 14 | 15 | @Override 16 | public Subject createSubject(SubjectContext context) { 17 | // 不创建session 18 | context.setSessionCreationEnabled(false); 19 | Subject subject = super.createSubject(context); 20 | return subject; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/security/JwtRealm.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.security; 2 | 3 | import java.util.List; 4 | 5 | import com.hu.zhcc.shiro.service.ResourceService; 6 | import org.apache.shiro.authc.AuthenticationException; 7 | import org.apache.shiro.authc.AuthenticationInfo; 8 | import org.apache.shiro.authc.AuthenticationToken; 9 | import org.apache.shiro.authc.SimpleAuthenticationInfo; 10 | import org.apache.shiro.authz.AuthorizationInfo; 11 | import org.apache.shiro.authz.SimpleAuthorizationInfo; 12 | import org.apache.shiro.realm.AuthorizingRealm; 13 | import org.apache.shiro.subject.PrincipalCollection; 14 | import org.springframework.beans.factory.annotation.Autowired; 15 | 16 | import com.alibaba.fastjson.JSONObject; 17 | import com.hu.zhcc.shiro.utils.JwtUtils; 18 | 19 | import io.jsonwebtoken.Claims; 20 | import io.jsonwebtoken.ExpiredJwtException; 21 | import io.jsonwebtoken.MalformedJwtException; 22 | import io.jsonwebtoken.SignatureException; 23 | import io.jsonwebtoken.UnsupportedJwtException; 24 | 25 | /** 26 | * 无状态Shiro Realm 27 | * 28 | * @author hulichao 29 | * @date 2018/3/15 30 | */ 31 | public class JwtRealm extends AuthorizingRealm { 32 | 33 | @Autowired 34 | private ResourceService resourceService; 35 | 36 | @Autowired 37 | private JwtUtils jwtUtils; 38 | 39 | @Override 40 | public boolean supports(AuthenticationToken token) { 41 | //仅支持StatelessToken类型的Token 42 | return token instanceof JwtToken; 43 | } 44 | 45 | @Override 46 | protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { 47 | /********************************************* 48 | * RestfulPermissionFilter 过滤器说明: 49 | * Actions representing HTTP Method values (GET -> read, POST -> create, etc) 50 | * private static final String CREATE_ACTION = "create"; 51 | * private static final String READ_ACTION = "read"; 52 | * private static final String UPDATE_ACTION = "update"; 53 | * private static final String DELETE_ACTION = "delete"; 54 | *********************************************/ 55 | // 如果不为securityManager配置缓存管理器,该方法在每次鉴权前都会从数据库中查询权限数据。 56 | // 分布式环境下,建议将权限保存在redis中,避免每次从数据库中加载。 57 | //JSONObject json = JSONObject.parseObject(principals.toString()); 58 | Claims claims = jwtUtils.parseJWT(principals.toString()); 59 | JSONObject json = JSONObject.parseObject(claims.getSubject()); 60 | // 得到用户的权限code 61 | List permissionCodeList = resourceService.listPermissionCodeByUserId(json.getIntValue("userId")); 62 | SimpleAuthorizationInfo simpleAuthorInfo = new SimpleAuthorizationInfo(); 63 | for (String permissionCode : permissionCodeList) { 64 | if (permissionCode != null && !permissionCode.trim().equals("")) { 65 | simpleAuthorInfo.addStringPermission(permissionCode); 66 | } 67 | // 如果要添加基于角色的鉴权,可调用simpleAuthorInfo.addRole("role_name")添加用户所属角色。 68 | } 69 | return simpleAuthorInfo; 70 | } 71 | 72 | @Override 73 | protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { 74 | JwtToken jwtToken = (JwtToken) token; 75 | String jwt = (String) jwtToken.getPrincipal(); 76 | try { 77 | Claims claims = jwtUtils.parseJWT(jwt); 78 | //验证host 79 | JSONObject json = JSONObject.parseObject(claims.getSubject()); 80 | if(!jwtToken.getHost().equals(json.getString("host"))) { 81 | throw new AuthenticationException("令牌来路非法"); 82 | } 83 | return new SimpleAuthenticationInfo(jwt, Boolean.TRUE, json.getString("username")); 84 | } catch (ExpiredJwtException e) { 85 | throw new AuthenticationException("令牌过期:" + e.getMessage()); 86 | } catch (UnsupportedJwtException e) { 87 | throw new AuthenticationException("令牌无效:" + e.getMessage()); 88 | } catch (MalformedJwtException e) { 89 | throw new AuthenticationException("令牌格式错误:" + e.getMessage()); 90 | } catch (SignatureException e) { 91 | throw new AuthenticationException("令牌签名无效:" + e.getMessage()); 92 | } catch (IllegalArgumentException e) { 93 | throw new AuthenticationException("令牌参数异常:" + e.getMessage()); 94 | } catch (Exception e) { 95 | throw new AuthenticationException("令牌错误:" + e.getMessage()); 96 | } 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/security/JwtToken.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.security; 2 | 3 | import org.apache.shiro.authc.AuthenticationToken; 4 | 5 | /** 6 | * JWT token 7 | * 8 | * @author hulichao 9 | * @date 2018/3/17 10 | */ 11 | public final class JwtToken implements AuthenticationToken { 12 | 13 | private static final long serialVersionUID = 9028340303836653244L; 14 | 15 | private String jwt; 16 | 17 | private String host; 18 | 19 | public JwtToken(String jwt, String host) { 20 | this.jwt = jwt; 21 | this.host = host; 22 | } 23 | 24 | @Override 25 | public Object getPrincipal() { 26 | return this.jwt; 27 | } 28 | 29 | @Override 30 | public Object getCredentials() { 31 | return Boolean.TRUE; 32 | } 33 | 34 | public String getHost() { 35 | return this.host; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/security/RestfulPermissionFilter.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.security; 2 | 3 | import javax.servlet.ServletRequest; 4 | import javax.servlet.ServletResponse; 5 | import javax.servlet.http.HttpServletRequest; 6 | 7 | import org.apache.commons.lang3.StringUtils; 8 | import org.apache.shiro.subject.Subject; 9 | import org.apache.shiro.web.filter.authz.AuthorizationFilter; 10 | import org.apache.shiro.web.servlet.ShiroHttpServletRequest; 11 | 12 | /** 13 | * RESTful权限过滤器 14 | * 15 | * @author hulichao 16 | * @date 2018/4/6 17 | */ 18 | public class RestfulPermissionFilter extends AuthorizationFilter { 19 | 20 | @Override 21 | protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) 22 | throws Exception { 23 | String method = ((HttpServletRequest) request).getMethod(); 24 | 25 | // 处理跨域请求 26 | if (request instanceof ShiroHttpServletRequest) { 27 | if (StringUtils.equalsIgnoreCase("OPTIONS", method)) { 28 | return true; 29 | } 30 | } 31 | 32 | String permissionString = ((String[]) mappedValue)[0]; 33 | Subject subject = getSubject(request, response); 34 | switch (method.toLowerCase()) { 35 | case "get": 36 | permissionString += ":read"; 37 | break; 38 | case "put": 39 | permissionString += ":update"; 40 | break; 41 | case "post": 42 | permissionString += ":create"; 43 | break; 44 | case "delete": 45 | permissionString += ":delete"; 46 | break; 47 | } 48 | return subject.isPermitted(permissionString); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/service/ResourceService.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.service; 2 | 3 | 4 | import com.hu.zhcc.shiro.entity.dto.AvailableResourceDTO; 5 | import com.hu.zhcc.shiro.entity.dto.ResourceDTO; 6 | import com.hu.zhcc.shiro.entity.dto.ResourcePermissionDTO; 7 | import com.hu.zhcc.shiro.entity.bo.Resource; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | * 资源服务接口 13 | * 14 | * @author hulichao 15 | * @date 2018/3/25 16 | */ 17 | public interface ResourceService { 18 | 19 | /** 20 | * 得到所有可用的资源 21 | * @return 22 | */ 23 | List listAllAvailable(); 24 | 25 | /** 26 | * 得到用户授权的资源 27 | * @param userId 用户Id 28 | * @return 29 | */ 30 | List listAuthorizedByUserId(int userId); 31 | 32 | /** 33 | * 根据路由id得到资源列表 34 | * @param routerId 35 | * @return 36 | */ 37 | List listByRouterId(int routerId); 38 | 39 | /** 40 | * 根据id得到资源 41 | * @param id 42 | * @return 43 | */ 44 | ResourceDTO getById(int id); 45 | 46 | /** 47 | * 添加资源 48 | * @param dto 49 | * @return 新增资源的主键值 50 | */ 51 | int saveResource(ResourceDTO dto); 52 | 53 | /** 54 | * 更新资源 55 | * @param dto 56 | * @return 数据库中受影响的行数 57 | */ 58 | int updateResource(ResourceDTO dto); 59 | 60 | /** 61 | * 删除资源 62 | * @param id 63 | * @return 数据库中受影响的行数 64 | */ 65 | int removeResource(int id); 66 | 67 | /** 68 | * 得到用户资源权限 69 | * 70 | * @param userId 71 | * 用户Id 72 | * @return 73 | */ 74 | List listPermissionByUserId(int userId); 75 | 76 | /** 77 | * 根据路由id得到资源权限 78 | * @param userId 79 | * @param routerId 80 | * @return 81 | */ 82 | List listPermission(int userId, int routerId); 83 | 84 | /** 85 | * 得到用户资源权限代码 86 | * 87 | * @param userId 88 | * 用户Id 89 | * @return 90 | */ 91 | List listPermissionCodeByUserId(int userId); 92 | 93 | } -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/service/ResourceServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.service; 2 | 3 | import com.hu.zhcc.shiro.dao.ResourceDAO; 4 | import com.hu.zhcc.shiro.dao.ResourcePermissionDAO; 5 | import com.hu.zhcc.shiro.entity.dto.AvailableResourceDTO; 6 | import com.hu.zhcc.shiro.entity.dto.ResourceDTO; 7 | import com.hu.zhcc.shiro.entity.dto.ResourcePermissionDTO; 8 | import com.hu.zhcc.shiro.entity.bo.Resource; 9 | import com.hu.zhcc.shiro.entity.bo.Router; 10 | import org.springframework.beans.BeanUtils; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.stereotype.Service; 13 | import org.springframework.transaction.annotation.Transactional; 14 | 15 | import java.util.ArrayList; 16 | import java.util.HashMap; 17 | import java.util.List; 18 | import java.util.Map; 19 | 20 | /** 21 | * 资源服务实现类 22 | * 23 | * @author hulichao 24 | * @date 2018/3/26 25 | */ 26 | @Service("resourceService") 27 | public class ResourceServiceImpl implements ResourceService { 28 | 29 | @Autowired 30 | private ResourceDAO resourceDAO; 31 | 32 | @Autowired 33 | private ResourcePermissionDAO permissionDAO; 34 | 35 | /** 36 | * 得到所有可用的资源 37 | * 38 | * @return 39 | */ 40 | @Override 41 | public List listAllAvailable() { 42 | Map params = new HashMap(); 43 | params.put("locked", 0); 44 | List list = this.resourceDAO.listResource(params); 45 | List dtoList = new ArrayList(list.size()); 46 | for(Resource resource : list) { 47 | AvailableResourceDTO dto = new AvailableResourceDTO(); 48 | BeanUtils.copyProperties(resource, dto); 49 | dto.setRouterId(resource.getRouter() != null ? resource.getRouter().getId() : 0); 50 | dtoList.add(dto); 51 | } 52 | return dtoList; 53 | } 54 | 55 | /** 56 | * 得到用户授权的资源 57 | * 58 | * @param userId 59 | * 用户Id 60 | * @return 61 | */ 62 | @Override 63 | public List listAuthorizedByUserId(int userId) { 64 | List list = this.resourceDAO.listAuthorized(userId); 65 | return getDtoList(list); 66 | } 67 | 68 | /** 69 | * 根据路由id得到资源列表 70 | * @param routerId 71 | * @return 72 | */ 73 | @Override 74 | public List listByRouterId(int routerId) { 75 | List list = this.resourceDAO.listByRouterId(routerId); 76 | return getDtoList(list); 77 | } 78 | 79 | /** 80 | * 根据id得到资源 81 | * @param id 82 | * @return 83 | */ 84 | @Override 85 | public ResourceDTO getById(int id) { 86 | Resource resource = this.resourceDAO.getById(id); 87 | if(resource == null) { 88 | return null; 89 | } 90 | ResourceDTO dto = new ResourceDTO(); 91 | BeanUtils.copyProperties(resource, dto); 92 | dto.setRouterId(resource.getRouter().getId()); 93 | dto.setRouterName(resource.getRouter().getName()); 94 | return dto; 95 | } 96 | 97 | /** 98 | * 添加资源 99 | * @param dto 100 | * @return 新增资源的主键值 101 | */ 102 | @Override 103 | @Transactional 104 | public int saveResource(ResourceDTO dto) { 105 | Resource resource = new Resource(); 106 | BeanUtils.copyProperties(dto, resource); 107 | Router router = new Router(); 108 | router.setId(dto.getRouterId()); 109 | resource.setRouter(router); 110 | return this.resourceDAO.saveResource(resource); 111 | } 112 | 113 | /** 114 | * 更新资源 115 | * @param dto 116 | * @return 数据库中受影响的行数 117 | */ 118 | @Override 119 | @Transactional 120 | public int updateResource(ResourceDTO dto) { 121 | Resource resource = new Resource(); 122 | BeanUtils.copyProperties(dto, resource); 123 | Router router = new Router(); 124 | router.setId(dto.getRouterId()); 125 | resource.setRouter(router); 126 | return this.resourceDAO.updateResource(resource); 127 | } 128 | 129 | /** 130 | * 删除资源 131 | * @param id 132 | * @return 数据库中受影响的行数 133 | */ 134 | @Override 135 | @Transactional 136 | public int removeResource(int id) { 137 | return this.resourceDAO.removeResource(id); 138 | } 139 | 140 | /** 141 | * 得到用户资源权限 142 | * 143 | * @param userId 144 | * 用户Id 145 | * @return 146 | */ 147 | @Override 148 | public List listPermissionByUserId(int userId) { 149 | return this.permissionDAO.listByUserId(userId); 150 | } 151 | 152 | /** 153 | * 得到资源权限 154 | * @param userId 155 | * @param routerId 156 | * @return 157 | */ 158 | @Override 159 | public List listPermission(int userId, int routerId) { 160 | List resourceList = this.permissionDAO.listPermission(userId, routerId); 161 | List dtoList = new ArrayList(resourceList.size()); 162 | for(Resource resource : resourceList) { 163 | ResourcePermissionDTO dto = new ResourcePermissionDTO(); 164 | dto.setResourceId(resource.getId()); 165 | dto.setResourcePermission(resource.getPermission()); 166 | dtoList.add(dto); 167 | } 168 | return dtoList; 169 | } 170 | 171 | /** 172 | * 得到用户资源权限代码 173 | * 174 | * @param userId 175 | * 用户Id 176 | * @return 177 | */ 178 | @Override 179 | public List listPermissionCodeByUserId(int userId) { 180 | return this.permissionDAO.listCodeByUserId(userId); 181 | } 182 | 183 | /** 184 | * 得到dto列表 185 | * @param list 186 | * @return 187 | */ 188 | private List getDtoList(List list) { 189 | List dtoList = new ArrayList(list.size()); 190 | for(Resource resource : list) { 191 | ResourceDTO dto = new ResourceDTO(); 192 | BeanUtils.copyProperties(resource, dto); 193 | dto.setRouterId(resource.getRouter().getId()); 194 | dtoList.add(dto); 195 | } 196 | return dtoList; 197 | } 198 | 199 | } 200 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/service/RoleService.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.service; 2 | 3 | 4 | import com.hu.zhcc.shiro.entity.dto.RoleDTO; 5 | import com.hu.zhcc.common.entity.Page; 6 | 7 | import java.util.List; 8 | import java.util.Map; 9 | 10 | /** 11 | * 角色服务接口 12 | * 13 | * @author hulichao 14 | * @date 2018/3/27 15 | */ 16 | public interface RoleService { 17 | 18 | /** 19 | * 得到角色列表 20 | * @param paremeters 21 | * @param offset 22 | * @param limit 23 | * @return 24 | */ 25 | Page listRole(Map paremeters, int offset, int limit); 26 | 27 | /** 28 | * 添加角色 29 | * @param dto 30 | * @return 31 | */ 32 | RoleDTO saveRole(RoleDTO dto); 33 | 34 | /** 35 | * 根据id得到角色 36 | * @param id 37 | * @return 38 | */ 39 | RoleDTO getById(int id); 40 | 41 | /** 42 | * 更新角色 43 | * @param dto 44 | * @return 45 | */ 46 | int updateRole(RoleDTO dto); 47 | 48 | /** 49 | * 得到所有角色 50 | * @return 51 | */ 52 | List listAllRole(); 53 | 54 | /** 55 | * 删除角色 56 | * @param id 57 | * @return 58 | */ 59 | int removeRole(int id); 60 | 61 | /** 62 | * 得到资源权限列表 63 | * @param roleId 64 | * @return 65 | */ 66 | List listResourcePermission(int roleId); 67 | 68 | /** 69 | * 保存角色权限 70 | * @param roleId 71 | * @param routerIds 72 | * @param resourceIds 73 | * @return 74 | */ 75 | int savePermission(int roleId, int[] routerIds, int[] resourceIds); 76 | 77 | } 78 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/service/RoleServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.service; 2 | 3 | import com.hu.zhcc.shiro.dao.ResourcePermissionDAO; 4 | import com.hu.zhcc.shiro.dao.RoleDAO; 5 | import com.hu.zhcc.shiro.dao.RouterPermissionDAO; 6 | import com.hu.zhcc.shiro.dao.UserRoleDAO; 7 | import com.hu.zhcc.shiro.entity.dto.RoleDTO; 8 | import com.hu.zhcc.shiro.entity.bo.Role; 9 | import com.hu.zhcc.common.entity.Page; 10 | import com.hu.zhcc.common.entity.PageRequest; 11 | import org.springframework.beans.BeanUtils; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.stereotype.Service; 14 | import org.springframework.transaction.annotation.Transactional; 15 | 16 | import java.util.ArrayList; 17 | import java.util.List; 18 | import java.util.Map; 19 | 20 | /** 21 | * 角色服务实现类 22 | * 23 | * @author hulichao 24 | * @date 2018/3/27 25 | */ 26 | @Service("roleService") 27 | public class RoleServiceImpl implements RoleService { 28 | 29 | @Autowired 30 | private RoleDAO roleDAO; 31 | 32 | @Autowired 33 | private UserRoleDAO userRoleDAO; 34 | 35 | @Autowired 36 | private ResourcePermissionDAO resourcePermissionDAO; 37 | 38 | @Autowired 39 | private RouterPermissionDAO routerPermissionDAO; 40 | 41 | /** 42 | * 得到角色列表 43 | * @param parameters 44 | * @param offset 45 | * @param limit 46 | * @return 47 | */ 48 | @Override 49 | public Page listRole(Map parameters, int offset, int limit) { 50 | int total = this.roleDAO.countRole(parameters); 51 | List dtoList = null; 52 | if(total > 0) { 53 | PageRequest pageRequest = new PageRequest(offset, limit, parameters, null); 54 | List list = this.roleDAO.listRole(pageRequest); 55 | dtoList = new ArrayList(list.size()); 56 | for(Role role : list) { 57 | RoleDTO dto = new RoleDTO(); 58 | BeanUtils.copyProperties(role, dto); 59 | dtoList.add(dto); 60 | } 61 | } 62 | return new Page(total, dtoList); 63 | } 64 | 65 | /** 66 | * 添加角色 67 | * @param dto 68 | * @return 69 | */ 70 | @Override 71 | @Transactional 72 | public RoleDTO saveRole(RoleDTO dto) { 73 | Role role = new Role(); 74 | BeanUtils.copyProperties(dto, role); 75 | this.roleDAO.saveRole(role); 76 | dto.setId(role.getId()); 77 | return dto; 78 | } 79 | 80 | /** 81 | * 根据id得到角色 82 | * @param id 83 | * @return 84 | */ 85 | @Override 86 | public RoleDTO getById(int id) { 87 | Role role = this.roleDAO.getById(id); 88 | if(role == null) { 89 | return null; 90 | } 91 | RoleDTO dto = new RoleDTO(); 92 | BeanUtils.copyProperties(role, dto); 93 | return dto; 94 | } 95 | 96 | /** 97 | * 更新角色 98 | * @param dto 99 | * @return 100 | */ 101 | @Override 102 | @Transactional 103 | public int updateRole(RoleDTO dto) { 104 | Role role = new Role(); 105 | BeanUtils.copyProperties(dto, role); 106 | return this.roleDAO.updateRole(role); 107 | } 108 | 109 | /** 110 | * 得到所有角色 111 | * @return 112 | */ 113 | @Override 114 | public List listAllRole() { 115 | List list = this.roleDAO.listAllRole(); 116 | List dtoList = new ArrayList(list.size()); 117 | for(Role role : list) { 118 | RoleDTO dto = new RoleDTO(); 119 | BeanUtils.copyProperties(role, dto); 120 | dtoList.add(dto); 121 | } 122 | return dtoList; 123 | } 124 | 125 | /** 126 | * 删除角色 127 | * @param id 128 | * @return 129 | */ 130 | @Override 131 | @Transactional 132 | public int removeRole(int id) { 133 | int rows = this.roleDAO.removeRole(id); 134 | this.userRoleDAO.removeByRoleId(id); 135 | this.resourcePermissionDAO.removeByRoleId(id); 136 | this.routerPermissionDAO.removeByRoleId(id); 137 | return rows; 138 | } 139 | 140 | /** 141 | * 得到资源权限列表 142 | * @param roleId 143 | * @return 144 | */ 145 | @Override 146 | public List listResourcePermission(int roleId) { 147 | return this.resourcePermissionDAO.listByRoleId(roleId); 148 | } 149 | 150 | /** 151 | * 保存角色权限 152 | * @param roleId 153 | * @param routerIds 154 | * @param resourceIds 155 | * @return 156 | */ 157 | @Override 158 | @Transactional 159 | public int savePermission(int roleId, int[] routerIds, int[] resourceIds) { 160 | // 先删除后添加 161 | this.routerPermissionDAO.removeByRoleId(roleId); 162 | this.resourcePermissionDAO.removeByRoleId(roleId); 163 | 164 | int rows = 0; 165 | rows += this.routerPermissionDAO.savePermission(roleId, routerIds); 166 | rows += this.resourcePermissionDAO.savePermission(roleId, resourceIds); 167 | return rows; 168 | } 169 | 170 | } 171 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/service/RouterService.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.service; 2 | 3 | 4 | import com.hu.zhcc.shiro.entity.dto.RouterDTO; 5 | import com.hu.zhcc.common.entity.Page; 6 | 7 | import java.util.List; 8 | import java.util.Map; 9 | 10 | /** 11 | * 路由服务接口 12 | * 13 | * @author hulichao 14 | * @date 2018/3/19 15 | */ 16 | public interface RouterService { 17 | 18 | 19 | /** 20 | * 得到路由列表 21 | * @param parameters 22 | * @param offset 23 | * @param limit 24 | * @return 25 | */ 26 | Page listRouter(Map parameters, int offset, int limit); 27 | 28 | /** 29 | * 得到已授权的路由 30 | * 31 | * @param userId 32 | * 用户id 33 | * @return 34 | */ 35 | List listAuthorizedRouter(int userId); 36 | 37 | /** 38 | * 根据路由id得到路由信息 39 | * @param id 40 | * @return 41 | */ 42 | RouterDTO getById(int id); 43 | 44 | /** 45 | * 根据上级路由id得到路由列表 46 | * @param parentId 47 | * @return 48 | */ 49 | List listByParentId(int parentId); 50 | 51 | /** 52 | * 更新路由 53 | * @param routerDto 54 | * @return 55 | */ 56 | int updateRouter(RouterDTO routerDto); 57 | 58 | /** 59 | * 添加路由 60 | * @param dto 61 | * @return 62 | */ 63 | RouterDTO saveRouter(RouterDTO dto); 64 | 65 | /** 66 | * 删除路由 67 | * @param id 68 | * @return 69 | */ 70 | int removeRouter(int id); 71 | 72 | /** 73 | * 得到所有路由 74 | * @param includeLocked 包括状态为已禁用的 75 | * @return 76 | */ 77 | List listAll(boolean includeLocked); 78 | 79 | } 80 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/service/RouterServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.service; 2 | 3 | import com.hu.zhcc.common.entity.Page; 4 | import com.hu.zhcc.common.entity.PageRequest; 5 | import com.hu.zhcc.shiro.dao.RouterDAO; 6 | import com.hu.zhcc.shiro.dao.RouterPermissionDAO; 7 | import com.hu.zhcc.shiro.entity.bo.Router; 8 | import com.hu.zhcc.shiro.entity.dto.RouterDTO; 9 | import org.springframework.beans.BeanUtils; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.stereotype.Service; 12 | import org.springframework.transaction.annotation.Transactional; 13 | 14 | import java.util.ArrayList; 15 | import java.util.List; 16 | import java.util.Map; 17 | 18 | /** 19 | * 前端路由服务实现类 20 | * 21 | * @author hulichao 22 | * @date 2018/3/19 23 | */ 24 | @Service("routerService") 25 | public class RouterServiceImpl implements RouterService { 26 | 27 | @Autowired 28 | private RouterDAO routerDAO; 29 | 30 | @Autowired 31 | private RouterPermissionDAO routerPermissionDAO; 32 | 33 | /** 34 | * 得到路由列表 35 | * @param parameters 36 | * @param offset 37 | * @param limit 38 | * @return 39 | */ 40 | @Override 41 | public Page listRouter(Map parameters, int offset, int limit) { 42 | int total = routerDAO.countRouter(parameters); 43 | List routerList = null; 44 | List dtoList = null; 45 | if(total > 0) { 46 | PageRequest pageRequest = new PageRequest(offset, limit, parameters, null); 47 | routerList = routerDAO.listRouter(pageRequest); 48 | 49 | dtoList = new ArrayList(routerList.size()); 50 | for(Router router : routerList) { 51 | RouterDTO dto = new RouterDTO(); 52 | BeanUtils.copyProperties(router, dto); 53 | if(router.getParent() != null) { 54 | dto.setParentId(router.getParent().getId()); 55 | dto.setParentName(router.getParent().getName()); 56 | } 57 | dtoList.add(dto); 58 | } 59 | } 60 | return new Page(total, dtoList); 61 | } 62 | 63 | /** 64 | * 得到已授权的路由 65 | * 66 | * @param userId 67 | * 用户id 68 | * @return 69 | */ 70 | @Override 71 | public List listAuthorizedRouter(int userId) { 72 | List routerList = this.routerPermissionDAO.listByUserId(userId); 73 | List dtoList = new ArrayList(routerList.size()); 74 | for(Router router : routerList) { 75 | RouterDTO dto = new RouterDTO(); 76 | BeanUtils.copyProperties(router, dto); 77 | if(router.getParent() != null) { 78 | dto.setParentId(router.getParent().getId()); 79 | dto.setParentName(router.getParent().getName()); 80 | } 81 | dtoList.add(dto); 82 | } 83 | return dtoList; 84 | } 85 | 86 | /** 87 | * 根绝id得到路由信息 88 | * @param id 89 | * @return 90 | */ 91 | @Override 92 | public RouterDTO getById(int id) { 93 | Router router = this.routerDAO.getById(id); 94 | RouterDTO dto = new RouterDTO(); 95 | BeanUtils.copyProperties(router, dto); 96 | if(router.getParent() != null) { 97 | dto.setParentId(router.getParent().getId()); 98 | dto.setParentName(router.getParent().getName()); 99 | } 100 | return dto; 101 | } 102 | 103 | /** 104 | * 根据上级路由id得到路由列表 105 | * @param parentId 106 | * @return 107 | */ 108 | @Override 109 | public List listByParentId(int parentId) { 110 | List routerList = this.routerDAO.listByParentId(parentId); 111 | List dtoList = new ArrayList(routerList.size()); 112 | for(Router router : routerList) { 113 | RouterDTO dto = new RouterDTO(); 114 | BeanUtils.copyProperties(router, dto); 115 | dtoList.add(dto); 116 | } 117 | return dtoList; 118 | } 119 | 120 | /** 121 | * 更新路由 122 | * @param routerDto 123 | * @return 124 | */ 125 | @Override 126 | @Transactional 127 | public int updateRouter(RouterDTO routerDto) { 128 | Router router = new Router(); 129 | BeanUtils.copyProperties(routerDto, router); 130 | Router parent = new Router(); 131 | parent.setId(routerDto.getParentId()); 132 | router.setParent(parent); 133 | return this.routerDAO.updateRouter(router); 134 | } 135 | 136 | /** 137 | * 添加路由 138 | * @param dto 139 | * @return 140 | */ 141 | @Override 142 | @Transactional 143 | public RouterDTO saveRouter(RouterDTO dto) { 144 | Router router = new Router(); 145 | BeanUtils.copyProperties(dto, router); 146 | Router parent = new Router(); 147 | parent.setId(dto.getParentId()); 148 | router.setParent(parent); 149 | this.routerDAO.saveRouter(router); 150 | dto.setId(router.getId()); 151 | return dto; 152 | } 153 | 154 | /** 155 | * 删除路由 156 | * @param id 157 | * @return 158 | */ 159 | @Override 160 | @Transactional 161 | public int removeRouter(int id) { 162 | return this.routerDAO.removeRouter(id); 163 | } 164 | 165 | /** 166 | * 得到所有路由 167 | * @param includeLocked 包括状态为已禁用的 168 | * @return 169 | */ 170 | @Override 171 | public List listAll(boolean includeLocked) { 172 | List list = this.routerDAO.listAll(includeLocked); 173 | List dtoList = new ArrayList(list.size()); 174 | for(Router router : list) { 175 | RouterDTO dto = new RouterDTO(); 176 | BeanUtils.copyProperties(router, dto); 177 | dto.setParentId(router.getParent() != null ? router.getParent().getId() : 0); 178 | dtoList.add(dto); 179 | } 180 | return dtoList; 181 | } 182 | 183 | } 184 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.service; 2 | 3 | 4 | import com.hu.zhcc.common.entity.Page; 5 | import com.hu.zhcc.shiro.entity.dto.UserDTO; 6 | 7 | import java.util.Map; 8 | 9 | /** 10 | * 用户管理服务接口 11 | * 12 | * @author hulichao 13 | * @date 2018/3/28 14 | */ 15 | public interface UserService { 16 | 17 | /** 18 | * 根据登录名称得到系统用户 19 | * 20 | * @param loginName 21 | * 登录名称 22 | * @return 23 | */ 24 | UserDTO getByLoginName(String loginName); 25 | 26 | /** 27 | * 添加系统用户 28 | * 29 | * @param dto 30 | * User类的实例 31 | * @return 32 | */ 33 | UserDTO saveUser(UserDTO dto); 34 | 35 | /** 36 | * 修改用户口令 37 | * 38 | * @param userId 39 | * 用户id 40 | * @param salt 41 | * 盐 42 | * @param password 43 | * 口令 44 | * @return 45 | */ 46 | int changePassword(int userId, String salt, String password); 47 | 48 | /** 49 | * 得到用户列表 50 | * @param parameters 包含查询字段和value的map 51 | * @param order 排序字符串 52 | * @param offset 偏移量 53 | * @param limit 查询数据条数 54 | * @return 55 | */ 56 | Page listUser(Map parameters, String order, int offset, int limit); 57 | 58 | /** 59 | * 根据Id查询用户 60 | * @param id 用户id 61 | * @return 62 | */ 63 | UserDTO getById(int id); 64 | 65 | /** 66 | * 更新用户信息 67 | * @param dto 用户dto类 68 | * @return 更新影响的行数 69 | */ 70 | int updateUser(UserDTO dto); 71 | 72 | /** 73 | * 删除用户 74 | * @param id 75 | * @return 76 | */ 77 | int removeUser(int id); 78 | 79 | /** 80 | * 更新当前用户信息 81 | * @param dto 82 | * @return 83 | */ 84 | int updateCurrentUser(UserDTO dto); 85 | 86 | } 87 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/service/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.service; 2 | 3 | import com.hu.zhcc.shiro.dao.UserDAO; 4 | import com.hu.zhcc.shiro.dao.UserRoleDAO; 5 | import com.hu.zhcc.shiro.entity.dto.UserDTO; 6 | import com.hu.zhcc.shiro.entity.bo.Role; 7 | import com.hu.zhcc.shiro.entity.bo.User; 8 | import com.hu.zhcc.common.entity.Page; 9 | import com.hu.zhcc.common.entity.PageRequest; 10 | import com.hu.zhcc.common.exception.ServiceException; 11 | import org.apache.commons.lang3.StringUtils; 12 | import org.springframework.beans.BeanUtils; 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.stereotype.Service; 15 | import org.springframework.transaction.annotation.Transactional; 16 | 17 | import java.util.ArrayList; 18 | import java.util.Date; 19 | import java.util.List; 20 | import java.util.Map; 21 | 22 | /** 23 | * 用户服务实现类 24 | * 25 | * @author hulichao 26 | * @date 2018/3/29 27 | */ 28 | @Service("userService") 29 | public class UserServiceImpl implements UserService { 30 | 31 | @Autowired 32 | private UserDAO userDAO; 33 | 34 | @Autowired 35 | private UserRoleDAO userRoleDAO; 36 | 37 | /** 38 | * 系统登录 39 | * 40 | * @param loginName 41 | * @return true:成功;false:失败 42 | */ 43 | @Override 44 | public UserDTO getByLoginName(String loginName) { 45 | User user = userDAO.getByLoginName(loginName); 46 | if (user == null) { 47 | return null; 48 | } 49 | UserDTO dto = new UserDTO(); 50 | BeanUtils.copyProperties(user, dto); 51 | if (user.getCreatedBy() != null) { 52 | dto.setCreatorName(user.getCreatedBy().getName()); 53 | } 54 | return dto; 55 | } 56 | 57 | /** 58 | * 添加系统用户 59 | * 60 | * @param dto 61 | * UserDTO类的实例 62 | * @return 63 | */ 64 | @Override 65 | @Transactional 66 | public UserDTO saveUser(UserDTO dto) { 67 | // 判断登录名是否重复 68 | User existsUser = userDAO.getByLoginName(dto.getLoginName()); 69 | if(existsUser != null) { 70 | throw new ServiceException("登录名 " + dto.getLoginName() + " 已存在"); 71 | } 72 | 73 | User user = new User(); 74 | BeanUtils.copyProperties(dto, user); 75 | User creator = new User(); 76 | creator.setId(dto.getCreatorId()); 77 | user.setCreatedBy(creator); 78 | user.setCreatedAt(new Date()); 79 | userDAO.saveUser(user); 80 | 81 | // 保存用户角色 82 | userRoleDAO.removeByUserId(user.getId()); 83 | userRoleDAO.saveUserRole(user.getId(), dto.getRoleIds()); 84 | 85 | dto.setId(user.getId()); 86 | return dto; 87 | } 88 | 89 | /** 90 | * 修改用户口令 91 | * 92 | * @param userId 93 | * 用户id 94 | * @param salt 95 | * 盐 96 | * @param password 97 | * 口令 98 | * @return 99 | */ 100 | @Override 101 | @Transactional 102 | public int changePassword(int userId, String salt, String password) { 103 | return userDAO.updatePassword(userId, salt, password); 104 | } 105 | 106 | /** 107 | * 得到用户列表 108 | * 109 | * @param parameters 110 | * 包含查询字段和value的map 111 | * @param order 112 | * 排序字符串 113 | * @param offset 114 | * 偏移量 115 | * @param limit 116 | * 查询数据条数 117 | * @return 118 | */ 119 | @Override 120 | public Page listUser(Map parameters, String order, int offset, int limit) { 121 | int total = userDAO.countUser(parameters); 122 | List userList = null; 123 | List dtoList = null; 124 | if (total > 0) { 125 | PageRequest pageRequest = new PageRequest(offset, limit, parameters, order); 126 | userList = userDAO.listUser(pageRequest); 127 | dtoList = new ArrayList(userList.size()); 128 | 129 | for(User user : userList) { 130 | UserDTO dto = new UserDTO(); 131 | BeanUtils.copyProperties(user, dto); 132 | if (user.getCreatedBy() != null) { 133 | dto.setCreatorName(user.getCreatedBy().getName()); 134 | } 135 | dtoList.add(dto); 136 | } 137 | } 138 | return new Page(total, dtoList); 139 | } 140 | 141 | /** 142 | * 根据Id查询用户 143 | * 144 | * @param id 145 | * 用户id 146 | * @return 147 | */ 148 | @Override 149 | public UserDTO getById(int id) { 150 | User user = userDAO.getById(id); 151 | if(user == null) { 152 | return null; 153 | } 154 | UserDTO dto = new UserDTO(); 155 | BeanUtils.copyProperties(user, dto); 156 | if (user.getCreatedBy() != null) { 157 | dto.setCreatorName(user.getCreatedBy().getName()); 158 | } 159 | 160 | // 得到用户角色 161 | List roles = userRoleDAO.listUserRole(id); 162 | int[] roleIds = new int[roles.size()]; 163 | for(int i = 0; i < roleIds.length; i++) { 164 | roleIds[i] = roles.get(i).getId(); 165 | } 166 | dto.setRoleIds(roleIds); 167 | return dto; 168 | } 169 | 170 | /** 171 | * 更新用户信息 172 | * 173 | * @param dto 174 | * 用户dto类 175 | * @return 更新影响的行数 176 | */ 177 | @Override 178 | @Transactional 179 | public int updateUser(UserDTO dto) { 180 | // 判断登录名是否重复 181 | User existsUser = userDAO.getByLoginName(dto.getLoginName()); 182 | if(existsUser != null && existsUser.getId() != dto.getId()) { 183 | throw new ServiceException("登录名" + dto.getLoginName() + " 已存在"); 184 | } 185 | 186 | User user = new User(); 187 | BeanUtils.copyProperties(dto, user); 188 | int rows = userDAO.updateUser(user); 189 | // 保存用户角色 190 | if(rows > 0) { 191 | userRoleDAO.removeByUserId(dto.getId()); 192 | if(dto.getRoleIds() != null && dto.getRoleIds().length > 0) { 193 | userRoleDAO.saveUserRole(dto.getId(), dto.getRoleIds()); 194 | } 195 | } 196 | return rows; 197 | } 198 | 199 | /** 200 | * 删除用户 201 | * 202 | * @param id 203 | * @return 204 | */ 205 | @Override 206 | @Transactional 207 | public int removeUser(int id) { 208 | int rows = userDAO.removeUser(id, new Date().getTime()); 209 | // 删除用户角色 210 | if(rows > 0) { 211 | userRoleDAO.removeByUserId(id); 212 | } 213 | return rows; 214 | } 215 | 216 | /** 217 | * 更新当前用户信息 218 | * @param dto 219 | * @return 220 | */ 221 | @Override 222 | @Transactional 223 | public int updateCurrentUser(UserDTO dto) { 224 | User user = new User(); 225 | user.setId(dto.getId()); 226 | user.setLoginName(dto.getLoginName()); 227 | user.setName(dto.getName()); 228 | int rows = userDAO.updateUser(user); 229 | if(rows > 0 && !StringUtils.isBlank(dto.getPassword())) { 230 | userDAO.updatePassword(dto.getId(), dto.getSalt(), dto.getPassword()); 231 | } 232 | return rows; 233 | } 234 | 235 | } 236 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/java/com/hu/zhcc/shiro/utils/JwtUtils.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc.shiro.utils; 2 | 3 | import io.jsonwebtoken.Claims; 4 | import io.jsonwebtoken.JwtBuilder; 5 | import io.jsonwebtoken.Jwts; 6 | import io.jsonwebtoken.SignatureAlgorithm; 7 | 8 | import javax.crypto.SecretKey; 9 | import javax.crypto.spec.SecretKeySpec; 10 | import java.util.Base64; 11 | import java.util.Date; 12 | 13 | /** 14 | * Jwt工具类 15 | * 16 | * @author hulichao 17 | * @date 2018/3/2 18 | */ 19 | public class JwtUtils { 20 | 21 | private String id; 22 | 23 | private String secret; 24 | 25 | /** 26 | * 存活时间(毫秒) 27 | */ 28 | private long ttlMillis; 29 | 30 | public JwtUtils(String id, String secret, long ttlMillis) { 31 | this.id = id; 32 | this.secret = secret; 33 | this.ttlMillis = ttlMillis; 34 | } 35 | 36 | /** 37 | * 生成加密key 38 | * 39 | * @return 40 | */ 41 | private SecretKey generalKey() { 42 | byte[] encodedKey = Base64.getDecoder().decode(secret); 43 | SecretKey key = new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES"); 44 | return key; 45 | } 46 | 47 | /** 48 | * 创建jwt 49 | * 50 | * @param subject 51 | * @return 52 | */ 53 | public String createJWT(String subject) { 54 | SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; 55 | long nowMillis = System.currentTimeMillis(); 56 | Date now = new Date(nowMillis); 57 | SecretKey key = generalKey(); 58 | JwtBuilder builder = Jwts.builder().setId(id).setIssuedAt(now).setSubject(subject).signWith(signatureAlgorithm, 59 | key); 60 | if (ttlMillis >= 0) { 61 | long expMillis = nowMillis + ttlMillis; 62 | Date exp = new Date(expMillis); 63 | builder.setExpiration(exp); 64 | } 65 | return builder.compact(); 66 | } 67 | 68 | /** 69 | * 解密jwt 70 | * 71 | * @param jwt 72 | * @return 73 | * @throws Exception 74 | */ 75 | public Claims parseJWT(String jwt) { 76 | SecretKey key = generalKey(); 77 | Claims claims = Jwts.parser().setSigningKey(key).parseClaimsJws(jwt).getBody(); 78 | return claims; 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/resources/mapper.com.hu.zhcc.shiro.dao/ResourceDAO.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 41 | 42 | 55 | 56 | 57 | INSERT INTO sys_resource(name,code,url,permission,is_locked,router_id) 58 | VALUES(#{name}, #{code}, #{url}, #{permission}, #{locked}, #{router.id}) 59 | 60 | 61 | 62 | UPDATE sys_resource SET 63 | name=#{name}, code=#{code}, url=#{url}, permission=#{permission}, is_locked=#{locked}, router_id=#{router.id} 64 | WHERE id=#{id} 65 | 66 | 67 | 68 | DELETE FROM sys_resource WHERE id=#{id} 69 | 70 | 71 | 76 | 77 | 82 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/resources/mapper.com.hu.zhcc.shiro.dao/ResourcePermissionDAO.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 17 | 18 | 28 | 29 | 37 | 38 | 39 | DELETE FROM sys_resource_permission WHERE role_id=#{roleId} 40 | 41 | 42 | 45 | 46 | 47 | INSERT INTO sys_resource_permission 48 | VALUES 49 | 50 | (#{arg0}, #{resourceId}) 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/resources/mapper.com.hu.zhcc.shiro.dao/RoleDAO.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 21 | 30 | 31 | 32 | INSERT INTO sys_role(code,name,is_locked) 33 | VALUES(#{code}, #{name}, #{locked}) 34 | 35 | 36 | 39 | 40 | 41 | UPDATE sys_role SET code=#{code}, name=#{name}, is_locked=#{locked} 42 | WHERE id=#{id} 43 | 44 | 45 | 46 | DELETE FROM sys_role WHERE id=#{id} 47 | 48 | 49 | 52 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/resources/mapper.com.hu.zhcc.shiro.dao/RouterDAO.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 43 | 54 | 55 | 60 | 61 | 65 | 66 | 67 | UPDATE sys_router 68 | SET name=#{name},code=#{code},url=#{url},parent_id=#{parent.id},level=#{level}, 69 | is_locked=#{locked},display_order=#{displayOrder},properties=#{properties} 70 | WHERE id=#{id} 71 | 72 | 73 | 74 | INSERT INTO sys_router 75 | 76 | name,parent_id,level,is_locked,display_order, 77 | 78 | code, 79 | 80 | 81 | url, 82 | 83 | 84 | properties, 85 | 86 | 87 | 88 | #{name},#{parent.id}, #{level}, #{locked}, #{displayOrder}, 89 | 90 | #{code}, 91 | 92 | 93 | #{url}, 94 | 95 | 96 | #{properties}, 97 | 98 | 99 | 100 | 101 | 102 | DELETE FROM sys_router WHERE id=#{id} 103 | 104 | 105 | 115 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/resources/mapper.com.hu.zhcc.shiro.dao/RouterPermissionDAO.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 27 | 28 | 29 | DELETE FROM sys_router_permission WHERE role_id=#{roleId} 30 | 31 | 32 | 33 | INSERT INTO sys_router_permission 34 | VALUES 35 | 36 | (#{arg0}, #{routerId}) 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/resources/mapper.com.hu.zhcc.shiro.dao/UserDAO.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 28 | 29 | 30 | 31 | 32 | 37 | 38 | 43 | 44 | 45 | INSERT INTO sys_user(name, 46 | login_name,salt,login_password,is_locked,created_at,created_by) 47 | VALUES(#{name}, #{loginName}, #{salt}, #{password}, #{locked}, 48 | #{createdAt}, #{createdBy.id}) 49 | 50 | 51 | 52 | UPDATE sys_user SET 53 | salt=#{arg1}, 54 | login_password=#{arg2} 55 | WHERE id=#{arg0} 56 | 57 | 58 | 59 | UPDATE sys_user 60 | 61 | name=#{name}, 62 | login_name=#{loginName}, 63 | is_locked=#{locked}, 64 | 65 | WHERE id=#{id} 66 | 67 | 68 | 77 | 95 | 96 | 97 | UPDATE sys_user SET deleted_at=#{arg1} 98 | WHERE id=#{arg0} 99 | 100 | -------------------------------------------------------------------------------- /zhcc-shiro/src/main/resources/mapper.com.hu.zhcc.shiro.dao/UserRoleDAO.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 16 | 17 | 18 | INSERT INTO sys_user_role(user_id,role_id) VALUES 19 | 20 | (#{arg0}, #{roleId}) 21 | 22 | 23 | 24 | 25 | DELETE FROM sys_user_role WHERE user_id=#{userId} 26 | 27 | 28 | 29 | DELETE FROM sys_user_role WHERE role_id=#{roleId} 30 | 31 | -------------------------------------------------------------------------------- /zhcc-shiro/src/test/java/com/hu/TestParameter8.java: -------------------------------------------------------------------------------- 1 | package com.hu; 2 | import java.lang.reflect.Method; 3 | import java.lang.reflect.Parameter; 4 | 5 | /** 6 | * @author hulichao 7 | * @date 2018/7/27 8 | */ 9 | public class TestParameter8 { 10 | public void createUser(String name, int age, int version) { 11 | } 12 | 13 | public static void main(String[] args) throws Exception { 14 | for (Method m : TestParameter8.class.getMethods()) { 15 | System.out.println("--------------------"); 16 | System.out.println(" method: " + m.getName()); 17 | System.out.println(" return: " + m.getReturnType().getName()); 18 | for (Parameter p : m.getParameters()) { 19 | System.out.println("parameter:" + p.getType().getName() + ", " + p.getName()); 20 | } 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /zhcc-shiro/src/test/resources/spring-test-mybatis.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /zhcc-test/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | zhcc-server 7 | com.hu.zhcc 8 | 1.0 9 | 10 | 4.0.0 11 | zhcc-test 12 | jar 13 | zhcc-test 14 | http://hulichao.top 15 | 16 | 此模块主要用来测试,测试的公共组件,需要测试的模块依赖zhcc-test或者顶层依赖也可以 17 | 这是 maven 官方推荐的做法,既解决了测试代码对测试工具类的依赖问题,又不会污染功能代码的目录 18 | 注意:测试注解是可以继承的 19 | 20 | 21 | 22 | 23 | org.apache.maven.plugins 24 | maven-surefire-plugin 25 | 2.4.2 26 | 27 | true 28 | 29 | 30 | 31 | 32 | 33 | 34 | junit 35 | junit 36 | 4.13.1 37 | test 38 | 39 | 40 | junit 41 | junit 42 | 4.13.1 43 | compile 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /zhcc-test/src/main/java/com/hu/zhcc/BaseJunit4Test.java: -------------------------------------------------------------------------------- 1 | package com.hu.zhcc; 2 | 3 | 4 | import org.junit.After; 5 | import org.junit.Before; 6 | import org.junit.runner.RunWith; 7 | import org.springframework.test.context.ContextConfiguration; 8 | import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; 9 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 10 | 11 | @RunWith(SpringJUnit4ClassRunner.class) //使用junit4进行测试 12 | @ContextConfiguration(locations={"classpath:spring/applicationContext.xml"}) //加载配置文件 13 | //------------如果加入以下代码,所有继承该类的测试类都会遵循该配置,也可以不加,在测试类的方法上///控制事务,参见下一个实例 14 | //这个非常关键,如果不加入这个注解配置,事务控制就会完全失效! 15 | //@Transactional 16 | //这里的事务关联到配置文件中的事务控制器(transactionManager = "transactionManager"),同时//指定自动回滚(defaultRollback = true)。这样做操作的数据才不会污染数据库! 17 | //@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true) 18 | //------------ 19 | public class BaseJunit4Test extends AbstractJUnit4SpringContextTests { 20 | @Before 21 | public void init() { 22 | //在运行测试之前的业务代码 23 | } 24 | @After 25 | public void after() { 26 | //在测试完成之后的业务代码 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /zhcc-test/src/main/resources/db.properties: -------------------------------------------------------------------------------- 1 | #jdbc url 2 | jdbc.url=jdbc:mysql://localhost:3306/zhcc?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8 3 | #jdbc \u7528\u6237\u540D 4 | jdbc.username=root 5 | #jdbc \u5BC6\u7801 6 | jdbc.password=123456 7 | 8 | #\u8FDE\u63A5\u6C60\u521D\u59CB\u5316\u5927\u5C0F 9 | jdbc.pool.initialSize=1 10 | #\u6700\u5C0F\u8FDE\u63A5\u6C60\u6570\u91CF 11 | jdbc.pool.minIdle=1 12 | #\u6700\u5927\u8FDE\u63A5\u6C60\u6570\u91CF 13 | jdbc.pool.maxActive=20 14 | 15 | 16 | #redis settings 17 | redis.host=127.0.0.1 18 | redis.port=6379 19 | #\u6CE8\u610F\uFF0C\u5982\u679C\u6CA1\u6709password\uFF0C\u6B64\u5904\u4E0D\u8BBE\u7F6E\u503C\uFF0C\u4F46\u8FD9\u4E00\u9879\u8981\u4FDD\u7559 20 | redis.password= 21 | redis.db.index=1 22 | redis.max_active=500 23 | redis.max_idle=5 24 | redis.max_wait=10000 25 | redis.timeout=10000 -------------------------------------------------------------------------------- /zhcc-test/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg [%file:%line] %n 6 | UTF-8 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /zhcc-test/src/main/resources/mybatis/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /zhcc-test/src/main/resources/spring/applicationContext-jedis.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /zhcc-test/src/main/resources/spring/applicationContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | 16 | 17 | 18 | 20 | 21 | classpath:db.properties 22 | 23 | 24 | 25 | 26 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /zhcc-test/src/main/resources/spring/spring-mybatis.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /zhcc-web/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /zhcc-web/Dockerfile: -------------------------------------------------------------------------------- 1 | #基础镜像 2 | FROM tomcat:7 3 | #作者信息 4 | MAINTAINER hoult jsu_hlc@163.com 5 | #定义变量、后续会使用,具体路径可以先启动容器然后进入进行查看 6 | ENV DIR_WEBAPP /usr/local/tomcat/webapps/ 7 | #删除webapp下所有文件,因为当前应用作为根应用 8 | RUN rm -rf $DIR_WEBAPP/* 9 | #添加本地的war包到远程容器中 10 | ADD ./target/zhcc-web.war $DIR_WEBAPP/ROOT.war 11 | #配置文件夹映射 12 | VOLUME /usr/local/tomcat/webapps 13 | #配置工作目录 14 | WORKDIR /usr/local/tomcat/webapps 15 | #解压war包到ROOT目录 16 | RUN unzip $DIR_WEBAPP/ROOT.war -d $DIR_WEBAPP/ROOT/ 17 | #暴露端口 18 | EXPOSE 8080 19 | #启动tomcat 20 | CMD ["catalina.sh", "run"] -------------------------------------------------------------------------------- /zhcc-web/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.hu.zhcc 8 | zhcc-server 9 | 1.0 10 | 11 | zhcc-web 12 | war 13 | zhcc-web RESTful Service 14 | http://hulichao.top 15 | 16 | 17 | 18 | com.hu.zhcc 19 | zhcc-shiro 20 | 1.0 21 | 22 | 23 | 24 | zhcc-web 25 | 26 | 27 | -------------------------------------------------------------------------------- /zhcc-web/src/main/resources/db.properties: -------------------------------------------------------------------------------- 1 | #jdbc url 2 | jdbc.url=jdbc:mysql://localhost:3306/zhcc?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8 3 | #jdbc \u7528\u6237\u540D 4 | jdbc.username=root 5 | #jdbc \u5BC6\u7801 6 | jdbc.password=123456 7 | 8 | #\u8FDE\u63A5\u6C60\u521D\u59CB\u5316\u5927\u5C0F 9 | jdbc.pool.initialSize=1 10 | #\u6700\u5C0F\u8FDE\u63A5\u6C60\u6570\u91CF 11 | jdbc.pool.minIdle=1 12 | #\u6700\u5927\u8FDE\u63A5\u6C60\u6570\u91CF 13 | jdbc.pool.maxActive=20 14 | 15 | 16 | #redis settings 17 | redis.host=127.0.0.1 18 | redis.port=6379 19 | #\u6CE8\u610F\uFF0C\u5982\u679C\u6CA1\u6709password\uFF0C\u6B64\u5904\u4E0D\u8BBE\u7F6E\u503C\uFF0C\u4F46\u8FD9\u4E00\u9879\u8981\u4FDD\u7559 20 | redis.password= 21 | redis.db.index=1 22 | redis.max_active=500 23 | redis.max_idle=5 24 | redis.max_wait=10000 25 | redis.timeout=10000 -------------------------------------------------------------------------------- /zhcc-web/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg [%file:%line] %n 6 | UTF-8 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /zhcc-web/src/main/resources/mybatis/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /zhcc-web/src/main/resources/spring/applicationContext-jedis.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /zhcc-web/src/main/resources/spring/applicationContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | 16 | 17 | 18 | 20 | 21 | classpath:db.properties 22 | 23 | 24 | 25 | 26 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /zhcc-web/src/main/resources/spring/spring-beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 17 | -------------------------------------------------------------------------------- /zhcc-web/src/main/resources/spring/spring-mvc.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 22 | 23 | 24 | text/html;charset=UTF-8 25 | application/json 26 | 27 | 28 | 29 | 30 | WriteMapNullValue 31 | QuoteFieldNames 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /zhcc-web/src/main/resources/spring/spring-mybatis.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /zhcc-web/src/main/resources/spring/spring-shiro.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | / = anon 46 | /index.html = anon 47 | /static/** = anon 48 | /auth/token = anon 49 | /logout = anon 50 | /swagger-ui.html = user 51 | /permissions/functionPermissions = jwtAuthcFilter 52 | /users/me = jwtAuthcFilter 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /zhcc-web/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | Archetype Created Web Application 8 | 9 | 10 | webAppRootKey 11 | zhcc-web.root 12 | 13 | 14 | 15 | org.springframework.web.util.WebAppRootListener 16 | 17 | 18 | 19 | 20 | 21 | 22 | encodingFilter 23 | org.springframework.web.filter.CharacterEncodingFilter 24 | 25 | encoding 26 | UTF-8 27 | 28 | 29 | forceEncoding 30 | true 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | shiroFilter 40 | org.springframework.web.filter.DelegatingFilterProxy 41 | true 42 | 43 | 44 | targetFilterLifecycle 45 | true 46 | 47 | 48 | 49 | 50 | 51 | CorsFilter 52 | com.hu.zhcc.common.filter.CORSFilter 53 | 54 | 55 | CorsFilter 56 | /* 57 | 58 | 59 | encodingFilter 60 | /* 61 | 62 | 63 | shiroFilter 64 | /* 65 | 66 | 67 | 68 | appServlet 69 | org.springframework.web.servlet.DispatcherServlet 70 | 71 | contextConfigLocation 72 | classpath:/spring/applicationContext.xml 73 | 74 | 1 75 | 76 | 77 | appServlet 78 | / 79 | 80 | 81 | index.html 82 | 83 | 84 | -------------------------------------------------------------------------------- /zhcc-web/src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | zhcc-view
-------------------------------------------------------------------------------- /zhcc-web/src/main/webapp/static/fonts/element-icons.6f0a763.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hulichao/zhcc-server/e0160e018b4722815c1f422bd77c3dc2eea744f6/zhcc-web/src/main/webapp/static/fonts/element-icons.6f0a763.ttf -------------------------------------------------------------------------------- /zhcc-web/src/main/webapp/static/img/personal_avatar.67a7159.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hulichao/zhcc-server/e0160e018b4722815c1f422bd77c3dc2eea744f6/zhcc-web/src/main/webapp/static/img/personal_avatar.67a7159.png -------------------------------------------------------------------------------- /zhcc-web/src/main/webapp/static/js/manifest.bb527afcf3012cd9ef8e.js: -------------------------------------------------------------------------------- 1 | !function(e){var n=window.webpackJsonp;window.webpackJsonp=function(r,c,i){for(var u,a,f,s=0,l=[];s