Spring+Struts 2 實現細粒度許可權控制問題
1.新建註解Permission
@Retention(RetentionPolicy.RUNTIME)//指定該註解是在執行期進行
@Target(ElementType.METHOD)//指定該註解要在方法上使用
public @interface Permission {
String model();
String privilegeValue();
}
2.註解解析器:
public class ValidatePermission {
public static Permission parsePermission(Class<?> clazz, String methodName,
Class<?>... parameterTypes) throws NoSuchMethodException {
// 根據方法名,取得方法,如果有則返回
Method method = clazz.getMethod(methodName, parameterTypes);
if (method != null && method.isAnnotationPresent(Permission.class)) {
Permission permission = method.getAnnotation(Permission.class);
if(null!=permission)
return permission;
}
return null;
}
}
3.自定義攔截器
public class PermissionInterceptor extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation invocation) throws Exception {
ActionContext context = invocation.getInvocationContext();
ActionProxy proxy = invocation.getProxy();
String methodName = proxy.getMethod();
Object action = proxy.getAction();
String auth = null;
HttpServletRequest request = ServletActionContext.getRequest();
if(request.getRequestURI().startsWith("/control")){
if(!validate(action.getClass(),methodName,request)){
ActionContext.getContext().put("message", "您沒有執行該操作的許可權");
ActionContext.getContext().put("urladdress", SiteUrl.readUrl("control.control.right"));
return "message";
}
}
return null;
}
private boolean validate(Class<?> clazz, String methodName,
HttpServletRequest request) throws NoSuchMethodException {
Permission permission = ValidatePermission.parsePermission(clazz, methodName, null);
SystemPrivilege methodPrivilege = new SystemPrivilege(
new SystemPrivilegePK(permission.model(), permission.privilegeValue()));
request = ServletActionContext.getRequest();
Employee employee = (Employee) request.getSession().getAttribute("employee");
for (PrivilegeGroup p : employee.getGroups()) {
if(p.getPrivileges().contains(methodPrivilege))
return true;
}
return false;
}
}
4.配置strut.xml
<interceptors>
<interceptor name="permission" class="cn.kugou.web.interceptor.PermissionInterceptor"/>
<interceptor-stack name="employeePermissionStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="permission"/>
</interceptor-stack>
</interceptors>
/**
* 系統許可權
*/
@Entity
public class SystemPrivilege {
private SystemPrivilegePK id;
/* 許可權名稱 */
private String name;
public SystemPrivilege(String model, String privilegeValue, String name) {
this.id = new SystemPrivilegePK(model, privilegeValue);
this.name = name;
}
public SystemPrivilege(SystemPrivilegePK id) {
this.id = id;
}
public SystemPrivilege(){}
@EmbeddedId
public SystemPrivilegePK getId() {
return id;
}
public void setId(SystemPrivilegePK id) {
this.id = id;
}
@Column(length=20,nullable=false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final SystemPrivilege other = (SystemPrivilege) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
@Embeddable//聯合主鍵(好比name有firstname和lastname)
public class SystemPrivilegePK implements Serializable{
private static final long serialVersionUID = 8605550883829066859L;
/* 模組名 */
private String model;
/* 許可權值 */
private String privilegeValue;
public SystemPrivilegePK(){}
public SystemPrivilegePK(String model, String privilegeValue) {
this.model =model;
this.privilegeValue= privilegeValue;
}
@Column(length=25, name="model")
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
@Column(length=25, name="privilegeValue")
public String getPrivilegeValue() {
return privilegeValue;
}
public void setPrivilegeValue(String privilegeValue) {
this.privilegeValue = privilegeValue;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((model == null) ? 0 : model.hashCode());
result = prime * result
+ ((privilegeValue == null) ? 0 : privilegeValue.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final SystemPrivilegePK other = (SystemPrivilegePK) obj;
if (model == null) {
if (other.model != null)
return false;
} else if (!model.equals(other.model))
return false;
if (privilegeValue == null) {
if (other.privilegeValue != null)
return false;
} else if (!privilegeValue.equals(other.privilegeValue))
return false;
return true;
}
}
這樣配置完成後,啟動程式卻出錯了。
具體錯誤是:SystemPrivilege methodPrivilege = new SystemPrivilege(
new SystemPrivilegePK(permission.model(), permission.privilegeValue()));
它出錯了。
在也弄不懂了???????
錯誤報告:
Struts Problem Report
Struts has detected an unhandled exception:
Messages:
File: cn/kugou/web/interceptor/PermissionInterceptor.java
Line number: 45
--------------------------------------------------------------------------------
Stacktraces
java.lang.NullPointerException
cn.kugou.web.interceptor.PermissionInterceptor.validate(PermissionInterceptor.java:45)
cn.kugou.web.interceptor.PermissionInterceptor.intercept(PermissionInterceptor.java:31)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
cn.kugou.web.interceptor.EmployeeInterceptor.intercept(EmployeeInterceptor.java:31)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept
@Retention(RetentionPolicy.RUNTIME)//指定該註解是在執行期進行
@Target(ElementType.METHOD)//指定該註解要在方法上使用
public @interface Permission {
String model();
String privilegeValue();
}
2.註解解析器:
public class ValidatePermission {
public static Permission parsePermission(Class<?> clazz, String methodName,
Class<?>... parameterTypes) throws NoSuchMethodException {
// 根據方法名,取得方法,如果有則返回
Method method = clazz.getMethod(methodName, parameterTypes);
if (method != null && method.isAnnotationPresent(Permission.class)) {
Permission permission = method.getAnnotation(Permission.class);
if(null!=permission)
return permission;
}
return null;
}
}
3.自定義攔截器
public class PermissionInterceptor extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation invocation) throws Exception {
ActionContext context = invocation.getInvocationContext();
ActionProxy proxy = invocation.getProxy();
String methodName = proxy.getMethod();
Object action = proxy.getAction();
String auth = null;
HttpServletRequest request = ServletActionContext.getRequest();
if(request.getRequestURI().startsWith("/control")){
if(!validate(action.getClass(),methodName,request)){
ActionContext.getContext().put("message", "您沒有執行該操作的許可權");
ActionContext.getContext().put("urladdress", SiteUrl.readUrl("control.control.right"));
return "message";
}
}
return null;
}
private boolean validate(Class<?> clazz, String methodName,
HttpServletRequest request) throws NoSuchMethodException {
Permission permission = ValidatePermission.parsePermission(clazz, methodName, null);
SystemPrivilege methodPrivilege = new SystemPrivilege(
new SystemPrivilegePK(permission.model(), permission.privilegeValue()));
request = ServletActionContext.getRequest();
Employee employee = (Employee) request.getSession().getAttribute("employee");
for (PrivilegeGroup p : employee.getGroups()) {
if(p.getPrivileges().contains(methodPrivilege))
return true;
}
return false;
}
}
4.配置strut.xml
<interceptors>
<interceptor name="permission" class="cn.kugou.web.interceptor.PermissionInterceptor"/>
<interceptor-stack name="employeePermissionStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="permission"/>
</interceptor-stack>
</interceptors>
/**
* 系統許可權
*/
@Entity
public class SystemPrivilege {
private SystemPrivilegePK id;
/* 許可權名稱 */
private String name;
public SystemPrivilege(String model, String privilegeValue, String name) {
this.id = new SystemPrivilegePK(model, privilegeValue);
this.name = name;
}
public SystemPrivilege(SystemPrivilegePK id) {
this.id = id;
}
public SystemPrivilege(){}
@EmbeddedId
public SystemPrivilegePK getId() {
return id;
}
public void setId(SystemPrivilegePK id) {
this.id = id;
}
@Column(length=20,nullable=false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final SystemPrivilege other = (SystemPrivilege) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
@Embeddable//聯合主鍵(好比name有firstname和lastname)
public class SystemPrivilegePK implements Serializable{
private static final long serialVersionUID = 8605550883829066859L;
/* 模組名 */
private String model;
/* 許可權值 */
private String privilegeValue;
public SystemPrivilegePK(){}
public SystemPrivilegePK(String model, String privilegeValue) {
this.model =model;
this.privilegeValue= privilegeValue;
}
@Column(length=25, name="model")
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
@Column(length=25, name="privilegeValue")
public String getPrivilegeValue() {
return privilegeValue;
}
public void setPrivilegeValue(String privilegeValue) {
this.privilegeValue = privilegeValue;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((model == null) ? 0 : model.hashCode());
result = prime * result
+ ((privilegeValue == null) ? 0 : privilegeValue.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final SystemPrivilegePK other = (SystemPrivilegePK) obj;
if (model == null) {
if (other.model != null)
return false;
} else if (!model.equals(other.model))
return false;
if (privilegeValue == null) {
if (other.privilegeValue != null)
return false;
} else if (!privilegeValue.equals(other.privilegeValue))
return false;
return true;
}
}
這樣配置完成後,啟動程式卻出錯了。
具體錯誤是:SystemPrivilege methodPrivilege = new SystemPrivilege(
new SystemPrivilegePK(permission.model(), permission.privilegeValue()));
它出錯了。
在也弄不懂了???????
錯誤報告:
Struts Problem Report
Struts has detected an unhandled exception:
Messages:
File: cn/kugou/web/interceptor/PermissionInterceptor.java
Line number: 45
--------------------------------------------------------------------------------
Stacktraces
java.lang.NullPointerException
cn.kugou.web.interceptor.PermissionInterceptor.validate(PermissionInterceptor.java:45)
cn.kugou.web.interceptor.PermissionInterceptor.intercept(PermissionInterceptor.java:31)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
cn.kugou.web.interceptor.EmployeeInterceptor.intercept(EmployeeInterceptor.java:31)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept
相關文章
- ORACLE FGAC(細粒度許可權控制)(轉)Oracle
- API閘道器控制靈活,支援細粒度許可權控制API
- Laravel實現許可權控制Laravel
- mysql中許可權控制粒度與效能的平衡MySql
- 如何用 Vue 實現前端許可權控制(路由許可權 + 檢視許可權 + 請求許可權)Vue前端路由
- 使用Virtual Private Database實現細粒度訪問控制Database
- 使用DBMS_RLS實現細粒度訪問控制
- canal mysql select許可權粒度MySql
- 無程式碼實現CRM角色許可權問題
- spring aop實現許可權控制,路徑控制Spring
- 【Struts2】:Interceptor實戰之許可權控制
- MySQL許可權問題MySql
- Java 訪問許可權控制(6)Java訪問許可權
- Vue2-利用自定義指令實現按鈕許可權控制Vue
- 前端許可權控制系統的實現思路前端
- Swift 中 Selector 方法的訪問許可權控制問題Swift訪問許可權
- android 許可權問題Android
- SQL Server許可權問題SQLServer
- 使用nginx控制ElasticSearch訪問許可權NginxElasticsearch訪問許可權
- Think IN JAVA --------JAVA訪問許可權控制Java訪問許可權
- Elasticsearch 許可權控制Elasticsearch
- SpringMVC使用攔截器實現許可權控制SpringMVC
- Atlas 2.1.0 實踐(4)—— 許可權控制
- moderator permission的許可權問題
- autohotkey透過com物件控制excel的許可權問題物件Excel
- SpringBoot(一) 如何實現AOP的許可權控制Spring Boot
- spring aop實現簡單的許可權控制功能Spring
- Linux許可權控制Linux
- Appfuse:許可權控制APP
- 程式設計實現遍歷ACL訪問控制列表檢查程式訪問許可權程式設計訪問許可權
- 提問:使用spring aop實現許可權管理Spring
- 認證/授權與許可權的問題
- k8s結合jumpserver做kubectl許可權控制 使用者在多個namespaces的訪問許可權 rbac許可權控制K8SServernamespace訪問許可權
- HIVE的許可權控制和超級管理員的實現Hive
- Vue | 自定義指令和動態路由實現許可權控制Vue路由
- Spring Security實現統一登入與許可權控制Spring
- springcloud-gateway整合jwt+jcasbin實現許可權控制SpringGCCloudGatewayJWT
- SQLServer控制使用者訪問許可權表SQLServer訪問許可權