Privilege Control Series 3 - Fine-grained control privileges using interceptors
Keywords:
Database
log4j
Spring
Preface
- The granularity of permissions can be fine-grained, and can even be partitioned to each method in the code. As long as the user does not have permission to access a CRUD method, we can intercept it.
General thinking
- Add a global interceptor to intercept the user's request and determine whether the user has permission to request this method. If the user has permission, it will release it. If the user has no permission, it will intercept, prompting the user to have no permission to use this function.
Custom Annotation Code
@Retention(RetentionPolicy.RUNTIME)
public @interface AnnotationLimit {
String mid();
String pid();
}
Interceptor code
public class ErrorAndLimitInterceptor extends MethodFilterInterceptor {
/**Interceptor*/
@Override
protected String doIntercept(ActionInvocation actioninvocation) throws Exception {
HttpServletRequest request = (HttpServletRequest) actioninvocation
.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);
try {
Object action = actioninvocation.getAction();
String methodName = actioninvocation.getProxy().getMethod();
Method method = action.getClass().getMethod(methodName, null);
String result = null;
boolean flag=isCheckLimit(request,method);
if (flag) {
result=actioninvocation.invoke();
}else{
request.setAttribute("errorMsg", "Sorry, you do not have permission to operate this function.");
return "errorMsg";
}
return result;
} catch (Exception e) {
/**
* Handling exceptions
*/
String errorMsg = "Error message appears, please check the log!";
if (e instanceof RuntimeException) {
RuntimeException re = (RuntimeException) e;
errorMsg = re.getMessage().trim();
}
/**
* Send an error message to the page
*/
request.setAttribute("errorMsg", errorMsg);
/**
* log4j Log
*/
Log log = LogFactory
.getLog(actioninvocation.getAction().getClass());
log.error(errorMsg, e);
return "errorMsg";
}
}
private boolean isCheckLimit(HttpServletRequest request, Method method) {
if (method==null) {
return false;
}
ElecUser elecUser=(ElecUser)request.getSession().getAttribute("globle_user");
if (elecUser==null) {
return false;
}
Hashtable<String,String> ht =(Hashtable<String, String>) request.getSession().getAttribute("globle_role");
if (ht==null) {
return false;
}
boolean isAnnotationPresent=method.isAnnotationPresent(AnnotationLimit.class);
if (!isAnnotationPresent) {
return false;
}
AnnotationLimit limit=method.getAnnotation(AnnotationLimit.class);
String mid=limit.mid();
String pid=limit.pid();
boolean flag=false;
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
IElecRoleService elecRoleService = (IElecRoleService)wac.getBean(IElecRoleService.SERVICE_NAME);
if (ht!=null && ht.size()>0) {
for (Iterator<Entry<String, String>> ite = ht.entrySet().iterator();ite.hasNext();) {
Entry<String,String> entry=ite.next();
String roleId =entry.getKey();
flag=elecRoleService.findRolePopedomByID(roleId,mid,pid);
if (flag) {
break;
}
}
}
return flag;
}
}
Interceptor configuration
<interceptors>
<! - Declare the interceptor - >
<interceptor name="errorAndLimitInterceptor" class="com.itheima.elec.utils.ErrorAndLimitInterceptor" />
<! - Configure the interceptor stack - >
<interceptor-stack name="myErrorAndLimitInterceptor">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="errorAndLimitInterceptor" >
<param name="excludeMethods">menuHome,title,left,change,loading,logout,alermStation,alermDevice,showMenu
</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<! - The interceptor stack that covers the underlying layer is valid for all action s in the package - >
<default-interceptor-ref name="myErrorAndLimitInterceptor"/>
Annotations to add permissions to methods
@AnnotationLimit(mid="an",pid="am")
public String home() {
//Method body
.......
.......
return "home";
}
Summary
- The function of the code in the interceptor is to read the annotation code on the body of the method. The annotation code stores the access information of the method. After the user gets the privilege information, he adds his role id. Then he uses the information to check whether the user has the privilege in the database. If he has the privilege, he releases it, otherwise he intercepts it. To do this, we control the granularity of permissions on each method, and we can enter the Url address in the browser's address bar without accessing illegal pages.
Posted by imawake on Thu, 11 Apr 2019 09:18:31 -0700