Java struts2检查权限拦截器
在我当前的struts2+SpringWeb应用程序中,我的自定义userSessionInterceptor工作得很好。我现在还想开始强制执行用户角色。我在网上做过很多研究,好像有很多方法可以做到,比如例外,发送重定向 最合适的方法是什么?我已经在用户会话中保存了用户角色,并且我在检测和发现错误方面没有问题。我唯一需要决定的是,当许可不正确时,如何应对 在拦截器中,我知道我可以返回“xxx”并执行“xxx”操作。然而,我想实现的是,当用户试图做一些他们没有权限的事情时,会显示一条消息。我假设我可以返回到上一页并向url添加一个参数 有什么建议吗Java struts2检查权限拦截器,java,spring,jsp,struts2,struts,Java,Spring,Jsp,Struts2,Struts,在我当前的struts2+SpringWeb应用程序中,我的自定义userSessionInterceptor工作得很好。我现在还想开始强制执行用户角色。我在网上做过很多研究,好像有很多方法可以做到,比如例外,发送重定向 最合适的方法是什么?我已经在用户会话中保存了用户角色,并且我在检测和发现错误方面没有问题。我唯一需要决定的是,当许可不正确时,如何应对 在拦截器中,我知道我可以返回“xxx”并执行“xxx”操作。然而,我想实现的是,当用户试图做一些他们没有权限的事情时,会显示一条消息。我假设我
谢谢这是一个示例程序 RoleInterceptor.java struts.xml
检索
userDetails.jsp
/common/error.jsp
/common/busy.jsp
/common/test.jsp
/common/login.jsp
/通用/未经授权的duser.jsp
没有“最合适”的方法;这取决于你的需要。存储请求很容易,显示消息取决于您实际想要如何处理它。重定向到“拒绝访问”页面是最简单的。谢谢。确切地说,使用permission error url参数将其重定向回上一页的最佳方式是什么?如果您的应用程序允许任何安全API,我强烈建议您选择Spring security或Apache Shrio,这两种框架都将满足您的所有要求
package com.sample.common.interceptor;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.util.ValueStack;
public class RoleInterceptor implements Interceptor {
private static final long serialVersionUID = 5031826078189685536L;
@Override
public void init() {
}
@Override
public void destroy() {
}
@Override
public String intercept(ActionInvocation actionInvocation) {
String result = null;
try {
ActionContext actionContext = actionInvocation
.getInvocationContext();
ValueStack vStack = actionContext.getValueStack();
HttpSession httpSession = ServletActionContext.getRequest()
.getSession();
StringBuilder actionUrl = new StringBuilder(actionInvocation
.getProxy().getNamespace());
actionUrl.append("/");
actionUrl.append(actionInvocation.getProxy().getActionName());
if (httpSession != null) {
boolean hasPermission = true; // if the role has the permission
if (hasPermission) {
result = actionInvocation.invoke();
} else {
vStack.set("userMsg",
"You are not authorized to access this link");
result = "user.unauthorized";
}
} else {
vStack.set("userMsg", "Please login to your account");
result = "user.login";
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="userRoleInterceptor">
<interceptors>
<interceptor name="userRole"
class="com.sample.common.interceptor.RoleInterceptor" />
</interceptors>
</package>
<package name="user" namespace="/user" extends="struts-default, json-default, userRoleInterceptor">
<action name="user_details" method="getUserDetails"
class="com.sample.user.web.action.User">
<interceptor-ref name="userRole"/>
<interceptor-ref name="store">
<param name="operationMode">RETRIEVE</param>
</interceptor-ref>
<result name="success">userDetails.jsp</result>
<result name="input">/common/error.jsp</result>
<result name="busy" type="redirectAction" >/common/busy.jsp</result>
<result name="error" type="redirectAction" >/common/test.jsp</result>
<result name="user.login" type="redirectAction" >/common/login.jsp</result>
<result name="user.unauthorized" type="redirectAction" >/common/unauthorizedUser.jsp</result>
</action>
</package>
</struts>