spring How to Manage permissions through all the paths and the corresponding notes

 

1 Introduction

In the realization of rights management for web applications often require us to manually add permissions, names and related information corresponding to each path in order to achieve unified management, but a bit too tedious manual entry, by means of notes here way to achieve similar functionality swagger , can be added directly in the development path information to achieve unified management.

path name Permissions point Remark
/home/page/index.sdo Home P_HOME Home page

2. Environment

The entire web environment is based on springboot + maven build, well-known, springboot just a simplified version of the configuration of the spring, so this method is also applicable in ordinary spring projects

3. To achieve

  1. Definitions comment

/**
 * 用于注解需要进行权限控制的类
 */
@Retention(RetentionPolicy.RUNTIME)
public @interface URLControl {

}

 

/**
 * 注解方法,生成权限信息
 */
@Retention(RetentionPolicy.RUNTIME)
public @interface URLMessage {

    String permissionName();

    String url();

    String description() default "";

    String parent() default "";
}

 Two or more annotation, before a label is required for the path management controller class, the label for a specified interface inside the controller, the interface needs to provide information in the annotations, we have only included, authority name, path, description and four parent path information, there are other needs can extend their

   2. Get all the information notes runtime

package com.fastweb.fundation.message;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component("urlMessageCenter")
public class URLMessageCenter {
    private static List<Map<String,String>> urls;

    @Autowired
    ApplicationContext applicationContext;

    public void loadURL(){
        urls = new ArrayList<>();
        Map o = applicationContext.getBeansWithAnnotation(URLControl.class);
        for(Object key:o.keySet()){
            Method[] methods = o.get(key).getClass().getMethods();
            for(int i = 0;i < methods.length;i++){
                URLMessage message = methods[i].getAnnotation(URLMessage.class);
                if(message != null){
                    Map url = new HashMap();
                    url.put("description",message.description());
                    url.put("parent",message.parent());
                    url.put("permissionName",message.permissionName());
                    url.put("url",message.url());
                    urls.add(url);
                }
            }
        }
    }
    public List<Map<String,String>> getURL(){
        if(urls == null){
            synchronized (URLMessageCenter.class){
                if(urls == null){
                    loadURL();
                }
            }
        }
        return urls;
    }
}

  With URLMessageCenter can get all the current path information is annotated at any time, to route information to operate, for example, operate on the route information when a project starts

package com.fastweb.fundation.start;

import com.fastweb.fundation.message.URLMessageCenter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class PermissionRunner implements ApplicationRunner {
    @Autowired
    URLMessageCenter urlMessageCenter;

    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        List<Map<String,String>> urls = urlMessageCenter.getURL();
        /**
         * 对路径进行统一操作
         */
    }
}

4. Test

package com.fastweb.mod.admin.home;

import com.fastweb.Authorization.mod.UserDetail;
import com.fastweb.fundation.action.BaseAction;
import com.fastweb.fundation.message.URLControl;
import com.fastweb.fundation.message.URLMessage;
import com.fastweb.fundation.message.URLMessageCenter;
import com.fastweb.fundation.mod.menu.Menu;
import com.fastweb.fundation.mod.menu.MenuService;
import com.fastweb.fundation.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Controller("adminHome")
@RequestMapping("admin/home")
@URLControl
public class HomeAction extends BaseAction {

    @Autowired
    private MenuService menuService;


    @Override
    protected void setService(BaseService service) {
        this.service = service;
    }

    @Override
    protected String getPrefix() {
        return "admin/home/";
    }

    @URLMessage(permissionName = "P_ADMIN_INDEX",url = "/admin/home/page/index",description = "PAGE-后台外层页面",parent = "")
    public String index(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SQLException, IllegalAccessException {
        List<Menu> menus = this.menuService.getMenus();
        UserDetail user = (UserDetail) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        modelMap.put("menus",menus);
        modelMap.put("user",user);
        return "index";
    }

    @URLMessage(permissionName = "P_ADMIN_MAIN",url = "/admin/home/page/main",description = "PAGE-后台首页",parent = "")
    public String main(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){
        return "main";
    }
}

Measured success, to get the path information, do not write additional code

Guess you like

Origin blog.csdn.net/qq_35488769/article/details/82682384