2013-01-15 64 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

相关问题