日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

aop (权限控制之功能权限)

發(fā)布時間:2024/6/21 综合教程 28 生活家
生活随笔 收集整理的這篇文章主要介紹了 aop (权限控制之功能权限) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在實(shí)際web開發(fā)過程中通常會存在功能權(quán)限的控制,不如這個角色只允許擁有查詢權(quán)限,這個角色擁有CRUD權(quán)限,當(dāng)然按鈕權(quán)限顯示控制上可以用button.tld來控制,本文就不說明。

具體控制流程就是通過登錄系統(tǒng)時候請求控制層將用戶的所擁有功能權(quán)限查詢出來存入session中,然后通過aop切面編程技術(shù)獲取session里的功能權(quán)限與當(dāng)前方法標(biāo)簽注解權(quán)限匹配,當(dāng)存在則繼續(xù)執(zhí)行,若不存在,通過springmvc簡單的異常重定向到自己的無權(quán)限頁面。

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、通過反射獲取注解上的標(biāo)簽的封裝類

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、創(chuàng)建控制層

LoginController.java

具體session創(chuàng)建在之前那一篇文章中有寫到,通過攔截器創(chuàng)建的

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() {
        // 這里創(chuàng)建一個對象當(dāng)做登錄成功
        User user = new User();
        user.setLogin_name("taop");
        user.setLogin_pwd("1");

        // 登錄查詢
        if (user != null) {
            // 根據(jù)用戶查詢出所有權(quán)限,本來存入數(shù)據(jù)庫,這邊就生動生成taop的權(quán)限
            // 不如只有添加權(quán)限
            Privilege privilege = new Privilege();
            privilege.setName("query");
            privilege.setDesc("查詢權(quán)限");
            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") 可以通過數(shù)組的形式 如name={"query", "add"}

創(chuàng)建異常類

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
    }
    
}

創(chuàng)建權(quán)限對象

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);
        
        //獲取注解上的標(biāo)簽
        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("您無權(quán)操作!");
        }
        
        //是否通過訪問
        boolean flag = false;
        if(privilegeName == "" || privilegeName == null){
            flag = true;
        }else{
            for (Privilege privilege : privileges) {
                if(privilegeName.equals(privilege.getName())){
                    //用戶訪問權(quán)限(add) 是否包含當(dāng)前方法的訪問權(quán)限
                    flag = true;
                    break;
                }
            }
        }
        
        if(flag){
            return jpj.proceed();
        }else{
            //權(quán)限不足
            System.out.println("權(quán)限不足");
            throw new AssessDeniedException("您無權(quán)操作!");
        }
    }
}

配置spring-mvc.xml 異常類重定向跳轉(zhuǎn)

<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>

當(dāng)無權(quán)限拋出異常時候即會重定向到toHashNoPower 然后modelandview 自己寫的一個無權(quán)限的頁面

至此全部結(jié)束

請求結(jié)果:當(dāng)直接訪問查詢方法

當(dāng)訪問

再訪問

當(dāng)將權(quán)限設(shè)置成

繼續(xù)訪問

總結(jié)

以上是生活随笔為你收集整理的aop (权限控制之功能权限)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。