我对 Spring 不太熟悉,但我已经阅读了一些文章和操作方法。
所需业务为:
- 典型的客户端-服务器架构:移动客户端和服务器端的 RESTful 服务
- 客户端登录移动应用程序有不同的选择:应用程序登录和 Facebook 登录
- 必须保护服务器端的所有 RESTful 服务免受未经授权的用户的攻击
我的职责是开发 RESTful 服务。我非常擅长应用服务器、Java EE、JMS、JDBC、分布式事务 (XA),但我不太擅长安全性 :(
我用 Spring 开发了一些 STATELESS RESTful web 服务。这些服务不受保护,因此每个人都可以使用它们。
例如:
http://...../api/country/{**user_id**}
http://...../api/country/{**user_id**},{country_id}
- ...
我的每个 Web 服务都有一个user_id输入参数,因为我需要确定哪个用户进行了服务器调用。网络服务的结果取决于用户。当然,这绝对是正常的。
现在,我必须开发一些新东西,因为我必须保护这些 Web 服务免受未经授权的用户的攻击。
我的想法是:
(*) 我将像这样创建两个新的 Web 服务:
applicationLogin(String username, String password)[/INDENT] 和 facebookLogin(String accessToken)
http://...../api/login/{username}, {password}
http://...../api/login/{facebook access token}
(*) 我必须保护我的网络服务免受未经授权的用户的侵害
用户登录过程可能如下所示:
(1) 用户在他/她的移动设备上填写用户名和密码字段 (2) 单击应用程序登录按钮 (3) 移动应用程序向http://...../api/login/{username}, {password}
公共服务发出服务器调用 ( 4)如果用户名和密码正确,我将生成一个令牌(一个带有到期日期信息的长字符串),然后我会将用户名和令牌字符串放入 HTTP 标头(5)的答案中,之后所有客户端都必须发回当他们进行 web 服务调用时,将这两个参数(用户名和令牌)发送给服务器。
在服务器端,我可以从 HTTP 请求中读取用户名,这样我就可以从所有 Web 服务的签名中删除user_id参数。
我正在尝试在 Spring 中实现这个过程。我想我需要使用 Spring 安全模块中的PRE_AUTH_FILTER。但是不知道我的想法好不好?
我做到了:
网页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>
您如何看待我的登录过程?这是开始实现令牌处理方法的好方法吗?