From 7a70489620ed7ddfe18e0115316c3d8841744eae Mon Sep 17 00:00:00 2001 From: zhou Date: Wed, 31 Dec 2014 22:36:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 9 +++- .../backend/controller/BackendController.java | 11 ++-- .../backend/controller/CommentController.java | 2 +- .../backend/controller/LinkController.java | 10 ++++ .../backend/controller/PostController.java | 31 +++++------ .../backend/controller/UserController.java | 26 ++++++--- src/main/java/com/zblog/biz/PostManager.java | 12 ----- .../com/zblog/biz/aop/StaticTemplate.java | 32 ++++++----- .../com/zblog/common/dal/entity/Comment.java | 2 +- .../common/dal/mybatis/CategoryMapper.xml | 4 ++ .../zblog/common/dal/mybatis/PostMapper.xml | 2 +- .../zblog/common/dal/mybatis/UserMapper.xml | 6 +-- .../com/zblog/common/lucene/DocConverter.java | 14 +++-- .../com/zblog/common/lucene/QueryBuilder.java | 11 ++-- .../common/util/constants/Constants.java | 4 +- .../com/zblog/common/util/web/WebContext.java | 45 +++++----------- .../zblog/service/AuthenticationService.java | 4 +- .../java/com/zblog/service/UserService.java | 2 +- .../com/zblog/template/ftl/common/recent.html | 2 +- .../zblog/web/controller/IndexController.java | 6 +-- .../com/zblog/web/filter/LoginFilter.java | 54 ++++--------------- src/main/resources/config/spring-biz.xml | 12 +++-- 22 files changed, 141 insertions(+), 160 deletions(-) diff --git a/pom.xml b/pom.xml index 3e97420f..9518a05a 100644 --- a/pom.xml +++ b/pom.xml @@ -80,6 +80,13 @@ 3.2.5.RELEASE + + + org.jsoup + jsoup + 1.8.1 + + org.apache.lucene @@ -110,7 +117,7 @@ IKAnalyzer IKAnalyzer - 2012_u6 + 2012FF_u1 diff --git a/src/main/java/com/zblog/backend/controller/BackendController.java b/src/main/java/com/zblog/backend/controller/BackendController.java index 4e4fe0b1..1ce89a80 100644 --- a/src/main/java/com/zblog/backend/controller/BackendController.java +++ b/src/main/java/com/zblog/backend/controller/BackendController.java @@ -34,11 +34,16 @@ public String login(){ } @RequestMapping(value = "/login", method = RequestMethod.POST) - public String dashboard(String username, String pass, String code, HttpServletRequest request, - HttpServletResponse response){ + public String dashboard(String username, String pass, HttpServletRequest request, HttpServletResponse response){ User user = userService.login(username, pass); + if(user == null){ + request.setAttribute("msg", "用户名密码错误"); + return "backend/login"; + } + CookieUtil cookieUtil = new CookieUtil(request, response); - cookieUtil.setCookie(Constants.COOKIE_CONTEXT_ID, user.getId() + ":" + user.getId()); + cookieUtil.setCookie(Constants.COOKIE_CONTEXT_ID, user.getId()); + cookieUtil.setCookie(Constants.COOKIE_USER_NAME, username, 7 * 24 * 3600); return "redirect:/backend/index"; } diff --git a/src/main/java/com/zblog/backend/controller/CommentController.java b/src/main/java/com/zblog/backend/controller/CommentController.java index 2f5e02ec..8755ef99 100644 --- a/src/main/java/com/zblog/backend/controller/CommentController.java +++ b/src/main/java/com/zblog/backend/controller/CommentController.java @@ -7,7 +7,7 @@ import com.zblog.common.plugin.MapContainer; -@Controller +@Controller("bCommentController") @RequestMapping("/backend/comments") public class CommentController{ diff --git a/src/main/java/com/zblog/backend/controller/LinkController.java b/src/main/java/com/zblog/backend/controller/LinkController.java index 1bf92520..f30e5b90 100644 --- a/src/main/java/com/zblog/backend/controller/LinkController.java +++ b/src/main/java/com/zblog/backend/controller/LinkController.java @@ -5,11 +5,14 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; import com.zblog.common.dal.entity.Link; +import com.zblog.common.plugin.MapContainer; import com.zblog.common.util.IdGenarater; import com.zblog.service.LinkService; @@ -34,6 +37,13 @@ public String insert(Link link){ linkService.insert(link); return "redirect:/backend/links"; } + + @ResponseBody + @RequestMapping(value="/{linkid}",method = RequestMethod.DELETE) + public Object remove(@PathVariable("linkid")String linkid){ + linkService.deleteById(linkid); + return new MapContainer("success", true); + } @RequestMapping(value = "/edit", method = RequestMethod.GET) public String edit(Model model){ diff --git a/src/main/java/com/zblog/backend/controller/PostController.java b/src/main/java/com/zblog/backend/controller/PostController.java index 2a8470e3..d3358e8d 100644 --- a/src/main/java/com/zblog/backend/controller/PostController.java +++ b/src/main/java/com/zblog/backend/controller/PostController.java @@ -2,6 +2,7 @@ import java.util.Date; +import org.jsoup.Jsoup; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -10,10 +11,12 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.util.HtmlUtils; import com.zblog.biz.PostManager; import com.zblog.common.dal.entity.Post; import com.zblog.common.plugin.MapContainer; +import com.zblog.common.plugin.PageModel; import com.zblog.service.CategoryService; import com.zblog.service.PostService; @@ -29,23 +32,28 @@ public class PostController{ @RequestMapping(method = RequestMethod.GET) public String index(@RequestParam(value = "page", defaultValue = "1") int page, Model model){ - model.addAttribute("page", postService.listPost(page, 15)); + PageModel pageModel = postService.listPost(page, 15); + model.addAttribute("page", pageModel); return "backend/post/list"; } @ResponseBody @RequestMapping(method = RequestMethod.POST) - public Object insert(Post post, String txt, String uploadToken){ + public Object insert(Post post, String uploadToken){ post.setId(postService.createPostid()); post.setLastUpdate(new Date()); + post.setTitle(HtmlUtils.htmlEscape(post.getTitle().trim())); + post.setContent(post.getContent()); + String cleanTxt = Jsoup.parse(post.getContent()).text(); + post.setExcerpt(cleanTxt.length() > 350 ? cleanTxt.substring(0, 350) : cleanTxt); post.setCreator("admin"); postManager.insertPost(post, uploadToken); return new MapContainer("success", true); } - + @ResponseBody - @RequestMapping(value="/{postid}",method = RequestMethod.DELETE) - public Object remove(@PathVariable("postid")String postid){ + @RequestMapping(value = "/{postid}", method = RequestMethod.DELETE) + public Object remove(@PathVariable("postid") String postid){ postManager.removePost(postid); return new MapContainer("success", true); } @@ -53,20 +61,7 @@ public Object remove(@PathVariable("postid")String postid){ @RequestMapping(value = "/edit", method = RequestMethod.GET) public String edit(Model model){ model.addAttribute("categorys", categoryService.list()); - return "backend/post/edit"; } - // @InitBinder - // public void initBinder(WebDataBinder binder){ - // binder.registerCustomEditor(Reader.class, new PropertyEditorSupport(){ - // - // @Override - // public void setAsText(String text) throws IllegalArgumentException{ - // setValue(new StringReader(text)); - // } - // - // }); - // } - } diff --git a/src/main/java/com/zblog/backend/controller/UserController.java b/src/main/java/com/zblog/backend/controller/UserController.java index 681d4540..11f5f5a1 100644 --- a/src/main/java/com/zblog/backend/controller/UserController.java +++ b/src/main/java/com/zblog/backend/controller/UserController.java @@ -5,6 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @@ -22,18 +23,29 @@ public class UserController{ private UserService userService; @RequestMapping(method = RequestMethod.GET) - public String index(@RequestParam(value = "page", defaultValue = "1") int page,Model model){ - model.addAttribute("users", userService.list(page, 15)); - return "backend/users/list"; + public String index(@RequestParam(value = "page", defaultValue = "1") int page, Model model){ + model.addAttribute("page", userService.list(page, 15)); + return "backend/user/list"; } - - @ResponseBody + @RequestMapping(method = RequestMethod.POST) - public Object insert(User user){ + public String insert(User user){ user.setId(IdGenarater.uuid19()); user.setCreateTime(new Date()); user.setLastUpdate(new Date()); - return new MapContainer("success", userService.insert(user)); + return "backend/user/list"; + } + + @ResponseBody + @RequestMapping(value = "/{userid}", method = RequestMethod.DELETE) + public Object remove(@PathVariable("userid") String userid){ + userService.deleteById(userid); + return new MapContainer("success", true); + } + + @RequestMapping(value = "/edit", method = RequestMethod.GET) + public String edit(){ + return "backend/user/edit"; } } diff --git a/src/main/java/com/zblog/biz/PostManager.java b/src/main/java/com/zblog/biz/PostManager.java index 28b7c642..f639c29c 100644 --- a/src/main/java/com/zblog/biz/PostManager.java +++ b/src/main/java/com/zblog/biz/PostManager.java @@ -8,10 +8,7 @@ import org.springframework.transaction.annotation.Transactional; import com.zblog.common.dal.entity.Post; -import com.zblog.common.lucene.QueryBuilder; -import com.zblog.common.lucene.SearchEnginer; import com.zblog.common.plugin.MapContainer; -import com.zblog.common.plugin.PageModel; import com.zblog.common.util.constants.WebConstants; import com.zblog.service.PostService; import com.zblog.service.UploadService; @@ -50,14 +47,5 @@ public void removePost(String postid){ uploadService.deleteByPostid(postid); postService.deleteById(postid); } - - public PageModel search(String word,int pageIndex){ - PageModel result = new PageModel(pageIndex, 15); - QueryBuilder builder = new QueryBuilder(SearchEnginer.postEnginer().getAnalyzer()); - builder.addShould("title", word).addShould("content", word); - SearchEnginer.postEnginer().searchHighlight(builder, result); - - return result; - } } diff --git a/src/main/java/com/zblog/biz/aop/StaticTemplate.java b/src/main/java/com/zblog/biz/aop/StaticTemplate.java index c9dec6ea..37abdda8 100644 --- a/src/main/java/com/zblog/biz/aop/StaticTemplate.java +++ b/src/main/java/com/zblog/biz/aop/StaticTemplate.java @@ -6,10 +6,10 @@ import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; +import com.zblog.common.dal.entity.Category; import com.zblog.common.dal.entity.Post; import com.zblog.common.plugin.ApplicationContextUtil; import com.zblog.common.plugin.MapContainer; -import com.zblog.common.util.DateUtils; import com.zblog.common.util.constants.Constants; import com.zblog.common.util.constants.WebConstants; import com.zblog.service.CategoryService; @@ -57,12 +57,27 @@ public void staticLinks(){ * @param post */ public void staticPost(Post post){ - File file = new File(WebConstants.APPLICATION_PATH, DateUtils.formatDate("yyyy/MM", post.getCreateTime())); - if(!file.exists()) - file.mkdirs(); - FreeMarkerUtils.genHtml("/post.html", new File(file, "post-" + post.getId() + ".html"), post); + CategoryService categoryService = ApplicationContextUtil.getBean(CategoryService.class); + Category category = categoryService.loadById(post.getCategoryid()); + MapContainer param = new MapContainer("domain", Constants.DOMAIN).put("post", post).put("categoryName", + category.getName()); + FreeMarkerUtils.genHtml("/post.html", + new File(WebConstants.APPLICATION_PATH, "post/post-" + post.getId() + ".html"), param); logger.info("staticPost"); + + staticRecent(); + } + + public void removeStaticPost(String postid){ + String path = "post/post-" + postid + ".html"; + File postFile = new File(WebConstants.APPLICATION_PATH, path); + postFile.delete(); + logger.info("removeStaticPost"); + + staticRecent(); + } + private void staticRecent(){ PostService postService = ApplicationContextUtil.getBean(PostService.class); MapContainer param = new MapContainer("domain", Constants.DOMAIN); param.put("posts", postService.listRecent()); @@ -71,11 +86,4 @@ public void staticPost(Post post){ logger.info("staticRecent"); } - public void removeStaticPost(String postid){ - String path = "post/" + postid.substring(0, 4) + "/" + postid.substring(4, 6) + "/post-" + postid + ".html"; - File postFile = new File(WebConstants.APPLICATION_PATH, path); - postFile.delete(); - logger.info("removeStaticPost"); - } - } diff --git a/src/main/java/com/zblog/common/dal/entity/Comment.java b/src/main/java/com/zblog/common/dal/entity/Comment.java index 6108df59..35aa0da8 100644 --- a/src/main/java/com/zblog/common/dal/entity/Comment.java +++ b/src/main/java/com/zblog/common/dal/entity/Comment.java @@ -11,7 +11,7 @@ public class Comment extends BaseEntity{ /* 内容 */ private String content; /* 是否批准 */ - private boolean approved; + private boolean approved = false; /* 评论者的userAgent */ private String agent; /* 父评论ID */ diff --git a/src/main/java/com/zblog/common/dal/mybatis/CategoryMapper.xml b/src/main/java/com/zblog/common/dal/mybatis/CategoryMapper.xml index 1d6ff375..50dff155 100644 --- a/src/main/java/com/zblog/common/dal/mybatis/CategoryMapper.xml +++ b/src/main/java/com/zblog/common/dal/mybatis/CategoryMapper.xml @@ -12,6 +12,10 @@ select id, name,leftv, rightv, visible from category where name = #{name} + + insert into category(id,name,leftv,rightv,visible,createTime) values(#{id},#{name},#{leftv},#{rightv},#{visible},#{createTime}) diff --git a/src/main/java/com/zblog/common/dal/mybatis/PostMapper.xml b/src/main/java/com/zblog/common/dal/mybatis/PostMapper.xml index 7b9d4e1d..328c2355 100644 --- a/src/main/java/com/zblog/common/dal/mybatis/PostMapper.xml +++ b/src/main/java/com/zblog/common/dal/mybatis/PostMapper.xml @@ -14,7 +14,7 @@ - insert into user(id,nickName,realName,email,status,description,createTime,creator,lastUpdate) - values(#{id},#{nickName},#{#realName},#{email},#{status},#{description},#{createTime},#{creator},#{lastUpdate}) + insert into user(id,nickName,realName,password,email,status,description,createTime,creator,lastUpdate) + values(#{id},#{nickName},#{realName},#{password},#{email},#{status},#{description},#{createTime},#{creator},#{lastUpdate}) diff --git a/src/main/java/com/zblog/common/lucene/DocConverter.java b/src/main/java/com/zblog/common/lucene/DocConverter.java index 213a7b5e..98beb972 100644 --- a/src/main/java/com/zblog/common/lucene/DocConverter.java +++ b/src/main/java/com/zblog/common/lucene/DocConverter.java @@ -1,7 +1,7 @@ package com.zblog.common.lucene; import java.util.Arrays; -import java.util.List; +import java.util.Collection; import org.apache.lucene.document.Document; import org.apache.lucene.index.IndexableField; @@ -9,8 +9,9 @@ import com.zblog.common.plugin.MapContainer; public class DocConverter{ - - private DocConverter(){} + + private DocConverter(){ + } public static MapContainer convert(Document obj){ MapContainer mc = new MapContainer(); @@ -22,10 +23,13 @@ public static MapContainer convert(Document obj){ } public static MapContainer convert(Document obj, String... filters){ + return convert(obj, Arrays.asList(filters)); + } + + public static MapContainer convert(Document obj, Collection filters){ MapContainer mc = new MapContainer(); - List list = Arrays.asList(filters); for(IndexableField field : obj.getFields()){ - if(list.contains(field.name())) + if(filters.contains(field.name())) continue; mc.put(field.name(), field.stringValue()); } diff --git a/src/main/java/com/zblog/common/lucene/QueryBuilder.java b/src/main/java/com/zblog/common/lucene/QueryBuilder.java index 1e96389d..774da1ce 100644 --- a/src/main/java/com/zblog/common/lucene/QueryBuilder.java +++ b/src/main/java/com/zblog/common/lucene/QueryBuilder.java @@ -48,6 +48,7 @@ public QueryBuilder(Analyzer analyzer){ this.analyzer = analyzer; must = new LinkedList(); should = new LinkedList(); + lighters = new ArrayList(); } public QueryBuilder addMust(String field, String value){ @@ -83,16 +84,13 @@ public QueryBuilder addShould(String field, String value){ * @return */ public QueryBuilder addLighter(String... fields){ - if(lighters == null) - lighters = new ArrayList(); lighters.addAll(Arrays.asList(fields)); return this; } - public String[] getHighlighter(){ - String[] result = new String[lighters.size()]; - return lighters.toArray(result); + public List getHighlighter(){ + return lighters; } public QueryBuilder sortBy(SortField field){ @@ -144,12 +142,13 @@ public String[] token(String str){ List list = new ArrayList(); /* tokenStream的fieldName随意 */ stream = analyzer.tokenStream("any", new StringReader(str)); + CharTermAttribute cta = stream.addAttribute(CharTermAttribute.class); stream.reset(); - CharTermAttribute cta = stream.getAttribute(CharTermAttribute.class); while(stream.incrementToken() && list.size() <= MAX_QUERY_TOKENS){ list.add(cta.toString()); } + stream.end(); list = reSubset(list); String[] result = new String[list.size()]; return list.toArray(result); diff --git a/src/main/java/com/zblog/common/util/constants/Constants.java b/src/main/java/com/zblog/common/util/constants/Constants.java index 24f58a02..b1d70ccc 100644 --- a/src/main/java/com/zblog/common/util/constants/Constants.java +++ b/src/main/java/com/zblog/common/util/constants/Constants.java @@ -4,13 +4,11 @@ public final class Constants{ private Constants(){} public static final String APPLICATION_NAME = "JavaTalk-赚够钱背着画板去旅行"; - public static final String DOMAIN = "http://www.zhc.com"; + public static String DOMAIN = "http://www.zhc.com"; public static final String ENCODING_UTF_8 = "UTF-8"; public static final String COOKIE_CONTEXT_ID = "c_id"; public static final String COOKIE_USER_NAME = "un"; - public static final String COOKIE_SESSION_ID = "s_id"; - } diff --git a/src/main/java/com/zblog/common/util/web/WebContext.java b/src/main/java/com/zblog/common/util/web/WebContext.java index da923762..a8627eb4 100644 --- a/src/main/java/com/zblog/common/util/web/WebContext.java +++ b/src/main/java/com/zblog/common/util/web/WebContext.java @@ -1,13 +1,12 @@ package com.zblog.common.util.web; +import com.zblog.common.dal.entity.User; + public class WebContext{ private String sid; private int loginId; - private String userId; - private String userName; - private String nickName; + private User user; private String ip; - private boolean logon; private String requestURI; private String lastLoginTime; @@ -27,30 +26,6 @@ public void setLoginId(int loginId){ this.loginId = loginId; } - public String getUserId(){ - return userId; - } - - public void setUserId(String userId){ - this.userId = userId; - } - - public String getUserName(){ - return userName; - } - - public void setUserName(String userName){ - this.userName = userName; - } - - public String getNickName(){ - return nickName; - } - - public void setNickName(String nickName){ - this.nickName = nickName; - } - public String getIp(){ return ip; } @@ -59,12 +34,16 @@ public void setIp(String ip){ this.ip = ip; } - public boolean isLogon(){ - return logon; + public User getUser(){ + return user; } - public void setLogon(boolean logon){ - this.logon = logon; + public void setUser(User user){ + this.user = user; + } + + public boolean isLogon(){ + return user != null; } public String getRequestURI(){ @@ -82,5 +61,5 @@ public String getLastLoginTime(){ public void setLastLoginTime(String lastLoginTime){ this.lastLoginTime = lastLoginTime; } - + } diff --git a/src/main/java/com/zblog/service/AuthenticationService.java b/src/main/java/com/zblog/service/AuthenticationService.java index 578122a3..6af81844 100644 --- a/src/main/java/com/zblog/service/AuthenticationService.java +++ b/src/main/java/com/zblog/service/AuthenticationService.java @@ -2,10 +2,12 @@ import org.springframework.stereotype.Service; +import com.zblog.common.dal.entity.User; + @Service public class AuthenticationService{ - public boolean isAuthentication(String uri, boolean isLogin){ + public boolean isAuthentication(String uri, User user){ return true; } diff --git a/src/main/java/com/zblog/service/UserService.java b/src/main/java/com/zblog/service/UserService.java index e74758a4..cda95dc4 100644 --- a/src/main/java/com/zblog/service/UserService.java +++ b/src/main/java/com/zblog/service/UserService.java @@ -14,7 +14,7 @@ public class UserService extends BaseService{ private UserMapper userMapper; public PageModel list(int pageIndex, int pageSize){ - PageModel page = new PageModel(pageSize, pageSize); + PageModel page = new PageModel(pageIndex, pageSize); super.list(page); return page; } diff --git a/src/main/java/com/zblog/template/ftl/common/recent.html b/src/main/java/com/zblog/template/ftl/common/recent.html index 0fa9240a..3aac1c24 100644 --- a/src/main/java/com/zblog/template/ftl/common/recent.html +++ b/src/main/java/com/zblog/template/ftl/common/recent.html @@ -2,7 +2,7 @@

近期文章

\ No newline at end of file diff --git a/src/main/java/com/zblog/web/controller/IndexController.java b/src/main/java/com/zblog/web/controller/IndexController.java index 81526c9c..7739a16b 100644 --- a/src/main/java/com/zblog/web/controller/IndexController.java +++ b/src/main/java/com/zblog/web/controller/IndexController.java @@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; -import com.zblog.biz.PostManager; +import com.zblog.biz.aop.PostIndexManager; import com.zblog.common.plugin.MapContainer; import com.zblog.common.util.StringUtils; import com.zblog.common.util.constants.Constants; @@ -24,12 +24,12 @@ public class IndexController{ @Autowired private PostService postService; @Autowired - private PostManager postManager; + private PostIndexManager postIndexManager; @RequestMapping(value = "/", method = RequestMethod.GET) public String index(@RequestParam(value = "page", defaultValue = "1") int page, String word, Model model){ if(!StringUtils.isBlank(word)){ - model.addAttribute("page", postManager.search(word, page)); + model.addAttribute("page", postIndexManager.search(word, page)); }else{ model.addAttribute("page", postService.listPost(page, 10)); } diff --git a/src/main/java/com/zblog/web/filter/LoginFilter.java b/src/main/java/com/zblog/web/filter/LoginFilter.java index cb0d4554..2f6894f5 100644 --- a/src/main/java/com/zblog/web/filter/LoginFilter.java +++ b/src/main/java/com/zblog/web/filter/LoginFilter.java @@ -15,7 +15,6 @@ import com.zblog.common.util.IpUtils; import com.zblog.common.util.StringUtils; import com.zblog.common.util.UrlUtil; -import com.zblog.common.util.ValidateCodeGenerater; import com.zblog.common.util.constants.Constants; import com.zblog.common.util.web.WebContext; import com.zblog.common.util.web.WebContextHolder; @@ -30,19 +29,20 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse WebContext context = WebContextHolder.get(); if(context != null) return; - + try{ context = getWebContext(request, response); String uri = request.getRequestURI(); context.setRequestURI("".equals(uri) ? "/" : uri); // 保存上下文 WebContextHolder.set(context); - - boolean ajax=isAjax(request); - if(!ajax) addGloableAttr(request); + + boolean ajax = isAjax(request); + if(!ajax) + addGloableAttr(request); AuthenticationService service = ApplicationContextUtil.getBean(AuthenticationService.class); - if(service.isAuthentication(uri, context.isLogon())){ + if(service.isAuthentication(uri, context.getUser())){ filterChain.doFilter(request, response); return; } @@ -72,7 +72,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse private boolean isAjax(HttpServletRequest request){ return "XMLHttpRequest".equals(request.getHeader("X-Requested-With")); } - + private void addGloableAttr(HttpServletRequest request){ String result = request.getScheme() + "://" + request.getServerName(); if(request.getServerPort() != 80){ @@ -88,50 +88,16 @@ private WebContext getWebContext(HttpServletRequest request, HttpServletResponse // 该处实现登录控制 CookieUtil cookieUtil = new CookieUtil(request, response); - String sid = cookieUtil.getCookie(Constants.COOKIE_SESSION_ID); - if(StringUtils.isBlank(sid)){ - cookieUtil.setCookie(Constants.COOKIE_SESSION_ID, ValidateCodeGenerater.generateSid(), 1000 * 24 * 3600); - } - context.setSid(sid); - String cid = cookieUtil.getCookie(Constants.COOKIE_CONTEXT_ID); - ContextId contextId = new ContextId(cid); - if(!contextId.isValid()){ + if(StringUtils.isBlank(cid) || !cid.matches("[0-9a-zA-Z]{19}")) return context; - } UserService userService = ApplicationContextUtil.getBean(UserService.class); - User user = userService.loadById(contextId.getUserId()); - if(user != null){ - context.setLogon(true); - context.setNickName(user.getNickName()); - context.setUserId(user.getId()); - } + User user = userService.loadById(cid); + context.setUser(user); return context; } - public static class ContextId{ - private String[] idSplits; - - public ContextId(String cid){ - if(!StringUtils.isBlank(cid)) - idSplits = cid.split(":"); - } - - public String getUserId(){ - return idSplits[0]; - } - - public String getloginId(){ - return idSplits[1]; - } - - public boolean isValid(){ - return idSplits != null && idSplits.length == 2; - } - - } - } diff --git a/src/main/resources/config/spring-biz.xml b/src/main/resources/config/spring-biz.xml index 531db3e9..e54670ef 100644 --- a/src/main/resources/config/spring-biz.xml +++ b/src/main/resources/config/spring-biz.xml @@ -12,21 +12,25 @@ + + - + - - - + + + + + \ No newline at end of file