aop (权限控制之功能权限)
生活随笔
收集整理的這篇文章主要介紹了
aop (权限控制之功能权限)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在實際web開發過程中通常會存在功能權限的控制,不如這個角色只允許擁有查詢權限,這個角色擁有CRUD權限,當然按鈕權限顯示控制上可以用button.tld來控制,本文就不說明。
具體控制流程就是通過登錄系統時候請求控制層將用戶的所擁有功能權限查詢出來存入session中,然后通過aop切面編程技術獲取session里的功能權限與當前方法標簽注解權限匹配,當存在則繼續執行,若不存在,通過springmvc簡單的異常重定向到自己的無權限頁面。
1、配置注解方式
privilegeInfo.java
package com.tp.soft.common.util;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface PrivilegeInfo {
String name() default "";
}
2、通過反射獲取注解上的標簽的封裝類
PrivilegeInfoAnnotationParse.java
package com.tp.soft.common.util;
import java.lang.reflect.Method;
public class PrivilegeInfoAnnotationParse {
public static String parse(Class targetClass, String methodName) throws NoSuchMethodException, SecurityException{
String privilegeName = "";
Method method = targetClass.getMethod(methodName);
//判斷方法上是否存在@PrivilegeInfo 注解
if(method.isAnnotationPresent(PrivilegeInfo.class)){
//獲取注解對象
PrivilegeInfo annotation = method.getAnnotation(PrivilegeInfo.class);
//獲取注解對象上的名字@PrivilegeInfo(name="admin")
//即name為"admin"
privilegeName = annotation.name();
}
return privilegeName;
}
}
3、創建控制層
LoginController.java
具體session創建在之前那一篇文章中有寫到,通過攔截器創建的
package com.tp.soft.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.tp.soft.common.util.SysContext;
import com.tp.soft.entity.Privilege;
import com.tp.soft.entity.User;
@Controller
public class LoginController {
@RequestMapping(value = "/login")
public ModelAndView login() {
// 這里創建一個對象當做登錄成功
User user = new User();
user.setLogin_name("taop");
user.setLogin_pwd("1");
// 登錄查詢
if (user != null) {
// 根據用戶查詢出所有權限,本來存入數據庫,這邊就生動生成taop的權限
// 不如只有添加權限
Privilege privilege = new Privilege();
privilege.setName("query");
privilege.setDesc("查詢權限");
List<Privilege> privilegeList = new ArrayList<Privilege>();
privilegeList.add(privilege);
SysContext.getSession().setAttribute("privileges", privilegeList);
SysContext.getSession().setAttribute("user", user);
return new ModelAndView("/pc/main");
}
return null;
}
@RequestMapping(value="/toHasNoPower")
public ModelAndView toHasNoPower(){
return new ModelAndView("/pc/privilege/noPower");
}
}
UserController.java
package com.tp.soft.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.tp.soft.common.util.PrivilegeInfo;
import com.tp.soft.common.util.SysContext;
import com.tp.soft.entity.Privilege;
import com.tp.soft.entity.User;
import com.tp.soft.service.login.LoginSvc;
import com.tp.soft.service.sys.UserSvc;
@Controller
public class UserController {
@Resource
private UserSvc userSvc;
@Resource
private LoginSvc loginSvc;
@RequestMapping(value="/toQueryUser")
@PrivilegeInfo(name="query")
public ModelAndView toQueryUser(){
User user = userSvc.getUser(21);
Map<String, Object> map = new HashMap<String, Object>();
map.put("user", user);
return new ModelAndView("/pc/userTest");
}
}
@PrivilegeInfo(name="query") 可以通過數組的形式 如name={"query", "add"}
創建異常類
AssessDeniedException.java
package com.tp.soft.common.exception;
public class AssessDeniedException extends RuntimeException{
/**
*
*/
private static final long serialVersionUID = 5188167616201017971L;
public AssessDeniedException() {
super();
// TODO Auto-generated constructor stub
}
public AssessDeniedException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
public AssessDeniedException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public AssessDeniedException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public AssessDeniedException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
創建權限對象
Privilege.java
public class Privilege {
private int pid;
private String name;
private String desc;
...省略set get
}
4、配置aop切面類
AdminAspect.java
package com.tp.soft.aop;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import com.tp.soft.common.exception.AssessDeniedException;
import com.tp.soft.common.util.PrivilegeInfoAnnotationParse;
import com.tp.soft.common.util.SysContext;
import com.tp.soft.entity.Privilege;
import com.tp.soft.entity.User;
@Aspect
public class AdminAspect {
@Pointcut("execution(* com.tp.soft.controller..*.*(..)) && !execution(* com.tp.soft.controller.LoginController.*(..))")
public void pointCutMethod(){
}
@Around("pointCutMethod()")
public Object dealPrivilege(ProceedingJoinPoint jpj) throws Throwable{
//獲取請求的類和方法名
Class<? extends Object> cls = jpj.getTarget().getClass();
String name = jpj.getSignature().getName();
System.out.println(cls);
System.out.println(name);
//獲取注解上的標簽
String privilegeName = PrivilegeInfoAnnotationParse.parse(cls, name);
HttpSession session = SysContext.getSession();
User user = (User) session.getAttribute("user");
List<Privilege> privileges = (List<Privilege>) session.getAttribute("privileges");
if(user == null){
throw new AssessDeniedException("您無權操作!");
}
//是否通過訪問
boolean flag = false;
if(privilegeName == "" || privilegeName == null){
flag = true;
}else{
for (Privilege privilege : privileges) {
if(privilegeName.equals(privilege.getName())){
//用戶訪問權限(add) 是否包含當前方法的訪問權限
flag = true;
break;
}
}
}
if(flag){
return jpj.proceed();
}else{
//權限不足
System.out.println("權限不足");
throw new AssessDeniedException("您無權操作!");
}
}
}
配置spring-mvc.xml 異常類重定向跳轉
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="com.tp.soft.common.exception.AssessDeniedException">forward:/toHasNoPower</prop>
</props>
</property>
</bean>
當無權限拋出異常時候即會重定向到toHashNoPower 然后modelandview 自己寫的一個無權限的頁面
至此全部結束
請求結果:當直接訪問查詢方法
當訪問
再訪問
當將權限設置成
繼續訪問
總結
以上是生活随笔為你收集整理的aop (权限控制之功能权限)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Zookeeper02】ZK的作用以及
- 下一篇: rsync详解