1. 程式人生 > >spring+spring-mvc+mybatis框架許可權的實現

spring+spring-mvc+mybatis框架許可權的實現

/**
 * 登陸的時候需要攔截器
 * 框架的攔截器體現了一種設計模式(介面卡模式)
 * 1)實現介面 HandlerInterceptor
 * 2)繼承父類 HandlerInterceptorAdapter
 * 
 * a)判斷使用者是否登陸
 * b)如果登陸,繼續訪問
 * c)如果沒有登陸,跳轉回到登陸頁面。
 * @author 18801

 * */

package com.atguigu.atcrowdfunding.interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.atguigu.atcrowdfunding.bean.User;

public class LoginInterceptor extends HandlerInterceptorAdapter {

	/**
	 * 在控制器執行之前進行攔截和處理
	 * 請求可以根據方法的返回結果來確定是否需要繼續執行,true,繼續執行,false,請求結束
	 */
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
				
		HttpSession session = request.getSession();


			//判斷使用者是否登陸
			
			User loginUser = (User)session.getAttribute("loginUser");
			
			if ( loginUser == null ) {
				//如果沒有登陸,跳轉回到登陸頁面。 
				response.sendRedirect(session.getServletContext().getContextPath() + "/login.htm");
				return false;
			} else {
				//如果登陸,繼續訪問	
				return true;
			}
//		}
	}

	/**
	 * 此方法在處理器完成之後執行
	 */
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		super.postHandle(request, response, handler, modelAndView);
	}

	/**
	 * 此方法在請求結束時(檢視渲染完畢)執行
	 */
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		super.afterCompletion(request, response, handler, ex);
	}


}
在Spring_-mvc。xml的配置檔案加入配置 

 
 
   <mvc:interceptors>
           <mvc:interceptor>
              <mvc:mapping path="/**"/>
              <mvc:exclude-mapping path="/login/index.do"/>
              <bean class="com.lanke.app.interceptor.LoginInterceptor"></bean>
          </mvc:interceptor>

      </mvc:interceptors> 


<mvc:exclude-mapping path="/login/index.do"/>的意思是 對這個連結不進行攔截(可以設定多個)
 
 
 
 
做到這裡我們只是讓攔截器起攔截作用,接下來是登陸授權(使用者的許可權是放在session裡的)

 
 

 
package com.atguigu.atcrowdfunding.interceptors;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.atguigu.atcrowdfunding.bean.Permission;
import com.atguigu.atcrowdfunding.manager.service.PermissionService;
import com.atguigu.atcrowdfunding.util.StringUtil;

/**
 * 授權攔截器
 * 只有使用者擁有相應的許可權,才能傳送相應的請求。
 * 1)獲取請求路徑
 * 2)判斷當前路徑需要不需要授權
 * 3)如果不需要授權,那麼直接訪問
 * 4)如果需要授權,那麼判斷當前的使用者是否具有相應的許可權
 * 5)如果有相應的許可權,那麼繼續訪問
 * 6)如果沒有相應的許可權,跳轉到錯誤頁面
 * @author 18801
 *
 */
public class AuthInterceptor extends HandlerInterceptorAdapter {

	@Autowired
	private PermissionService permissionService;
	
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		
		// 1)獲取請求路徑
		 String uri = request.getRequestURI();
		
		 // 2)判斷當前路徑需要不需要授權
		 // 2-1) 獲取所有的授權訪問路徑

		 Set<String> authPathSet =
		     (Set<String>)request.getSession().getServletContext().getAttribute("authPathSet");
		 
		 if ( authPathSet.contains(uri) ) {
			 // 4)如果需要授權,那麼判斷當前的使用者是否具有相應的許可權
			 Set<String> userAuthPathSet = (Set<String>)request.getSession().getAttribute("userAuthPathSet");
			 if ( userAuthPathSet.contains(uri) ) {
				// 5)如果有相應的許可權,那麼繼續訪問
				 return true;
			 } else {
				// 6)如果沒有相應的許可權,跳轉到錯誤頁面
				 response.sendRedirect(request.getSession().getServletContext().getContextPath() + "/error.htm");
				 return false;
			 }
			 
		 } else {
			// 3)如果不需要授權,那麼直接訪問
			 return true;
		 }
	}

}

簡單的許可權控制就可以實現了
 
 
 
 

 
 
 
 
 
 
  
 <mvc:exclude-mapping path="/login/index.do"/>的意思是 對這個連結不進行攔截(可以設定多個)
使用者的許可權是放在session裡的