2013-01-15 61 views
2

我們想隱藏一些基於Tomcat用戶登錄的代碼功能。我們正在使用基本身份驗證。有什麼建議麼?如何根據用戶登錄隱藏某些功能?

+0

檢查用戶角色([授權](http://en.wikipedia.org/wiki/Authorization))。以下是官方Java EE文檔中的相關信息:[什麼是角色?](http://docs.oracle.com/javaee/6/tutorial/doc/bnbxj.html#bnbxp)。這裏是與Tomcat相關的信息:[Realm Configuration HOW-TO](http://tomcat.apache.org/tomcat-7.0-doc/realm-howto.html) – informatik01

回答

8

IF你的意思只是隱藏一些資源依賴於在用戶是否登錄或不那麼它僅僅是一個限制訪問某些網頁的事(見下面的參考資料)。

IF要隱藏基礎上,一些功能是誰在記錄,那麼解決方案之一是相應的檢查權裏面的內容JSP和輸出的用戶角色。

原始例如:
sample.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<!DOCTYPE html> 
<html> 
<head> 
    <title>Sample Page</title> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
</head> 
<body> 
    <c:choose> 
     <c:when test="${pageContext.request.isUserInRole('admin')}"> 
      <p>Content for admin.<p> 
     </c:when> 
     <c:when test=${pageContext.request.isUserInRole('someRole')}"> 
      <p>Some content here</p> 
     <c:when> 
     <c:otherwise> 
      <p>Another Content</p> 
     </c:otherwise> 
    </c:choose> 
</body> 
</html> 

NB!
爲了能夠使用EL調用帶參數的方法,您必須至少使用Servlet版本3。從這裏
引用:https://stackoverflow.com/tags/el/info

由於EL 2.2,其被保持爲的Servlet 3.0的一部分和/ JSP 2.2 (Tomcat的7,Glassfish的3,JBoss應用服務器6等),有可能調用 非getter方法,如果需要的話帶參數。


另一種方式來隱藏/限制訪問某些取決於用戶角色是使web.xml中安全配置,或使用標註您的網頁(最低的Java EE 5)或者創建您自己的篩選器,用於檢查發出請求的用戶的角色。

要創建自己的過濾,創建一個實現javax.servlet.Filter接口的類,並在doFilter()方法檢查用戶通過使用HttpServletRequest的方法isUserInRole()提出的請求中的作用。

下面是實現自定義過濾的一個簡單的例子:
RoleCheckFilter.java

package com.example.filter; 

import java.io.IOException; 

import javax.servlet.Filter; 
import javax.servlet.FilterChain; 
import javax.servlet.FilterConfig; 
import javax.servlet.ServletException; 
import javax.servlet.ServletRequest; 
import javax.servlet.ServletResponse; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 


/** 
* Servlet Filter implementation class RoleCheckFilter. 
* Its purpose is to check logged-in user's role and 
* and accordingly allow or prevent access to the web resources. 
*/ 
public class RoleCheckFilter implements Filter { 

    /** 
    * @see Filter#init(FilterConfig) 
    */ 
    public void init(FilterConfig filterConfig) throws ServletException {} 

    /** 
    * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) 
    */ 
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) 
       throws IOException, ServletException { 
     HttpServletRequest request = (HttpServletRequest) req; 
     HttpServletResponse response = (HttpServletResponse) res; 

     if (request.isUserInRole("admin")) { 
      // user have the appropriate rights, allow the request 
      chain.doFilter(request, response); 
     } else { 
      // user does not have the appropriate rights, do something about it 
      request.setAttribute("error", "You don't have enough rights to access this resource"); 
      response.sendRedirect(request.getContextPath() + "/login.jsp"); 
      // or you could forward a user request somewhere 
     } 
    } 


    /** 
    * @see Filter#destroy() 
    */ 
    public void destroy() {} 

} 

添加適當的過濾器配置在網。XML

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    version="3.0"> 

    ... 

    <filter> 
     <filter-name>Role Check Filter</filter-name> 
     <filter-class>com.example.filter.RoleCheckFilter</filter-class> 
    </filter> 
    <filter-mapping> 
     <filter-name>Role Check Filter</filter-name> 
     <url-pattern>/admin/*</url-pattern> 
    </filter-mapping> 

    ... 

</web-app> 

你的情況

當然中,考慮到你使用基本身份驗證,這是很容易做出正確的web.xml中安全配置的事實(聲明安全)或使用編程安全

從官方的Java EE文檔

報價:

的Java EE安全服務可以爲Web應用程序在 實現以下幾種方式:

  • 元數據批註(或簡稱註解)用於指定類文件中有關安全性的信息。部署應用程序時,此信息可以由應用程序部署描述符使用或覆蓋。

  • 聲明式安全表示應用程序的安全結構,包括應用程序外部的部署描述符中的安全角色,訪問控制和身份驗證要求。
    在部署描述符中顯式指定的任何值將覆蓋註釋中指定的任何值。

  • 程序性安全嵌入在應用程序中,用於制定安全決策。僅當聲明式安全性不足以表達應用程序的安全模型時,程序式安全性非常有用。


退房有關保護Java EE應用官方Java EE文件(在你的情況要注意指定授權約束部分):
Java EE 6: Securing Web Applications
Java EE 5: Securing Web Applications

檢查從官方文檔也出來例子:
Java EE 6. Examples: Securing Web Applications
Java EE 5. Examples: Securing Web Applications

相關問題