使用 spring ws 后端 Web 应用程序进行会话管理
session management with spring ws backed webapplication
我正在尝试创建一个 web 应用程序,它将使用 js 前端并在后端调用 Spring WS。假设这是一个购物网站类型的网站。因此,我可以获得 OrderService
InventoryService
ShippingService
等服务。唯一需要会话的是用户的购物车。现在,就这个购物车而言,使用 servlet 容器进行会话管理是否有意义?或者,我应该 CartService
将会话信息持久保存到数据库中吗?
关于使用 Web 服务进行会话管理的最佳实践是什么?我想,最好的做法确实是保持服务无状态,但如果我有无状态网络服务,我该如何授权用户?
使用 servlet 容器来进行会话管理,然后让不同的控制器充当 web 服务的代理,这到底是不是一个好习惯?
我附上一张图片,让您更好地理解上下文。
How can I authorize users if I have stateless webservices?
如果您的应用程序使用外部 ws,则描述了相当常见的方法 here。
如果所有的ws都是你的交付的一部分,你当然可以使用spring-security。
一种非常常见的方法也是使用 (apache) http 服务器作为代理,同时使用类似 ldap 的东西来进行身份验证和授权。
Is it a good practice at all to use a servlet container just to do session management and then have different controllers acting as proxies to the webservices?
我认为不是。正如 here 所讨论的那样,您只能从保持 Web 服务无状态中受益,如果您需要在请求之间保持状态,请使用 cookie。
如果状态(购物车)在注销后仍然存在,像 CartService 这样的东西对我来说听起来是个好主意。
你可以参考Spring-WS security
检查 here,以获取在 Spring 启动应用程序中使用 WS-Security 的示例。具体参见WebServiceServerConfig。
If webservice is stateless, how do I know that an ajax request is authorized? How do I know that ajax request is coming from user1 of webapp1, who is authorized? and not from user2 of webapp2, who is not authorized to access the service?
好问题。快速回答是:
对于 基本身份验证 : username:password 是 base64 编码并存储在 Authorization http header 对于客户端发送的每个请求。参见 this wiki entry。 header 看起来像这样:
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
使用spring security,配置可以这样:
<http pattern="/api/**" create-session="stateless">
<intercept-url pattern='/**' access="hasRole('REMOTE')" />
<http-basic />
</http>
对于基于form的WS认证,看一下this article。
首先,您向 /j_spring_security_check 发送了一个 post 请求。此请求将 return Cookie,然后将由针对 Web 服务的任何后续请求使用。他们将其存储在文本文件中:
curl -i -X POST -d j_username=user -d j_password=userPass -c /tmp/cookies.txt
<a href="http://localhost:8080/app/j_spring_security_check" rel="nofollow">http://localhost:8080/app/j_spring_security_check</a>
然后您可以使用文件中的 cookie 进行进一步的身份验证请求:
curl -i --header "Accept:application/json" -X GET -b /tmp/cookies.txt
<a href="http://localhost:8080/app/api/foos" rel="nofollow">http://localhost:8080/app/api/foos</a>
xml spring 安全配置可能如下所示:
<http pattern="/api/**" create-session="stateless">
<intercept-url pattern='/**' access="hasRole('REMOTE')" />
<form-login />
</http>
我正在尝试创建一个 web 应用程序,它将使用 js 前端并在后端调用 Spring WS。假设这是一个购物网站类型的网站。因此,我可以获得 OrderService
InventoryService
ShippingService
等服务。唯一需要会话的是用户的购物车。现在,就这个购物车而言,使用 servlet 容器进行会话管理是否有意义?或者,我应该 CartService
将会话信息持久保存到数据库中吗?
关于使用 Web 服务进行会话管理的最佳实践是什么?我想,最好的做法确实是保持服务无状态,但如果我有无状态网络服务,我该如何授权用户?
使用 servlet 容器来进行会话管理,然后让不同的控制器充当 web 服务的代理,这到底是不是一个好习惯?
我附上一张图片,让您更好地理解上下文。
How can I authorize users if I have stateless webservices?
如果您的应用程序使用外部 ws,则描述了相当常见的方法 here。
如果所有的ws都是你的交付的一部分,你当然可以使用spring-security。
一种非常常见的方法也是使用 (apache) http 服务器作为代理,同时使用类似 ldap 的东西来进行身份验证和授权。
Is it a good practice at all to use a servlet container just to do session management and then have different controllers acting as proxies to the webservices?
我认为不是。正如 here 所讨论的那样,您只能从保持 Web 服务无状态中受益,如果您需要在请求之间保持状态,请使用 cookie。
如果状态(购物车)在注销后仍然存在,像 CartService 这样的东西对我来说听起来是个好主意。
你可以参考Spring-WS security
检查 here,以获取在 Spring 启动应用程序中使用 WS-Security 的示例。具体参见WebServiceServerConfig。
If webservice is stateless, how do I know that an ajax request is authorized? How do I know that ajax request is coming from user1 of webapp1, who is authorized? and not from user2 of webapp2, who is not authorized to access the service?
好问题。快速回答是:
对于 基本身份验证 : username:password 是 base64 编码并存储在 Authorization http header 对于客户端发送的每个请求。参见 this wiki entry。 header 看起来像这样:
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
使用spring security,配置可以这样:
<http pattern="/api/**" create-session="stateless">
<intercept-url pattern='/**' access="hasRole('REMOTE')" />
<http-basic />
</http>
对于基于form的WS认证,看一下this article。
首先,您向 /j_spring_security_check 发送了一个 post 请求。此请求将 return Cookie,然后将由针对 Web 服务的任何后续请求使用。他们将其存储在文本文件中:
curl -i -X POST -d j_username=user -d j_password=userPass -c /tmp/cookies.txt <a href="http://localhost:8080/app/j_spring_security_check" rel="nofollow">http://localhost:8080/app/j_spring_security_check</a>
然后您可以使用文件中的 cookie 进行进一步的身份验证请求:
curl -i --header "Accept:application/json" -X GET -b /tmp/cookies.txt <a href="http://localhost:8080/app/api/foos" rel="nofollow">http://localhost:8080/app/api/foos</a>
xml spring 安全配置可能如下所示:
<http pattern="/api/**" create-session="stateless">
<intercept-url pattern='/**' access="hasRole('REMOTE')" />
<form-login />
</http>