public class User { private int id; private String name; private String password; private Set<String> groups = new HashSet<String>(); } |
<hibernate-mapping auto-import="true" default-lazy="false"> <class name="net.ideawu.User" table="User"> ?。糲ache usage="read-write" /> <id name="id" column="id"> ?。糶enerator class="native"/> </id> ?。紁roperty name="name" column="name"/> <property name="password" column="password"/> ?。約et name="groups" table="UserGroup" cascade="save-update" lazy="false"> ?。糼ey column="user" /> <element column="`group`" type="string" /> ?。?set> </class> </hibernate-mapping> |
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import org.springframework.web.util.UrlPathHelper; import org.springframework.util.AntPathMatcher; import org.springframework.util.PathMatcher; ... public class AuthorizeInterceptor extends HandlerInterceptorAdapter { private UrlPathHelper urlPathHelper = new UrlPathHelper(); private PathMatcher pathMatcher = new AntPathMatcher(); private Properties groupMappings; /** * Attach URL paths to group. */ public void setGroupMappings(Properties groupMappings) { this.groupMappings = groupMappings; } public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String url = urlPathHelper.getLookupPathForRequest(request); String group = lookupGroup(url); // 找出資源所需要的權(quán)限, 即組名 if(group == null){ // 所請求的資源不需要保護(hù). return true; } // 如果已經(jīng)登錄, 一個User實(shí)例被保存在session中. User loginUser = (User)request.getSession().getAttribute("loginUser"); ModelAndView mav = new ModelAndView("system/authorizeError"); if(loginUser == null){ mav.addObject("errorMsg", "你還沒有登錄!"); throw new ModelAndViewDefiningException(mav); }else{ if(!loginUser.getGroups().contains(group)){ mav.addObject("errorMsg", "授權(quán)失敗! 你不在 <b>" + group + "</b> 組!"); throw new ModelAndViewDefiningException(mav); } return true; } } /* * 查看 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.lookupHandler() * Ant模式的最長子串匹配法. */ private String lookupGroup(String url){ String group = groupMappings.getProperty(url); if (group == null) { String bestPathMatch = null; for (Iterator it = this.groupMappings.keySet().iterator();it.hasNext();) { String registeredPath = (String) it.next(); if (this.pathMatcher.match(registeredPath, url) && (bestPathMatch == null || bestPathMatch.length() <= registeredPath.length())) { group = this.groupMappings.getProperty(registeredPath); bestPathMatch = registeredPath; } } } return group; } } |
<bean id="authorizeInterceptor" class="net.ideawu.AuthorizeInterceptor"> ?。紁roperty name="groupMappings"> ?。紇alue> ?。?-- Attach URL paths to group --> /admin/*=admin </value> ?。?property> </bean> <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> ?。紁roperty name="interceptors"> ?。糽ist> ?。紃ef bean="authorizeInterceptor" /> </list> ?。?property> ?。紁roperty name="mappings"> ?。紇alue> /index.do=indexController /browse.do=browseController /admin/removeArticle.do=removeArticleController ?。?value> ?。?property> </bean> |