简体   繁体   English

Java AJAX Webapp安全性

[英]Java AJAX webapp security

I'm experimenting with creating a simple AJAX Java webapp. 我正在尝试创建一个简单的AJAX Java Webapp。

I'd like to use the container managed security provided by the Servlet container, but having investigated it have a feeling that it isn't going to work how I'd like it to. 我想使用Servlet容器提供的容器管理的安全性,但是在研究了它之后,感觉到它无法按我希望的那样工作。 Basically I'd like to be able to have a login prompt on the main page, without the user having to navigate away to a login form. 基本上,我希望能够在主页上显示一个登录提示,而无需用户导航到登录表单。

As an example imagine a simple message board application and a user who wants to post a new message enters the text directly into a form on the screen, when it comes to pressing the submit button I would like be able to make an ajax call to determine if the user is already logged in, if not then I'd like to display a login div on the same page to allow the user to log in without navigating away to a separate login page. 举例来说,假设有一个简单的留言板应用程序,想要发布新消息的用户将文本直接输入屏幕上的表单中,当涉及到按下提交按钮时,我希望能够进行ajax调用来确定如果用户已经登录,那么我想在同一页面上显示一个登录div,以允许用户登录而无需导航到单独的登录页面。

The form based login provided by the servlet container seems to be based on the assumption that the user will be redirected to a separate login page at the point where they try to access something that is "secured", which isn't what I want to happen. servlet容器提供的基于表单的登录似乎是基于这样的假设,即用户将在尝试访问“受保护的”内容时被重定向到单独的登录页面,这不是我想要的。发生。

So I suppose what I'd like to know is if there is a standard way of allowing users to login within a single page ajax application using the container managed security, or some 3rd party library? 因此,我想我想知道的是,是否存在一种允许用户使用容器管理的安全性或某些第三方库在单页ajax应用程序中登录的标准方法? I'm going to have a look at Spring security, but at first glance it seems to have the same problem that I'm having with the standard security. 我将看一下Spring安全性,但是乍一看它似乎与标准安全性存在相同的问题。

EDIT:Found this very useful, think it may be the basis of my solution http://www.jroller.com/sjivan/entry/ajax_based_login_using_aceci 编辑:发现这非常有用,认为它可能是我的解决方案的基础http://www.jroller.com/sjivan/entry/ajax_based_login_using_aceci

I've used Spring Security in the past, and while I didn't use it to do this specifically, I remember it was very pluggable. 我过去曾使用过Spring Security,虽然我没有专门使用它来完成此操作,但我记得它是可插拔的。 While the classic action to take if a user was unauthenticated is issue a redirect to a login form page, you can use one of several classes provided to change the behaviour in this case, or it's quite easy to write your own which will do whatever you want (in this case return an appropriate AJAX response by the looks of it). 虽然在未经身份验证的情况下采取的经典动作是将用户重定向到登录表单页面,但在这种情况下,您可以使用提供的几种类中的一种来更改行为,或者编写自己的类很容易,它将执行您的任何操作想要(在这种情况下,根据其外观返回适当的AJAX响应)。

The logic I ended up having to implement was pretty horrible, for legacy reasons, and I was still able to do it without any specific hassles. 由于遗留原因,我最终不得不实现的逻辑非常可怕,而且我仍然能够做到这一点而没有任何特定的麻烦。 I don't doubt that you could get Spring Security to work in this respect too (and I wouldn't be surprised if they already have the specific classes for your requirements). 我毫不怀疑,您也可以使Spring Security在这方面工作(如果他们已经有了满足您要求的特定类,我也不会感到惊讶)。

Hey Mike, I'm going thru the same process- trying to figure out best practice for integrating security packages like Spring Security, Apache Shiro, etc into ajax world. 嗨,迈克,我要经历同样的过程-试图找出将Spring Security,Apache Shiro等安全软件包集成到ajax世界中的最佳实践。

From what I understand so far, at the end of the day (regardless of which security package you're using to implement the authorization and authentication), the ajax call that you make to check whether a user is authenticated has to call a servlet that renders a response (such as json or xml) that the javascript on the client can then handle and react to appropriately (display a login form inline, etc). 根据我到目前为止的了解,最终(无论您使用哪个安全软件包来实现授权和身份验证),检查用户是否已通过身份验证的ajax调用都必须调用一个呈现一个响应(例如json或xml),客户端上的javascript随后可以处理该响应并做出适当的反应(内嵌显示登录表单等)。

Here's the flow as I understand it: 据我了解,这是流程:

  1. Initial browser request, example: http://mysite/homepage 初始浏览器请求,例如: http:// mysite / homepage
  2. Server renders page (using any of the plethora of java frameworks available, for example, simple servlet, jsp, jsf, etc, etc) 服务器呈现页面(使用大量可用的Java框架中的任何一种,例如,简单的servlet,jsp,jsf等)
  3. Rendered Page includes some sort of javascript library (gwt, jquery, prototype, etc) 渲染页面包括某种javascript库(gwt,jquery,prototype等)
  4. javascript library makes async request. javascript库发出异步请求。 This request must be made to a url where the java servlet container knows to respond with json and/or xml rather than html. 必须在Java Servlet容器知道使用json和/或xml而不是html响应的URL处发出此请求。 For example, http://mysite/ajax/isAuthenticated 例如, http:// mysite / ajax / isAuthenticated
  5. javascript on client listens for response. 客户端上的javascript侦听响应。
  6. javascript on client parses json/xml response and either displays login page, or continues on to next thing. 客户端上的javascript解析json / xml响应并显示登录页面,或继续进行下一个操作。

There are probably hundreds of combinations of server side + client side frameworks to help with this. 服务器端+客户端框架的组合可能有数百种,以提供帮助。 So far, I've tried grails + prototype, grails + jquery, grails + gwt, simple servlets + gwt, and currently looking into using Spring MVC + Gwt. 到目前为止,我已经尝试过grails +原型,grails + jquery,grails + gwt,简单的servlets + gwt,目前正在研究使用Spring MVC + Gwt。 Also, outside of java world, I just started reading about Ruby on Rails (which, as I understand was basically created to make this ajax communication between client and server easy). 另外,在Java世界之外,我刚刚开始阅读Ruby on Rails(据我所了解,Ruby on Rails的创建基本上是为了使客户端和服务器之间的这种ajax通信变得容易)。 I haven't found the one that really works for me yet, so if you have any luck, please let me know your results! 我还没有找到最适合我的产品,所以如果您有运气,请告诉我您的结果!

Found a similar question has been answered on the Spring security forum 发现类似的问题已在Spring安全论坛上得到解答

http://forum.springsource.org/showthread.php?t=80549 http://forum.springsource.org/showthread.php?t=80549

Not as straighforward as I hoped, but looks like a good suggestion once you've got up to speed with Spring Security - I may leave getting the logon for now and concentrate on other functionality. 不像我希望的那样简单明了,但是一旦您熟悉Spring Security,这似乎是一个很好的建议-我可能暂时不进行登录并专注于其他功能。 Will update here if I get as far as implementing a solution. 如果我能实施解决方案,将在这里更新。

OK - have got a basic solution, I'm not saying its the best, but looks like it will do the job. 好的-有一个基本的解决方案,我并不是说它是最好的,但看起来它可以胜任。

Its based on a blog post by Sanjiv Jivan 它基于Sanjiv Jivan的博客文章

http://www.jroller.com/sjivan/entry/ajax_based_login_using_aceci http://www.jroller.com/sjivan/entry/ajax_based_login_using_aceci

EDIT: Having just read the comments on that post it looks like there may be another way to do it, also the post is 3+ years old, so wouldn't be surprised if there's better solutions out there, maybe using new features in Spring or J2EE. 编辑:刚刚阅读了该帖子的评论后,看来可能还有另一种方法,该帖子已有3年以上的历史,因此,如果那里有更好的解决方案,也许会在Spring中使用新功能也不会感到惊讶或J2EE。 But I'm going to stick with this for now. 但是我现在要坚持下去。

He's done a very good job of documenting it there, so I'll just comment here that I've got a basic example going by starting with copying Spring Security sample app., then adding the ServletFilter as described by Sanjiv. 他在此处进行了很好的记录,因此,我在这里仅作评论,这里有一个基本示例,首先复制Spring Security示例应用程序,然后按照Sanjiv的描述添加ServletFilter。

As he was using an earlier version of Acegi Security (now called Spring Security) there were one or two changes required: 当他使用早期版本的Acegi Security(现在称为Spring Security)时,需要进行一两个更改:

The url pattern is now " /j_spring_security_check " (not " /j_acegi_security_check "), and I found I had to add an input named "ajax" to get the isAjaxRequest method to return true - guessing I missed something in the javascript for that. URL模式现在是“ /j_spring_security_check ”(而不是“ /j_acegi_security_check ”),我发现我不得不添加一个名为‘Ajax’的输入来获得isAjaxRequest方法返回true -猜我错过了JavaScript来的东西。

Had to change one line in the filter to check for "login_error" instead of "login_error=1", guessing that's something thats changed in newer versions of SpringSecurity. 不得不在过滤器中更改一行以检查“ login_error”而不是“ login_error = 1”,猜测这是在较新版本的SpringSecurity中所做的更改。 IE: IE浏览器:

               if (redirectURL.indexOf("login_error") == -1) {  

Also as I'm using jquery not prototype I created a basic html page with javascript to test using jquery. 另外,由于我使用的不是jQuery原型,所以我用JavaScript创建了一个基本的html页面,以使用jQuery进行测试。

Its rough and ready at the moment, the status that gets displayed is just "url:/msgbrd/" or "error:Bad credentials", but can tidy that up easily enough. 目前它的状态很粗糙,显示的状态只是“ url:/ msgbrd /”或“ error:Bad凭证”,但是可以很容易地整理它。

In case it helps here's my example index.html: 如果这有帮助,这是我的示例index.html:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script type="text/javascript" src="jquery.form.js"></script>
<script type="text/javascript">

var loginOptions = { 
beforeSubmit:  postingLogin,  // pre-submit callback 
success:       showResponse,  // post-submit callback 
    type: 'post',
    url: '/msgbrd/j_spring_security_check'  ,
    clearForm: true
}; 


$(document).ready(function(){
    $('#loginForm').ajaxForm( loginOptions );   
        });


function postingLogin() {
    console.log("Login posting: ");

}
function doneLogin() {

    console.log("Login posted ");
}
function showResponse(responseText, statusText)  { 
    $("#statusMessage").empty();
    $("#statusMessage").append( responseText); 
} 

</script>


</head>
<body>
<h1>msgbrd</h1>
<div>
<form id="loginForm" action="" method="POST">
<p><label for="username"> User Name: </label> <input id="username"
    type="text" name="j_username" class="loginText"></p>

<p><input id="ajax" name="ajax" value="ajax"></input></p>
<p><label for="password"> Password: </label> <input id="password"
    type="password" name="j_password" class="loginText"
    onkeydown="loginOnEnter(event);"></p>

<p><label for="remember_me"> <input id="remember_me"
    type="checkbox" name="_acegi_security_remember_me">Remember Me</label>
</p>


<p><input type="submit" value="Go"></input></p>
<p id="statusMessage"></p>
</form>
</div>
</body>
</html>

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

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