diff --git a/src/main/resources/modules/SpringBoot/java/Application.ftl b/src/main/resources/modules/SpringBoot/java/Application.ftl index dbaf7f3e..8143094f 100644 --- a/src/main/resources/modules/SpringBoot/java/Application.ftl +++ b/src/main/resources/modules/SpringBoot/java/Application.ftl @@ -8,7 +8,7 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; -import org.springframework.cache.annotation.EnableCaching; +import ${basePackage}.frame.auth.LocalData; @SpringBootApplication <#if multiDB> @@ -24,6 +24,6 @@ public class Application extends SpringBootServletInitializer { } public static void main(String[] args) { - SpringApplication.run(Application.class, args); + LocalData.setApplicationContext(SpringApplication.run(Application.class, args)); } } \ No newline at end of file diff --git a/src/main/resources/modules/SpringBoot/java/action/GlobalController.java b/src/main/resources/modules/SpringBoot/java/action/GlobalController.java index 829972e0..6944deb4 100644 --- a/src/main/resources/modules/SpringBoot/java/action/GlobalController.java +++ b/src/main/resources/modules/SpringBoot/java/action/GlobalController.java @@ -113,10 +113,12 @@ public class GlobalController implements ErrorController { case 404: return "404"; case 403: - try { - response.sendRedirect("/login.htm"); - } catch (IOException e) { - e.printStackTrace(); + if (LocalData.getToken() == null) { + try { + response.sendRedirect("/login.htm"); + } catch (IOException e) { + e.printStackTrace(); + } } return "403"; case 500: diff --git a/src/main/resources/modules/SpringBoot/java/config/SecurityConfig.java b/src/main/resources/modules/SpringBoot/java/config/SecurityConfig.java index 8fe8eba5..d6937177 100644 --- a/src/main/resources/modules/SpringBoot/java/config/SecurityConfig.java +++ b/src/main/resources/modules/SpringBoot/java/config/SecurityConfig.java @@ -32,11 +32,11 @@ import java.util.regex.Pattern; @EnableGlobalMethodSecurity(securedEnabled = true) public class SecurityConfig extends WebSecurityConfigurerAdapter { - @Value("${r"${web.url.auth.included}"}") + @Value("${r'${web.url.auth.included}'}") private String[] included; - @Value("${r"${web.url.auth.excluded}"}") + @Value("${r'${web.url.auth.excluded}'}") private String[] excluded; - @Value("${r"${spring.mvc.static-path-pattern}"}") + @Value("${r'${spring.mvc.static-path-pattern}'}") private String[] staticPath; @Value("${r'${web.login.page}'}") private String loginPage; @@ -51,8 +51,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and() .addFilterBefore(new TokenFilter(), FilterSecurityInterceptor.class)// 过滤器用于处理Token - .formLogin().loginPage(loginPage) - .and().authorizeRequests() + .authorizeRequests() .antMatchers(excluded).permitAll()// 放行排除的URL .antMatchers(included).access("@Authorization.hasPermission(request,authentication)")// 需要权限的URL .and().cors() @@ -83,23 +82,19 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { token = CookieUtil.getCookieValue(request.getCookies(), "token"); } - if (token == null) { - LocalData.setToken(LocalData.getTempToken()); - } else { - // 组装Token ~ 这边根据实际的业务组装Token + // 组装Token ~ 这边根据实际的业务组装Token + if (token != null){ TokensManager tokensManager = LocalData.getBean(TokensManager.class); TokensBuildRequest tokensBuildRequest = new TokensBuildRequest(); tokensBuildRequest.setToken(token); TokensBuildResponse tokensBuildResponse = tokensManager.build(tokensBuildRequest, LocalData.getSysToken()); - if (tokensBuildResponse.hasError()) { - LocalData.setToken(LocalData.getTempToken()); - } else { + if (!tokensBuildResponse.hasError()) { LocalData.setToken(tokensBuildResponse.getToken()); } } // Action - String servletPath = request.getServletPath(); + String servletPath = request.getServletPath().toLowerCase(); Pattern compile = Pattern.compile("^/(.+)\\.htm"); Matcher matcher = compile.matcher(servletPath); if (matcher.find()) { @@ -117,7 +112,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { // 授权 Token token_ = LocalData.getToken(); - if (token_.hasResource(request.getServletPath())) { + if (token_ != null && token_.hasResource(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 2b545120..97b00caf 100644 --- a/src/main/resources/modules/SpringBoot/java/frame/auth/LocalData.java +++ b/src/main/resources/modules/SpringBoot/java/frame/auth/LocalData.java @@ -4,7 +4,6 @@ import org.springframework.context.ApplicationContext; import org.springframework.core.env.Environment; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; -import org.springframework.web.context.support.WebApplicationContextUtils; import ${basePackage}.frame.base.Token; import javax.servlet.http.HttpServletRequest; @@ -19,16 +18,12 @@ import javax.servlet.http.HttpServletResponse; */ public class LocalData { - private static Token temp = null; + private static ApplicationContext applicationContext = null; private static Token system = null; static { - // 组装临时Token和系统Token - temp = new Token(); - temp.setId(-1); - temp.setUserId(-1); - temp.setUserName("临时"); + // 组装系统Token system = new Token(); system.setId(0); system.setUserId(0); @@ -36,10 +31,6 @@ public class LocalData { system.putResource(".*"); } - public static Token getTempToken() { - return temp; - } - public static Token getSysToken() { return system; } @@ -79,7 +70,11 @@ public class LocalData { } public static ApplicationContext getApplicationContext() { - return WebApplicationContextUtils.getWebApplicationContext(getRequest().getServletContext()); + return ${basePackage}.frame.auth.LocalData.applicationContext; + } + + public static void setApplicationContext(ApplicationContext applicationContext) { + ${basePackage}.frame.auth.LocalData.applicationContext = applicationContext; } public static T getBean(Class t) { @@ -88,6 +83,6 @@ public class LocalData { public static String getProp(String key) { Environment env = getBean(Environment.class); - return env.getProperty(key,""); + return env.getProperty(key, ""); } } diff --git a/src/main/resources/modules/SpringBoot/java/frame/schedule/RunCronTask.java b/src/main/resources/modules/SpringBoot/java/frame/schedule/RunCronTask.java index edbb08fa..014507c1 100644 --- a/src/main/resources/modules/SpringBoot/java/frame/schedule/RunCronTask.java +++ b/src/main/resources/modules/SpringBoot/java/frame/schedule/RunCronTask.java @@ -1,4 +1,4 @@ -package xyz.wbsite.frame.schedule; +package ${basePackage}.frame.schedule; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.scheduling.support.CronTrigger; diff --git a/src/main/resources/modules/SpringBoot/java/frame/schedule/RunDelayRepeatTask.java b/src/main/resources/modules/SpringBoot/java/frame/schedule/RunDelayRepeatTask.java index f52c639a..e8afc3f8 100644 --- a/src/main/resources/modules/SpringBoot/java/frame/schedule/RunDelayRepeatTask.java +++ b/src/main/resources/modules/SpringBoot/java/frame/schedule/RunDelayRepeatTask.java @@ -1,4 +1,4 @@ -package xyz.wbsite.frame.schedule; +package ${basePackage}.frame.schedule; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.util.Assert; diff --git a/src/main/resources/modules/SpringBoot/java/frame/schedule/RunFixRepeatTask.java b/src/main/resources/modules/SpringBoot/java/frame/schedule/RunFixRepeatTask.java index 1abf7e69..84891ba5 100644 --- a/src/main/resources/modules/SpringBoot/java/frame/schedule/RunFixRepeatTask.java +++ b/src/main/resources/modules/SpringBoot/java/frame/schedule/RunFixRepeatTask.java @@ -1,4 +1,4 @@ -package xyz.wbsite.frame.schedule; +package ${basePackage}.frame.schedule; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.util.Assert; diff --git a/src/main/resources/modules/SpringBoot/java/frame/schedule/RunSqlTask.java b/src/main/resources/modules/SpringBoot/java/frame/schedule/RunSqlTask.java index 1582ab6f..3074e6da 100644 --- a/src/main/resources/modules/SpringBoot/java/frame/schedule/RunSqlTask.java +++ b/src/main/resources/modules/SpringBoot/java/frame/schedule/RunSqlTask.java @@ -1,4 +1,4 @@ -package xyz.wbsite.frame.schedule; +package ${basePackage}.frame.schedule; public abstract class RunSqlTask extends RunFixRepeatTask { diff --git a/src/main/resources/modules/SpringBoot/java/frame/schedule/RunTask.java b/src/main/resources/modules/SpringBoot/java/frame/schedule/RunTask.java index 897dea69..dadce388 100644 --- a/src/main/resources/modules/SpringBoot/java/frame/schedule/RunTask.java +++ b/src/main/resources/modules/SpringBoot/java/frame/schedule/RunTask.java @@ -1,4 +1,4 @@ -package xyz.wbsite.frame.schedule; +package ${basePackage}.frame.schedule; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; 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 eebacd0e..180970bf 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 @@ -6,12 +6,14 @@ import ${basePackage}.module.system.req.TokensCreateRequest; import ${basePackage}.module.system.req.TokensDeleteRequest; 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.TokensBuildResponse; import ${basePackage}.module.system.rsp.TokensCreateResponse; import ${basePackage}.module.system.rsp.TokensDeleteResponse; import ${basePackage}.module.system.rsp.TokensFindResponse; import ${basePackage}.module.system.rsp.TokensGetResponse; +import ${basePackage}.module.system.rsp.TokensLogoutResponse; import ${basePackage}.module.system.rsp.TokensUpdateResponse; /** @@ -76,4 +78,13 @@ public interface TokensManager { * @return */ TokensBuildResponse build(TokensBuildRequest request, Token token); + + /** + * 注销Token + * + * @param request 请求对象 + * @param token 令牌 + * @return + */ + ${basePackage}.module.system.rsp.TokensLogoutResponse logout(${basePackage}.module.system.req.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 9fbbb742..d0849423 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 @@ -5,6 +5,7 @@ import com.github.pagehelper.PageInfo; import com.github.pagehelper.util.StringUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -22,12 +23,14 @@ import ${basePackage}.module.system.req.TokensCreateRequest; import ${basePackage}.module.system.req.TokensDeleteRequest; 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.TokensBuildResponse; import ${basePackage}.module.system.rsp.TokensCreateResponse; import ${basePackage}.module.system.rsp.TokensDeleteResponse; import ${basePackage}.module.system.rsp.TokensFindResponse; import ${basePackage}.module.system.rsp.TokensGetResponse; +import ${basePackage}.module.system.rsp.TokensLogoutResponse; import ${basePackage}.module.system.rsp.TokensUpdateResponse; /** @@ -39,7 +42,7 @@ import ${basePackage}.module.system.rsp.TokensUpdateResponse; */ @Transactional @Service -public class TokensManagerImpl implements TokensManager { +public class TokensManagerImpl implements ${basePackage}.module.system.mgr.TokensManager { @Value("${r'${web.url.auth.admin}'}") private String admin; @@ -82,6 +85,7 @@ public class TokensManagerImpl implements TokensManager { * @param token 令牌 * @return 响应 */ + @CacheEvict(value = CacheConfig.TOKEN_CACHE, key = "#request.token", condition = "!#result.hasError()") public TokensDeleteResponse delete(TokensDeleteRequest request, Token token) { TokensDeleteResponse response = new TokensDeleteResponse(); @@ -107,6 +111,7 @@ public class TokensManagerImpl implements TokensManager { * @param token 令牌 * @return 响应 */ + @CacheEvict(value = CacheConfig.TOKEN_CACHE, key = "#request.token", condition = "!#result.hasError()") public TokensUpdateResponse update(TokensUpdateRequest request, Token token) { TokensUpdateResponse response = new TokensUpdateResponse(); @@ -188,7 +193,7 @@ public class TokensManagerImpl implements TokensManager { * @return */ @Override - @Cacheable(value = CacheConfig.TOKEN_CACHE, key = "#request.token") + @Cacheable(value = CacheConfig.TOKEN_CACHE, key = "#request.token", unless = "#result.hasError()") public TokensBuildResponse build(TokensBuildRequest request, Token token) { TokensBuildResponse response = new TokensBuildResponse(); @@ -230,4 +235,35 @@ public class TokensManagerImpl implements TokensManager { return response; } + + @Override + @CacheEvict(value = CacheConfig.TOKEN_CACHE, key = "#request.token", condition = "!#result.hasError()") + public TokensLogoutResponse logout(TokensLogoutRequest request, Token token) { + TokensLogoutResponse response = new TokensLogoutResponse(); + + ValidationUtil.validate(request, response); + if (response.hasError()) { + return response; + } + + TokensFindRequest tokensFindRequest = MapperUtil.map(request, TokensFindRequest.class); + tokensFindRequest.setValid(true); + TokensFindResponse tokensFindResponse = this.find(tokensFindRequest, token); + if (tokensFindResponse.hasError()) { + response.addErrors(tokensFindResponse.getErrors()); + return response; + } + + for (Tokens tokens : tokensFindResponse.getResult()) { + TokensUpdateRequest tokensUpdateRequest = MapperUtil.map(tokens, TokensUpdateRequest.class); + tokensUpdateRequest.setValid(false); + long result = tokensMapper.update(tokensUpdateRequest, token); + if (1L != result) { + response.addError(ErrorType.BUSINESS_ERROR, Message.UPDATE_FAILURE); + return response; + } + } + + return response; + } } diff --git a/src/main/resources/modules/SpringBoot/java/module/system/req/TokensDeleteRequest.java b/src/main/resources/modules/SpringBoot/java/module/system/req/TokensDeleteRequest.java index 10d2386e..8bc7a846 100644 --- a/src/main/resources/modules/SpringBoot/java/module/system/req/TokensDeleteRequest.java +++ b/src/main/resources/modules/SpringBoot/java/module/system/req/TokensDeleteRequest.java @@ -1,7 +1,8 @@ package ${basePackage}.module.system.req; import ${basePackage}.frame.base.BaseUpdateRequest; -import javax.validation.constraints.NotNull; + +import javax.validation.constraints.NotBlank; /** * TokensDeleteRequest - 通行证删除 @@ -13,16 +14,16 @@ import javax.validation.constraints.NotNull; public class TokensDeleteRequest extends BaseUpdateRequest { /** - * 主键 + * 登录令牌 */ - @NotNull(message = "[id]主键不能为空") - private long id; + @NotBlank(message = "登录令牌不能为空") + private String token; - public long getId() { - return this.id; + public String getToken() { + return token; } - public void setId(long id) { - this.id = id; + public void setToken(String token) { + this.token = token; } } diff --git a/src/main/resources/modules/SpringBoot/java/module/system/req/TokensLogoutRequest.java b/src/main/resources/modules/SpringBoot/java/module/system/req/TokensLogoutRequest.java new file mode 100644 index 00000000..9ec0b8e8 --- /dev/null +++ b/src/main/resources/modules/SpringBoot/java/module/system/req/TokensLogoutRequest.java @@ -0,0 +1,29 @@ +package ${basePackage}.module.system.req; + +import ${basePackage}.frame.base.BaseFindRequest; + +import javax.validation.constraints.NotBlank; + +/** + * TokensLogoutRequest - 通行证注销 + * + * @author wangbing + * @version 0.0.1 + * @since 2017-01-01 + */ +public class TokensLogoutRequest extends BaseFindRequest { + + /** + * 登录令牌 + */ + @NotBlank(message = "登录令牌不能为空") + private String token; + + public String getToken() { + return token; + } + + public void setToken(String token) { + this.token = token; + } +} diff --git a/src/main/resources/modules/SpringBoot/java/module/system/rsp/TokensLogoutResponse.java b/src/main/resources/modules/SpringBoot/java/module/system/rsp/TokensLogoutResponse.java new file mode 100644 index 00000000..5abb4215 --- /dev/null +++ b/src/main/resources/modules/SpringBoot/java/module/system/rsp/TokensLogoutResponse.java @@ -0,0 +1,27 @@ +package ${basePackage}.module.system.rsp; + +import ${basePackage}.frame.base.BaseResponse; +import ${basePackage}.frame.base.Token; + +/** + * TokensGetResponse - 通行证 + * + * @author author + * @version 0.0.1 + * @since 2017-01-01 + */ +public class TokensLogoutResponse extends BaseResponse { + + /** + * 通行证 + */ + private Token token; + + public Token getToken() { + return token; + } + + public void setToken(Token token) { + this.token = token; + } +} diff --git a/src/main/resources/modules/SpringBoot/resources/templates/control/nav.ftl b/src/main/resources/modules/SpringBoot/resources/templates/control/nav.ftl index 9c352c70..def9b65d 100644 --- a/src/main/resources/modules/SpringBoot/resources/templates/control/nav.ftl +++ b/src/main/resources/modules/SpringBoot/resources/templates/control/nav.ftl @@ -63,7 +63,7 @@ } else { rsp.errors.push(handleError(error.response.status)); } - return Promise.reject(rsp); + return Promise.resolve(rsp); }) } function upRequest(data) { @@ -110,7 +110,7 @@ } else { rsp.errors.push(handleError(error.response.status)); } - return Promise.reject(rsp); + return Promise.resolve(rsp); }) }; function downRequest(data) { diff --git a/src/main/resources/modules/SpringBoot/test/system/TokensTest.java b/src/main/resources/modules/SpringBoot/test/system/TokensTest.java index a717a572..a4fee066 100644 --- a/src/main/resources/modules/SpringBoot/test/system/TokensTest.java +++ b/src/main/resources/modules/SpringBoot/test/system/TokensTest.java @@ -7,9 +7,17 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.transaction.annotation.Transactional; import ${basePackage}.frame.base.Token; +import ${basePackage}.frame.utils.IDgenerator; import ${basePackage}.module.system.mgr.TokensManager; -import ${basePackage}.module.system.req.*; -import ${basePackage}.module.system.rsp.*; +import ${basePackage}.module.system.req.TokensCreateRequest; +import ${basePackage}.module.system.req.TokensFindRequest; +import ${basePackage}.module.system.req.TokensGetRequest; +import ${basePackage}.module.system.req.TokensUpdateRequest; +import ${basePackage}.module.system.rsp.TokensCreateResponse; +import ${basePackage}.module.system.rsp.TokensDeleteResponse; +import ${basePackage}.module.system.rsp.TokensFindResponse; +import ${basePackage}.module.system.rsp.TokensGetResponse; +import ${basePackage}.module.system.rsp.TokensUpdateResponse; import java.util.Date; @@ -37,6 +45,7 @@ public class TokensTest { @Test public void testCreate() { TokensCreateRequest request = new TokensCreateRequest(); + request.setToken(IDgenerator.nextUUID()); request.setUserId(1L); request.setUserName("用户名称"); request.setLoginTime(new Date()); @@ -49,7 +58,7 @@ public class TokensTest { request.setTerminalIp("终端地址"); request.setTerminalInfo("终端信息"); - TokensCreateResponse response = tokensManager.create(request,token); + TokensCreateResponse response = tokensManager.create(request, token); assertTrue(!response.hasError()); } @@ -59,6 +68,7 @@ public class TokensTest { //创建数据 TokensCreateRequest createRequest = new TokensCreateRequest(); + createRequest.setToken(IDgenerator.nextUUID()); createRequest.setUserId(1L); createRequest.setUserName("用户名称"); createRequest.setLoginTime(new Date()); @@ -71,14 +81,14 @@ public class TokensTest { createRequest.setTerminalIp("终端地址"); createRequest.setTerminalInfo("终端信息"); - TokensCreateResponse createResponse = tokensManager.create(createRequest,token); + TokensCreateResponse createResponse = tokensManager.create(createRequest, token); assertTrue(!createResponse.hasError() && createResponse.getId() > 0); //删除数据 - TokensDeleteRequest request = new TokensDeleteRequest(); - request.setId(createResponse.getId()); + ${basePackage}.module.system.req.TokensDeleteRequest request = new ${basePackage}.module.system.req.TokensDeleteRequest(); + request.setToken(createRequest.getToken()); - TokensDeleteResponse response = tokensManager.delete(request,token); + TokensDeleteResponse response = tokensManager.delete(request, token); assertTrue(!response.hasError() && response.getResult() == 1L); } @@ -87,6 +97,7 @@ public class TokensTest { public void testUpdate() { //创建数据 TokensCreateRequest createRequest = new TokensCreateRequest(); + createRequest.setToken(IDgenerator.nextUUID()); createRequest.setUserId(1L); createRequest.setUserName("用户名称"); createRequest.setLoginTime(new Date()); @@ -118,7 +129,7 @@ public class TokensTest { request.setTerminalIp("终端地址"); request.setTerminalInfo("终端信息"); - TokensUpdateResponse response = tokensManager.update(request,token); + TokensUpdateResponse response = tokensManager.update(request, token); assertTrue(!response.hasError() && response.getResult() == 1L); } @@ -138,7 +149,7 @@ public class TokensTest { request.setTerminalIp("终端地址"); request.setTerminalInfo("终端信息"); - TokensFindResponse response = tokensManager.find(request,token); + TokensFindResponse response = tokensManager.find(request, token); assertTrue(!response.hasError()); } @@ -147,6 +158,7 @@ public class TokensTest { public void testGet() { //创建数据 TokensCreateRequest createRequest = new TokensCreateRequest(); + createRequest.setToken(IDgenerator.nextUUID()); createRequest.setUserId(1L); createRequest.setUserName("用户名称"); createRequest.setLoginTime(new Date()); @@ -167,7 +179,7 @@ public class TokensTest { TokensGetRequest request = new TokensGetRequest(); request.setId(createResponse.getId()); - TokensGetResponse response = tokensManager.get(request,token); + TokensGetResponse response = tokensManager.get(request, token); assertTrue(!response.hasError() && response.getTokens() != null); }