java annotation + springMVC 实现用户角色权限管理

场景描述:现在需要对部分Controller或者Controller里面的服务方法进行权限拦截。如果存在我们自定义的注解,通过自定义注解提取所需的权限值,然后对比session中的权限判断当前用户是否具有对该控制器或控制器方法的访问权限。如果没有相关权限则终止控制器方法执行直接返回。有两种方式对这种情况进行处理。


常用的权限系统设计模式是以角色为核心的,即角色是具有相同权限的一类人员的集合:

1.     一个角色可以有包含多个操作人员,一个操作人员也可以属于多个角色

2.     一个角色可以具有多个功能的操作权限,一个功能也可以被多个角色所拥有。

在登录时通过查询登录用户所属角色,即可得到个用户的所有功能集合,如下图:



方式一:使用Spring web拦截器

annotation 注解类

package com.wlsq.kso.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @company  深圳未来社区技术有限公司
* @description 权限的方法描述注解
* @author zzg
* @create 2017-02-03==17
*/
@Target(ElementType.METHOD)   
@Retention(RetentionPolicy.RUNTIME)   
@Documented  
@Inherited  
public @interface AuthorityAction {	
	/**
	 * 权限url
	 * @return
	 */
	public String permissionUrl(); 

}


springMVC 权限拦截器

package com.wlsq.kso.filter;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import com.wlsq.kso.annotation.AuthorityAction;

public class UserInterceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		// TODO Auto-generated method stub
		return roleControl(request, response, handler);
	}
	
	/**角色权限控制访问
	 * @throws IOException */
	private boolean roleControl(HttpServletRequest request,HttpServletResponse response, Object handler) throws IOException{
		 HttpSession session=request.getSession();
		 if(handler instanceof HandlerMethod){
			 HandlerMethod hm=(HandlerMethod)handler;
	         Object target=hm.getBean();
	         Class<?> clazz=hm.getBeanType();
	         Method m=hm.getMethod();
	         boolean isClzAnnotation= clazz.isAnnotationPresent(AuthorityAction.class);
             boolean isMethondAnnotation=m.isAnnotationPresent(AuthorityAction.class);
             AuthorityAction rc=null;
             //如果方法和类声明中同时存在这个注解,那么方法中的会覆盖类中的设定。
             if(isMethondAnnotation){
                 rc=m.getAnnotation(AuthorityAction.class);
                 String value=rc.permissionUrl();
                 //用户登入时,将用户相关的权限信息存储到session中。
                 List<String> obj=(List<String>) session.getAttribute("user_auth");
                 boolean isEquals = obj.contains(value);
                 if(!isEquals){
                     //401未授权访问
                	 String url = response.encodeRedirectURL(request.getContextPath()  
                             + "/401.jsp");  
                	 response.sendRedirect(url);  
                     return false;
                 }			 
             }else if(isClzAnnotation){
                 rc=clazz.getAnnotation(AuthorityAction.class);
             }           
		 }
		return true;
	}
	
	

}


Controller 类实现:

package com.wlsq.kso.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wlsq.kso.annotation.AuthorityAction;
import com.wlsq.kso.annotation.MethodAction;

/**
 * 系统主页Controller
 * 
 * @author zzg
 * @date 2017-02-27
 */

@Controller
@RequestMapping({"/main"})
public class MainController {
	
	  @RequestMapping({"/main.html"})
	  public ModelAndView main()
	  {
		 ModelAndView modelAndView = new ModelAndView();
		 modelAndView.setViewName("trans_statistics");
	     return modelAndView;
	  }
	  
	  //测试权限页面(成功)
	  @AuthorityAction(permissionUrl ="/role/manager")
	  @RequestMapping({"/one.html"})
	  public ModelAndView one()
	  {
		 ModelAndView modelAndView = new ModelAndView();
		 modelAndView.setViewName("one");
	     return modelAndView;
	  }
	  
	  
	  
	  //测试权限页面(失败)
	  @AuthorityAction(permissionUrl ="/aliPay/notify")
	  @RequestMapping({"/two.html"})
	  public ModelAndView two()
	  {
		 ModelAndView modelAndView = new ModelAndView();
		 modelAndView.setViewName("two");
	     return modelAndView;
	  }


}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值