RESTful Web服务 + Spring令牌身份验证

4
我对Spring不是很熟悉,但我已经阅读了一些文章和教程。
业务需求如下:
- 典型的客户端-服务器架构:移动客户端和服务器端RESTful服务。 - 客户端可以选择使用应用程序登录或Facebook登录。 - 必须保护所有RESTful服务免受未经授权的用户的攻击。
我的职责是开发RESTful服务。我非常擅长应用服务器、Java EE、JMS、JDBC和分布式事务(XA),但我在安全方面并不太好:(
我使用Spring开发了一些无状态RESTful web服务。这些服务没有得到保护,所以任何人都可以使用它们。
例如:
- http://...../api/country/{**user_id**} - http://...../api/country/{**user_id**},{country_id} - ...
我的每个web服务都有一个user_id输入参数,因为我需要识别哪个用户进行了服务器调用。web服务的结果取决于用户。当然,这是绝对正常的。
现在,我必须开发一些新东西,因为我必须保护这些web服务免受未经授权的用户的攻击。
我的想法是:
(*) 我将创建两个像这样的新web服务:
- applicationLogin(String username, String password) - facebookLogin(String accessToken)
- http://...../api/login/{username}, {password} - http://...../api/login/{facebook access token}
(*) 我将不得不保护我的web服务免受未经授权的用户的攻击
用户登录过程可能如下所示:
- 用户在移动设备上填写用户名和密码字段 - 点击应用程序登录按钮 - 移动应用程序调用公共服务http://...../api/login/{username}, {password} - 如果用户名和密码正确,我将生成一个令牌(一个带有过期日期信息的长字符串),并将用户名和令牌字符串放入HTTP头的答案中 - 之后,所有客户端在进行web服务调用时都必须发送这两个参数(用户名和令牌)。
在服务器端,我可以从HTTP请求中读取用户名,因此可以从所有web服务的签名中删除user_id参数。
我正在尝试在Spring中实现这个过程。我认为我需要使用Spring安全模块中的PRE_AUTH_FILTER。但我不知道我的想法是否好?
我已经完成了它:
web xml
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/api/country/*</url-pattern>
</filter-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext-security.xml, /WEB-INF/applicationContext.xml</param-value>
</context-param>

applicationContext-security.xml

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
            xmlns:security="http://www.springframework.org/schema/security"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">

    <security:http use-expressions="true" create-session="stateless" auto-config="false" entry-point-ref="authenticationEntryPoint">
        <security:intercept-url pattern="/api/login/*" access="permitAll"/>
        <security:intercept-url pattern="/api/country/*" access="isAuthenticated()" />
        <security:custom-filter position="PRE_AUTH_FILTER" ref="authenticationTokenProcessingFilter" />
    </security:http>

    <bean id="authenticationEntryPoint" class="com.samples.spring.auth.ForbiddenAuthenticationEntryPoint" />

    <bean id="userDetailsServiceImpl" class="com.samples.spring.auth.UserDetailsServiceImpl" />

    <bean id="preAuthenticationProvider" class="org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider">

        <property name="preAuthenticatedUserDetailsService" ref="userDetailsServiceImpl" />
    </bean>

    <bean id="authenticationTokenProcessingFilter" class="com.samples.spring.auth.AuthenticationFilter">

        <property name="authenticationManager" ref="appControlAuthenticationManager" />
    </bean>

    <security:authentication-manager alias="appControlAuthenticationManager">
        <security:authentication-provider ref="preAuthenticationProvider" />
    </security:authentication-manager>      

</beans>

你认为我的登录过程怎么样?实现令牌处理方法是一个好的开始吗?
2个回答

1
除了使用Spring Security外,Spring还有另一个模块叫做Spring Social。我认为这肯定会对你有所帮助,并且还可以减少开发工作量。它还允许你使用其他社交网络,如Twitter,LinkedIn来连接到你的应用程序。还要检查Spring Mobile

但是我不确定哪种Spring安全过滤器适合我。 - zappee
我检查了Spring Social和Spring Mobile。Spring Mobile:如果您想为PC用户和移动用户构建Web内容,它非常好。Spring Social:它使Facebook登录重定向到Facebook网页。但是,我正在编写一些服务器端服务,并且服务器端没有任何GUI / UI。没有HTML或JSP或...页面,因为我正在编写Web服务。首先,请忘记Facebook的问题。我需要实现一个令牌处理身份验证方法,以保护我的Web服务免受未经授权的用户访问。请告诉我,哪种Spring安全过滤器适合我? - zappee

0

如果您接受一个小改动,您可以直接使用安全功能。

Spring Security使用HTTP会话来存储用户详细信息。而HTTPS通常通过一个包含会话密钥或jSessionId参数的会话cookie来跟踪。

还有一个提示:请使用HTTPS而不是HTTP。


当然。我知道我必须使用HTTPS。 但我并不确切知道哪种Spring安全过滤器适合我。 - zappee
啊,现在我明白了。我建议使用标准而不是自己的实现。那么为什么不使用HTTP基本身份验证?Spring支持它。 - Ralph

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接