简体   繁体   English

如何使Servlet识别调用方JSP和会话

[英]How to make Servlet recognize caller JSP and session

I am building a simple WebApplication using servlets. 我正在使用servlet构建一个简单的WebApplication。 I am a beginner but have tried to learn the most of this technology. 我是一个初学者,但已尝试学习大多数此技术。 There is something I cannot figure out. 我无法解决某些问题。 One of my servlets is the useful BalusC FileServlet 我的servlet之一是有用的BalusC FileServlet

http://balusc.blogspot.mx/2007/07/fileservlet.html

It responds to GET requests with the required file, nice and clean. 它使用所需的文件(干净)来响应GET请求。

I use this FileServlet to serve CSV files for a Dygraph 我使用此FileServlet为Dygraph提供CSV文件

http://dygraphs.com/

I have two types of users: guests and admins. 我有两种类型的用户:来宾和管理员。 Guests should be able to SEE the graph BUT NOT be able to DOWNLOAD the CSV file. 来宾应该可以看到图表,但不能下载CSV文件。 Admins should be able to do both. 管理员应该能够同时做到。

The fileServlet responds to URL-patterns as: file/* (* is the filename), and it is VERY convenient as the Dygraph reads for a file as specified in an URL. fileServlet以以下格式响应URL模式:file / *(*是文件名),这非常方便,因为Dygraph读取URL中指定的文件。

There is a loginServlet built within this webapp, and I want to be able to avoid the fileservlet to GIVE the file if the user just copy-pastes the URL that is given for the Dygraph. 在此web应用程序中内置了一个loginServlet,如果用户只是复制粘贴Dygraph给出的URL,我希望能够避免fileservlet提供文件。 The FileServlet is already capable of getting the session and loggeduser from that session, but I don't know how to detect what was the page that called the GET method. FileServlet已经能够从该会话中获取会话和登录用户,但是我不知道如何检测调用GET方法的页面。 I want the fileservlet to serve the file ONLY when called from within the JSP code, and not from the browser's address bar. 我希望fileservlet仅在从JSP代码而不是从浏览器的地址栏中调用时才提供文件。

Let me explain a bit: 让我解释一下:

I mean -as a guest user- the following Javascript code should display the graph (the FileServlet serves the file) 我的意思是-作为来宾用户-以下Javascript代码应显示图形(由FileServlet提供文件)

<div id="graphdiv2" style="width:640px; height:480px;">
<script type="text/javascript">
g2 = new Dygraph(
document.getElementById("graphdiv2"),
"${messages.rutacsv}", // path to CSV file
{
rollPeriod: 10,
showRoller: true
}
);
</script>            
</div>

The variable: "${messages.rutacsv}" gets replaced by the servlet for something that looks like this: 变量“ $ {messages.rutacsv}”被servlet替换,看起来像这样:

"file/2012-04-20_1.csv" “文件/ 2012-04-20_1.csv”

So the Dygraph loads the file nicely and plots the lines. 因此Dygraph可以很好地加载文件并绘制线条。

BUT, I want the FileServlet to be able to detect when the user copypastes this URL after the ContextName and block it, so only the Dygraph can download the file. 但是,我希望FileServlet能够检测到用户何时在ContextName之后复制粘贴此URL并将其阻止,因此只有Dygraph可以下载该文件。

For example, if the user types in his browser: 例如,如果用户在浏览器中键入:

http://localhost:8080/MyWebApp/file/2012-04-20_1.csv

It shouldn't be able to download it. 它不应该能够下载它。 Only admins should be able to. 只有管​​理员应该能够。

NOW, I am thinking that maybe I should implement the FileServlet so it has to be called with another URL pattern or with a POST method so a simple user copy-pasta can't get past the "origining-JSP" check. 现在,我在想也许应该实现FileServlet,以便必须使用其他URL模式或POST方法来调用它,以便简单的用户copy-pasta无法通过“ origining-JSP”检查。

BTW, I'm coming back from trying with Struts2, which is by far too complicated for this application. 顺便说一句,我是从尝试Struts2回来的,对于该应用程序来说这太复杂了。 I abandoned it for convenience and ease of development with simple servlets and JSPs. 为了方便和简化简单的servlet和JSP的开发,我放弃了它。

Use a filter to check a user role. 使用过滤器检查用户角色。 That's, before the any important action is necessary to check whether the user has a right to this action. 也就是说,在需要采取任何重要措施来检查用户是否有权执行此操作之前。 This is the task servlet filter. 这是任务servlet过滤器。

You must implement the method doFilter() in your class extending javax.servlet.Filter as follows: 您必须在扩展javax.servlet.Filter的类中实现doFilter()方法,如下所示:

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

    HttpServletRequest req = (HttpServletRequest) request;

    HttpSession session = req.getSession();

    String currentRole = (String) session.getAttribute("userRole");

    if ("admin".equals(currentRole)) {
         successRedirect(); 
    } else {
         failRedirect();
    }
    chain.doFilter(request, response);
}

And don't forget map this filter to the needed address in the web.xml file: 并且不要忘记将此过滤器映射到web.xml文件中所需的地址:

<filter>
    <filter-name>CheckRightAccessFilter</filter-name>
    <filter-class>yourproject.CheckRightAccessFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>CheckRightAccessFilter</filter-name>
    <url-pattern>*.csv</url-pattern>
</filter-mapping>

Use servlet filter who check the submiited url and on the basis of the session object it identifies the user role. 使用servlet筛选器检查谁被替换的url,并根据会话对象识别用户角色。 If it finds the authorized user then it can redirct to the download page 如果找到授权用户,则可以重定向到下载页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM