国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

Spring框架下實(shí)現(xiàn)基于組的用戶權(quán)限管理

2019-11-18 15:28:40
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

  在幾乎所有的web應(yīng)用中都需要對(duì)訪問(wèn)者(用戶)進(jìn)行權(quán)限治理, 因?yàn)槲覀兿M承╉?yè)面只對(duì)特定的用戶開(kāi)放, 以及某些操作只有符合身份的用戶才能進(jìn)行。這之中涉及到了身份驗(yàn)證和權(quán)限治理. 只有單用戶系統(tǒng)和多用戶單權(quán)限系統(tǒng)才不需要權(quán)限治理。

  在本文中, 使用了基于組的權(quán)限治理, 并在SPRing框架下利用HandlerInterceptorAdapter和Hibernate進(jìn)行實(shí)現(xiàn)。

  User的結(jié)構(gòu)是:

public class User {
 private int id;
 private String name;
 private String passWord;
 private Set<String> groups = new HashSet<String>();
}
  UserGroup表:

  user:intgroup:String使用聯(lián)合主鍵, 在java中沒(méi)有對(duì)應(yīng)的類。

  Hibernate映射文件是:

<hibernate-mapping auto-import="true" default-lazy="false">
 <class name="net.ideawu.User" table="User">
 <cache usage="read-write" />
 <id name="id" column="id">
  <generator class="native"/>
 </id>
 <property name="name" column="name"/>
 <property name="password" column="password"/>
 <set name="groups" table="UserGroup" cascade="save-update" lazy="false">
  <key column="user" />
  <element column="`group`" type="string" />
 </set>
 </class>
</hibernate-mapping>
  一切的身份驗(yàn)證交給一個(gè)繼續(xù)HandlerInterceptorAdapter的類來(lái)做:

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){
   // 所請(qǐng)求的資源不需要保護(hù).
   return true;
  }
  // 假如已經(jīng)登錄, 一個(gè)User實(shí)例被保存在session中.
  User loginUser = (User)request.getSession().getAttribute("loginUser");
  ModelAndView mav = new ModelAndView("system/authorizeError");
  if(loginUser == null){
   mav.addObject("errorMsg", "你還沒(méi)有登錄!");
   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模式的最長(zhǎng)子串匹配法.
 */
 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;
 }
}
  下面我們需要在Spring的應(yīng)用上下文配置文件中設(shè)置:



發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 甘孜县| 九寨沟县| 丰原市| 栾城县| 湘潭县| 县级市| 华蓥市| 岗巴县| 军事| 吐鲁番市| 灌阳县| 渑池县| 鸡东县| 宁明县| 桐梓县| 庐江县| 新疆| 留坝县| 桓仁| 平阴县| 安多县| 和顺县| 开封县| 焦作市| 垦利县| 郧西县| 德格县| 政和县| 珠海市| 钟祥市| 连江县| 文水县| 吉水县| 安多县| 晋州市| 岳西县| 上饶县| 苏尼特右旗| 上林县| 绥宁县| 确山县|