• struct2 拦截所有没有登录的用户,强行转到登录界面AuthorizationInterceptor


    package com.sise.action;
    
    import java.util.Map;
    
    import com.opensymphony.xwork2.Action;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
    
    public class AuthorizationInterceptor extends AbstractInterceptor {
    	private String ignoreActions;
    
    	// ignoreActions属性的getter方法
    	public String getIgnoreActios() {
    		return ignoreActions;
    	}
    	// ignoreActions属性的setter方法
    	public void setIgnoreActions(String ignoreActions) {
    		this.ignoreActions = ignoreActions;
    	}
    	@Override
    	public String intercept(ActionInvocation invocation) throws Exception {
    		ActionContext ctx = invocation.getInvocationContext();
    		Map session = ctx.getSession();
    		Object user = session.get("currentUserId");
    		boolean ignore = false;
    		if (user != null || ignore == true) {
    			return invocation.invoke();
    		} else {
    			return Action.LOGIN;
    		}
    	}
    }
    

      struct.xml配置文件

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
    <struts>
    	<package name="default" extends="struts-default">
    		<interceptors>
    			<interceptor name="loginInterceptor"
    				class="com.sise.action.AuthorizationInterceptor"></interceptor>
    			<interceptor-stack name="loginDefaultStack">
    				<interceptor-ref name="loginInterceptor">
    					<param name="ignoreActions">
    						validate_code,register.*,.*login.*,upload,connector</param>
    				</interceptor-ref>
    				<interceptor-ref name="defaultStack"></interceptor-ref>
    			</interceptor-stack>
    		</interceptors>
    		<default-interceptor-ref name="loginDefaultStack"></default-interceptor-ref>
    		<global-results>
    			<result name="login" type="redirect">/login.html</result>
    		</global-results>
    		<action name="index" class="com.sise.action.IndexAction"
    			method="index">
    			<result name="success">/index.html</result>
    		</action>
    		<action name="login" class="com.sise.action.LoginAction"
    			method="login">
    			<result type="redirectAction">index</result>
    			<result name="login" >/login.html</result>
    			<interceptor-ref name="defaultStack"></interceptor-ref>
    		</action>
    	</package>
    </struts>  
    

      

  • 相关阅读:
    SQL查询出现1055 this is incompatible with sql_mode=only_full_group_by
    Nacos的9848端口报错:Docker容器的Nacos2.x版本
    将Object数组转换为String数组
    nacos2.x配置
    出现错误vuecli · Failed to download repo vuejstemplates/webpack: XXX的解决方法
    iOS开发——设置支持的iOS设备(512m内存以上设备)
    js 深度合并两个对象
    vue 前端解决跨域
    git 在idea中解决冲突
    前端抱怨API响应慢,怎么办
  • 原文地址:https://www.cnblogs.com/taoweiji/p/3248822.html
Copyright © 2020-2023  润新知