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

java哥 阅读:682 2021-03-31 21:22:52 评论:0

场景描述:现在需要对部分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; 
	  } 
 
 
} 




标签:springMVC
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号