diff --git a/src/main/resources/modules/SpringBoot/java/action/AjaxController.java b/src/main/resources/modules/SpringBoot/java/action/AjaxController.java index e9372b76..f9c889f5 100644 --- a/src/main/resources/modules/SpringBoot/java/action/AjaxController.java +++ b/src/main/resources/modules/SpringBoot/java/action/AjaxController.java @@ -46,7 +46,7 @@ public class AjaxController { if (token == null) { token = LocalData.getTempToken(); } - if (!token.hasResource(method)) { + if (!token.hasRes(method)) { baseResponse.addError(new Error(ErrorType.BUSINESS_ERROR, "无权调用该接口!")); return baseResponse; } diff --git a/src/main/resources/modules/SpringBoot/java/action/GlobalController.java b/src/main/resources/modules/SpringBoot/java/action/GlobalController.java index ae9beafa..12cb70d6 100644 --- a/src/main/resources/modules/SpringBoot/java/action/GlobalController.java +++ b/src/main/resources/modules/SpringBoot/java/action/GlobalController.java @@ -355,7 +355,7 @@ public class GlobalController implements ErrorController { } // 权限验证 - if (!LocalData.getToken().hasResource(httpServletRequest.getServletPath())) { + if (!LocalData.getToken().hasRes(httpServletRequest.getServletPath())) { response.addError(ErrorType.BUSINESS_ERROR, "[" + httpServletRequest.getServletPath() + "]未授权的资源!"); return AESUtil.encrypt2Base64(MapperUtil.toJson(response).getBytes(), appSecret); } diff --git a/src/main/resources/modules/SpringBoot/java/action/ajax/system/UserAjax.java b/src/main/resources/modules/SpringBoot/java/action/ajax/system/UserAjax.java index b6c30860..2d2b64b8 100644 --- a/src/main/resources/modules/SpringBoot/java/action/ajax/system/UserAjax.java +++ b/src/main/resources/modules/SpringBoot/java/action/ajax/system/UserAjax.java @@ -21,18 +21,22 @@ import ${basePackage}.module.system.ent.User; import ${basePackage}.module.system.mgr.TokensManager; import ${basePackage}.module.system.mgr.UserManager; import ${basePackage}.module.system.req.TokensCreateRequest; +import ${basePackage}.module.system.req.TokensLogoutRequest; import ${basePackage}.module.system.req.UserCreateRequest; import ${basePackage}.module.system.req.UserDeleteRequest; import ${basePackage}.module.system.req.UserFindRequest; import ${basePackage}.module.system.req.UserGetRequest; import ${basePackage}.module.system.req.UserLoginRequest; +import ${basePackage}.module.system.req.UserLogoutRequest; import ${basePackage}.module.system.req.UserUpdateRequest; import ${basePackage}.module.system.rsp.TokensCreateResponse; +import ${basePackage}.module.system.rsp.TokensLogoutResponse; import ${basePackage}.module.system.rsp.UserCreateResponse; import ${basePackage}.module.system.rsp.UserDeleteResponse; import ${basePackage}.module.system.rsp.UserFindResponse; import ${basePackage}.module.system.rsp.UserGetResponse; import ${basePackage}.module.system.rsp.UserLoginResponse; +import ${basePackage}.module.system.rsp.UserLogoutResponse; import ${basePackage}.module.system.rsp.UserUpdateResponse; import javax.servlet.http.Cookie; @@ -174,6 +178,23 @@ public class UserAjax { return response; } + public UserLogoutResponse logout(UserLogoutRequest request) { + UserLogoutResponse response = new UserLogoutResponse(); + + TokensLogoutRequest tokensLogoutRequest = new TokensLogoutRequest(); + tokensLogoutRequest.setToken(LocalData.getToken().getToken()); + TokensLogoutResponse tokensLogoutResponse = tokensManager.logout(tokensLogoutRequest, LocalData.getSysToken()); + if (tokensLogoutResponse.hasError()) { + response.addErrors(tokensLogoutResponse.getErrors()); + return response; + } + + HttpServletResponse HttpServletResponse = LocalData.getResponse(); + Cookie token = CookieUtil.newCookie("token", ""); + HttpServletResponse.addCookie(token); + return response; + } + public Object template() { return ResponseUtil.apply(new WSheet<>(User.class)); } diff --git a/src/main/resources/modules/SpringBoot/java/config/CacheConfig.java b/src/main/resources/modules/SpringBoot/java/config/CacheConfig.java index 33c06eee..909dba9a 100644 --- a/src/main/resources/modules/SpringBoot/java/config/CacheConfig.java +++ b/src/main/resources/modules/SpringBoot/java/config/CacheConfig.java @@ -15,7 +15,7 @@ public class CacheConfig { public static final String TOKEN_CACHE = "tokenCache"; - @Bean(name = TOKEN_CACHE) + @Bean public EhCacheCacheManager getCacheManager() { net.sf.ehcache.config.Configuration configuration = new net.sf.ehcache.config.Configuration(); configuration.setMaxBytesLocalHeap("100M"); diff --git a/src/main/resources/modules/SpringBoot/java/config/SecurityConfig.java b/src/main/resources/modules/SpringBoot/java/config/SecurityConfig.java index 23f0acb5..d5ce3ee8 100644 --- a/src/main/resources/modules/SpringBoot/java/config/SecurityConfig.java +++ b/src/main/resources/modules/SpringBoot/java/config/SecurityConfig.java @@ -121,7 +121,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { // 授权 Token token_ = LocalData.getToken(); - if (token_ != null && token_.hasResource(request.getServletPath())) { + if (token_ != null && token_.hasRes(request.getServletPath())) { return true; } diff --git a/src/main/resources/modules/SpringBoot/java/frame/auth/LocalData.java b/src/main/resources/modules/SpringBoot/java/frame/auth/LocalData.java index 37b25dfd..32d727ae 100644 --- a/src/main/resources/modules/SpringBoot/java/frame/auth/LocalData.java +++ b/src/main/resources/modules/SpringBoot/java/frame/auth/LocalData.java @@ -28,7 +28,7 @@ public class LocalData { system.setId(0); system.setUserId(0); system.setUserName("system"); - system.putResource(".*"); + system.putRes(".*"); } public static Token getSysToken() { diff --git a/src/main/resources/modules/SpringBoot/java/frame/base/Token.java b/src/main/resources/modules/SpringBoot/java/frame/base/Token.java index 306058f0..1df3db65 100644 --- a/src/main/resources/modules/SpringBoot/java/frame/base/Token.java +++ b/src/main/resources/modules/SpringBoot/java/frame/base/Token.java @@ -1,7 +1,10 @@ package ${basePackage}.frame.base; +import ${basePackage}.module.system.ent.Res; + import java.io.Serializable; import java.util.HashSet; +import java.util.List; import java.util.Set; /** @@ -13,14 +16,52 @@ import java.util.Set; */ public class Token implements Serializable { private static final Long serialVersionUID = 1L; - /** ID */ + /** + * ID + */ private long id; - /** 用户ID */ + /** + * TOKEN + */ + private String token; + /** + * 用户ID + */ private long userId; - /** 用户名称 */ + /** + * 用户名称 + */ private String userName; - private Set resourceSet = new HashSet<>(); + private Set resSet = new HashSet<>(); + + public boolean hasRes(String resource) { + for (String s : resSet) { + if (resource.matches(s)) { + return true; + } + } + return false; + } + + public void putRes(String resource) { + resSet.add(resource); + } + + public void putRes(Set resourceSet) { + this.resSet.addAll(resourceSet); + } + + public void putRes(List resList) { + if (resList == null) return; + for (Res res : resList) { + this.resSet.add(res.getResValue()); + } + } + + public Set getResSet() { + return resSet; + } public long getId() { @@ -47,28 +88,11 @@ public class Token implements Serializable { this.userName = userName; } - public boolean hasResource(String resource){ - for (String s : resourceSet) { - if (resource.matches(s)){ - return true; - } - } - return false; - } - - public void putResource(String resource){ - resourceSet.add(resource); - } - - public Set getResourceSet() { - return resourceSet; - } - - public void addResourceSet(Set resourceSet){ - this.resourceSet.addAll(resourceSet); + public String getToken() { + return token; } - public void addResourceSet(Token token){ - addResourceSet(token.getResourceSet()); + public void setToken(String token) { + this.token = token; } } diff --git a/src/main/resources/modules/SpringBoot/java/module/system/mgr/RoleResManagerImpl.java b/src/main/resources/modules/SpringBoot/java/module/system/mgr/RoleResManagerImpl.java index e95b378a..00feef0a 100644 --- a/src/main/resources/modules/SpringBoot/java/module/system/mgr/RoleResManagerImpl.java +++ b/src/main/resources/modules/SpringBoot/java/module/system/mgr/RoleResManagerImpl.java @@ -205,7 +205,9 @@ public class RoleResManagerImpl implements RoleResManager { if (request.getChecked() && roleResFindResponse.getResult().size() == 0) { RoleResCreateRequest roleResCreateRequest = new RoleResCreateRequest(); roleResCreateRequest.setRoleId(request.getRoleId()); + roleResCreateRequest.setRoleCode(request.getRoleCode()); roleResCreateRequest.setResId(request.getResId()); + roleResCreateRequest.setResCode(request.getResCode()); RoleResCreateResponse roleResCreateResponse = this.create(roleResCreateRequest, token); if (roleResCreateResponse.hasError()) { response.addErrors(roleResCreateResponse.getErrors()); diff --git a/src/main/resources/modules/SpringBoot/java/module/system/mgr/TokensManager.java b/src/main/resources/modules/SpringBoot/java/module/system/mgr/TokensManager.java index 180970bf..08d231a0 100644 --- a/src/main/resources/modules/SpringBoot/java/module/system/mgr/TokensManager.java +++ b/src/main/resources/modules/SpringBoot/java/module/system/mgr/TokensManager.java @@ -86,5 +86,5 @@ public interface TokensManager { * @param token 令牌 * @return */ - ${basePackage}.module.system.rsp.TokensLogoutResponse logout(${basePackage}.module.system.req.TokensLogoutRequest request, Token token); + TokensLogoutResponse logout(TokensLogoutRequest request, Token token); } diff --git a/src/main/resources/modules/SpringBoot/java/module/system/mgr/TokensManagerImpl.java b/src/main/resources/modules/SpringBoot/java/module/system/mgr/TokensManagerImpl.java index 0091219f..bd0c8a95 100644 --- a/src/main/resources/modules/SpringBoot/java/module/system/mgr/TokensManagerImpl.java +++ b/src/main/resources/modules/SpringBoot/java/module/system/mgr/TokensManagerImpl.java @@ -18,6 +18,7 @@ import ${basePackage}.frame.utils.Message; import ${basePackage}.frame.utils.ValidationUtil; import ${basePackage}.module.system.ent.Tokens; import ${basePackage}.module.system.mpr.TokensMapper; +import ${basePackage}.module.system.req.ResFindRequest; import ${basePackage}.module.system.req.TokensBuildRequest; import ${basePackage}.module.system.req.TokensCreateRequest; import ${basePackage}.module.system.req.TokensDeleteRequest; @@ -25,6 +26,7 @@ import ${basePackage}.module.system.req.TokensFindRequest; import ${basePackage}.module.system.req.TokensGetRequest; import ${basePackage}.module.system.req.TokensLogoutRequest; import ${basePackage}.module.system.req.TokensUpdateRequest; +import ${basePackage}.module.system.rsp.ResFindResponse; import ${basePackage}.module.system.rsp.TokensBuildResponse; import ${basePackage}.module.system.rsp.TokensCreateResponse; import ${basePackage}.module.system.rsp.TokensDeleteResponse; @@ -42,12 +44,16 @@ import ${basePackage}.module.system.rsp.TokensUpdateResponse; */ @Transactional @Service -public class TokensManagerImpl implements ${basePackage}.module.system.mgr.TokensManager { +public class TokensManagerImpl implements xyz.wbsite.module.system.mgr.TokensManager { - @Value("${r'${web.url.auth.admin}'}") + @Value("${web.url.auth.admin}") private String admin; @Autowired private TokensMapper tokensMapper; + @Autowired + private UserRoleManager userRoleManager; + @Autowired + private ResManager resManager; /** * 插入 @@ -221,15 +227,38 @@ public class TokensManagerImpl implements ${basePackage}.module.system.mgr.Token } else { Tokens tokens = tokensFindResponse.getResult().get(0); Token newToken = new Token(); - if (tokens.getUserName().equals(admin)) { + if (tokens.getUserName().equals(admin)) {//超级管理员 newToken.setId(0); + newToken.setToken(tokensFindResponse.getResult().get(0).getToken()); newToken.setUserId(0); newToken.setUserName(admin); - newToken.putResource(".*"); - } else { + newToken.putRes(".*"); + + // 获取用户的资源 + ResFindRequest resFindRequest = new ResFindRequest(); + resFindRequest.setPageSize(0); + ResFindResponse resFindResponse = resManager.find(resFindRequest, token); + if (resFindResponse.hasError()) { + response.addErrors(resFindResponse.getErrors()); + return response; + } + newToken.putRes(resFindResponse.getResult()); + } else { // 管理员或用户 newToken.setId(tokens.getId()); + newToken.setToken(tokensFindResponse.getResult().get(0).getToken()); newToken.setUserId(tokens.getUserId()); newToken.setUserName(tokens.getUserName()); + + // 获取用户的资源 + ResFindRequest resFindRequest = new ResFindRequest(); + resFindRequest.setPageSize(0); + resFindRequest.setUserId(token.getUserId()); + ResFindResponse resFindResponse = resManager.find(resFindRequest, token); + if (resFindResponse.hasError()) { + response.addErrors(resFindResponse.getErrors()); + return response; + } + newToken.putRes(resFindResponse.getResult()); } response.setToken(newToken); diff --git a/src/main/resources/modules/SpringBoot/java/module/system/mpr/MySQL_ResMapper.xml b/src/main/resources/modules/SpringBoot/java/module/system/mpr/MySQL_ResMapper.xml index af1a1d87..59176858 100644 --- a/src/main/resources/modules/SpringBoot/java/module/system/mpr/MySQL_ResMapper.xml +++ b/src/main/resources/modules/SpringBoot/java/module/system/mpr/MySQL_ResMapper.xml @@ -55,6 +55,24 @@ AND `VALID` = ${r'#'}{request.valid} + + AND `ID` IN ( + SELECT + t2.RES_ID + FROM + `sys_user_role` t1, + `sys_role_res` t2 + WHERE t1.ROLE_ID = t2.ROLE_ID + AND t1.IS_DELETED = '0' + AND t2.IS_DELETED = '0' + AND t1.USER_ID = ${r'#'}{request.userId} + ) + + + AND `ID` IN ( + SELECT RES_ID FROM `sys_role_res` WHERE ROLE_CODE = ${r'#'}{request.roleCode} + ) +