国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
利用filter實現(xiàn)權(quán)限管理 - zhaozhenlin1224 - BlogJava

配置文件如下:

   <filter>
     
<filter-name>actionrolefilter</filter-name>
     
<display-name>Openlot Action Role Filter</display-name>
     
<filter-class>com.openlot.controller.web.authorisation.AclFilter</filter-class>
   
</filter>

   
<filter-mapping>
     
<filter-name>actionrolefilter</filter-name>
     
<servlet-name>action</servlet-name>
   
</filter-mapping>

  
<servlet>
    
<servlet-name>action</servlet-name>
    
<servlet-class>com.opensymphony.webwork.dispatcher.ServletDispatcher</servlet-class>
    
<load-on-startup>1</load-on-startup>
  
</servlet>

   關(guān)于filter mapping的配置:
     <filter-mapping>

           <filter-name>HelloWorldFilter</filter-name>

           <url-pattern>/filter/*</url-pattern>//對請求URL對應的資源過濾

            <servlet-name>testServlet</servlet-name>//對servlet過濾

        </filter-mapping>
filter類的代碼:

public class AclFilter implements Filter {

    
private static final Logger logger = Logger.getLogger(AclFilter.class);

    
public static final String SIGN_IN_KEY = "sign_in";
    
public static final String SIGN_IN_MESSAGE = "You're not logged in";
    
public static final String AUTHORISATION_FAILED_MESSAGE = 
        
"You are not authorized to view this page";
    
public static final String USER_PORTAL = "/portal!default.action";
    
public static final String ADMIN_PORTAL = "/adminPortal!default.action";
    
public static final String INDEX_JSP = "/index.jsp";

    
private FilterConfig mConfig;

    
public void init(FilterConfig config) throws ServletException {

        logger.info(
"initializing ACL Filter");
        mConfig 
= config;
    }


    
public void doFilter(
        ServletRequest request, ServletResponse response, FilterChain chain)
            
throws IOException, ServletException {

        HttpServletRequest httpRequest 
= (HttpServletRequest) request;
        HttpSession session 
= httpRequest.getSession();
        String requestURI 
= httpRequest.getRequestURI();
        String role 
= (String) session.getAttribute("role");

        
// TODO: Should not be hardcoded. 
        if (requestURI.matches(".*[lL]ogin.*")
            
|| requestURI.matches(".*logout.*")
            
|| requestURI.matches(".*registration.*")
            
|| requestURI.matches(".*selectEnglish.*")
            
|| requestURI.matches(".*selectLanguage.*")
            
|| requestURI.matches(".*forgotpass.*")
            
|| requestURI.matches(".*activeuser.*")
            
|| requestURI.matches(".*result.*")
            
|| requestURI.matches(".*directplayerhistory.*")
            
|| requestURI.matches(".*playerhistory.*")
            
|| requestURI.matches(".*addFunds.*")
            
|| requestURI.matches(".*processCupPayByInvoke.*")
            
|| requestURI.matches(".*purchase.*")
            
|| requestURI.matches(".*news.*")
            
|| requestURI.matches(".*download.*")
            
|| requestURI.matches(".*alipayPayInvoke.*")
            
|| requestURI.matches(".*wappush.*")
            
|| requestURI.matches(".*trustedService.*")
            
|| requestURI.matches(".*selectSwahili.*")) 
        
{
            chain.doFilter(request, response);
        }
 else {
            Long userId 
= (Long) session.getAttribute(BaseActionSupport.USER_ID_KEY);
            Long adminId 
= (Long) session.getAttribute(BaseActionSupport.ADMIN_ID_KEY);
            
if (userId == null && adminId == null{
                session.setAttribute(SIGN_IN_KEY, SIGN_IN_MESSAGE);
                logger.info(
"added signin error message");
                mConfig.getServletContext().getRequestDispatcher(
                    INDEX_JSP).forward(request, response);
            }
 else {
                
if (hasAccess(role, requestURI)) {
                    logger.info(
"authorised access to resource '" + requestURI + "'");
                    chain.doFilter(request, response);
                }
 else {
                    logger.info(
"unauthorised access to resource '" 
                        
+ requestURI + "'");
                    session.setAttribute(SIGN_IN_KEY, AUTHORISATION_FAILED_MESSAGE);
                    mConfig.getServletContext().getRequestDispatcher(
                        getPortal(userId, adminId)).forward(request, response);
                }

            }

        }

    }


    
public void destroy() {}

    
private boolean hasAccess(String roleId, String requestURI) {
        Role role 
= Role.getInstance();
        
return role.hasAccess(roleId, requestURI);
    }


    
private String getPortal(Long userId, Long adminId) {
        
if (userId != null)
            
return USER_PORTAL;
        
else if (adminId != null)
            
return ADMIN_PORTAL;
        
else
            
return INDEX_JSP;
    }

}

Role的方法:

    
public boolean hasAccess(String role, String requestURI) {

        
if (role == null)
            
return false;

        List patterns 
= (List) mRoleMap.get(role);
        Iterator iter 
= patterns.iterator();
        
while (iter.hasNext()) {
            String pattern 
= (String) iter.next();
            
if (Pattern.matches(pattern, requestURI))
                
return true;
        }


        
return false;
    }
本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
在 Web 應用中增加用戶跟蹤功能,學習在多線程環(huán)境下 Apache Log4j 的 ND...
配置Java Xss保護過濾器
使用AntiSamy防范XSS跨站腳本攻擊
WEB安全實戰(zhàn)(七)會話標識未更新
Servlet過濾器
Vaadin框架學習(login DEMO)
更多類似文章 >>
生活服務
分享 收藏 導長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服