Former-commit-id: 50fd812afcf010aef5b1c2d12985e481b8580ab4
master
wangbing 5 years ago
parent 2db8190b5b
commit a52b0e09d0

@ -8,7 +8,7 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
</#if>
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));
}
}

@ -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:

@ -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;
}

@ -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> T getBean(Class<T> 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, "");
}
}

@ -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;

@ -1,4 +1,4 @@
package xyz.wbsite.frame.schedule;
package ${basePackage}.frame.schedule;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.util.Assert;

@ -1,4 +1,4 @@
package xyz.wbsite.frame.schedule;
package ${basePackage}.frame.schedule;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.util.Assert;

@ -1,4 +1,4 @@
package xyz.wbsite.frame.schedule;
package ${basePackage}.frame.schedule;
public abstract class RunSqlTask extends RunFixRepeatTask {

@ -1,4 +1,4 @@
package xyz.wbsite.frame.schedule;
package ${basePackage}.frame.schedule;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

@ -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);
}

@ -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;
}
}

@ -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;
}
}

@ -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;
}
}

@ -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;
}
}

@ -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) {

@ -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);
}

Loading…
Cancel
Save

Powered by TurnKey Linux.